Skip to content

Commit fc87f6e

Browse files
committed
fix race condition bug in test harness of std.atomic
1 parent e1f56c9 commit fc87f6e

File tree

2 files changed

+6
-8
lines changed

2 files changed

+6
-8
lines changed

std/atomic/queue.zig

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,14 @@ fn startPuts(ctx: *Context) u8 {
124124

125125
fn startGets(ctx: *Context) u8 {
126126
while (true) {
127+
const last = @atomicLoad(u8, &ctx.puts_done, builtin.AtomicOrder.SeqCst) == 1;
128+
127129
while (ctx.queue.get()) |node| {
128130
std.os.time.sleep(0, 1); // let the os scheduler be our fuzz
129131
_ = @atomicRmw(isize, &ctx.get_sum, builtin.AtomicRmwOp.Add, node.data, builtin.AtomicOrder.SeqCst);
130132
_ = @atomicRmw(usize, &ctx.get_count, builtin.AtomicRmwOp.Add, 1, builtin.AtomicOrder.SeqCst);
131133
}
132134

133-
if (@atomicLoad(u8, &ctx.puts_done, builtin.AtomicOrder.SeqCst) == 1) {
134-
break;
135-
}
135+
if (last) return 0;
136136
}
137-
return 0;
138137
}

std/atomic/stack.zig

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,14 @@ fn startPuts(ctx: *Context) u8 {
127127

128128
fn startGets(ctx: *Context) u8 {
129129
while (true) {
130+
const last = @atomicLoad(u8, &ctx.puts_done, builtin.AtomicOrder.SeqCst) == 1;
131+
130132
while (ctx.stack.pop()) |node| {
131133
std.os.time.sleep(0, 1); // let the os scheduler be our fuzz
132134
_ = @atomicRmw(isize, &ctx.get_sum, builtin.AtomicRmwOp.Add, node.data, builtin.AtomicOrder.SeqCst);
133135
_ = @atomicRmw(usize, &ctx.get_count, builtin.AtomicRmwOp.Add, 1, builtin.AtomicOrder.SeqCst);
134136
}
135137

136-
if (@atomicLoad(u8, &ctx.puts_done, builtin.AtomicOrder.SeqCst) == 1) {
137-
break;
138-
}
138+
if (last) return 0;
139139
}
140-
return 0;
141140
}

0 commit comments

Comments
 (0)