22 lines
373 B
Zig
22 lines
373 B
Zig
//! Atomic based spin mutex
|
|
const std = @import("std");
|
|
|
|
mutex: std.atomic.Mutex = .unlocked,
|
|
|
|
pub fn lock(self: *@This()) void {
|
|
if (self.mutex.tryLock()) {
|
|
@branchHint(.likely);
|
|
return;
|
|
}
|
|
|
|
while (true) {
|
|
if (self.mutex.tryLock()) {
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn unlock(self: *@This()) void {
|
|
self.mutex.unlock();
|
|
}
|