zig/test/cases/atomics.zig

40 lines
1014 B
Zig
Raw Normal View History

2017-01-05 00:57:48 -08:00
const assert = @import("std").debug.assert;
2018-02-28 18:19:51 -08:00
const builtin = @import("builtin");
const AtomicRmwOp = builtin.AtomicRmwOp;
const AtomicOrder = builtin.AtomicOrder;
2017-01-05 00:57:48 -08:00
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;
}
2018-02-28 18:19:51 -08:00
2018-04-15 15:12:00 -07:00
test "atomicrmw and atomicload" {
2018-02-28 18:19:51 -08:00
var data: u8 = 200;
testAtomicRmw(&data);
assert(data == 42);
2018-04-15 15:12:00 -07:00
testAtomicLoad(&data);
2018-02-28 18:19:51 -08:00
}
fn testAtomicRmw(ptr: &u8) void {
const prev_value = @atomicRmw(u8, ptr, AtomicRmwOp.Xchg, 42, AtomicOrder.SeqCst);
assert(prev_value == 200);
2018-04-15 15:12:00 -07:00
comptime {
var x: i32 = 1234;
const y: i32 = 12345;
assert(@atomicLoad(i32, &x, AtomicOrder.SeqCst) == 1234);
assert(@atomicLoad(i32, &y, AtomicOrder.SeqCst) == 12345);
}
}
fn testAtomicLoad(ptr: &u8) void {
const x = @atomicLoad(u8, ptr, AtomicOrder.SeqCst);
assert(x == 42);
2018-02-28 18:19:51 -08:00
}