authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-12-09 21:04:39+01:00
committergravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-12-10 09:23:48+01:00
log5e91cc2fe38cd5fd4a481629ba1d72a8c66b46e6
tree280a1c10a5a922776a67f01efc3a16b3f9191c15
parent584451140843f8e1524f3cebf0ca3ab580685b3d

std: Validate the atomic ordering parameter in atomic.Int


2 files changed, 24 insertions(+), 0 deletions(-)

lib/std/atomic/bool.zig+12
......@@ -21,14 +21,26 @@ pub const Bool = extern struct {
2121 // xchg is only valid rmw operation for a bool
2222 /// Atomically modifies memory and then returns the previous value.
2323 pub fn xchg(self: *Self, operand: bool, comptime ordering: std.builtin.AtomicOrder) bool {
24 switch (ordering) {
25 .Monotonic, .Acquire, .Release, .AcqRel, .SeqCst => {},
26 else => @compileError("Invalid ordering '" ++ @tagName(ordering) ++ "' for a RMW operation"),
27 }
2428 return @atomicRmw(bool, &self.unprotected_value, .Xchg, operand, ordering);
2529 }
2630
2731 pub fn load(self: *Self, comptime ordering: std.builtin.AtomicOrder) bool {
32 switch (ordering) {
33 .Unordered, .Monotonic, .Acquire, .SeqCst => {},
34 else => @compileError("Invalid ordering '" ++ @tagName(ordering) ++ "' for a load operation"),
35 }
2836 return @atomicLoad(bool, &self.unprotected_value, ordering);
2937 }
3038
3139 pub fn store(self: *Self, value: bool, comptime ordering: std.builtin.AtomicOrder) void {
40 switch (ordering) {
41 .Unordered, .Monotonic, .Release, .SeqCst => {},
42 else => @compileError("Invalid ordering '" ++ @tagName(ordering) ++ "' for a store operation"),
43 }
3244 @atomicStore(bool, &self.unprotected_value, value, ordering);
3345 }
3446};
lib/std/atomic/int.zig+12
......@@ -24,14 +24,26 @@ pub fn Int(comptime T: type) type {
2424
2525 /// Read, Modify, Write
2626 pub fn rmw(self: *Self, comptime op: builtin.AtomicRmwOp, operand: T, comptime ordering: builtin.AtomicOrder) T {
27 switch (ordering) {
28 .Monotonic, .Acquire, .Release, .AcqRel, .SeqCst => {},
29 else => @compileError("Invalid ordering '" ++ @tagName(ordering) ++ "' for a RMW operation"),
30 }
2731 return @atomicRmw(T, &self.unprotected_value, op, operand, ordering);
2832 }
2933
3034 pub fn load(self: *Self, comptime ordering: builtin.AtomicOrder) T {
35 switch (ordering) {
36 .Unordered, .Monotonic, .Acquire, .SeqCst => {},
37 else => @compileError("Invalid ordering '" ++ @tagName(ordering) ++ "' for a load operation"),
38 }
3139 return @atomicLoad(T, &self.unprotected_value, ordering);
3240 }
3341
3442 pub fn store(self: *Self, value: T, comptime ordering: builtin.AtomicOrder) void {
43 switch (ordering) {
44 .Unordered, .Monotonic, .Release, .SeqCst => {},
45 else => @compileError("Invalid ordering '" ++ @tagName(ordering) ++ "' for a store operation"),
46 }
3547 @atomicStore(T, &self.unprotected_value, value, ordering);
3648 }
3749