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. The mutex is implemented using FreeRTOS recursive mutexes, supporting priority inheritance to prevent priority inversion.
§Type Parameters
T- The type of data protected by the mutex
§Examples
§Basic usage
ⓘ
use osal_rs::os::Mutex;
let mutex = Mutex::new(0);
// Acquire the lock and modify the data
{
let mut guard = mutex.lock().unwrap();
*guard += 1;
} // Lock is automatically released here§Sharing between threads
ⓘ
use osal_rs::os::{Mutex, Thread};
use alloc::sync::Arc;
let counter = Arc::new(Mutex::new(0));
let counter_clone = counter.clone();
let thread = Thread::new("worker", 2048, 5, move || {
let mut guard = counter_clone.lock().unwrap();
*guard += 1;
}).unwrap();
thread.start().unwrap();§Using from ISR context
ⓘ
use osal_rs::os::Mutex;
let mutex = Mutex::new(0);
// In an interrupt handler:
if let Ok(mut guard) = mutex.lock_from_isr() {
*guard = 42;
}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>>
Acquires the mutex from ISR context, returning a specific ISR guard.
This is an explicit version of lock_from_isr that returns the ISR-specific guard type.
§Returns
Ok(MutexGuardFromIsr)- Lock acquiredErr(Error::MutexLockFailed)- Failed to acquire lock
§Examples
ⓘ
// In ISR context:
if let Ok(guard) = mutex.lock_from_isr_explicit() {
*guard = new_value;
}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>
Creates a new mutex wrapped in an Arc for easy sharing between threads.
This is a convenience method equivalent to Arc::new(Mutex::new(data)).
§Examples
ⓘ
use osal_rs::os::Mutex;
use alloc::sync::Arc;
let shared_data = Mutex::new_arc(0u32);
let data_clone = Arc::clone(&shared_data);
// Use in thread...
let thread = Thread::new("worker", 2048, 5, move || {
let mut guard = data_clone.lock().unwrap();
*guard += 1;
});Trait Implementations§
Source§impl<T: ?Sized> Mutex<T> for Mutex<T>
impl<T: ?Sized> Mutex<T> for Mutex<T>
Source§fn into_inner(self) -> Result<T>
fn into_inner(self) -> Result<T>
Source§fn get_mut(&mut self) -> &mut T
fn get_mut(&mut self) -> &mut T
Returns a mutable reference to the inner data.
Since this takes &mut self, we know there are no other references
to the data, so we can safely return a mutable reference.
§Examples
ⓘ
use osal_rs::os::{Mutex, MutexFn};
let mut mutex = Mutex::new(0);
*mutex.get_mut() = 10;
assert_eq!(*mutex.get_mut(), 10);Source§type Guard<'a> = MutexGuard<'a, T>
where
Self: 'a,
T: 'a
type Guard<'a> = MutexGuard<'a, T> where Self: 'a, T: 'a
The guard type for normal mutex locks
Source§type GuardFromIsr<'a> = MutexGuardFromIsr<'a, T>
where
Self: 'a,
T: 'a
type GuardFromIsr<'a> = MutexGuardFromIsr<'a, T> where Self: 'a, T: 'a
The guard type for ISR-context mutex locks
Source§fn lock(&self) -> Result<Self::Guard<'_>>
fn lock(&self) -> Result<Self::Guard<'_>>
Acquires the mutex, blocking the current thread until it is able to do so
Source§fn lock_from_isr(&self) -> Result<Self::GuardFromIsr<'_>>
fn lock_from_isr(&self) -> Result<Self::GuardFromIsr<'_>>
Acquires the mutex from ISR context
impl<T: ?Sized + Send> Send for Mutex<T>
impl<T: ?Sized + Send> Sync for Mutex<T>
Auto Trait Implementations§
impl<T> !Freeze for Mutex<T>
impl<T> !RefUnwindSafe for Mutex<T>
impl<T> Unpin for Mutex<T>
impl<T> UnwindSafe for Mutex<T>where
T: UnwindSafe + ?Sized,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more