2022-06-15 10:24:36 +02:00
|
|
|
//! Blocking mutex.
|
|
|
|
//!
|
|
|
|
//! This module provides a blocking mutex that can be used to synchronize data.
|
2022-02-11 23:25:30 +01:00
|
|
|
pub mod raw;
|
2021-09-12 23:27:13 +02:00
|
|
|
|
2021-01-11 10:38:43 +01:00
|
|
|
use core::cell::UnsafeCell;
|
|
|
|
|
2022-06-12 22:15:44 +02:00
|
|
|
use self::raw::RawMutex;
|
|
|
|
|
2022-06-15 10:24:36 +02:00
|
|
|
/// Blocking mutex (not async)
|
|
|
|
///
|
|
|
|
/// Provides a blocking mutual exclusion primitive backed by an implementation of [`raw::RawMutex`].
|
|
|
|
///
|
2022-06-15 13:06:35 +02:00
|
|
|
/// Which implementation you select depends on the context in which you're using the mutex, and you can choose which kind
|
|
|
|
/// of interior mutability fits your use case.
|
2022-06-15 10:24:36 +02:00
|
|
|
///
|
|
|
|
/// Use [`CriticalSectionMutex`] when data can be shared between threads and interrupts.
|
|
|
|
///
|
|
|
|
/// Use [`NoopMutex`] when data is only shared between tasks running on the same executor.
|
|
|
|
///
|
|
|
|
/// Use [`ThreadModeMutex`] when data is shared between tasks running on the same executor but you want a global singleton.
|
2022-06-15 13:06:35 +02:00
|
|
|
///
|
|
|
|
/// In all cases, the blocking mutex is intended to be short lived and not held across await points.
|
|
|
|
/// Use the async [`Mutex`](crate::mutex::Mutex) if you need a lock that is held across await points.
|
2022-02-11 23:25:30 +01:00
|
|
|
pub struct Mutex<R, T: ?Sized> {
|
|
|
|
// NOTE: `raw` must be FIRST, so when using ThreadModeMutex the "can't drop in non-thread-mode" gets
|
|
|
|
// to run BEFORE dropping `data`.
|
|
|
|
raw: R,
|
|
|
|
data: UnsafeCell<T>,
|
|
|
|
}
|
2021-06-06 10:36:16 +02:00
|
|
|
|
2022-02-11 23:25:30 +01:00
|
|
|
unsafe impl<R: RawMutex + Send, T: ?Sized + Send> Send for Mutex<R, T> {}
|
|
|
|
unsafe impl<R: RawMutex + Sync, T: ?Sized + Send> Sync for Mutex<R, T> {}
|
|
|
|
|
|
|
|
impl<R: RawMutex, T> Mutex<R, T> {
|
|
|
|
/// Creates a new mutex in an unlocked state ready for use.
|
|
|
|
#[inline]
|
|
|
|
pub const fn new(val: T) -> Mutex<R, T> {
|
|
|
|
Mutex {
|
|
|
|
raw: R::INIT,
|
|
|
|
data: UnsafeCell::new(val),
|
|
|
|
}
|
|
|
|
}
|
2021-07-15 04:08:35 +02:00
|
|
|
|
2022-02-11 23:25:30 +01:00
|
|
|
/// Creates a critical section and grants temporary access to the protected data.
|
|
|
|
pub fn lock<U>(&self, f: impl FnOnce(&T) -> U) -> U {
|
|
|
|
self.raw.lock(|| {
|
|
|
|
let ptr = self.data.get() as *const T;
|
|
|
|
let inner = unsafe { &*ptr };
|
|
|
|
f(inner)
|
|
|
|
})
|
|
|
|
}
|
2021-01-11 10:38:43 +01:00
|
|
|
}
|
2021-02-03 16:29:35 +01:00
|
|
|
|
2022-02-11 23:25:30 +01:00
|
|
|
impl<R, T> Mutex<R, T> {
|
|
|
|
/// Creates a new mutex based on a pre-existing raw mutex.
|
|
|
|
///
|
|
|
|
/// This allows creating a mutex in a constant context on stable Rust.
|
|
|
|
#[inline]
|
|
|
|
pub const fn const_new(raw_mutex: R, val: T) -> Mutex<R, T> {
|
|
|
|
Mutex {
|
|
|
|
raw: raw_mutex,
|
|
|
|
data: UnsafeCell::new(val),
|
2021-01-11 10:38:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-11 23:25:30 +01:00
|
|
|
/// Consumes this mutex, returning the underlying data.
|
|
|
|
#[inline]
|
|
|
|
pub fn into_inner(self) -> T {
|
|
|
|
self.data.into_inner()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a mutable reference to the underlying data.
|
|
|
|
///
|
|
|
|
/// Since this call borrows the `Mutex` mutably, no actual locking needs to
|
|
|
|
/// take place---the mutable borrow statically guarantees no locks exist.
|
|
|
|
#[inline]
|
|
|
|
pub fn get_mut(&mut self) -> &mut T {
|
|
|
|
unsafe { &mut *self.data.get() }
|
2021-01-11 10:38:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-15 10:24:36 +02:00
|
|
|
/// A mutex that allows borrowing data across executors and interrupts.
|
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// This mutex is safe to share between different executors and interrupts.
|
2022-02-11 23:25:30 +01:00
|
|
|
pub type CriticalSectionMutex<T> = Mutex<raw::CriticalSectionRawMutex, T>;
|
2022-06-15 10:24:36 +02:00
|
|
|
|
|
|
|
/// A mutex that allows borrowing data in the context of a single executor.
|
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// **This Mutex is only safe within a single executor.**
|
2022-02-11 23:25:30 +01:00
|
|
|
pub type NoopMutex<T> = Mutex<raw::NoopRawMutex, T>;
|
2021-06-06 10:36:16 +02:00
|
|
|
|
2022-02-11 23:25:30 +01:00
|
|
|
impl<T> Mutex<raw::CriticalSectionRawMutex, T> {
|
|
|
|
/// Borrows the data for the duration of the critical section
|
|
|
|
pub fn borrow<'cs>(&'cs self, _cs: critical_section::CriticalSection<'cs>) -> &'cs T {
|
|
|
|
let ptr = self.data.get() as *const T;
|
|
|
|
unsafe { &*ptr }
|
2021-07-15 04:08:35 +02:00
|
|
|
}
|
2022-02-11 23:25:30 +01:00
|
|
|
}
|
2021-07-15 04:08:35 +02:00
|
|
|
|
2022-02-11 23:25:30 +01:00
|
|
|
impl<T> Mutex<raw::NoopRawMutex, T> {
|
|
|
|
/// Borrows the data
|
|
|
|
pub fn borrow(&self) -> &T {
|
|
|
|
let ptr = self.data.get() as *const T;
|
|
|
|
unsafe { &*ptr }
|
2021-06-06 10:36:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-11 23:25:30 +01:00
|
|
|
// ThreadModeMutex does NOT use the generic mutex from above because it's special:
|
|
|
|
// it's Send+Sync even if T: !Send. There's no way to do that without specialization (I think?).
|
|
|
|
//
|
|
|
|
// There's still a ThreadModeRawMutex for use with the generic Mutex (handy with Channel, for example),
|
|
|
|
// but that will require T: Send even though it shouldn't be needed.
|
|
|
|
|
2021-12-23 13:34:15 +01:00
|
|
|
#[cfg(any(cortex_m, feature = "std"))]
|
|
|
|
pub use thread_mode_mutex::*;
|
|
|
|
#[cfg(any(cortex_m, feature = "std"))]
|
|
|
|
mod thread_mode_mutex {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
/// A "mutex" that only allows borrowing from thread mode.
|
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// **This Mutex is only safe on single-core systems.**
|
|
|
|
///
|
|
|
|
/// On multi-core systems, a `ThreadModeMutex` **is not sufficient** to ensure exclusive access.
|
2022-02-11 23:25:30 +01:00
|
|
|
pub struct ThreadModeMutex<T: ?Sized> {
|
2021-12-23 13:34:15 +01:00
|
|
|
inner: UnsafeCell<T>,
|
|
|
|
}
|
2021-01-11 10:38:43 +01:00
|
|
|
|
2021-12-23 13:34:15 +01:00
|
|
|
// NOTE: ThreadModeMutex only allows borrowing from one execution context ever: thread mode.
|
|
|
|
// Therefore it cannot be used to send non-sendable stuff between execution contexts, so it can
|
|
|
|
// be Send+Sync even if T is not Send (unlike CriticalSectionMutex)
|
2022-02-11 23:25:30 +01:00
|
|
|
unsafe impl<T: ?Sized> Sync for ThreadModeMutex<T> {}
|
|
|
|
unsafe impl<T: ?Sized> Send for ThreadModeMutex<T> {}
|
2021-12-23 13:34:15 +01:00
|
|
|
|
|
|
|
impl<T> ThreadModeMutex<T> {
|
|
|
|
/// Creates a new mutex
|
|
|
|
pub const fn new(value: T) -> Self {
|
|
|
|
ThreadModeMutex {
|
|
|
|
inner: UnsafeCell::new(value),
|
|
|
|
}
|
2021-01-11 10:38:43 +01:00
|
|
|
}
|
2022-02-11 23:25:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: ?Sized> ThreadModeMutex<T> {
|
2022-06-26 00:53:35 +02:00
|
|
|
/// Lock the `ThreadModeMutex`, granting access to the data.
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// This will panic if not currently running in thread mode.
|
2022-02-11 23:25:30 +01:00
|
|
|
pub fn lock<R>(&self, f: impl FnOnce(&T) -> R) -> R {
|
|
|
|
f(self.borrow())
|
|
|
|
}
|
2021-01-11 10:38:43 +01:00
|
|
|
|
2021-12-23 13:34:15 +01:00
|
|
|
/// Borrows the data
|
2022-06-26 00:53:35 +02:00
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// This will panic if not currently running in thread mode.
|
2021-12-23 13:34:15 +01:00
|
|
|
pub fn borrow(&self) -> &T {
|
|
|
|
assert!(
|
2022-02-11 23:25:30 +01:00
|
|
|
raw::in_thread_mode(),
|
2021-12-23 13:34:15 +01:00
|
|
|
"ThreadModeMutex can only be borrowed from thread mode."
|
|
|
|
);
|
|
|
|
unsafe { &*self.inner.get() }
|
|
|
|
}
|
2021-01-11 10:38:43 +01:00
|
|
|
}
|
|
|
|
|
2022-02-11 23:25:30 +01:00
|
|
|
impl<T: ?Sized> Drop for ThreadModeMutex<T> {
|
2021-12-23 13:34:15 +01:00
|
|
|
fn drop(&mut self) {
|
|
|
|
// Only allow dropping from thread mode. Dropping calls drop on the inner `T`, so
|
|
|
|
// `drop` needs the same guarantees as `lock`. `ThreadModeMutex<T>` is Send even if
|
|
|
|
// T isn't, so without this check a user could create a ThreadModeMutex in thread mode,
|
|
|
|
// send it to interrupt context and drop it there, which would "send" a T even if T is not Send.
|
|
|
|
assert!(
|
2022-02-11 23:25:30 +01:00
|
|
|
raw::in_thread_mode(),
|
2021-12-23 13:34:15 +01:00
|
|
|
"ThreadModeMutex can only be dropped from thread mode."
|
|
|
|
);
|
|
|
|
|
|
|
|
// Drop of the inner `T` happens after this.
|
|
|
|
}
|
2021-08-05 22:20:16 +02:00
|
|
|
}
|
2021-07-09 04:13:07 +02:00
|
|
|
}
|