-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
expand thread::park explanation #56157
Conversation
r? @Kimundi (rust_highfive has picked a reviewer for you, use r? to override) |
One thing I still dislike is that the example does not |
I'd add it - perhaps some kind of Btw, looks like we don't need the thread builder in the example - a simple |
This looks fine to me, however, I'm wondering why we don't make the guarantee it will wait? The current implementation does and I can't really think of a sensible implementation that wouldn't. Even if you just implemented it with a raw FUTEX_WAIT, adding an extra CAS to stop a spurious return is negligible compared to the syscall itself. |
According to @carllerche, it is a standard "litmus test" for correct usage of @parched Are you essentially proposing to rule out spurious wakeups, or what else do you mean by "it will wait"? |
Yes exactly, because I don't think we gain anything by allowing it. |
I think part of the answer is that most users wouldn't even benefit -- if you e.g. implement a lock on top of this, you anyway already have another channel to know if you can go on. But I'll wait other people that know more about this APIs to fill us in here. ;) |
@parched Spurious notifications are permitted to enable more efficient implementations. IMO it would not be reasonable to rule them out. park / unpark should never be used as part of the logic of the algorithm, just a way to block the thread. As such, there should always be some other variable used to determine if the unpark was "spurious". |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Other than the wording nit around the paragraph permitting spuriousness this looks good to me. I would like to see this reworded, but otherwise r=me.
src/libstd/thread/mod.rs
Outdated
@@ -806,9 +806,13 @@ const NOTIFIED: usize = 2; | |||
/// In other words, each [`Thread`] acts a bit like a spinlock that can be | |||
/// locked and unlocked using `park` and `unpark`. | |||
/// | |||
/// Notice that it would be a valid (but inefficient) implementation to make both [`park`] and | |||
/// [`unpark`] NOPs that return immediately. Being unblocked does not imply |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/NOPs that return immediately/return immediately/
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general since this is a public API, I would prefer wording to not be in terms of how the implementation could be (i.e. implemented to return immediately), but what the implementation is allowed to do (return spuriously without necessarily synchronizing with anything).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think examples are very useful to demonstrate where spurious wakeups may come from. For me personally, I have seen this "spurious wakeups allowed" stuff often, but only when I recently read "NOPs are a valid implementation" then it clicked for me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have changed the order a bit, putting the API contract first and the example second. What do you think?
@bors r+
…On Sun, Dec 9, 2018, 21:25 Ralf Jung ***@***.*** wrote:
@Kimundi <https://github.com/Kimundi> @nagisa <https://github.com/nagisa>
anything left to r+ this?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#56157 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AApc0vp0ess-i-QikkS967PwEUWytlOdks5u3WOigaJpZM4YvARK>
.
|
📌 Commit 76cd8f0 has been approved by |
⌛ Testing commit 76cd8f0 with merge c7c3310ff44af9f13e5f9e171f0a1ce6c6eb3c87... |
💔 Test failed - status-travis |
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
Looks spurious to me (the changed doctest passed on the PR CI, and the failure looks to be in a different crate anyway) @bors retry |
expand thread::park explanation Cc @carllerche @parched @stjepang
☀️ Test successful - status-appveyor, status-travis |
Cc @carllerche @parched @stjepang