commit 60ac4e78ebb6de925b7bd1dc8571af55d096f964 (tree)
parent aacf8ce03d2b52e52830873c40eabb1cb1eb8272
Author: Andrew Kelley <andrew@ziglang.org>
Date: Sun, 1 Feb 2026 18:44:08 -0800
std.Io.Mutex: fix tryLock
`@cmpxchgWeak` can return the expected value sometimes.
Diffstat:
1 file changed, 1 insertion(+), 9 deletions(-)
diff --git a/lib/std/Io.zig b/lib/std/Io.zig
@@ -1236,15 +1236,7 @@ pub const Mutex = extern struct {
};
pub fn tryLock(m: *Mutex) bool {
- switch (m.state.cmpxchgWeak(
- .unlocked,
- .locked_once,
- .acquire,
- .monotonic,
- ) orelse return true) {
- .unlocked => unreachable,
- .locked_once, .contended => return false,
- }
+ return m.state.cmpxchgWeak(.unlocked, .locked_once, .acquire, .monotonic) == null;
}
pub fn lock(m: *Mutex, io: Io) Cancelable!void {