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
|
|
|
|
2017-03-16 13:02:35 -07:00
|
|
|
test "cmpxchg" {
|
2016-12-18 21:41:37 -08:00
|
|
|
var x: i32 = 1234;
|
|
|
|
while (!@cmpxchg(&x, 1234, 5678, AtomicOrder.SeqCst, AtomicOrder.SeqCst)) {}
|
|
|
|
assert(x == 5678);
|
|
|
|
}
|
|
|
|
|
2017-03-16 13:02:35 -07:00
|
|
|
test "fence" {
|
2016-12-18 21:41:37 -08:00
|
|
|
var x: i32 = 1234;
|
|
|
|
@fence(AtomicOrder.SeqCst);
|
|
|
|
x = 5678;
|
|
|
|
}
|
2018-02-28 18:19:51 -08:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|