authorgravatar for justus@klausecker.deJustus Klausecker <justus@klausecker.de> 2026-03-21 01:51:04+01:00
committergravatar for justus@klausecker.deJustus Klausecker <justus@klausecker.de> 2026-03-21 03:00:50+01:00
log1f78e34de08bb1fdaa93291334bd86a4c8b2825d
treea9ca53d8422c30028bc2f8d0724d4fce199e1962
parentb6f99a59a3270baf6bfd8e3152e58f1c47455d08

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()`.

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

lib/std/Io/Threaded.zig+1-20
...@@ -1337,25 +1337,6 @@ const Thread = struct {...@@ -1337,25 +1337,6 @@ const Thread = struct {
1337 return @ptrFromInt(@as(usize, @bitCast(split)));1337 return @ptrFromInt(@as(usize, @bitCast(split)));
1338 }1338 }
1339 };1339 };
1340
1341 /// Same as `Io.Mutex.lock` but avoids the VTable.
1342 fn mutexLock(m: *Io.Mutex) Io.Cancelable!void {
1343 const initial_state = m.state.cmpxchgWeak(
1344 .unlocked,
1345 .locked_once,
1346 .acquire,
1347 .monotonic,
1348 ) orelse {
1349 @branchHint(.likely);
1350 return;
1351 };
1352 if (initial_state == .contended) {
1353 try Thread.futexWait(@ptrCast(&m.state.raw), @intFromEnum(Io.Mutex.State.contended), null);
1354 }
1355 while (m.state.swap(.contended, .acquire) != .unlocked) {
1356 try Thread.futexWait(@ptrCast(&m.state.raw), @intFromEnum(Io.Mutex.State.contended), null);
1357 }
1358 }
1359};1340};
13601341
1361const Syscall = struct {1342const Syscall = struct {
...@@ -18663,7 +18644,7 @@ fn condWait(cond: *Io.Condition, mutex: *Io.Mutex) void {...@@ -18663,7 +18644,7 @@ fn condWait(cond: *Io.Condition, mutex: *Io.Mutex) void {
1866318644
18664/// Same as `Io.Mutex.lockUncancelable` but avoids the VTable.18645/// Same as `Io.Mutex.lockUncancelable` but avoids the VTable.
18665pub fn mutexLock(m: *Io.Mutex) void {18646pub fn mutexLock(m: *Io.Mutex) void {
18666 const initial_state = m.state.cmpxchgWeak(18647 const initial_state = m.state.cmpxchgStrong(
18667 .unlocked,18648 .unlocked,
18668 .locked_once,18649 .locked_once,
18669 .acquire,18650 .acquire,