embassy/blocking_mutex: add MutexKind to allow writing code generic over mutex kinds.
This commit is contained in:
parent
e1f9dd1170
commit
5be5bdfd20
19
embassy/src/blocking_mutex/kind.rs
Normal file
19
embassy/src/blocking_mutex/kind.rs
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
use super::{CriticalSectionMutex, Mutex, NoopMutex, ThreadModeMutex};
|
||||||
|
|
||||||
|
pub trait MutexKind {
|
||||||
|
type Mutex<T>: Mutex<Data = T>;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum CriticalSection {}
|
||||||
|
impl MutexKind for CriticalSection {
|
||||||
|
type Mutex<T> = CriticalSectionMutex<T>;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum ThreadMode {}
|
||||||
|
impl MutexKind for ThreadMode {
|
||||||
|
type Mutex<T> = ThreadModeMutex<T>;
|
||||||
|
}
|
||||||
|
pub enum Noop {}
|
||||||
|
impl MutexKind for Noop {
|
||||||
|
type Mutex<T> = NoopMutex<T>;
|
||||||
|
}
|
@ -1,5 +1,7 @@
|
|||||||
//! Blocking mutex (not async)
|
//! Blocking mutex (not async)
|
||||||
|
|
||||||
|
pub mod kind;
|
||||||
|
|
||||||
use core::cell::UnsafeCell;
|
use core::cell::UnsafeCell;
|
||||||
use critical_section::CriticalSection;
|
use critical_section::CriticalSection;
|
||||||
|
|
||||||
@ -13,7 +15,7 @@ pub trait Mutex {
|
|||||||
fn new(data: Self::Data) -> Self;
|
fn new(data: Self::Data) -> Self;
|
||||||
|
|
||||||
/// Creates a critical section and grants temporary access to the protected data.
|
/// Creates a critical section and grants temporary access to the protected data.
|
||||||
fn lock<R>(&mut self, f: impl FnOnce(&Self::Data) -> R) -> R;
|
fn lock<R>(&self, f: impl FnOnce(&Self::Data) -> R) -> R;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A "mutex" based on critical sections
|
/// A "mutex" based on critical sections
|
||||||
@ -55,7 +57,7 @@ impl<T> Mutex for CriticalSectionMutex<T> {
|
|||||||
Self::new(data)
|
Self::new(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn lock<R>(&mut self, f: impl FnOnce(&Self::Data) -> R) -> R {
|
fn lock<R>(&self, f: impl FnOnce(&Self::Data) -> R) -> R {
|
||||||
critical_section::with(|cs| f(self.borrow(cs)))
|
critical_section::with(|cs| f(self.borrow(cs)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -102,7 +104,7 @@ impl<T> Mutex for ThreadModeMutex<T> {
|
|||||||
Self::new(data)
|
Self::new(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn lock<R>(&mut self, f: impl FnOnce(&Self::Data) -> R) -> R {
|
fn lock<R>(&self, f: impl FnOnce(&Self::Data) -> R) -> R {
|
||||||
f(self.borrow())
|
f(self.borrow())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -155,7 +157,7 @@ impl<T> Mutex for NoopMutex<T> {
|
|||||||
Self::new(data)
|
Self::new(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn lock<R>(&mut self, f: impl FnOnce(&Self::Data) -> R) -> R {
|
fn lock<R>(&self, f: impl FnOnce(&Self::Data) -> R) -> R {
|
||||||
f(self.borrow())
|
f(self.borrow())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user