pub struct Mutex<T: ?Sized> { /* private fields */ }Expand description
A mutual exclusion primitive useful for protecting shared data.
This mutex will block threads waiting for the lock to become available. This is modeled after
std::sync::Mutex, and attempts
to implement that API as closely as makes sense on Zephyr. Currently, it has the following
differences:
- Poisoning: This does not yet implement poisoning, as there is no way to recover from panic at this time on Zephyr.
- Allocation:
newis not yet provided, and will be provided once kernel object pools are implemented. Please usenew_fromwhich takes a reference to a statically allocatedsys::Mutex.
Implementations§
Source§impl<T> Mutex<T>
impl<T> Mutex<T>
Sourcepub const fn new_from(t: T, raw_mutex: Mutex) -> Mutex<T>
pub const fn new_from(t: T, raw_mutex: Mutex) -> Mutex<T>
Construct a new wrapped Mutex, using the given underlying sys mutex. This is different that
std::sync::Mutex in that in Zephyr, objects are frequently allocated statically, and the
sys Mutex will be taken by this structure. It is safe to share the underlying Mutex between
different items, but without careful use, it is easy to deadlock, so it is not recommended.
Source§impl<T: ?Sized> Mutex<T>
impl<T: ?Sized> Mutex<T>
Sourcepub fn lock(&self) -> LockResult<MutexGuard<'_, T>>
pub fn lock(&self) -> LockResult<MutexGuard<'_, T>>
Acquires a mutex, blocking the current thread until it is able to do so.
This function will block the local thread until it is available to acquire the mutex. Upon returning, the thread is the only thread with the lock held. An RAII guard is returned to allow scoped unlock of the lock. When the guard goes out of scope, the mutex will be unlocked.
Attempting to lock a mutex that is already held by the current thread is not permitted and
will panic. Because MutexGuard provides mutable access to the protected data via
DerefMut, allowing reentrancy would produce multiple mutable references to the same
data, which is undefined behavior in Rust.
Sourcepub fn try_lock(&self) -> TryLockResult<MutexGuard<'_, T>>
pub fn try_lock(&self) -> TryLockResult<MutexGuard<'_, T>>
Attempts to acquire this lock.
If the lock could not be acquired 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. Attempting to lock a mutex that is already held by the current
thread will return Err(TryLockError::WouldBlock).