pub struct Mutex<T: ?Sized> { /* private fields */ }Expand description
Mutex types and guards for mutual exclusion. 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 new(data: T) -> Selfwhere
T: Sized,
pub fn new(data: T) -> Selfwhere
T: Sized,
Creates a new mutex wrapping the supplied data.
The mutex is created using FreeRTOS recursive mutexes, which support priority inheritance and can be locked multiple times by the same thread.
§Arguments
data- The data to protect with the mutex
§Examples
use osal_rs::os::{Mutex, MutexFn};
let mutex = Mutex::new(0);
let mut guard = mutex.lock().unwrap();
*guard = 42;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 lock(&self) -> Result<Self::Guard<'_>>
fn lock(&self) -> Result<Self::Guard<'_>>
Acquires the mutex, blocking until it becomes available.
Returns a RAII guard that will automatically unlock the mutex when dropped.
The guard provides access to the protected data through Deref and DerefMut.
§Returns
Ok(MutexGuard)- Successfully acquired, guard provides data accessErr(Error::MutexLockFailed)- Failed to acquire the mutex
§Examples
use osal_rs::os::{Mutex, MutexFn};
let mutex = Mutex::new(0);
let mut guard = mutex.lock().unwrap();
*guard += 1;
// Mutex automatically unlocked when guard goes out of scopeSource§fn lock_from_isr(&self) -> Result<Self::GuardFromIsr<'_>>
fn lock_from_isr(&self) -> Result<Self::GuardFromIsr<'_>>
Acquires the mutex from an ISR context.
This is the ISR-safe version of lock(). It attempts to acquire the mutex
without blocking and returns an ISR-specific guard.
§Returns
Ok(MutexGuardFromIsr)- Successfully acquired, guard provides data accessErr(Error::MutexLockFailed)- Mutex is already locked
§Safety
Must only be called from ISR context.
§Examples
// In interrupt handler
use osal_rs::os::{Mutex, MutexFn};
fn irq_handler(mutex: &Mutex<u32>) {
if let Ok(mut guard) = mutex.lock_from_isr() {
*guard = 42;
}
}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);