diff --git a/embassy/src/blocking_mutex/kind.rs b/embassy/src/blocking_mutex/kind.rs new file mode 100644 index 00000000..30fc9049 --- /dev/null +++ b/embassy/src/blocking_mutex/kind.rs @@ -0,0 +1,19 @@ +use super::{CriticalSectionMutex, Mutex, NoopMutex, ThreadModeMutex}; + +pub trait MutexKind { + type Mutex: Mutex; +} + +pub enum CriticalSection {} +impl MutexKind for CriticalSection { + type Mutex = CriticalSectionMutex; +} + +pub enum ThreadMode {} +impl MutexKind for ThreadMode { + type Mutex = ThreadModeMutex; +} +pub enum Noop {} +impl MutexKind for Noop { + type Mutex = NoopMutex; +} diff --git a/embassy/src/blocking_mutex/mod.rs b/embassy/src/blocking_mutex/mod.rs index d112d2ed..641a1ed9 100644 --- a/embassy/src/blocking_mutex/mod.rs +++ b/embassy/src/blocking_mutex/mod.rs @@ -1,5 +1,7 @@ //! Blocking mutex (not async) +pub mod kind; + use core::cell::UnsafeCell; use critical_section::CriticalSection; @@ -13,7 +15,7 @@ pub trait Mutex { fn new(data: Self::Data) -> Self; /// Creates a critical section and grants temporary access to the protected data. - fn lock(&mut self, f: impl FnOnce(&Self::Data) -> R) -> R; + fn lock(&self, f: impl FnOnce(&Self::Data) -> R) -> R; } /// A "mutex" based on critical sections @@ -55,7 +57,7 @@ impl Mutex for CriticalSectionMutex { Self::new(data) } - fn lock(&mut self, f: impl FnOnce(&Self::Data) -> R) -> R { + fn lock(&self, f: impl FnOnce(&Self::Data) -> R) -> R { critical_section::with(|cs| f(self.borrow(cs))) } } @@ -102,7 +104,7 @@ impl Mutex for ThreadModeMutex { Self::new(data) } - fn lock(&mut self, f: impl FnOnce(&Self::Data) -> R) -> R { + fn lock(&self, f: impl FnOnce(&Self::Data) -> R) -> R { f(self.borrow()) } } @@ -155,7 +157,7 @@ impl Mutex for NoopMutex { Self::new(data) } - fn lock(&mut self, f: impl FnOnce(&Self::Data) -> R) -> R { + fn lock(&self, f: impl FnOnce(&Self::Data) -> R) -> R { f(self.borrow()) } }