-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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.Thread.Futex addition #9070
Conversation
|
||
/// Unblocks at most `num_waiters` callers blocked in a `wait()` call on `ptr`. | ||
/// `num_waiters` of 1 unblocks at most one `wait(ptr, ...)` and `maxInt(u32)` unblocks effectively all `wait(ptr, ...)`. | ||
pub fn wake(ptr: *const Atomic(u32), num_waiters: u32) void { |
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.
It looks like quite a few platforms only support waking up either 1 or all waiters. What do you think about replacing num_waiters: u32
to wake_all: bool
, or replacing wake()
with something like notify()
(wake one waiter) and broadcast()
(wake all waiters)?
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.
num_waiters
is more of a hint and can be used to influence the exact count for platforms that support it. Other platforms would just wake up more threads than stated. The waiters already have to handle spurious wake ups, and this keeps it consistent with linux's futex(2) api.
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.
Great work!
Having a futex primitive is the first step towards building unified/simpler/efficient synchronization primitives. Some notes on platform compatibility:
__ulock_wait/wake
API available since 10.12. Its also used in LLVM's libc++/libcxx so it should be still available. There's a possibility it may prevent apps from being uploaded to the AppStore since dynamically linked functions to odd places are checked iirc. If this becomes an issue, we could switch to the generic posix backend.