From 60ac4e78ebb6de925b7bd1dc8571af55d096f964 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 1 Feb 2026 18:44:08 -0800 Subject: [PATCH] std.Io.Mutex: fix tryLock `@cmpxchgWeak` can return the expected value sometimes. --- lib/std/Io.zig | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/lib/std/Io.zig b/lib/std/Io.zig index d2b9648245d822660b871aaabe1d29f496edd573..bb9610c0904b670b1eff8933efac16bd3138eca2 100644 --- 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 { -- 2.54.0