std: Validate the atomic ordering parameter in atomic.Int

This commit is contained in:
LemonBoy 2020-12-09 21:04:39 +01:00
parent 5844511408
commit 5e91cc2fe3
2 changed files with 24 additions and 0 deletions

View File

@ -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);
}
};

View File

@ -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);
}