aboutsummaryrefslogtreecommitdiff
path: root/src/mutex.rs
diff options
context:
space:
mode:
authorGravatar Emil Fresk <emil.fresk@gmail.com> 2020-04-21 21:14:43 +0200
committerGravatar Emil Fresk <emil.fresk@gmail.com> 2020-04-21 21:14:43 +0200
commite0034b6e9d7d15bcd5a5b9990e31d26c887846d5 (patch)
tree037201985bc4f726b5cc3b6076f7c69661b36a16 /src/mutex.rs
parent22d47dd75e9fb5004e0192666123d28f0a418310 (diff)
downloadcortex-m-e0034b6e9d7d15bcd5a5b9990e31d26c887846d5.tar.gz
cortex-m-e0034b6e9d7d15bcd5a5b9990e31d26c887846d5.tar.zst
cortex-m-e0034b6e9d7d15bcd5a5b9990e31d26c887846d5.zip
Added arch specific mutex implementation
Diffstat (limited to 'src/mutex.rs')
-rw-r--r--src/mutex.rs25
1 files changed, 25 insertions, 0 deletions
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()))
+ }
+}