use core::{
cell::UnsafeCell,
ops::{Deref, DerefMut},
sync::atomic::{
AtomicBool,
Ordering::{Acquire, Release},
},
};
pub struct SpinLock<T> {
locked: AtomicBool,
contents: UnsafeCell<T>,
}
unsafe impl<T> Sync for SpinLock<T> {}
pub struct SpinLockGuard<'a, T> {
lock: &'a SpinLock<T>,
}
impl<T> SpinLock<T> {
pub const fn new(inner: T) -> Self {
Self {
locked: AtomicBool::new(false),
contents: UnsafeCell::new(inner),
}
}
pub fn lock(&self) -> SpinLockGuard<T> {
while self
.locked
.compare_exchange(false, true, Acquire, Acquire)
.is_err()
{
core::hint::spin_loop();
}
SpinLockGuard { lock: self }
}
}
impl<T> Drop for SpinLockGuard<'_, T> {
fn drop(&mut self) {
self.lock.locked.store(false, Release);
}
}
impl<T> Deref for SpinLockGuard<'_, T> {
type Target = T;
fn deref(&self) -> &T {
unsafe { &*self.lock.contents.get() }
}
}
impl<T> DerefMut for SpinLockGuard<'_, T> {
fn deref_mut(&mut self) -> &mut T {
unsafe { &mut *self.lock.contents.get() }
}
}