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; }