mirror of
https://github.com/rdmsr/zag.git
synced 2026-11-01 23:58:21 +00:00
feat(ke): add try_acquire to spinlock
This commit is contained in:
+11
-1
@@ -48,10 +48,20 @@ pub const SpinLock = struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// 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(self: *SpinLock) bool {
|
pub fn try_acquire_no_ipl(self: *SpinLock) bool {
|
||||||
return @cmpxchgStrong(u8, &self.locked, 0, 1, .acquire, .monotonic) == null;
|
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 {
|
pub fn is_locked(self: *SpinLock) bool {
|
||||||
return @atomicLoad(u8, &self.locked, .monotonic) == 1;
|
return @atomicLoad(u8, &self.locked, .monotonic) == 1;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user