From d21cefaf10d500c3a5e88233ff8ead38c38f318f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Gr=C3=A9goire?= Date: Sat, 18 Jul 2026 16:11:05 -0400 Subject: [PATCH] ke/spinlock: use std.atomic --- src/kern/amd64/impl.zig | 2 +- src/kern/spinlock.zig | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/kern/amd64/impl.zig b/src/kern/amd64/impl.zig index ca6227e..db3e35b 100644 --- a/src/kern/amd64/impl.zig +++ b/src/kern/amd64/impl.zig @@ -60,7 +60,7 @@ pub const ThreadContext = extern struct { pub fn switch_to(self: *ThreadContext, new: *ThreadContext) callconv(.c) void { 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); } }; diff --git a/src/kern/spinlock.zig b/src/kern/spinlock.zig index 0b715ad..5ce441c 100644 --- a/src/kern/spinlock.zig +++ b/src/kern/spinlock.zig @@ -75,23 +75,23 @@ fn LockTemplate(comptime T: type) type { /// Simple Spin lock implementation. pub const SpinLock = LockTemplate(struct { - locked: u8, + locked: std.atomic.Value(u8), const Self = @This(); pub fn init() Self { return .{ - .locked = 0, + .locked = .init(0), }; } /// Acquire the lock without changing the IPL. pub fn acquire_no_ipl(self: *Self) void { while (true) { - if (@cmpxchgWeak(u8, &self.locked, 0, 1, .acquire, .monotonic) == null) + if (self.locked.cmpxchgWeak(0, 1, .acquire, .monotonic) == null) return; - while (@atomicLoad(u8, &self.locked, .monotonic) != 0) { + while (self.locked.load(.monotonic) != 0) { std.atomic.spinLoopHint(); } } @@ -99,16 +99,16 @@ pub const SpinLock = LockTemplate(struct { /// Release the lock without changing the IPL. 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. 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 { - return @atomicLoad(u8, &self.locked, .monotonic) == 1; + return self.locked.load(.monotonic) == 1; } });