Use critical_section crate

This commit is contained in:
Dario Nieuwenhuis
2021-05-11 00:57:52 +02:00
parent f817f374b6
commit 7fa0e57172
17 changed files with 41 additions and 157 deletions

View File

@ -25,6 +25,7 @@ pin-project = { version = "1.0.2", default-features = false }
embassy-macros = { version = "0.1.0", path = "../embassy-macros"}
embassy-traits = { version = "0.1.0", path = "../embassy-traits"}
atomic-polyfill = { version = "0.1.1" }
critical-section = "0.2.0"
# Workaround https://github.com/japaric/cast.rs/pull/27
cast = { version = "=0.2.3", default-features = false }

View File

@ -1,30 +0,0 @@
pub use cs::{critical_section, CriticalSection};
#[cfg(feature = "std")]
mod cs {
static INIT: std::sync::Once = std::sync::Once::new();
static mut BKL: Option<std::sync::Mutex<()>> = None;
pub type CriticalSection = std::sync::MutexGuard<'static, ()>;
pub fn critical_section<F, R>(f: F) -> R
where
F: FnOnce(&CriticalSection) -> R,
{
INIT.call_once(|| unsafe {
BKL.replace(std::sync::Mutex::new(()));
});
let guard = unsafe { BKL.as_ref().unwrap().lock().unwrap() };
f(&guard)
}
}
#[cfg(not(feature = "std"))]
mod cs {
pub use cortex_m::interrupt::CriticalSection;
pub fn critical_section<F, R>(f: F) -> R
where
F: FnOnce(&CriticalSection) -> R,
{
cortex_m::interrupt::free(f)
}
}

View File

@ -1,5 +1,4 @@
//! Async utilities
mod critical_section;
mod drop_bomb;
mod forever;
mod mutex;
@ -10,7 +9,6 @@ mod signal;
#[cfg_attr(feature = "executor-agnostic", path = "waker_agnostic.rs")]
mod waker;
pub use critical_section::*;
pub use drop_bomb::*;
pub use forever::*;
pub use mutex::*;

View File

@ -1,5 +1,5 @@
use core::cell::UnsafeCell;
use cortex_m::interrupt::CriticalSection;
use critical_section::CriticalSection;
use crate::fmt::assert;
@ -30,7 +30,7 @@ impl<T> CriticalSectionMutex<T> {
impl<T> CriticalSectionMutex<T> {
/// Borrows the data for the duration of the critical section
pub fn borrow<'cs>(&'cs self, _cs: &'cs CriticalSection) -> &'cs T {
pub fn borrow<'cs>(&'cs self, _cs: CriticalSection<'cs>) -> &'cs T {
unsafe { &*self.inner.get() }
}
}

View File

@ -11,7 +11,6 @@ use ptr::NonNull;
use crate::executor;
use crate::fmt::panic;
use crate::interrupt::{Interrupt, InterruptExt};
use crate::util::critical_section::critical_section;
/// Synchronization primitive. Allows creating awaitable signals that may be passed between tasks.
///
@ -38,7 +37,7 @@ impl<T: Send> Signal<T> {
/// Mark this Signal as completed.
pub fn signal(&self, val: T) {
critical_section(|_| unsafe {
critical_section::with(|_| unsafe {
let state = &mut *self.state.get();
if let State::Waiting(waker) = mem::replace(state, State::Signaled(val)) {
waker.wake();
@ -47,14 +46,14 @@ impl<T: Send> Signal<T> {
}
pub fn reset(&self) {
critical_section(|_| unsafe {
critical_section::with(|_| unsafe {
let state = &mut *self.state.get();
*state = State::None
})
}
pub fn poll_wait(&self, cx: &mut Context<'_>) -> Poll<T> {
critical_section(|_| unsafe {
critical_section::with(|_| unsafe {
let state = &mut *self.state.get();
match state {
State::None => {
@ -78,7 +77,7 @@ impl<T: Send> Signal<T> {
/// non-blocking method to check whether this signal has been signaled.
pub fn signaled(&self) -> bool {
critical_section(|_| matches!(unsafe { &*self.state.get() }, State::Signaled(_)))
critical_section::with(|_| matches!(unsafe { &*self.state.get() }, State::Signaled(_)))
}
}

View File

@ -62,7 +62,7 @@ impl AtomicWaker {
/// Register a waker. Overwrites the previous waker, if any.
pub fn register(&mut self, w: &Waker) {
cortex_m::interrupt::free(|cs| {
critical_section::with(|cs| {
let cell = self.waker.borrow(cs);
cell.set(match cell.replace(None) {
Some(w2) if (w2.will_wake(w)) => Some(w2),
@ -73,7 +73,7 @@ impl AtomicWaker {
/// Wake the registered waker, if any.
pub fn wake(&mut self) {
cortex_m::interrupt::free(|cs| {
critical_section::with(|cs| {
let cell = self.waker.borrow(cs);
if let Some(w) = cell.replace(None) {
w.wake_by_ref();