diff options
| author | Jorge Aparicio <japaricious@gmail.com> | 2017-04-07 18:14:48 -0500 |
|---|---|---|
| committer | Jorge Aparicio <japaricious@gmail.com> | 2017-04-07 18:14:48 -0500 |
| commit | f2bab47aa4bdd04f79e608debe64e3ddcea07702 (patch) | |
| tree | 5626aba835b65a100fbefdb6f1f33a27bcfe81ce /src | |
| parent | 8e1c72057c6fdd09ad14e9b69596e17fcfd9335f (diff) | |
allow returns from checked::Resource.lock{,mut}
also add compiler barriers
Diffstat (limited to 'src')
| -rw-r--r-- | src/checked.rs | 18 | ||||
| -rw-r--r-- | src/lib.rs | 14 |
2 files changed, 26 insertions, 6 deletions
diff --git a/src/checked.rs b/src/checked.rs index fbdf692..4c91c05 100644 --- a/src/checked.rs +++ b/src/checked.rs @@ -51,27 +51,33 @@ where /// Locks the resource, blocking tasks with priority equal or smaller than /// the ceiling `C` - pub fn lock<F>(&'static self, f: F) + pub fn lock<R, F>(&'static self, f: F) -> R where - F: FnOnce(&T), + F: FnOnce(&T) -> R, { unsafe { let old_basepri = acquire(&self.locked, C::ceiling()); - f(&*self.data.get()); + ::compiler_barrier(); + let ret = f(&*self.data.get()); + ::compiler_barrier(); release(&self.locked, old_basepri); + ret } } /// Mutably locks the resource, blocking tasks with priority equal or /// smaller than the ceiling `C` - pub fn lock_mut<F>(&'static self, f: F) + pub fn lock_mut<R, F>(&'static self, f: F) -> R where - F: FnOnce(&mut T), + F: FnOnce(&mut T) -> R, { unsafe { let old_basepri = acquire(&self.locked, C::ceiling()); - f(&mut *self.data.get()); + ::compiler_barrier(); + let ret = f(&mut *self.data.get()); + ::compiler_barrier(); release(&self.locked, old_basepri); + ret } } } @@ -105,7 +105,9 @@ where lock_check(ceiling); let old_basepri = basepri::read(); basepri_max::write(ceiling); + compiler_barrier(); let ret = f(&*res, ptr::read(0 as *const _)); + compiler_barrier(); basepri::write(old_basepri); ret } @@ -121,11 +123,23 @@ where lock_check(ceiling); let old_basepri = basepri::read(); basepri_max::write(ceiling); + compiler_barrier(); let ret = f(&mut *res, ptr::read(0 as *const _)); + compiler_barrier(); basepri::write(old_basepri); ret } +fn compiler_barrier() { + unsafe { + asm!("" + : + : + : "memory" + : "volatile"); + } +} + /// A peripheral as a resource pub struct ResourceP<P, Ceiling> where |
