pub struct Mutex<T: ?Sized> { /* private fields */ }Expand description
Mutex types and guards for mutual exclusion.
A mutex protecting a T, unlocked through RAII guards rather than
explicit lock/unlock calls. Built on RawMutex, so it shares the same
priority-inheritance and recursive-locking behavior.
§Examples
use osal_rs::os::*;
let mutex = Mutex::new(0);
{
let mut guard = mutex.lock().unwrap();
*guard += 1;
} // Lock released here, when `guard` drops.
assert_eq!(*mutex.lock().unwrap(), 1);Implementations§
Source§impl<T: ?Sized> Mutex<T>
impl<T: ?Sized> Mutex<T>
Sourcepub fn lock_from_isr_explicit(&self) -> Result<MutexGuardFromIsr<'_, T>>
pub fn lock_from_isr_explicit(&self) -> Result<MutexGuardFromIsr<'_, T>>
Same as MutexFn::lock_from_isr, exposed as an inherent method so
it’s callable without importing the MutexFn trait.
§Examples
use osal_rs::os::*;
let mutex = Mutex::new(0);
let mut guard = mutex.lock_from_isr_explicit().unwrap();
*guard = 7;Source§impl<T> Mutex<T>
impl<T> Mutex<T>
Sourcepub fn new_arc(data: T) -> Arc<Self> ⓘ
pub fn new_arc(data: T) -> Arc<Self> ⓘ
Convenience constructor for the common case of sharing a mutex
between threads: equivalent to Arc::new(Mutex::new(data)).
§Examples
use osal_rs::os::*;
let shared = Mutex::new_arc(0);
let clone_for_worker = shared.clone();
*clone_for_worker.lock().unwrap() += 1;
assert_eq!(*shared.lock().unwrap(), 1);Trait Implementations§
Source§impl<T: ?Sized> Mutex<T> for Mutex<T>
impl<T: ?Sized> Mutex<T> for Mutex<T>
Source§fn lock_from_isr(&self) -> Result<Self::GuardFromIsr<'_>>
fn lock_from_isr(&self) -> Result<Self::GuardFromIsr<'_>>
ISR-safe variant of Mutex::lock: never blocks, failing with
Error::MutexLockFailed instead of waiting if the mutex is already
held (POSIX has no real interrupt context of its own, so this is the
non-blocking equivalent expected of _from_isr methods).
§Examples
use osal_rs::os::*;
let mutex = Mutex::new(0);
let mut guard = mutex.lock_from_isr().unwrap();
*guard = 42;
drop(guard);
assert_eq!(*mutex.lock().unwrap(), 42);Source§fn into_inner(self) -> Result<T>
fn into_inner(self) -> Result<T>
Consumes the mutex, returning the wrapped value without needing to lock it (the type system already guarantees exclusive access here).
§Examples
use osal_rs::os::*;
let mutex = Mutex::new(String::from("hello"));
let value = mutex.into_inner().unwrap();
assert_eq!(value, "hello");Source§fn get_mut(&mut self) -> &mut T
fn get_mut(&mut self) -> &mut T
Returns a mutable reference to the wrapped value without locking (a
&mut Mutex<T> already guarantees exclusive access).
§Examples
use osal_rs::os::*;
let mut mutex = Mutex::new(1);
*mutex.get_mut() += 1;
assert_eq!(*mutex.lock().unwrap(), 2);