diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2020-12-10 16:13:36 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-12-10 16:13:36 -0500 |
| commit | a3de27ef3bbc5212b2129ed75bab3503da09b9cf (patch) | |
| tree | c0b7d12e3d81df6ff1683c8376442efafbcdf803 /lib/std/atomic | |
| parent | 55cac65f957fc374e4e369e26bd338f11b8b37ee (diff) | |
| parent | 88e3a7d6dc7289103b8a644aaf6a63437ff6b6b5 (diff) | |
| download | zig-a3de27ef3bbc5212b2129ed75bab3503da09b9cf.tar.gz zig-a3de27ef3bbc5212b2129ed75bab3503da09b9cf.zip | |
Merge pull request #7372 from LemonBoy/atomicint
Improvements for std.atomic.{Int,Bool}
Diffstat (limited to 'lib/std/atomic')
| -rw-r--r-- | lib/std/atomic/bool.zig | 12 | ||||
| -rw-r--r-- | lib/std/atomic/int.zig | 15 |
2 files changed, 27 insertions, 0 deletions
diff --git a/lib/std/atomic/bool.zig b/lib/std/atomic/bool.zig index c686fdfae0..27a265bbc1 100644 --- a/lib/std/atomic/bool.zig +++ b/lib/std/atomic/bool.zig @@ -21,14 +21,26 @@ pub const Bool = extern struct { // xchg is only valid rmw operation for a bool /// Atomically modifies memory and then returns the previous value. pub fn xchg(self: *Self, operand: bool, comptime ordering: std.builtin.AtomicOrder) bool { + switch (ordering) { + .Monotonic, .Acquire, .Release, .AcqRel, .SeqCst => {}, + else => @compileError("Invalid ordering '" ++ @tagName(ordering) ++ "' for a RMW operation"), + } return @atomicRmw(bool, &self.unprotected_value, .Xchg, operand, ordering); } pub fn load(self: *Self, comptime ordering: std.builtin.AtomicOrder) bool { + switch (ordering) { + .Unordered, .Monotonic, .Acquire, .SeqCst => {}, + else => @compileError("Invalid ordering '" ++ @tagName(ordering) ++ "' for a load operation"), + } return @atomicLoad(bool, &self.unprotected_value, ordering); } pub fn store(self: *Self, value: bool, comptime ordering: std.builtin.AtomicOrder) void { + switch (ordering) { + .Unordered, .Monotonic, .Release, .SeqCst => {}, + else => @compileError("Invalid ordering '" ++ @tagName(ordering) ++ "' for a store operation"), + } @atomicStore(bool, &self.unprotected_value, value, ordering); } }; diff --git a/lib/std/atomic/int.zig b/lib/std/atomic/int.zig index 155a340b0a..b06575e05f 100644 --- a/lib/std/atomic/int.zig +++ b/lib/std/atomic/int.zig @@ -10,6 +10,9 @@ const testing = std.testing; /// Thread-safe, lock-free integer pub fn Int(comptime T: type) type { + if (!std.meta.trait.isIntegral(T)) + @compileError("Expected integral type, got '" ++ @typeName(T) ++ "'"); + return extern struct { unprotected_value: T, @@ -21,14 +24,26 @@ pub fn Int(comptime T: type) type { /// Read, Modify, Write pub fn rmw(self: *Self, comptime op: builtin.AtomicRmwOp, operand: T, comptime ordering: builtin.AtomicOrder) T { + switch (ordering) { + .Monotonic, .Acquire, .Release, .AcqRel, .SeqCst => {}, + else => @compileError("Invalid ordering '" ++ @tagName(ordering) ++ "' for a RMW operation"), + } return @atomicRmw(T, &self.unprotected_value, op, operand, ordering); } pub fn load(self: *Self, comptime ordering: builtin.AtomicOrder) T { + switch (ordering) { + .Unordered, .Monotonic, .Acquire, .SeqCst => {}, + else => @compileError("Invalid ordering '" ++ @tagName(ordering) ++ "' for a load operation"), + } return @atomicLoad(T, &self.unprotected_value, ordering); } pub fn store(self: *Self, value: T, comptime ordering: builtin.AtomicOrder) void { + switch (ordering) { + .Unordered, .Monotonic, .Release, .SeqCst => {}, + else => @compileError("Invalid ordering '" ++ @tagName(ordering) ++ "' for a store operation"), + } @atomicStore(T, &self.unprotected_value, value, ordering); } |
