From 185f368d63e6b083d1717c3625b7756e77782add Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Fri, 7 Apr 2017 18:57:13 -0500 Subject: [PATCH] use volatile read/writes for checked::Resource.locked --- Cargo.toml | 4 ++++ src/checked.rs | 15 ++++++++------- src/lib.rs | 1 + 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1ca881e436..c51a72ebda 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,3 +6,7 @@ version = "0.1.0" [dependencies] cortex-m = "0.2.0" + +[dependencies.vcell] +features = ["const-fn"] +version = "0.1.0" \ No newline at end of file diff --git a/src/checked.rs b/src/checked.rs index eb0da3d7fe..87318eca7f 100644 --- a/src/checked.rs +++ b/src/checked.rs @@ -5,26 +5,27 @@ use core::cell::UnsafeCell; use cortex_m::interrupt; use cortex_m::register::{basepri, basepri_max}; +use vcell::VolatileCell; use Ceiling; -unsafe fn acquire(locked: &UnsafeCell, ceiling: u8) -> u8 { +unsafe fn acquire(locked: &VolatileCell, ceiling: u8) -> u8 { interrupt::free( |_| { - assert!(!*locked.get(), "resource already locked"); + assert!(!locked.get(), "resource already locked"); let old_basepri = basepri::read(); basepri_max::write(ceiling); - *locked.get() = true; + locked.set(true); old_basepri }, ) } -unsafe fn release(locked: &UnsafeCell, old_basepri: u8) { +unsafe fn release(locked: &VolatileCell, old_basepri: u8) { // XXX Is it really memory safe to *not* use a global critical section here // interrupt::free( // |_| { - *locked.get() = false; + locked.set(false); basepri::write(old_basepri); // }, // ); @@ -34,7 +35,7 @@ unsafe fn release(locked: &UnsafeCell, old_basepri: u8) { pub struct Resource { _marker: PhantomData, data: UnsafeCell, - locked: UnsafeCell, + locked: VolatileCell, } impl Resource @@ -46,7 +47,7 @@ where Resource { _marker: PhantomData, data: UnsafeCell::new(data), - locked: UnsafeCell::new(false), + locked: VolatileCell::new(false), } } diff --git a/src/lib.rs b/src/lib.rs index 7cd0ff684c..a7b6412b52 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,6 +9,7 @@ #![no_std] extern crate cortex_m; +extern crate vcell; pub mod checked;