sparreal_kernel/sync/
spin.rsuse core::{
hint::spin_loop,
sync::atomic::{AtomicBool, Ordering},
};
use lock_api::{GuardSend, RawMutex};
pub struct RawSpinlock(AtomicBool);
unsafe impl RawMutex for RawSpinlock {
const INIT: RawSpinlock = RawSpinlock(AtomicBool::new(false));
type GuardMarker = GuardSend;
fn lock(&self) {
while !self.try_lock() {
spin_loop();
}
}
fn try_lock(&self) -> bool {
self.0
.compare_exchange(false, true, Ordering::Acquire, Ordering::Relaxed)
.is_ok()
}
unsafe fn unlock(&self) {
self.0.store(false, Ordering::Release);
}
}
pub type Spinlock<T> = lock_api::Mutex<RawSpinlock, T>;
pub type SpinlockGuard<'a, T> = lock_api::MutexGuard<'a, RawSpinlock, T>;