authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-02-01 18:44:08-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-02-02 00:09:48-08:00
log60ac4e78ebb6de925b7bd1dc8571af55d096f964
tree5985d84523871d9be9151f8a8589abfd70a11154
parentaacf8ce03d2b52e52830873c40eabb1cb1eb8272

std.Io.Mutex: fix tryLock

`@cmpxchgWeak` can return the expected value sometimes.

1 files changed, 1 insertions(+), 9 deletions(-)

lib/std/Io.zig+1-9
...@@ -1236,15 +1236,7 @@ pub const Mutex = extern struct {...@@ -1236,15 +1236,7 @@ pub const Mutex = extern struct {
1236 };1236 };
12371237
1238 pub fn tryLock(m: *Mutex) bool {1238 pub fn tryLock(m: *Mutex) bool {
1239 switch (m.state.cmpxchgWeak(1239 return m.state.cmpxchgWeak(.unlocked, .locked_once, .acquire, .monotonic) == null;
1240 .unlocked,
1241 .locked_once,
1242 .acquire,
1243 .monotonic,
1244 ) orelse return true) {
1245 .unlocked => unreachable,
1246 .locked_once, .contended => return false,
1247 }
1248 }1240 }
12491241
1250 pub fn lock(m: *Mutex, io: Io) Cancelable!void {1242 pub fn lock(m: *Mutex, io: Io) Cancelable!void {