#![allow(dead_code)]
use alloc::sync::Arc;
use core::{
cell::UnsafeCell,
fmt,
marker::PhantomData,
ops::{Deref, DerefMut},
sync::atomic::{AtomicBool, Ordering},
};
use crate::{
task::{disable_preempt, DisabledPreemptGuard},
trap::{disable_local, DisabledLocalIrqGuard},
};
#[repr(transparent)]
pub struct SpinLock<T: ?Sized, G = PreemptDisabled> {
phantom: PhantomData<G>,
inner: SpinLockInner<T>,
}
struct SpinLockInner<T: ?Sized> {
lock: AtomicBool,
val: UnsafeCell<T>,
}
pub trait Guardian {
type Guard;
fn guard() -> Self::Guard;
}
pub struct PreemptDisabled;
impl Guardian for PreemptDisabled {
type Guard = DisabledPreemptGuard;
fn guard() -> Self::Guard {
disable_preempt()
}
}
pub struct LocalIrqDisabled;
impl Guardian for LocalIrqDisabled {
type Guard = DisabledLocalIrqGuard;
fn guard() -> Self::Guard {
disable_local()
}
}
impl<T, G: Guardian> SpinLock<T, G> {
pub const fn new(val: T) -> Self {
let lock_inner = SpinLockInner {
lock: AtomicBool::new(false),
val: UnsafeCell::new(val),
};
Self {
phantom: PhantomData,
inner: lock_inner,
}
}
}
impl<T: ?Sized> SpinLock<T, PreemptDisabled> {
pub fn disable_irq(&self) -> &SpinLock<T, LocalIrqDisabled> {
let ptr = self as *const SpinLock<T, PreemptDisabled>;
let ptr = ptr as *const SpinLock<T, LocalIrqDisabled>;
unsafe { &*ptr }
}
}
impl<T: ?Sized, G: Guardian> SpinLock<T, G> {
pub fn lock(&self) -> SpinLockGuard<T, G> {
let inner_guard = G::guard();
self.acquire_lock();
SpinLockGuard_ {
lock: self,
guard: inner_guard,
}
}
pub fn lock_arc(self: &Arc<Self>) -> ArcSpinLockGuard<T, G> {
let inner_guard = G::guard();
self.acquire_lock();
SpinLockGuard_ {
lock: self.clone(),
guard: inner_guard,
}
}
pub fn try_lock(&self) -> Option<SpinLockGuard<T, G>> {
let inner_guard = G::guard();
if self.try_acquire_lock() {
let lock_guard = SpinLockGuard_ {
lock: self,
guard: inner_guard,
};
return Some(lock_guard);
}
None
}
fn acquire_lock(&self) {
while !self.try_acquire_lock() {
core::hint::spin_loop();
}
}
fn try_acquire_lock(&self) -> bool {
self.inner
.lock
.compare_exchange(false, true, Ordering::Acquire, Ordering::Relaxed)
.is_ok()
}
fn release_lock(&self) {
self.inner.lock.store(false, Ordering::Release);
}
}
impl<T: ?Sized + fmt::Debug, G> fmt::Debug for SpinLock<T, G> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(&self.inner.val, f)
}
}
unsafe impl<T: ?Sized + Send, G> Send for SpinLock<T, G> {}
unsafe impl<T: ?Sized + Send, G> Sync for SpinLock<T, G> {}
pub type SpinLockGuard<'a, T, G> = SpinLockGuard_<T, &'a SpinLock<T, G>, G>;
pub type ArcSpinLockGuard<T, G> = SpinLockGuard_<T, Arc<SpinLock<T, G>>, G>;
#[clippy::has_significant_drop]
#[must_use]
pub struct SpinLockGuard_<T: ?Sized, R: Deref<Target = SpinLock<T, G>>, G: Guardian> {
guard: G::Guard,
lock: R,
}
impl<T: ?Sized, R: Deref<Target = SpinLock<T, G>>, G: Guardian> Deref for SpinLockGuard_<T, R, G> {
type Target = T;
fn deref(&self) -> &T {
unsafe { &*self.lock.inner.val.get() }
}
}
impl<T: ?Sized, R: Deref<Target = SpinLock<T, G>>, G: Guardian> DerefMut
for SpinLockGuard_<T, R, G>
{
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { &mut *self.lock.inner.val.get() }
}
}
impl<T: ?Sized, R: Deref<Target = SpinLock<T, G>>, G: Guardian> Drop for SpinLockGuard_<T, R, G> {
fn drop(&mut self) {
self.lock.release_lock();
}
}
impl<T: ?Sized + fmt::Debug, R: Deref<Target = SpinLock<T, G>>, G: Guardian> fmt::Debug
for SpinLockGuard_<T, R, G>
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(&**self, f)
}
}
impl<T: ?Sized, R: Deref<Target = SpinLock<T, G>>, G: Guardian> !Send for SpinLockGuard_<T, R, G> {}
unsafe impl<T: ?Sized + Sync, R: Deref<Target = SpinLock<T, G>> + Sync, G: Guardian> Sync
for SpinLockGuard_<T, R, G>
{
}