Struct wasm::rw_spinlock::RwSpinLock

source ·
pub struct RwSpinLock<T> {
    inner: UnsafeCell<T>,
    state: AtomicU32,
}
Expand description

A spinlock based, read-write lock which favours writers over readers

§Properties

  • Read-write semantics allow for multiple readers at the same time, but require exclusive access for a writer
  • Spin based, e.g. waiting for the lock wastes CPU cycles in a busy loop
    • This design however enables an implementation independent of operating system features like condition variables, the only requirement are 32 bit atomics in the ISA
  • Biased towards writes: once a writer waits for the lock, all new reader wait until that writer got access

Fields§

§inner: UnsafeCell<T>

The inner data protected by this lock

§state: AtomicU32

Lock state (on ambiguity, the state closer to the top takes precedence)

  • 0 means there are no readers nor any writer
  • u32::MAX means there is a single active writer
  • state % 2 == 0 means there are state / 2 active readers
  • state % 2 != 0 means there are (state - 1) / 2 active readers and at least one waiting writer

Implementations§

source§

impl<T> RwSpinLock<T>

source

pub fn new(value: T) -> Self

Create a new instance of self, wrapping the value of type T

source

pub fn read(&self) -> ReadLockGuard<'_, T>

source

pub fn write(&self) -> WriteLockGuard<'_, T>

Trait Implementations§

source§

impl<T> Sync for RwSpinLock<T>
where T: Send + Sync,

Auto Trait Implementations§

§

impl<T> !Freeze for RwSpinLock<T>

§

impl<T> !RefUnwindSafe for RwSpinLock<T>

§

impl<T> Send for RwSpinLock<T>
where T: Send,

§

impl<T> Unpin for RwSpinLock<T>
where T: Unpin,

§

impl<T> UnwindSafe for RwSpinLock<T>
where T: UnwindSafe,

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.