From 66ee8a34f3bcde31f9d5919f2e0e363ac11f4aca Mon Sep 17 00:00:00 2001 From: Christian Cunningham Date: Thu, 18 Aug 2022 20:37:09 -0700 Subject: Formatted printing to UART --- src/sync.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/sync.rs (limited to 'src/sync.rs') 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 where T: ?Sized { + data: UnsafeCell, +} + +unsafe impl Send for NullLock where T: ?Sized + Send {} +unsafe impl Sync for NullLock where T: ?Sized + Send {} + +impl NullLock { + pub const fn new(data: T) -> Self { + Self { + data: UnsafeCell::new(data), + } + } +} + +impl interface::Mutex for NullLock { + 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) + } +} -- cgit v1.2.1