-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
30 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,34 @@ | ||
const std = @import("std"); | ||
const builtin = @import("builtin"); | ||
const atomic = @import("std").atomic; | ||
|
||
/// Test and test-and-set spinlock | ||
pub const SpinLock = struct { | ||
state: State = State.init(unlocked), | ||
state: State = State.init(.unlocked), | ||
|
||
const State = std.atomic.Atomic(u32); | ||
const unlocked = 0b00; | ||
const locked = 0b01; | ||
const contended = 0b11; // TODO: use contended for lockSlow / unlock | ||
const State = atomic.Atomic(enum(u32) { unlocked, locked }); | ||
|
||
/// return true on success | ||
pub inline fn tryLock(self: *SpinLock) bool { | ||
return self.lockFast("compareAndSwap"); | ||
return if (self.isUnlocked()) self.lockImpl("compareAndSwap") else false; | ||
} | ||
|
||
pub inline fn lock(self: *SpinLock) void { | ||
if (!self.lockFast("tryCompareAndSwap")) { | ||
self.lockSlow(); | ||
pub fn lock(self: *SpinLock) void { | ||
while (!self.lockImpl("tryCompareAndSwap")) { | ||
while (!self.isUnlocked()) { | ||
atomic.spinLoopHint(); | ||
} | ||
} | ||
} | ||
|
||
inline fn lockFast(self: *SpinLock, comptime cas_fn_name: []const u8) bool { | ||
// optimization for x86 | ||
if (comptime builtin.target.cpu.arch.isX86()) { | ||
const locked_bit = @ctz(@as(u32, locked)); | ||
return self.state.bitSet(locked_bit, .Acquire) == 0; | ||
} | ||
|
||
const casFn = @field(@TypeOf(self.state), cas_fn_name); | ||
return casFn(&self.state, unlocked, locked, .Acquire, .Monotonic) == null; | ||
pub inline fn unlock(self: *SpinLock) void { | ||
self.state.store(.unlocked, .Release); | ||
} | ||
|
||
noinline fn lockSlow(self: *SpinLock) void { | ||
@setCold(true); | ||
|
||
for (0..100_000_000) |_| { | ||
if (self.lockFast("tryCompareAndSwap")) { | ||
return; | ||
} | ||
std.atomic.spinLoopHint(); | ||
} | ||
|
||
@panic("Deadlock"); | ||
inline fn isUnlocked(self: *SpinLock) bool { | ||
return self.state.load(.Monotonic) == .unlocked; | ||
} | ||
|
||
pub inline fn unlock(self: *SpinLock) void { | ||
const state = self.state.swap(unlocked, .Release); | ||
std.debug.assert(state != unlocked); | ||
inline fn lockImpl(self: *SpinLock, comptime cas_fn_name: []const u8) bool { | ||
const casFn = @field(@TypeOf(self.state), cas_fn_name); | ||
return casFn(&self.state, .unlocked, .locked, .Acquire, .Monotonic) == null; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters