Skip to content
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

Merged
merged 5 commits into from
Jun 12, 2021
Merged

std.Thread.Futex addition #9070

merged 5 commits into from
Jun 12, 2021

Conversation

kprotty
Copy link
Member

@kprotty kprotty commented Jun 10, 2021

Having a futex primitive is the first step towards building unified/simpler/efficient synchronization primitives. Some notes on platform compatibility:

  • Linux already has a futex api. If unfamiliar with futexes in general, its a good resource.
  • Windows uses the WaitOnAddress functions which are only available on Windows 8 and higher. This kills windows 7 support, but we could add an NtKeyedEvent futex-emulation backend if xp to vista/7 is still needed. The implementation uses the ntdll functions because the kernel32 ones aren't actually in synchronization.lib.
  • Darwin uses the new undocumented __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.
  • The generic posix backend emulates the futex api using a global hash table of wait queues. Its purposely made simple for now but could be optimized by using fibonacci hashing on the address, using a custom Lock for each bucket which can be zero-initialized to reduce hash table size in binary with the .bss segment, and using a balanced tree (red-black, avl, treap, etc.) of queues for each address instead of a flat list. This generic hash table futex approach can also be used for other OS's without a native futex api besides posix.
  • For other OS's, we could specialize on their futex-equivalent apis in the future. For reference, WASM is working on one, FreeBSD has one, OpenBSD has one, DragonflyBSD has one, but NetBSD does not meaning it would need futex emulation like the generic posix backend.

lib/std/Thread/Futex.zig Outdated Show resolved Hide resolved

/// 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 {
Copy link
Contributor

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)?

Copy link
Member Author

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.

Copy link
Member

@andrewrk andrewrk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work!

@kprotty kprotty merged commit 2ba68f9 into ziglang:master Jun 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants