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: Mutex and Condition improvements #11497

Merged
merged 14 commits into from
Apr 24, 2022

Conversation

kprotty
Copy link
Member

@kprotty kprotty commented Apr 22, 2022

Main Goals

  • Optimize Mutex and Condiiton implementations to use Futex based algorithms.
  • More robust tests for Mutex and Condition.
  • More documentation in general, but specifically about Condition semantics.

Side Goals

  • Guarantee at most u64 large sync primitives (if Futex is removed in the future, they can always be made usize large).
  • Introduce std.Thread.Futex.Deadline for accurately waiting on a Futex in a loop with a relative timeout.
    • currently used by Condition's futex impl.
    • will be used by Semaphore and ResetEvent's futex impl.

@kprotty kprotty added the standard library This issue involves writing Zig code for the standard library. label Apr 22, 2022
lib/std/Thread/Mutex.zig Outdated Show resolved Hide resolved
@andrewrk
Copy link
Member

andrewrk commented Apr 22, 2022

error: <inline asm>:1:12: invalid operand for instruction
        lock btsl (%rax), %eax
                  ^~~~~

Is this a problem with stage2 inline assembly? If so, I can work on this to unblock you asap. However if it's possible to work around this with non-assembly code, you can check for @import("builtin").zig_backend != .stage1 and do the fallback condition. Then I can remove the fallback in favor of the assembly after the stage2 issue is fixed, at my leisure.

@kprotty
Copy link
Member Author

kprotty commented Apr 22, 2022

@andrewrk Seems like stage2 either doesn't properly lower the *p/*m constraint or treats the X constraint incorrectly.

Also, the CI also says noalias pointers aren't actually pointers. Is this a stage2 false positive?

Copy link
Contributor

@gracefuu gracefuu left a comment

Choose a reason for hiding this comment

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

LGTM! I can't really comment on the platform specific implementations, but the FutexImpls seem fine. Also, the Condition tests are quite good and can be reused for Semaphore when the time comes to update those.

Just some really minor typos/comments:

lib/std/Thread/Condition.zig Show resolved Hide resolved
lib/std/Thread/Condition.zig Outdated Show resolved Hide resolved
lib/std/Thread/Mutex.zig Outdated Show resolved Hide resolved
lib/std/Thread/Mutex.zig Outdated Show resolved Hide resolved
lib/std/Thread/Condition.zig Outdated Show resolved Hide resolved
@kprotty kprotty merged commit 963ac60 into ziglang:master Apr 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
standard library This issue involves writing Zig code for the standard library.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants