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 writeru32::MAX
means there is a single active writerstate % 2 == 0
means there arestate / 2
active readersstate % 2 != 0
means there are(state - 1) / 2
active readers and at least one waiting writer
Implementations§
source§impl<T> RwSpinLock<T>
impl<T> RwSpinLock<T>
pub fn read(&self) -> ReadLockGuard<'_, T>
pub fn write(&self) -> WriteLockGuard<'_, T>
Trait Implementations§
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> 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
Mutably borrows from an owned value. Read more