util: Do not unregister waker on wake in AtomicWaker.

This commit is contained in:
Dario Nieuwenhuis 2021-03-26 23:20:53 +01:00
parent eedb51bbb6
commit 0e8bb5dc0b
3 changed files with 15 additions and 21 deletions

View File

@ -9,7 +9,7 @@ use core::ptr;
use core::task::{Context, Poll}; use core::task::{Context, Poll};
use embassy::interrupt::InterruptExt; use embassy::interrupt::InterruptExt;
use embassy::traits::gpio::{WaitForHigh, WaitForLow}; use embassy::traits::gpio::{WaitForHigh, WaitForLow};
use embassy::util::{AtomicWakerRegistration, PeripheralBorrow, Signal}; use embassy::util::{AtomicWaker, PeripheralBorrow, Signal};
use embedded_hal::digital::v2::{InputPin, OutputPin, StatefulOutputPin}; use embedded_hal::digital::v2::{InputPin, OutputPin, StatefulOutputPin};
use crate::gpio::sealed::Pin as _; use crate::gpio::sealed::Pin as _;
@ -68,9 +68,9 @@ impl ChannelID for ChAny {
} }
} }
const NEW_AWR: AtomicWakerRegistration = AtomicWakerRegistration::new(); const NEW_AWR: AtomicWaker = AtomicWaker::new();
static CHANNEL_WAKERS: [AtomicWakerRegistration; CHANNEL_COUNT] = [NEW_AWR; CHANNEL_COUNT]; static CHANNEL_WAKERS: [AtomicWaker; CHANNEL_COUNT] = [NEW_AWR; CHANNEL_COUNT];
static PORT_WAKERS: [AtomicWakerRegistration; PIN_COUNT] = [NEW_AWR; PIN_COUNT]; static PORT_WAKERS: [AtomicWaker; PIN_COUNT] = [NEW_AWR; PIN_COUNT];
pub enum InputChannelPolarity { pub enum InputChannelPolarity {
None, None,

View File

@ -48,11 +48,11 @@ impl WakerRegistration {
} }
} }
pub struct AtomicWakerRegistration { pub struct AtomicWaker {
waker: AtomicPtr<TaskHeader>, waker: AtomicPtr<TaskHeader>,
} }
impl AtomicWakerRegistration { impl AtomicWaker {
pub const fn new() -> Self { pub const fn new() -> Self {
Self { Self {
waker: AtomicPtr::new(ptr::null_mut()), waker: AtomicPtr::new(ptr::null_mut()),
@ -62,17 +62,14 @@ impl AtomicWakerRegistration {
/// Register a waker. Overwrites the previous waker, if any. /// Register a waker. Overwrites the previous waker, if any.
pub fn register(&self, w: &Waker) { pub fn register(&self, w: &Waker) {
let w = unsafe { task_from_waker(w) }; let w = unsafe { task_from_waker(w) };
let w2 = self.waker.swap(w.as_ptr(), Ordering::Relaxed); self.waker.store(w.as_ptr(), Ordering::Relaxed);
if !w2.is_null() && w2 != w.as_ptr() {
unsafe { wake_task(NonNull::new_unchecked(w2)) };
}
} }
/// Wake the registered waker, if any. /// Wake the registered waker, if any.
pub fn wake(&self) { pub fn wake(&self) {
let w2 = self.waker.swap(ptr::null_mut(), Ordering::Relaxed); let w2 = self.waker.load(Ordering::Relaxed);
if !w2.is_null() { if let Some(w2) = NonNull::new(w2) {
unsafe { wake_task(NonNull::new_unchecked(w2)) }; unsafe { wake_task(w2) };
} }
} }
} }

View File

@ -49,11 +49,11 @@ impl WakerRegistration {
} }
/// Utility struct to register and wake a waker. /// Utility struct to register and wake a waker.
pub struct AtomicWakerRegistration { pub struct AtomicWaker {
waker: Mutex<Cell<Option<Waker>>>, waker: Mutex<Cell<Option<Waker>>>,
} }
impl AtomicWakerRegistration { impl AtomicWaker {
pub const fn new() -> Self { pub const fn new() -> Self {
Self { Self {
waker: Mutex::new(Cell::new(None)), waker: Mutex::new(Cell::new(None)),
@ -66,11 +66,7 @@ impl AtomicWakerRegistration {
let cell = self.waker.borrow(cs); let cell = self.waker.borrow(cs);
cell.set(match cell.replace(None) { cell.set(match cell.replace(None) {
Some(w2) if (w2.will_wake(w)) => Some(w2), Some(w2) if (w2.will_wake(w)) => Some(w2),
Some(w2) => { _ => Some(w.clone()),
w2.wake();
Some(w.clone())
}
None => Some(w.clone()),
}) })
}) })
} }
@ -80,7 +76,8 @@ impl AtomicWakerRegistration {
cortex_m::interrupt::free(|cs| { cortex_m::interrupt::free(|cs| {
let cell = self.waker.borrow(cs); let cell = self.waker.borrow(cs);
if let Some(w) = cell.replace(None) { if let Some(w) = cell.replace(None) {
w.wake() w.wake_by_ref();
cell.set(Some(w));
} }
}) })
} }