Skip to main content

Mutex

Struct Mutex 

Source
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>

Source

pub fn new(data: T) -> Self
where T: Sized,

Wraps data in a new, unlocked mutex.

§Examples
use osal_rs::os::*;

let mutex = Mutex::new(vec![1, 2, 3]);
assert_eq!(mutex.lock().unwrap().len(), 3);
Source§

impl<T: ?Sized> Mutex<T>

Source

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>

Source

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> Debug for Mutex<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: ?Sized> Display for Mutex<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: ?Sized> Mutex<T> for Mutex<T>

Source§

fn lock(&self) -> Result<Self::Guard<'_>>

Blocks the calling thread until the mutex is available, then returns a RAII guard that unlocks it on Drop.

§Examples
use osal_rs::os::*;

let mutex = Mutex::new(10);
let mut guard = mutex.lock().unwrap();
*guard += 5;
drop(guard);

assert_eq!(*mutex.lock().unwrap(), 15);
Source§

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>
where Self: Sized, T: Sized,

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

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);
Source§

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

The guard type for ISR-context mutex locks
Source§

impl<T: ?Sized + Send> Send for Mutex<T>

Source§

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>
where T: Unpin + ?Sized,

§

impl<T> UnsafeUnpin for Mutex<T>
where T: UnsafeUnpin + ?Sized,

§

impl<T> UnwindSafe for Mutex<T>
where T: UnwindSafe + ?Sized,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.