zig/test/cases/atomics.zig

28 lines
646 B
Zig

const assert = @import("std").debug.assert;
const builtin = @import("builtin");
const AtomicRmwOp = builtin.AtomicRmwOp;
const AtomicOrder = builtin.AtomicOrder;
test "cmpxchg" {
var x: i32 = 1234;
while (!@cmpxchg(&x, 1234, 5678, AtomicOrder.SeqCst, AtomicOrder.SeqCst)) {}
assert(x == 5678);
}
test "fence" {
var x: i32 = 1234;
@fence(AtomicOrder.SeqCst);
x = 5678;
}
test "atomicrmw" {
var data: u8 = 200;
testAtomicRmw(&data);
assert(data == 42);
}
fn testAtomicRmw(ptr: &u8) void {
const prev_value = @atomicRmw(u8, ptr, AtomicRmwOp.Xchg, 42, AtomicOrder.SeqCst);
assert(prev_value == 200);
}