diff --git a/Cargo.toml b/Cargo.toml index f3d9926acf..1ca881e436 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,8 @@ [package] +authors = ["Jorge Aparicio "] +build = "build.rs" name = "cortex-m-srp" version = "0.1.0" -authors = ["Jorge Aparicio "] [dependencies] cortex-m = "0.2.0" diff --git a/src/checked.rs b/src/checked.rs index fbdf692542..4c91c0543f 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(&'static self, f: F) + pub fn lock(&'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(&'static self, f: F) + pub fn lock_mut(&'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 } } } diff --git a/src/lib.rs b/src/lib.rs index 95aa8ba34c..7cd0ff684c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 where