aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/lib.rs1
-rw-r--r--src/mutex.rs25
2 files changed, 26 insertions, 0 deletions
diff --git a/src/lib.rs b/src/lib.rs
index f8b5606..5287041 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -62,5 +62,6 @@ pub mod interrupt;
pub mod itm;
pub mod peripheral;
pub mod register;
+pub mod mutex;
pub use crate::peripheral::Peripherals;
diff --git a/src/mutex.rs b/src/mutex.rs
new file mode 100644
index 0000000..7346e35
--- /dev/null
+++ b/src/mutex.rs
@@ -0,0 +1,25 @@
+//! Implementation of a critical section based mutex that also implements the `mutex-trait`.
+
+use core::cell::RefCell;
+
+/// A critical section based mutex
+pub struct CriticalSectionMutex<T> {
+ data: RefCell<T>,
+}
+
+impl<T> CriticalSectionMutex<T> {
+ /// Create a new mutex
+ pub const fn new(data: T) -> Self {
+ CriticalSectionMutex {
+ data: RefCell::new(data),
+ }
+ }
+}
+
+impl<T> mutex_trait::Mutex for &'_ CriticalSectionMutex<T> {
+ type Data = T;
+
+ fn lock<R>(&mut self, f: impl FnOnce(&mut Self::Data) -> R) -> R {
+ crate::interrupt::free(|_| f(&mut *self.data.borrow_mut()))
+ }
+}