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:
new
is not yet provided, and will be provided once kernel object pools are implemented. Please usenew_from
which 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.
In std
, an attempt to lock a mutex by a thread that already holds the mutex is
unspecified. Zephyr explicitly supports this behavior, by simply incrementing a lock
count.
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.