mirror of
https://github.com/ziglang/zig.git
synced 2025-02-15 09:00:16 +00:00
std: Validate the atomic ordering parameter in atomic.Int
This commit is contained in:
parent
5844511408
commit
5e91cc2fe3
@ -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);
|
||||
}
|
||||
};
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user