mirror of
https://github.com/ziglang/zig.git
synced 2026-01-16 12:25:21 +00:00
* remove @cmpxchg, add @cmpxchgWeak and @cmpxchgStrong - See explanations in the langref. * add operand type as first parameter * return type is ?T where T is the operand type closes #461
52 lines
1.3 KiB
Zig
52 lines
1.3 KiB
Zig
const std = @import("std");
|
|
const assert = std.debug.assert;
|
|
const builtin = @import("builtin");
|
|
const AtomicRmwOp = builtin.AtomicRmwOp;
|
|
const AtomicOrder = builtin.AtomicOrder;
|
|
|
|
test "cmpxchg" {
|
|
var x: i32 = 1234;
|
|
if (@cmpxchgWeak(i32, &x, 99, 5678, AtomicOrder.SeqCst, AtomicOrder.SeqCst)) |x1| {
|
|
assert(x1 == 1234);
|
|
} else {
|
|
@panic("cmpxchg should have failed");
|
|
}
|
|
|
|
while (@cmpxchgWeak(i32, &x, 1234, 5678, AtomicOrder.SeqCst, AtomicOrder.SeqCst)) |x1| {
|
|
assert(x1 == 1234);
|
|
}
|
|
assert(x == 5678);
|
|
|
|
assert(@cmpxchgStrong(i32, &x, 5678, 42, AtomicOrder.SeqCst, AtomicOrder.SeqCst) == null);
|
|
assert(x == 42);
|
|
}
|
|
|
|
test "fence" {
|
|
var x: i32 = 1234;
|
|
@fence(AtomicOrder.SeqCst);
|
|
x = 5678;
|
|
}
|
|
|
|
test "atomicrmw and atomicload" {
|
|
var data: u8 = 200;
|
|
testAtomicRmw(&data);
|
|
assert(data == 42);
|
|
testAtomicLoad(&data);
|
|
}
|
|
|
|
fn testAtomicRmw(ptr: &u8) void {
|
|
const prev_value = @atomicRmw(u8, ptr, AtomicRmwOp.Xchg, 42, AtomicOrder.SeqCst);
|
|
assert(prev_value == 200);
|
|
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);
|
|
}
|