Struct spinlock::BaseSpinLock
source · pub struct BaseSpinLock<G: BaseGuard, T: ?Sized> { /* private fields */ }
Expand description
A spin lock providing mutually exclusive access to data.
This is a base struct, the specific behavior depends on the generic
parameter G
that implements BaseGuard
, such as whether to disable
local IRQs or kernel preemption before acquiring the lock.
For single-core environment (without the “smp” feature), we remove the lock state, CPU can always get the lock if we follow the proper guard in use.
Implementations§
source§impl<G: BaseGuard, T> BaseSpinLock<G, T>
impl<G: BaseGuard, T> BaseSpinLock<G, T>
sourcepub const fn new(data: T) -> Self
pub const fn new(data: T) -> Self
Creates a new BaseSpinLock
wrapping the supplied data.
sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Consumes this BaseSpinLock
and unwraps the underlying data.
source§impl<G: BaseGuard, T: ?Sized> BaseSpinLock<G, T>
impl<G: BaseGuard, T: ?Sized> BaseSpinLock<G, T>
sourcepub fn lock(&self) -> BaseSpinLockGuard<'_, G, T>
pub fn lock(&self) -> BaseSpinLockGuard<'_, G, T>
Locks the BaseSpinLock
and returns a guard that permits access to the inner data.
The returned value may be dereferenced for data access and the lock will be dropped when the guard falls out of scope.
sourcepub fn is_locked(&self) -> bool
pub fn is_locked(&self) -> bool
Returns true
if the lock is currently held.
Safety
This function provides no synchronization guarantees and so its result should be considered ‘out of date’ the instant it is called. Do not use it for synchronization purposes. However, it may be useful as a heuristic.
sourcepub fn try_lock(&self) -> Option<BaseSpinLockGuard<'_, G, T>>
pub fn try_lock(&self) -> Option<BaseSpinLockGuard<'_, G, T>>
Try to lock this BaseSpinLock
, returning a lock guard if successful.
sourcepub unsafe fn force_unlock(&self)
pub unsafe fn force_unlock(&self)
Force unlock this BaseSpinLock
.
Safety
This is extremely unsafe if the lock is not held by the current thread. However, this can be useful in some instances for exposing the lock to FFI that doesn’t know how to deal with RAII.
sourcepub fn get_mut(&mut self) -> &mut T
pub fn get_mut(&mut self) -> &mut T
Returns a mutable reference to the underlying data.
Since this call borrows the BaseSpinLock
mutably, and a mutable reference is guaranteed to be exclusive in
Rust, no actual locking needs to take place – the mutable borrow statically guarantees no locks exist. As
such, this is a ‘zero-cost’ operation.