diff options
author | 2020-04-21 21:56:47 +0200 | |
---|---|---|
committer | 2020-04-21 21:56:47 +0200 | |
commit | 64ff1e185001fdb3d56b7fc166db08713e0d97d2 (patch) | |
tree | ceda41f730c44aebe05e7871a05b6b5f5f2c8937 | |
parent | e0034b6e9d7d15bcd5a5b9990e31d26c887846d5 (diff) | |
download | cortex-m-64ff1e185001fdb3d56b7fc166db08713e0d97d2.tar.gz cortex-m-64ff1e185001fdb3d56b7fc166db08713e0d97d2.tar.zst cortex-m-64ff1e185001fdb3d56b7fc166db08713e0d97d2.zip |
UnsafeCell test
-rw-r--r-- | src/mutex.rs | 13 |
1 files changed, 9 insertions, 4 deletions
diff --git a/src/mutex.rs b/src/mutex.rs index 7346e35..6948512 100644 --- a/src/mutex.rs +++ b/src/mutex.rs @@ -1,17 +1,17 @@ //! Implementation of a critical section based mutex that also implements the `mutex-trait`. -use core::cell::RefCell; +use core::cell::UnsafeCell; /// A critical section based mutex pub struct CriticalSectionMutex<T> { - data: RefCell<T>, + data: UnsafeCell<T>, } impl<T> CriticalSectionMutex<T> { /// Create a new mutex pub const fn new(data: T) -> Self { CriticalSectionMutex { - data: RefCell::new(data), + data: UnsafeCell::new(data), } } } @@ -20,6 +20,11 @@ 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())) + crate::interrupt::free(|_| f(unsafe { &mut *self.data.get() })) } } + +// NOTE A `Mutex` can be used as a channel so the protected data must be `Send` +// to prevent sending non-Sendable stuff (e.g. access tokens) across different +// execution contexts (e.g. interrupts) +unsafe impl<T> Sync for CriticalSectionMutex<T> where T: Send {} |