use volatile read/writes for checked::Resource.locked

This commit is contained in:
Jorge Aparicio 2017-04-07 18:57:13 -05:00
parent 22bb0d8eae
commit 185f368d63
3 changed files with 13 additions and 7 deletions

View file

@ -6,3 +6,7 @@ version = "0.1.0"
[dependencies] [dependencies]
cortex-m = "0.2.0" cortex-m = "0.2.0"
[dependencies.vcell]
features = ["const-fn"]
version = "0.1.0"

View file

@ -5,26 +5,27 @@ use core::cell::UnsafeCell;
use cortex_m::interrupt; use cortex_m::interrupt;
use cortex_m::register::{basepri, basepri_max}; use cortex_m::register::{basepri, basepri_max};
use vcell::VolatileCell;
use Ceiling; use Ceiling;
unsafe fn acquire(locked: &UnsafeCell<bool>, ceiling: u8) -> u8 { unsafe fn acquire(locked: &VolatileCell<bool>, ceiling: u8) -> u8 {
interrupt::free( interrupt::free(
|_| { |_| {
assert!(!*locked.get(), "resource already locked"); assert!(!locked.get(), "resource already locked");
let old_basepri = basepri::read(); let old_basepri = basepri::read();
basepri_max::write(ceiling); basepri_max::write(ceiling);
*locked.get() = true; locked.set(true);
old_basepri old_basepri
}, },
) )
} }
unsafe fn release(locked: &UnsafeCell<bool>, old_basepri: u8) { unsafe fn release(locked: &VolatileCell<bool>, old_basepri: u8) {
// XXX Is it really memory safe to *not* use a global critical section here // XXX Is it really memory safe to *not* use a global critical section here
// interrupt::free( // interrupt::free(
// |_| { // |_| {
*locked.get() = false; locked.set(false);
basepri::write(old_basepri); basepri::write(old_basepri);
// }, // },
// ); // );
@ -34,7 +35,7 @@ unsafe fn release(locked: &UnsafeCell<bool>, old_basepri: u8) {
pub struct Resource<T, C> { pub struct Resource<T, C> {
_marker: PhantomData<C>, _marker: PhantomData<C>,
data: UnsafeCell<T>, data: UnsafeCell<T>,
locked: UnsafeCell<bool>, locked: VolatileCell<bool>,
} }
impl<T, C> Resource<T, C> impl<T, C> Resource<T, C>
@ -46,7 +47,7 @@ where
Resource { Resource {
_marker: PhantomData, _marker: PhantomData,
data: UnsafeCell::new(data), data: UnsafeCell::new(data),
locked: UnsafeCell::new(false), locked: VolatileCell::new(false),
} }
} }

View file

@ -9,6 +9,7 @@
#![no_std] #![no_std]
extern crate cortex_m; extern crate cortex_m;
extern crate vcell;
pub mod checked; pub mod checked;