pub struct SpinLock<T: ?Sized, G: Guardian = IrqDisabled> {
locked: AtomicBool,
owner_cpu: AtomicUsize,
_guardian: PhantomData<G>,
data: UnsafeCell<T>,
}Expand description
A spinlock parameterised by a Guardian.
The default guardian is IrqDisabled, preserving existing call-site
semantics with no source changes required.
Supports T: ?Sized for SpinLock<dyn Trait> and other DST types.
The data field is last so that the struct can hold dynamically sized types.
Fields§
§locked: AtomicBool§owner_cpu: AtomicUsizeCPU index of the current lock holder (usize::MAX when unlocked).
Used for deadlock diagnostics only.
_guardian: PhantomData<G>§data: UnsafeCell<T>Must be the last field: when T: ?Sized, this is a DST and Rust
requires the dynamically sized field to be last in the struct.
Implementations§
Source§impl<T: ?Sized, G: Guardian> SpinLock<T, G>
impl<T: ?Sized, G: Guardian> SpinLock<T, G>
Sourcepub fn lock(&self) -> SpinLockGuard<'_, T, G>
pub fn lock(&self) -> SpinLockGuard<'_, T, G>
Acquire the lock, spinning until available.
The guardian’s enter() hook runs before the spin loop so that the
CPU is already in the protected mode while we wait. This closes the
window where an IRQ or preempt-switch could occur between protect and
acquire.
Sourcepub fn try_lock(&self) -> Option<SpinLockGuard<'_, T, G>>
pub fn try_lock(&self) -> Option<SpinLockGuard<'_, T, G>>
Try to acquire the lock without spinning.
Source§impl<T: ?Sized> SpinLock<T, IrqDisabled>
impl<T: ?Sized> SpinLock<T, IrqDisabled>
Sourcepub fn try_lock_no_irqsave(&self) -> Option<SpinLockGuard<'_, T, IrqDisabled>>
pub fn try_lock_no_irqsave(&self) -> Option<SpinLockGuard<'_, T, IrqDisabled>>
Try to acquire without touching RFLAGS.
Returns None if IRQs are currently enabled (no IrqDisabledToken can
be produced) or the lock is already held. The caller must ensure that
IRQs remain disabled for the entire lifetime of the returned guard.
Trait Implementations§
impl<T: ?Sized + Send, G: Guardian> Send for SpinLock<T, G>
impl<T: ?Sized + Send, G: Guardian> Sync for SpinLock<T, G>
Auto Trait Implementations§
impl<T, G = IrqDisabled> !Freeze for SpinLock<T, G>
impl<T, G = IrqDisabled> !RefUnwindSafe for SpinLock<T, G>
impl<T, G> Unpin for SpinLock<T, G>
impl<T, G> UnsafeUnpin for SpinLock<T, G>where
T: UnsafeUnpin + ?Sized,
impl<T, G> UnwindSafe for SpinLock<T, G>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more