aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2021-09-12 23:27:13 +0200
committerDario Nieuwenhuis <[email protected]>2021-09-12 23:28:00 +0200
commit5be5bdfd20900ee1973097cf46ec946cad547e30 (patch)
tree20eef5dd9902276930788f2651b1e389aa492345
parente1f9dd1170111aac85a1729e9e642f602198d44a (diff)
embassy/blocking_mutex: add MutexKind to allow writing code generic over mutex kinds.
-rw-r--r--embassy/src/blocking_mutex/kind.rs19
-rw-r--r--embassy/src/blocking_mutex/mod.rs10
2 files changed, 25 insertions, 4 deletions
diff --git a/embassy/src/blocking_mutex/kind.rs b/embassy/src/blocking_mutex/kind.rs
new file mode 100644
index 000000000..30fc90497
--- /dev/null
+++ b/embassy/src/blocking_mutex/kind.rs
@@ -0,0 +1,19 @@
1use super::{CriticalSectionMutex, Mutex, NoopMutex, ThreadModeMutex};
2
3pub trait MutexKind {
4 type Mutex<T>: Mutex<Data = T>;
5}
6
7pub enum CriticalSection {}
8impl MutexKind for CriticalSection {
9 type Mutex<T> = CriticalSectionMutex<T>;
10}
11
12pub enum ThreadMode {}
13impl MutexKind for ThreadMode {
14 type Mutex<T> = ThreadModeMutex<T>;
15}
16pub enum Noop {}
17impl MutexKind for Noop {
18 type Mutex<T> = NoopMutex<T>;
19}
diff --git a/embassy/src/blocking_mutex/mod.rs b/embassy/src/blocking_mutex/mod.rs
index d112d2ede..641a1ed93 100644
--- a/embassy/src/blocking_mutex/mod.rs
+++ b/embassy/src/blocking_mutex/mod.rs
@@ -1,5 +1,7 @@
1//! Blocking mutex (not async) 1//! Blocking mutex (not async)
2 2
3pub mod kind;
4
3use core::cell::UnsafeCell; 5use core::cell::UnsafeCell;
4use critical_section::CriticalSection; 6use critical_section::CriticalSection;
5 7
@@ -13,7 +15,7 @@ pub trait Mutex {
13 fn new(data: Self::Data) -> Self; 15 fn new(data: Self::Data) -> Self;
14 16
15 /// Creates a critical section and grants temporary access to the protected data. 17 /// Creates a critical section and grants temporary access to the protected data.
16 fn lock<R>(&mut self, f: impl FnOnce(&Self::Data) -> R) -> R; 18 fn lock<R>(&self, f: impl FnOnce(&Self::Data) -> R) -> R;
17} 19}
18 20
19/// A "mutex" based on critical sections 21/// A "mutex" based on critical sections
@@ -55,7 +57,7 @@ impl<T> Mutex for CriticalSectionMutex<T> {
55 Self::new(data) 57 Self::new(data)
56 } 58 }
57 59
58 fn lock<R>(&mut self, f: impl FnOnce(&Self::Data) -> R) -> R { 60 fn lock<R>(&self, f: impl FnOnce(&Self::Data) -> R) -> R {
59 critical_section::with(|cs| f(self.borrow(cs))) 61 critical_section::with(|cs| f(self.borrow(cs)))
60 } 62 }
61} 63}
@@ -102,7 +104,7 @@ impl<T> Mutex for ThreadModeMutex<T> {
102 Self::new(data) 104 Self::new(data)
103 } 105 }
104 106
105 fn lock<R>(&mut self, f: impl FnOnce(&Self::Data) -> R) -> R { 107 fn lock<R>(&self, f: impl FnOnce(&Self::Data) -> R) -> R {
106 f(self.borrow()) 108 f(self.borrow())
107 } 109 }
108} 110}
@@ -155,7 +157,7 @@ impl<T> Mutex for NoopMutex<T> {
155 Self::new(data) 157 Self::new(data)
156 } 158 }
157 159
158 fn lock<R>(&mut self, f: impl FnOnce(&Self::Data) -> R) -> R { 160 fn lock<R>(&self, f: impl FnOnce(&Self::Data) -> R) -> R {
159 f(self.borrow()) 161 f(self.borrow())
160 } 162 }
161} 163}