ke/spinlock: use std.atomic

This commit is contained in:
Loïc Grégoire
2026-07-18 16:11:05 -04:00
parent fddd9865f7
commit d21cefaf10
2 changed files with 8 additions and 8 deletions
+1 -1
View File
@@ -60,7 +60,7 @@ pub const ThreadContext = extern struct {
pub fn switch_to(self: *ThreadContext, new: *ThreadContext) callconv(.c) void { pub fn switch_to(self: *ThreadContext, new: *ThreadContext) callconv(.c) void {
const thread: *ke.Thread = @alignCast(@fieldParentPtr("context", self)); const thread: *ke.Thread = @alignCast(@fieldParentPtr("context", self));
do_context_switch(self, new, &thread.lock.inner.locked); do_context_switch(self, new, &thread.lock.inner.locked.raw);
} }
}; };
+7 -7
View File
@@ -75,23 +75,23 @@ fn LockTemplate(comptime T: type) type {
/// Simple Spin lock implementation. /// Simple Spin lock implementation.
pub const SpinLock = LockTemplate(struct { pub const SpinLock = LockTemplate(struct {
locked: u8, locked: std.atomic.Value(u8),
const Self = @This(); const Self = @This();
pub fn init() Self { pub fn init() Self {
return .{ return .{
.locked = 0, .locked = .init(0),
}; };
} }
/// Acquire the lock without changing the IPL. /// Acquire the lock without changing the IPL.
pub fn acquire_no_ipl(self: *Self) void { pub fn acquire_no_ipl(self: *Self) void {
while (true) { while (true) {
if (@cmpxchgWeak(u8, &self.locked, 0, 1, .acquire, .monotonic) == null) if (self.locked.cmpxchgWeak(0, 1, .acquire, .monotonic) == null)
return; return;
while (@atomicLoad(u8, &self.locked, .monotonic) != 0) { while (self.locked.load(.monotonic) != 0) {
std.atomic.spinLoopHint(); std.atomic.spinLoopHint();
} }
} }
@@ -99,16 +99,16 @@ pub const SpinLock = LockTemplate(struct {
/// Release the lock without changing the IPL. /// Release the lock without changing the IPL.
pub fn release_no_ipl(self: *Self) void { pub fn release_no_ipl(self: *Self) void {
@atomicStore(u8, &self.locked, 0, .release); self.locked.store(0, .release);
} }
/// Try to acquire the lock. Return true if lock was acquired. /// Try to acquire the lock. Return true if lock was acquired.
pub fn try_acquire_no_ipl(self: *Self) bool { pub fn try_acquire_no_ipl(self: *Self) bool {
return @cmpxchgStrong(u8, &self.locked, 0, 1, .acquire, .monotonic) == null; return self.locked.cmpxchgStrong(0, 1, .acquire, .monotonic) == null;
} }
pub fn is_locked(self: *Self) bool { pub fn is_locked(self: *Self) bool {
return @atomicLoad(u8, &self.locked, .monotonic) == 1; return self.locked.load(.monotonic) == 1;
} }
}); });