authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2026-03-21 04:14:44+01:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2026-03-21 04:14:44+01:00
logb4d134a0d2ee238cab70f1e967b2172edbec1b41
treea9ca53d8422c30028bc2f8d0724d4fce199e1962
parente938344100da959308aa27dee1295e5ce02efc30
parent1f78e34de08bb1fdaa93291334bd86a4c8b2825d

Merge pull request 'std: audit (most) usages of `cmpxchgWeak`' (#31608) from justusk/zig:cmpxchg-xchg into master

Reviewed-on: https://codeberg.org/ziglang/zig/pulls/31608 Reviewed-by: Alex Rønne Petersen <alex@alexrp.com>

2 files changed, 3 insertions(+), 22 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,
lib/std/Progress.zig+2-2
...@@ -450,7 +450,7 @@ pub const Node = struct {...@@ -450,7 +450,7 @@ pub const Node = struct {
450 const ipc = @atomicLoad(Ipc, ipc_ptr, .monotonic);450 const ipc = @atomicLoad(Ipc, ipc_ptr, .monotonic);
451 if (ipc.locked or ipc.valid) continue;451 if (ipc.locked or ipc.valid) continue;
452 const generation = ipc.generation +% 1;452 const generation = ipc.generation +% 1;
453 if (@cmpxchgWeak(453 if (@cmpxchgStrong(
454 Ipc,454 Ipc,
455 ipc_ptr,455 ipc_ptr,
456 ipc,456 ipc,
...@@ -1133,7 +1133,7 @@ fn serialize(io: Io, serialized_buffer: *Serialized.Buffer) !Serialized {...@@ -1133,7 +1133,7 @@ fn serialize(io: Io, serialized_buffer: *Serialized.Buffer) !Serialized {
1133 const ipc_data = &serialized_buffer.ipc_data[ipc_index.slot];1133 const ipc_data = &serialized_buffer.ipc_data[ipc_index.slot];
1134 state: switch (ipc_data.state) {1134 state: switch (ipc_data.state) {
1135 .unused => {1135 .unused => {
1136 if (@cmpxchgWeak(1136 if (@cmpxchgStrong(
1137 Ipc,1137 Ipc,
1138 ipc,1138 ipc,
1139 .{ .locked = false, .valid = true, .generation = ipc_index.generation },1139 .{ .locked = false, .valid = true, .generation = ipc_index.generation },