From b6f99a59a3270baf6bfd8e3152e58f1c47455d08 Mon Sep 17 00:00:00 2001 From: Justus Klausecker Date: Sat, 21 Mar 2026 01:11:26 +0100 Subject: [PATCH 1/2] std.Progress: use `cmpxchgStrong` instead of `cmpxchgWeak` for locking/unlocking IPC The `cmpxchgWeak` in `setIpcFile` could lead to the IPC file not being set even though there's still a slot available because one or more slots were spuriously skipped. The `cmpxcheWeak` in `serialize` could lead to an unused IPC slot not being locked and used even though it could be. --- lib/std/Progress.zig | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/std/Progress.zig b/lib/std/Progress.zig index aa9c9f54beb1930ffad0424ed6a4738c0a7caaca..64afcaaf17555794228bb3603859794a09ddfba8 100644 --- a/lib/std/Progress.zig +++ b/lib/std/Progress.zig @@ -450,7 +450,7 @@ pub const Node = struct { const ipc = @atomicLoad(Ipc, ipc_ptr, .monotonic); if (ipc.locked or ipc.valid) continue; const generation = ipc.generation +% 1; - if (@cmpxchgWeak( + if (@cmpxchgStrong( Ipc, ipc_ptr, ipc, @@ -1133,7 +1133,7 @@ fn serialize(io: Io, serialized_buffer: *Serialized.Buffer) !Serialized { const ipc_data = &serialized_buffer.ipc_data[ipc_index.slot]; state: switch (ipc_data.state) { .unused => { - if (@cmpxchgWeak( + if (@cmpxchgStrong( Ipc, ipc, .{ .locked = false, .valid = true, .generation = ipc_index.generation }, -- 2.54.0 From 1f78e34de08bb1fdaa93291334bd86a4c8b2825d Mon Sep 17 00:00:00 2001 From: Justus Klausecker Date: Sat, 21 Mar 2026 01:51:04 +0100 Subject: [PATCH 2/2] std.Io.Threaded: make `mutexLock()` use `cmpxchgStrong` instead of `cmpxchgWeak` As established in 048e38624e and d70bd0b37e, mutexes should use a strong cmpxchg when attempting to lock to guarantee that they actually succeed if they aren't locked yet. Also deletes an unused near-duplicate of `mutexLock()`. --- lib/std/Io/Threaded.zig | 21 +-------------------- 1 file changed, 1 insertion(+), 20 deletions(-) diff --git a/lib/std/Io/Threaded.zig b/lib/std/Io/Threaded.zig index 3f47272c12c407947422a9852aa891c0cd85eb9b..60b8758843cfcfd02aca36280d6b9128815ad177 100644 --- a/lib/std/Io/Threaded.zig +++ b/lib/std/Io/Threaded.zig @@ -1337,25 +1337,6 @@ const Thread = struct { return @ptrFromInt(@as(usize, @bitCast(split))); } }; - - /// Same as `Io.Mutex.lock` but avoids the VTable. - fn mutexLock(m: *Io.Mutex) Io.Cancelable!void { - const initial_state = m.state.cmpxchgWeak( - .unlocked, - .locked_once, - .acquire, - .monotonic, - ) orelse { - @branchHint(.likely); - return; - }; - if (initial_state == .contended) { - try Thread.futexWait(@ptrCast(&m.state.raw), @intFromEnum(Io.Mutex.State.contended), null); - } - while (m.state.swap(.contended, .acquire) != .unlocked) { - try Thread.futexWait(@ptrCast(&m.state.raw), @intFromEnum(Io.Mutex.State.contended), null); - } - } }; const Syscall = struct { @@ -18663,7 +18644,7 @@ fn condWait(cond: *Io.Condition, mutex: *Io.Mutex) void { /// Same as `Io.Mutex.lockUncancelable` but avoids the VTable. pub fn mutexLock(m: *Io.Mutex) void { - const initial_state = m.state.cmpxchgWeak( + const initial_state = m.state.cmpxchgStrong( .unlocked, .locked_once, .acquire, -- 2.54.0