| ... | ... | @@ -2,29 +2,32 @@ const std = @import("std"); |
| 2 | 2 | const expect = std.testing.expect; |
| 3 | 3 | const expectEqual = std.testing.expectEqual; |
| 4 | 4 | const builtin = @import("builtin"); |
| 5 | | const AtomicRmwOp = builtin.AtomicRmwOp; |
| 6 | | const AtomicOrder = builtin.AtomicOrder; |
| 7 | 5 | |
| 8 | 6 | test "cmpxchg" { |
| 7 | testCmpxchg(); |
| 8 | comptime testCmpxchg(); |
| 9 | } |
| 10 | |
| 11 | fn testCmpxchg() void { |
| 9 | 12 | var x: i32 = 1234; |
| 10 | | if (@cmpxchgWeak(i32, &x, 99, 5678, AtomicOrder.SeqCst, AtomicOrder.SeqCst)) |x1| { |
| 13 | if (@cmpxchgWeak(i32, &x, 99, 5678, .SeqCst, .SeqCst)) |x1| { |
| 11 | 14 | expect(x1 == 1234); |
| 12 | 15 | } else { |
| 13 | 16 | @panic("cmpxchg should have failed"); |
| 14 | 17 | } |
| 15 | 18 | |
| 16 | | while (@cmpxchgWeak(i32, &x, 1234, 5678, AtomicOrder.SeqCst, AtomicOrder.SeqCst)) |x1| { |
| 19 | while (@cmpxchgWeak(i32, &x, 1234, 5678, .SeqCst, .SeqCst)) |x1| { |
| 17 | 20 | expect(x1 == 1234); |
| 18 | 21 | } |
| 19 | 22 | expect(x == 5678); |
| 20 | 23 | |
| 21 | | expect(@cmpxchgStrong(i32, &x, 5678, 42, AtomicOrder.SeqCst, AtomicOrder.SeqCst) == null); |
| 24 | expect(@cmpxchgStrong(i32, &x, 5678, 42, .SeqCst, .SeqCst) == null); |
| 22 | 25 | expect(x == 42); |
| 23 | 26 | } |
| 24 | 27 | |
| 25 | 28 | test "fence" { |
| 26 | 29 | var x: i32 = 1234; |
| 27 | | @fence(AtomicOrder.SeqCst); |
| 30 | @fence(.SeqCst); |
| 28 | 31 | x = 5678; |
| 29 | 32 | } |
| 30 | 33 | |
| ... | ... | @@ -36,18 +39,18 @@ test "atomicrmw and atomicload" { |
| 36 | 39 | } |
| 37 | 40 | |
| 38 | 41 | fn testAtomicRmw(ptr: *u8) void { |
| 39 | | const prev_value = @atomicRmw(u8, ptr, AtomicRmwOp.Xchg, 42, AtomicOrder.SeqCst); |
| 42 | const prev_value = @atomicRmw(u8, ptr, .Xchg, 42, .SeqCst); |
| 40 | 43 | expect(prev_value == 200); |
| 41 | 44 | comptime { |
| 42 | 45 | var x: i32 = 1234; |
| 43 | 46 | const y: i32 = 12345; |
| 44 | | expect(@atomicLoad(i32, &x, AtomicOrder.SeqCst) == 1234); |
| 45 | | expect(@atomicLoad(i32, &y, AtomicOrder.SeqCst) == 12345); |
| 47 | expect(@atomicLoad(i32, &x, .SeqCst) == 1234); |
| 48 | expect(@atomicLoad(i32, &y, .SeqCst) == 12345); |
| 46 | 49 | } |
| 47 | 50 | } |
| 48 | 51 | |
| 49 | 52 | fn testAtomicLoad(ptr: *u8) void { |
| 50 | | const x = @atomicLoad(u8, ptr, AtomicOrder.SeqCst); |
| 53 | const x = @atomicLoad(u8, ptr, .SeqCst); |
| 51 | 54 | expect(x == 42); |
| 52 | 55 | } |
| 53 | 56 | |
| ... | ... | @@ -56,18 +59,18 @@ test "cmpxchg with ptr" { |
| 56 | 59 | var data2: i32 = 5678; |
| 57 | 60 | var data3: i32 = 9101; |
| 58 | 61 | var x: *i32 = &data1; |
| 59 | | if (@cmpxchgWeak(*i32, &x, &data2, &data3, AtomicOrder.SeqCst, AtomicOrder.SeqCst)) |x1| { |
| 62 | if (@cmpxchgWeak(*i32, &x, &data2, &data3, .SeqCst, .SeqCst)) |x1| { |
| 60 | 63 | expect(x1 == &data1); |
| 61 | 64 | } else { |
| 62 | 65 | @panic("cmpxchg should have failed"); |
| 63 | 66 | } |
| 64 | 67 | |
| 65 | | while (@cmpxchgWeak(*i32, &x, &data1, &data3, AtomicOrder.SeqCst, AtomicOrder.SeqCst)) |x1| { |
| 68 | while (@cmpxchgWeak(*i32, &x, &data1, &data3, .SeqCst, .SeqCst)) |x1| { |
| 66 | 69 | expect(x1 == &data1); |
| 67 | 70 | } |
| 68 | 71 | expect(x == &data3); |
| 69 | 72 | |
| 70 | | expect(@cmpxchgStrong(*i32, &x, &data3, &data2, AtomicOrder.SeqCst, AtomicOrder.SeqCst) == null); |
| 73 | expect(@cmpxchgStrong(*i32, &x, &data3, &data2, .SeqCst, .SeqCst) == null); |
| 71 | 74 | expect(x == &data2); |
| 72 | 75 | } |
| 73 | 76 | |
| ... | ... | @@ -163,16 +166,17 @@ fn testAtomicRmwFloat() void { |
| 163 | 166 | } |
| 164 | 167 | |
| 165 | 168 | test "atomics with different types" { |
| 166 | | // testAtomicsWithType(bool, true, false); |
| 167 | | // inline for (.{ u1, i5, u33 }) |T| { |
| 168 | | // var x: T = 0; |
| 169 | | // testAtomicsWithType(T, 0, 1); |
| 170 | | // } |
| 169 | testAtomicsWithType(bool, true, false); |
| 170 | inline for (.{ u1, i5, u33 }) |T| { |
| 171 | var x: T = 0; |
| 172 | testAtomicsWithType(T, 0, 1); |
| 173 | } |
| 171 | 174 | testAtomicsWithType(u0, 0, 0); |
| 172 | 175 | testAtomicsWithType(i0, 0, 0); |
| 173 | 176 | } |
| 174 | 177 | |
| 175 | | fn testAtomicsWithType(comptime T: type, a: T, b: T) void { |
| 178 | // a and b souldn't need to be comptime |
| 179 | fn testAtomicsWithType(comptime T: type, comptime a: T, comptime b: T) void { |
| 176 | 180 | var x: T = b; |
| 177 | 181 | @atomicStore(T, &x, a, .SeqCst); |
| 178 | 182 | expect(x == a); |