From 76e13c3b7a4901def562b42060d99a6ac3daaa45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Gr=C3=A9goire?= Date: Sat, 28 Mar 2026 01:00:34 -0400 Subject: [PATCH] feat(ke): add try_acquire to spinlock --- src/kern/spinlock.zig | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/kern/spinlock.zig b/src/kern/spinlock.zig index e1f62aa..6c4c303 100644 --- a/src/kern/spinlock.zig +++ b/src/kern/spinlock.zig @@ -48,10 +48,20 @@ pub const SpinLock = struct { } /// Try to acquire the lock. Return true if lock was acquired. - pub fn try_acquire(self: *SpinLock) bool { + pub fn try_acquire_no_ipl(self: *SpinLock) bool { return @cmpxchgStrong(u8, &self.locked, 0, 1, .acquire, .monotonic) == null; } + pub fn try_acquire(self: *SpinLock) ?ke.Ipl { + const old_ipl = ke.ipl.raise(.Dispatch); + if (self.try_acquire_no_ipl()) { + return old_ipl; + } else { + ke.ipl.lower(old_ipl); + return null; + } + } + pub fn is_locked(self: *SpinLock) bool { return @atomicLoad(u8, &self.locked, .monotonic) == 1; }