Skip to content
This repository was archived by the owner on May 6, 2020. It is now read-only.

Fix races #2

Merged
merged 2 commits into from
Apr 1, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions async-cortex-m/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ static VTABLE: RawWakerVTable = {
wake_by_ref(p)
}
unsafe fn wake_by_ref(p: *const ()) {
(*(p as *const AtomicBool)).store(true, Ordering::Relaxed)
(*(p as *const AtomicBool)).store(true, Ordering::Release)
}
unsafe fn drop(_: *const ()) {
// no-op
Expand Down Expand Up @@ -66,9 +66,12 @@ impl Executor {
let waker =
unsafe { Waker::from_raw(RawWaker::new(&ready as *const _ as *const _, &VTABLE)) };
let val = loop {
let mut task_woken = false;

// advance the main task
if ready.load(Ordering::Relaxed) {
ready.store(false, Ordering::Relaxed);
if ready.load(Ordering::Acquire) {
task_woken = true;
ready.store(false, Ordering::Release);

let mut cx = Context::from_waker(&waker);
if let Poll::Ready(val) = f.as_mut().poll(&mut cx) {
Expand All @@ -87,9 +90,11 @@ impl Executor {
// interrupt handlers (the only source of 'race conditions' (!= data races)) are
// "oneshot": they'll issue a `wake` and then disable themselves to not run again
// until the woken task has made more work
if task.ready.load(Ordering::Relaxed) {
if task.ready.load(Ordering::Acquire) {
task_woken = true;

// we are about to service the task so switch the `ready` flag to `false`
task.ready.store(false, Ordering::Relaxed);
task.ready.store(false, Ordering::Release);

// NOTE we never deallocate tasks so `&ready` is always pointing to
// allocated memory (`&'static AtomicBool`)
Expand All @@ -108,6 +113,11 @@ impl Executor {
}
}

if task_woken {
// If at least one task was woken up, do not sleep, try again
continue;
}

// try to sleep; this will be a no-op if any of the previous tasks generated a SEV or an
// interrupt ran (regardless of whether it generated a wake-up or not)
asm::wfe();
Expand Down