27 lines
572 B
Zig
27 lines
572 B
Zig
pub const SpinLock = struct {
|
|
const Self = @This();
|
|
lock: u8,
|
|
|
|
pub fn init() Self {
|
|
return .{
|
|
.lock = 0,
|
|
};
|
|
}
|
|
|
|
pub fn acquire(self: *Self) void {
|
|
while (@atomicRmw(u8, &self.lock, .Xchg, 1, .Acquire) != 0) {
|
|
asm volatile ("pause");
|
|
}
|
|
}
|
|
|
|
pub fn release(self: *Self) void {
|
|
@atomicStore(u8, &self.lock, 0, .Release);
|
|
}
|
|
|
|
pub fn wait(self: *const Self) void {
|
|
while (@atomicLoad(u8, &self.lock, .Acquire) != 0) {
|
|
asm volatile ("pause");
|
|
}
|
|
}
|
|
};
|