use core::cell::UnsafeCell;
use core::ops::{Deref, DerefMut};
use core::sync::atomic::AtomicBool;
use core::sync::atomic::Ordering::{AcqRel, Release};
#[derive(Default)]
pub struct SpinLock<T> {
locked: AtomicBool,
value: UnsafeCell<T>,
}
unsafe impl<T> Sync for SpinLock<T> where T: Send {}
pub struct Guard<'a, T> {
lock: &'a SpinLock<T>,
}
unsafe impl<T> Sync for Guard<'_, T> where T: Sync {}
impl<T> SpinLock<T> {
pub const fn new(value: T) -> Self {
Self {
locked: AtomicBool::new(false),
value: UnsafeCell::new(value),
}
}
#[inline]
pub fn lock(&self) -> Guard<'_, T> {
while self.locked.swap(true, AcqRel) {
core::hint::spin_loop();
}
Guard { lock: self }
}
}
impl<T> Deref for Guard<'_, T> {
type Target = T;
#[inline]
fn deref(&self) -> &T {
unsafe { &*self.lock.value.get() }
}
}
impl<T> DerefMut for Guard<'_, T> {
#[inline]
fn deref_mut(&mut self) -> &mut T {
unsafe { &mut *self.lock.value.get() }
}
}
impl<T> Drop for Guard<'_, T> {
#[inline]
fn drop(&mut self) {
self.lock.locked.store(false, Release);
}
}