pub use mutex::*;
mod mutex {
#![allow(clippy::should_implement_trait)]
use core::cell::UnsafeCell;
use embassy_sync::blocking_mutex::raw::RawMutex;
use crate::{
init::{init, Init, UnsafeCellInit},
sync::blocking::raw::MatterRawMutex,
};
pub struct Mutex<T: ?Sized, R = MatterRawMutex> {
raw: R,
data: UnsafeCell<T>,
}
unsafe impl<T: ?Sized + Send, R: RawMutex + Send> Send for Mutex<T, R> {}
unsafe impl<T: ?Sized + Send, R: RawMutex + Sync> Sync for Mutex<T, R> {}
impl<T, R: RawMutex> Mutex<T, R> {
#[inline]
pub const fn new(val: T) -> Self {
Self {
raw: R::INIT,
data: UnsafeCell::new(val),
}
}
pub fn init<I: Init<T>>(val: I) -> impl Init<Self> {
init!(Self {
raw: R::INIT,
data <- UnsafeCell::init(val),
})
}
#[inline(always)]
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)
})
}
}
impl<T, R> Mutex<T, R> {
#[inline]
pub const fn const_new(raw_mutex: R, val: T) -> Self {
Self {
raw: raw_mutex,
data: UnsafeCell::new(val),
}
}
#[inline]
pub fn into_inner(self) -> T {
self.data.into_inner()
}
#[inline]
pub fn get_mut(&mut self) -> &mut T {
unsafe { &mut *self.data.get() }
}
}
impl<T: Clone, R: RawMutex> Clone for Mutex<T, R> {
fn clone(&self) -> Self {
Self::new(self.lock(T::clone))
}
}
impl<T: core::fmt::Debug, R: RawMutex> core::fmt::Debug for Mutex<T, R> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
self.lock(|data| core::fmt::Debug::fmt(data, f))
}
}
#[cfg(feature = "defmt")]
impl<T: defmt::Format, R: RawMutex> defmt::Format for Mutex<T, R> {
fn format(&self, f: defmt::Formatter<'_>) {
self.lock(|data| data.format(f))
}
}
}
pub mod raw {
#[cfg(not(feature = "send-sync"))]
pub type MatterRawMutex = embassy_sync::blocking_mutex::raw::NoopRawMutex;
#[cfg(all(feature = "send-sync", not(feature = "std")))]
pub type MatterRawMutex = embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
#[cfg(all(feature = "send-sync", feature = "std"))]
pub type MatterRawMutex = StdRawMutex;
#[cfg(feature = "std")]
pub use std::*;
#[cfg(feature = "std")]
mod std {
use embassy_sync::blocking_mutex::raw::RawMutex;
#[derive(Default)]
pub struct StdRawMutex(std::sync::Mutex<()>);
impl StdRawMutex {
pub const fn new() -> Self {
Self(std::sync::Mutex::new(()))
}
}
unsafe impl RawMutex for StdRawMutex {
#[allow(clippy::declare_interior_mutable_const)]
const INIT: Self = StdRawMutex(std::sync::Mutex::new(()));
#[inline(always)]
fn lock<R>(&self, f: impl FnOnce() -> R) -> R {
let _guard = unwrap!(self.0.lock(), "Mutex lock failed");
f()
}
}
}
}
#[cfg(test)]
#[cfg_attr(coverage_nightly, coverage(off))]
mod tests {
use core::cell::Cell;
use core::mem::MaybeUninit;
use embassy_sync::blocking_mutex::raw::NoopRawMutex;
use crate::init::InitMaybeUninit;
use crate::storage::Vec;
use super::Mutex;
#[test]
fn lock_get_mut_into_inner() {
let mut m = Mutex::<_, NoopRawMutex>::new(Cell::new(1));
m.lock(|c| c.set(c.get() + 1));
assert_eq!(m.lock(|c| c.get()), 2);
m.get_mut().set(5);
assert_eq!(m.lock(|c| c.get()), 5);
assert_eq!(m.into_inner().get(), 5);
let m = Mutex::const_new(NoopRawMutex::new(), 7);
assert_eq!(m.lock(|v| *v), 7);
}
#[test]
fn init_in_place() {
let mut slot = MaybeUninit::<Mutex<Vec<u8, 4>, NoopRawMutex>>::uninit();
let m = slot.init_with(Mutex::init(Vec::init()));
m.get_mut().push(3).unwrap();
assert_eq!(m.lock(|v| (v.len(), v[0])), (1, 3));
unsafe { slot.assume_init_drop() };
}
}