Spinlock

Struct Spinlock 

Source
pub struct Spinlock { /* private fields */ }
Expand description

The underlying raw reader-writer primitive that implements lock_api::RawRwLock

This is fundamentally a spinlock, in that blocking operations on the lock will spin until they succeed in acquiring/releasing the lock.

To achieve the ability to share the underlying data with multiple readers, or hold exclusive access for one writer, the lock state is based on a “locked” count, where shared access increments the count by an even number, and acquiring exclusive access relies on the use of the lowest order bit to stop further shared acquisition, and indicate that the lock is exclusively held (the difference between the two is irrelevant from the perspective of a thread attempting to acquire the lock, but internally the state uses usize::MAX as the “exclusively locked” sentinel).

This mechanism gets us the following:

  • Whether the lock has been acquired (shared or exclusive)
  • Whether the lock is being exclusively acquired
  • How many times the lock has been acquired
  • Whether the acquisition(s) are exclusive or shared

Further implementation details, such as how we manage draining readers once an attempt to exclusively acquire the lock occurs, are described below.

NOTE: This is a simple implementation, meant for use in no-std environments; there are much more robust/performant implementations available when OS primitives can be used.

Implementations§

Source§

impl Spinlock

Source

pub const fn new() -> Self

Trait Implementations§

Source§

impl Default for Spinlock

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl RawRwLock for Spinlock

Source§

fn lock_shared(&self)

The operation invoked when calling RwLock::read, blocks the caller until acquired

Source§

fn try_lock_shared(&self) -> bool

The operation invoked when calling RwLock::try_read, returns whether or not the lock was acquired

Source§

unsafe fn unlock_shared(&self)

The operation invoked when dropping a RwLockReadGuard

Source§

fn lock_exclusive(&self)

The operation invoked when calling RwLock::write, blocks the caller until acquired

Source§

fn try_lock_exclusive(&self) -> bool

The operation invoked when calling RwLock::try_write, returns whether or not the lock was acquired

Source§

unsafe fn unlock_exclusive(&self)

The operation invoked when dropping a RwLockWriteGuard

Source§

const INIT: Spinlock

Initial value for an unlocked RwLock.
Source§

type GuardMarker = GuardSend

Marker type which determines whether a lock guard should be Send. Use one of the GuardSend or GuardNoSend helper types here.
Source§

fn is_locked(&self) -> bool

Checks if this RwLock is currently locked in any way.
Source§

fn is_locked_exclusive(&self) -> bool

Check if this RwLock is currently exclusively locked.

Auto Trait Implementations§

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, 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.