feat(ke): add try_acquire to spinlock

This commit is contained in:
Loïc Grégoire
2026-03-28 01:00:34 -04:00
parent d8003d06d3
commit 76e13c3b7a
+11 -1
View File
@@ -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;
}