Skip to content

Commit

Permalink
Move AtomicWaker, reimplement using Generic
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani committed Nov 22, 2024
1 parent d163632 commit 869f4bc
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 46 deletions.
2 changes: 1 addition & 1 deletion esp-hal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ digest = { version = "0.10.7", default-features = false, optio
document-features = "0.2.10"
embassy-embedded-hal = "0.2.0"
embassy-futures = "0.1.1"
embassy-sync = "0.6.0"
embassy-sync = "0.6.1"
embassy-usb-driver = { version = "0.1.0", optional = true }
embassy-usb-synopsys-otg = { version = "0.1.0", optional = true }
embedded-can = "0.4.1"
Expand Down
30 changes: 30 additions & 0 deletions esp-hal/src/asynch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//! Asynchronous utilities.
use core::task::Waker;

use embassy_sync::waitqueue::GenericAtomicWaker;

use crate::sync::RawMutex;

/// Utility struct to register and wake a waker.
pub struct AtomicWaker {
waker: GenericAtomicWaker<RawMutex>,
}

impl AtomicWaker {
/// Create a new `AtomicWaker`.
#[allow(clippy::new_without_default)]
pub const fn new() -> Self {
Self {
waker: GenericAtomicWaker::new(RawMutex::new()),
}
}

delegate::delegate! {
to self.waker {
/// Register a waker. Overwrites the previous waker, if any.
pub fn register(&self, w: &Waker);
/// Wake the registered waker, if any.
pub fn wake(&self);
}
}
}
47 changes: 2 additions & 45 deletions esp-hal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@
// MUST be the first module
mod fmt;

pub mod asynch;

#[cfg(riscv)]
pub use esp_riscv_rt::{self, entry, riscv};
#[cfg(xtensa)]
Expand Down Expand Up @@ -572,48 +574,3 @@ pub fn init(config: Config) -> Peripherals {

peripherals
}

/// Asynchronous utilities.
pub mod asynch {
use core::{cell::Cell, task::Waker};

use embassy_sync::blocking_mutex::Mutex;

use crate::sync::RawMutex;

/// TODO: this just exists to test RawMutex, otherwise it should be replaced
/// by embassy_sync::waitqueue::atomic_waker::GenericAtomicWaker.
pub struct AtomicWaker {
waker: Mutex<RawMutex, Cell<Option<Waker>>>,
}

impl AtomicWaker {
/// Create a new `AtomicWaker`.
#[allow(clippy::new_without_default)]
pub const fn new() -> Self {
Self {
waker: Mutex::const_new(RawMutex::new(), Cell::new(None)),
}
}

/// Register a waker. Overwrites the previous waker, if any.
pub fn register(&self, w: &Waker) {
self.waker.lock(|cell| {
cell.set(match cell.replace(None) {
Some(w2) if (w2.will_wake(w)) => Some(w2),
_ => Some(w.clone()),
})
})
}

/// Wake the registered waker, if any.
pub fn wake(&self) {
self.waker.lock(|cell| {
if let Some(w) = cell.replace(None) {
w.wake_by_ref();
cell.set(Some(w));
}
})
}
}
}

0 comments on commit 869f4bc

Please sign in to comment.