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