From 81c9d39ebc8e73baa396a60eb8d9f717a74b96f0 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Fri, 10 Mar 2017 20:35:52 -0500 Subject: CsCtxt renamed to CriticalSection. Mutex.lock removed in favor of Mutex.borrow --- src/interrupt.rs | 21 ++++++++------------- src/peripheral/mod.rs | 6 +++--- 2 files changed, 11 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/interrupt.rs b/src/interrupt.rs index 95829ca..6d84dec 100644 --- a/src/interrupt.rs +++ b/src/interrupt.rs @@ -15,14 +15,9 @@ impl Mutex { } impl Mutex { - /// Gets access to the inner data - /// - /// NOTE this prevents interrupts handlers from running thus gaining - /// exclusive access to the processor - pub fn lock(&self, f: F) -> R - where F: FnOnce(&mut T) -> R - { - unsafe { ::interrupt::free(|_| f(&mut *self.inner.get())) } + /// Borrows the data for the duration of the critical section + pub fn borrow<'cs>(&self, _ctxt: &'cs CriticalSection) -> &'cs T { + unsafe { &*self.inner.get() } } } @@ -32,7 +27,6 @@ pub unsafe trait Nr { fn nr(&self) -> u8; } -// FIXME `T` should have some bound: `Send` or `Sync`? unsafe impl Sync for Mutex {} /// Disable interrupts, globally @@ -69,10 +63,10 @@ pub fn enable() { } } -/// Critical section token +/// Critical section context /// /// Indicates that you are executing code within a critical section -pub struct CsCtxt { +pub struct CriticalSection { _0: (), } @@ -80,14 +74,15 @@ pub struct CsCtxt { /// /// This as also known as a "critical section". pub fn free(f: F) -> R - where F: FnOnce(&CsCtxt) -> R +where + F: FnOnce(&CriticalSection) -> R, { let primask = ::register::primask::read(); // disable interrupts disable(); - let r = f(&CsCtxt { _0: () }); + let r = f(&CriticalSection { _0: () }); // If the interrupts were active before our `disable` call, then re-enable // them. Otherwise, keep them disabled diff --git a/src/peripheral/mod.rs b/src/peripheral/mod.rs index f47baf5..f9ee217 100644 --- a/src/peripheral/mod.rs +++ b/src/peripheral/mod.rs @@ -10,7 +10,7 @@ use core::ptr; use volatile_register::{RO, RW, WO}; -use interrupt::{CsCtxt, Nr}; +use interrupt::{CriticalSection, Nr}; #[cfg(test)] mod test; @@ -69,8 +69,8 @@ impl Peripheral { } } - /// Borrows the peripheral for the duration of the critical section - pub fn borrow<'cs>(&self, _ctxt: &'cs CsCtxt) -> &'cs T { + /// Borrows the peripheral for the duration of a critical section + pub fn borrow<'cs>(&self, _ctxt: &'cs CriticalSection) -> &'cs T { unsafe { &*self.get() } } -- cgit v1.2.3