-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
std: use block_current_task
for thread parking on Hermit
#98534
Conversation
Hey! It looks like you've submitted a new PR for the library teams! If this PR contains changes to any Examples of
|
r? @kennytm (rust-highfive has picked a reviewer for you, use r? to override) |
This comment has been minimized.
This comment has been minimized.
This comment was marked as off-topic.
This comment was marked as off-topic.
@joboet: 🔑 Insufficient privileges: not in try users |
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.
Thanks a lot, @joboet.
This looks very good to 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.
Thanks a lot, @joboet.
☔ The latest upstream changes (presumably #98545) made this pull request unmergeable. Please resolve the merge conflicts. |
e26c164
to
dc14f3f
Compare
r? rust-lang/libs |
// Acquire ordering is necessary to obtain the token, as otherwise the parked thread | ||
// could perform memory operations visible before it was unparked. Since once set, | ||
// the token cannot be unset by other threads, the state can be reset with a relaxed | ||
// store once it has been read with acquire ordering. |
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 am not sure this is convincing to me. My understanding (perhaps flawed, I'm definitely not an expert in this) is that relaxed doesn't guarantee that other threads will observe the new empty state; we need a release store that synchronizes with the other thread's acquire ordering in order to guarantee correct behavior here.
In general, I'm not sure that hermit should deviate from the pattern of the general unix thread parker implementation, which uses SeqCst ordering for all of these operations.
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.
My understanding (perhaps flawed, I'm definitely not an expert in this) is that relaxed doesn't guarantee that other threads will observe the new empty state; we need a release store that synchronizes with the other thread's acquire ordering in order to guarantee correct behavior here.
That's not correct. SeqCst or Release+Acquire also don't guarantee any other thread will directly observe the new value either. The specification only talks about ordering, not about timing. And even Relaxed guarantees a perfectly consistent ordering for all operations on the same atomic variable. Using stronger memory ordering only means that you can under some conditions guarantee that something that happened to another piece of memory is observable once an atomic store/change is observed, but is unrelated to when that change is observed in the first place.
I'm also going to r? @m-ou-se who has put a lot more thought recently into this, since I suspect it'll be easy for me to miss something when reviewing this, and since I'm less than confident about the Relaxed memory ordering usage here. |
☔ The latest upstream changes (presumably #99251) made this pull request unmergeable. Please resolve the merge conflicts. |
Hey! It looks like you've submitted a new PR for the library teams! If this PR contains changes to any Examples of
|
This comment was marked as outdated.
This comment was marked as outdated.
Since Hermit now has futex syscalls, I'm going to close this PR, as Hermit can now share the futex parker used by other systems. |
Mutex
andCondvar
are being replaced by more efficient implementations, which need thread parking themselves (see #93740). Therefore, the generic Parker needs to be replaced on all platforms where the new lock implementation will be used.Hermit has the
block_current_task(_with_timeout)
/wakeup_task
syscalls for building custom synchronization primitives.block_current_task
sets the thread's state to parked. The next yield to the scheduler will block until the task is woken up again. As Hermit does not use preemptive multitasking, this can be used to run userspace code (e.g. checking a condition) in between the syscalls.This implementation uses an atomic state variable, similar to the futex parker, but instead of a
PARKED
state it stores the parking threads PID. The syscalls are used to emulate a futex operation:block_current_task
)a. if it was set to notified while changing the task state, "wakeup" the task, consume the token and return
b. else sleep (
yield_now
)CC @stlankes, @mkroening