use core::cell::UnsafeCell;
use core::ops::{Deref, DerefMut};
use embassy_sync::blocking_mutex::raw::RawMutex;
use crate::init::{init, Init, UnsafeCellInit};
use crate::sync::blocking::raw::MatterRawMutex;
use super::signal::{MultiSignal, Signal};
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct TryLockError;
pub struct IfMutex<T, M = MatterRawMutex>
where
T: ?Sized,
M: RawMutex,
{
state: Signal<bool, M>,
inner: UnsafeCell<T>,
}
unsafe impl<T: ?Sized + Send, M: RawMutex + Send> Send for IfMutex<T, M> {}
unsafe impl<T: ?Sized + Send, M: RawMutex + Sync> Sync for IfMutex<T, M> {}
impl<T, M> IfMutex<T, M>
where
M: RawMutex,
{
#[inline(always)]
pub const fn new(value: T) -> Self {
Self {
state: Signal::new(false),
inner: UnsafeCell::new(value),
}
}
pub fn init<I: Init<T>>(value: I) -> impl Init<Self> {
init!(Self {
state: Signal::new(false),
inner <- UnsafeCell::init(value),
})
}
}
impl<T, M> IfMutex<T, M>
where
T: ?Sized,
M: RawMutex,
{
pub async fn lock(&self) -> IfMutexGuard<'_, T, M> {
self.lock_if(|_| true).await
}
pub async fn lock_if<F>(&self, f: F) -> IfMutexGuard<'_, T, M>
where
F: Fn(&T) -> bool,
{
self.state
.wait(|locked| {
if !*locked && f(unsafe { &*self.inner.get() }) {
*locked = true;
Some(())
} else {
None
}
})
.await;
IfMutexGuard { mutex: self }
}
pub async fn with<F, R>(&self, mut f: F) -> R
where
F: FnMut(&mut T) -> Option<R>,
{
let result = self
.state
.wait(|locked| {
if !*locked {
if let Some(result) = f(unsafe { &mut *self.inner.get() }) {
*locked = true;
return Some(result);
}
}
None
})
.await;
let _ = IfMutexGuard { mutex: self };
result
}
pub async fn wait_until<F>(&self, f: F)
where
F: Fn(&T) -> bool,
{
self.state
.wait(|locked| {
if *locked {
return None;
}
let data = unsafe { &*self.inner.get() };
f(data).then_some(())
})
.await;
}
pub fn try_lock(&self) -> Result<IfMutexGuard<'_, T, M>, TryLockError> {
self.try_lock_if(|_| true)
}
pub fn try_lock_if<F>(&self, mut f: F) -> Result<IfMutexGuard<'_, T, M>, TryLockError>
where
F: FnMut(&T) -> bool,
{
self.state.modify(|locked| {
if *locked {
(false, Err(TryLockError))
} else if f(unsafe { &*self.inner.get() }) {
*locked = true;
(false, Ok(()))
} else {
(false, Err(TryLockError))
}
})?;
Ok(IfMutexGuard { mutex: self })
}
pub fn into_inner(self) -> T
where
T: Sized,
{
self.inner.into_inner()
}
pub fn get_mut(&mut self) -> &mut T {
self.inner.get_mut()
}
}
pub struct IfMutexGuard<'a, T, M = MatterRawMutex>
where
T: ?Sized,
M: RawMutex,
{
mutex: &'a IfMutex<T, M>,
}
impl<T, M> Drop for IfMutexGuard<'_, T, M>
where
T: ?Sized,
M: RawMutex,
{
fn drop(&mut self) {
self.mutex.state.modify(|locked| {
assert!(*locked);
*locked = false;
(true, ())
})
}
}
impl<T, M> Deref for IfMutexGuard<'_, T, M>
where
T: ?Sized,
M: RawMutex,
{
type Target = T;
fn deref(&self) -> &Self::Target {
unsafe { &*(self.mutex.inner.get() as *const T) }
}
}
impl<T, M> DerefMut for IfMutexGuard<'_, T, M>
where
T: ?Sized,
M: RawMutex,
{
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { &mut *(self.mutex.inner.get()) }
}
}
pub struct IfMultiMutex<T, M = MatterRawMutex>
where
T: ?Sized,
M: RawMutex,
{
state: MultiSignal<bool, M>,
inner: UnsafeCell<T>,
}
unsafe impl<T: ?Sized + Send, M: RawMutex + Send> Send for IfMultiMutex<T, M> {}
unsafe impl<T: ?Sized + Send, M: RawMutex + Sync> Sync for IfMultiMutex<T, M> {}
impl<T, M> IfMultiMutex<T, M>
where
M: RawMutex,
{
#[inline(always)]
pub const fn new(value: T) -> Self {
Self {
state: MultiSignal::new(false),
inner: UnsafeCell::new(value),
}
}
pub fn init<I: Init<T>>(value: I) -> impl Init<Self> {
init!(Self {
state: MultiSignal::new(false),
inner <- UnsafeCell::init(value),
})
}
}
impl<T, M> IfMultiMutex<T, M>
where
T: ?Sized,
M: RawMutex,
{
pub async fn lock(&self) -> IfMultiMutexGuard<'_, T, M> {
self.lock_if(|_| true).await
}
pub async fn lock_if<F>(&self, f: F) -> IfMultiMutexGuard<'_, T, M>
where
F: Fn(&T) -> bool,
{
self.state
.wait(|locked| {
if !*locked && f(unsafe { &*self.inner.get() }) {
*locked = true;
Some(())
} else {
None
}
})
.await;
IfMultiMutexGuard { mutex: self }
}
pub async fn with<F, R>(&self, mut f: F) -> R
where
F: FnMut(&mut T) -> Option<R>,
{
let result = self
.state
.wait(|locked| {
if !*locked {
if let Some(result) = f(unsafe { &mut *self.inner.get() }) {
*locked = true;
return Some(result);
}
}
None
})
.await;
let _ = IfMultiMutexGuard { mutex: self };
result
}
pub async fn wait_until<F>(&self, f: F)
where
F: Fn(&T) -> bool,
{
self.state
.wait(|locked| {
if *locked {
return None;
}
let data = unsafe { &*self.inner.get() };
f(data).then_some(())
})
.await;
}
pub fn try_lock(&self) -> Result<IfMultiMutexGuard<'_, T, M>, TryLockError> {
self.try_lock_if(|_| true)
}
pub fn try_lock_if<F>(&self, mut f: F) -> Result<IfMultiMutexGuard<'_, T, M>, TryLockError>
where
F: FnMut(&T) -> bool,
{
self.state.modify(|locked| {
if *locked {
(false, Err(TryLockError))
} else if f(unsafe { &*self.inner.get() }) {
*locked = true;
(false, Ok(()))
} else {
(false, Err(TryLockError))
}
})?;
Ok(IfMultiMutexGuard { mutex: self })
}
pub fn into_inner(self) -> T
where
T: Sized,
{
self.inner.into_inner()
}
pub fn get_mut(&mut self) -> &mut T {
self.inner.get_mut()
}
}
pub struct IfMultiMutexGuard<'a, T, M = MatterRawMutex>
where
T: ?Sized,
M: RawMutex,
{
mutex: &'a IfMultiMutex<T, M>,
}
impl<T, M> Drop for IfMultiMutexGuard<'_, T, M>
where
T: ?Sized,
M: RawMutex,
{
fn drop(&mut self) {
self.mutex.state.modify(|locked| {
assert!(*locked);
*locked = false;
(true, ())
})
}
}
impl<T, M> Deref for IfMultiMutexGuard<'_, T, M>
where
T: ?Sized,
M: RawMutex,
{
type Target = T;
fn deref(&self) -> &Self::Target {
unsafe { &*(self.mutex.inner.get() as *const T) }
}
}
impl<T, M> DerefMut for IfMultiMutexGuard<'_, T, M>
where
T: ?Sized,
M: RawMutex,
{
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { &mut *(self.mutex.inner.get()) }
}
}
#[cfg(test)]
#[cfg_attr(coverage_nightly, coverage(off))]
mod tests {
use core::future::Future;
use core::mem::MaybeUninit;
use core::pin::pin;
use core::task::{Context, Poll, Waker};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use std::task::Wake;
use embassy_sync::blocking_mutex::raw::NoopRawMutex;
use futures_lite::future::{block_on, poll_once, zip};
use crate::init::InitMaybeUninit;
use crate::storage::Vec;
use super::{IfMultiMutex, IfMutex, TryLockError};
struct Counter(AtomicUsize);
impl Wake for Counter {
fn wake(self: Arc<Self>) {
self.0.fetch_add(1, Ordering::SeqCst);
}
}
fn waker() -> (Waker, Arc<Counter>) {
let counter = Arc::new(Counter(AtomicUsize::new(0)));
(Waker::from(counter.clone()), counter)
}
fn count(counter: &Counter) -> usize {
counter.0.load(Ordering::SeqCst)
}
#[test]
fn try_lock_and_guard() {
let m = IfMutex::<_, NoopRawMutex>::new(1);
{
let mut g = m.try_lock().unwrap();
assert_eq!(m.try_lock().err(), Some(TryLockError));
assert_eq!(m.try_lock_if(|_| true).err(), Some(TryLockError));
assert_eq!(*g, 1);
*g += 1;
}
assert_eq!(*m.try_lock().unwrap(), 2);
assert!(m.try_lock_if(|v| *v == 3).is_err());
assert_eq!(*m.try_lock_if(|v| *v == 2).unwrap(), 2);
}
#[test]
fn lock_lock_if_and_with() {
let m = IfMutex::<_, NoopRawMutex>::new(0);
block_on(async {
{
let mut g = m.lock().await;
*g = 1;
}
let (g, ()) = zip(m.lock_if(|v| *v == 2), async {
let mut g = m.lock().await;
*g = 2;
})
.await;
assert_eq!(*g, 2);
drop(g);
let r = m
.with(|v| {
*v += 1;
Some(*v)
})
.await;
assert_eq!(r, 3);
});
let mut m = m;
*m.get_mut() += 1;
assert_eq!(m.into_inner(), 4);
}
#[test]
fn wait_until() {
let m = IfMutex::<_, NoopRawMutex>::new(0);
block_on(async {
let mut wait = pin!(m.wait_until(|v| *v == 1));
assert!(poll_once(wait.as_mut()).await.is_none());
zip(wait, async {
*m.lock().await = 1;
})
.await;
assert!(m.try_lock().is_ok());
let g = m.lock().await;
let mut wait = pin!(m.wait_until(|v| *v == 1));
assert!(poll_once(wait.as_mut()).await.is_none());
zip(wait, async move { drop(g) }).await;
assert!(m.try_lock().is_ok());
});
}
#[test]
fn init_in_place() {
let mut slot = MaybeUninit::<IfMutex<Vec<u8, 4>, NoopRawMutex>>::uninit();
let m = slot.init_with(IfMutex::init(Vec::init()));
m.get_mut().push(9).unwrap();
assert_eq!(m.try_lock().unwrap().as_slice(), &[9]);
unsafe { slot.assume_init_drop() };
}
#[test]
fn multi_lock_lock_if_with_and_wait_until() {
let m = IfMultiMutex::<_, NoopRawMutex>::new(0);
block_on(async {
{
let g = m.try_lock().unwrap();
assert_eq!(m.try_lock().err(), Some(TryLockError));
drop(g);
}
let (g, ()) = zip(m.lock_if(|v| *v == 1), async {
*m.lock().await = 1;
})
.await;
assert_eq!(*g, 1);
drop(g);
let r = m
.with(|v| {
*v += 1;
Some(*v)
})
.await;
assert_eq!(r, 2);
let g = m.lock().await;
let mut wait = pin!(m.wait_until(|v| *v == 2));
assert!(poll_once(wait.as_mut()).await.is_none());
zip(wait, async move { drop(g) }).await;
assert!(m.try_lock_if(|v| *v == 2).is_ok());
});
let mut m = m;
*m.get_mut() += 1;
assert_eq!(m.into_inner(), 3);
}
#[test]
fn multi_lockers_with_different_wakers() {
let m = IfMultiMutex::<_, NoopRawMutex>::new(0);
let (wa, ca) = waker();
let (wb, cb) = waker();
let mut cxa = Context::from_waker(&wa);
let mut cxb = Context::from_waker(&wb);
let g = m.try_lock().unwrap();
let mut a = pin!(m.lock());
let mut b = pin!(m.lock());
for _ in 0..3 {
assert!(a.as_mut().poll(&mut cxa).is_pending());
assert!(b.as_mut().poll(&mut cxb).is_pending());
}
assert_eq!((count(&ca), count(&cb)), (0, 0));
drop(g);
assert_eq!((count(&ca), count(&cb)), (1, 1));
let Poll::Ready(ga) = a.as_mut().poll(&mut cxa) else {
panic!("Not locked");
};
assert!(b.as_mut().poll(&mut cxb).is_pending());
drop(ga);
assert_eq!(count(&cb), 2);
assert!(b.as_mut().poll(&mut cxb).is_ready());
}
#[test]
fn multi_init_in_place() {
let mut slot = MaybeUninit::<IfMultiMutex<Vec<u8, 4>, NoopRawMutex>>::uninit();
let m = slot.init_with(IfMultiMutex::init(Vec::init()));
m.get_mut().push(9).unwrap();
assert_eq!(m.try_lock().unwrap().as_slice(), &[9]);
unsafe { slot.assume_init_drop() };
}
}