Skip to content

Commit

Permalink
std: Validate the atomic ordering parameter in atomic.Int
Browse files Browse the repository at this point in the history
  • Loading branch information
LemonBoy authored and aarvay committed Jan 4, 2021
1 parent ce6f262 commit 1484b43
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/std/atomic/bool.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};
Expand Down
12 changes: 12 additions & 0 deletions lib/std/atomic/int.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit 1484b43

Please sign in to comment.