pub struct SpinMutex<T: ?Sized> { /* private fields */ }
Expand description
A lower-level mutual exclusion primitive for protecting data.
This is modeled after [sync::Mutex
] but instead of using k_mutex
from Zephyr, it uses
k_spinlock
. It’s main advantage is that it is usable from IRQ context. However, it also is
uninterruptible, and prevents even IRQ handlers from running.
Implementations§
source§impl<T: ?Sized> SpinMutex<T>
impl<T: ?Sized> SpinMutex<T>
sourcepub fn lock(&self) -> Result<SpinMutexGuard<'_, T>, Infallible>
pub fn lock(&self) -> Result<SpinMutexGuard<'_, T>, Infallible>
Acquire a mutex, spinning as needed to aquire the controlling spinlock.
This function will spin the current thread until it is able to acquire the spinlock. Returns an RAII guard to allow scoped unlock of the lock. When the guard goes out of scope, the SpinMutex will be unlocked.
sourcepub fn try_lock(&self) -> Result<SpinMutexGuard<'_, T>, SpinTryLockError>
pub fn try_lock(&self) -> Result<SpinMutexGuard<'_, T>, SpinTryLockError>
Attempts to aquire this lock.
If the lock could not be aquired at this time, then Err
is returned. Otherwise an RAII
guard is returned. The lock will be unlocked when the guard is dropped.
This function does not block.
Trait Implementations§
impl<T: ?Sized + Send> Send for SpinMutex<T>
As the data is protected by spinlocks, with RAII ensuring the lock is always released, this satisfies Rust’s requirements for Send and Sync. The dependency of both on “Send” of the data type is intentional, as it is the Mutex that is providing the Sync semantics. However, it only makes sense for types that are usable from multiple thread contexts.