Commit 5e91cc2fe3

LemonBoy <thatlemon@gmail.com>
2020-12-09 21:04:39
std: Validate the atomic ordering parameter in atomic.Int
1 parent 5844511
Changed files (2)
lib
std
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);
     }
 };
lib/std/atomic/int.zig
@@ -24,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);
         }