Skip to main content

SpinLock

Struct SpinLock 

Source
pub struct SpinLock<T: ?Sized, G: Guardian = IrqDisabled> { /* private fields */ }
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.

Implementations§

Source§

impl<T, G: Guardian> SpinLock<T, G>

Source

pub const fn new(data: T) -> Self

Create a new, unlocked spinlock.

T must be Sized here because we construct a new value.

Source§

impl<T: ?Sized, G: Guardian> SpinLock<T, G>

Source

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.

Source

pub fn try_lock(&self) -> Option<SpinLockGuard<'_, T, G>>

Try to acquire the lock without spinning.

Source

pub fn owner_cpu(&self) -> usize

Returns the owner CPU index (usize::MAX if unlocked).

Source

pub fn get_mut(&mut self) -> &mut T

Returns a mutable reference to the underlying data.

By holding &mut self, the compiler guarantees exclusive access to the lock; no other reference exists, so the inner data can be accessed without acquiring the lock.

Source§

impl<T: ?Sized> SpinLock<T, IrqDisabled>

Source

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§

Source§

impl<T: ?Sized + Send, G: Guardian> Send for SpinLock<T, G>

Source§

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>
where G: Unpin, T: Unpin + ?Sized,

§

impl<T, G> UnsafeUnpin for SpinLock<T, G>
where T: UnsafeUnpin + ?Sized,

§

impl<T, G> UnwindSafe for SpinLock<T, G>
where G: UnwindSafe, T: UnwindSafe + ?Sized,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.