aboutsummaryrefslogtreecommitdiff
path: root/src/sync.rs
diff options
context:
space:
mode:
authorChristian Cunningham <cc@localhost>2022-08-18 20:37:09 -0700
committerChristian Cunningham <cc@localhost>2022-08-18 20:37:09 -0700
commit66ee8a34f3bcde31f9d5919f2e0e363ac11f4aca (patch)
tree7a63b38c6ea59d3058d0325d4d326a9b55390c32 /src/sync.rs
parent6f1e6acb1a9775eef4b0d8879c102df86e207687 (diff)
Formatted printing to UART
Diffstat (limited to 'src/sync.rs')
-rw-r--r--src/sync.rs33
1 files changed, 33 insertions, 0 deletions
diff --git a/src/sync.rs b/src/sync.rs
new file mode 100644
index 0000000..2b7e1ff
--- /dev/null
+++ b/src/sync.rs
@@ -0,0 +1,33 @@
+pub mod interface {
+ pub trait Mutex {
+ type Data;
+ fn lock<'a, R>(&'a self, f: impl FnOnce(&'a mut Self::Data) -> R) -> R;
+ }
+}
+
+use core::cell::UnsafeCell;
+
+pub struct NullLock<T> where T: ?Sized {
+ data: UnsafeCell<T>,
+}
+
+unsafe impl<T> Send for NullLock<T> where T: ?Sized + Send {}
+unsafe impl<T> Sync for NullLock<T> where T: ?Sized + Send {}
+
+impl<T> NullLock<T> {
+ pub const fn new(data: T) -> Self {
+ Self {
+ data: UnsafeCell::new(data),
+ }
+ }
+}
+
+impl<T> interface::Mutex for NullLock<T> {
+ type Data = T;
+
+ fn lock<'a, R>(&'a self, f: impl FnOnce(&'a mut T) -> R) -> R {
+ let data = unsafe { &mut *self.data.get() };
+
+ f(data)
+ }
+}