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 {
13371337 return @ptrFromInt(@as(usize, @bitCast(split)));
13381338 }
13391339 };
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 }
13591340};
13601341
13611342const Syscall = struct {
......@@ -18663,7 +18644,7 @@ fn condWait(cond: *Io.Condition, mutex: *Io.Mutex) void {
1866318644
1866418645/// Same as `Io.Mutex.lockUncancelable` but avoids the VTable.
1866518646pub fn mutexLock(m: *Io.Mutex) void {
18666 const initial_state = m.state.cmpxchgWeak(
18647 const initial_state = m.state.cmpxchgStrong(
1866718648 .unlocked,
1866818649 .locked_once,
1866918650 .acquire,
lib/std/Progress.zig+2-2
......@@ -450,7 +450,7 @@ pub const Node = struct {
450450 const ipc = @atomicLoad(Ipc, ipc_ptr, .monotonic);
451451 if (ipc.locked or ipc.valid) continue;
452452 const generation = ipc.generation +% 1;
453 if (@cmpxchgWeak(
453 if (@cmpxchgStrong(
454454 Ipc,
455455 ipc_ptr,
456456 ipc,
......@@ -1133,7 +1133,7 @@ fn serialize(io: Io, serialized_buffer: *Serialized.Buffer) !Serialized {
11331133 const ipc_data = &serialized_buffer.ipc_data[ipc_index.slot];
11341134 state: switch (ipc_data.state) {
11351135 .unused => {
1136 if (@cmpxchgWeak(
1136 if (@cmpxchgStrong(
11371137 Ipc,
11381138 ipc,
11391139 .{ .locked = false, .valid = true, .generation = ipc_index.generation },