Skip to content

Commit

Permalink
Add custom AtomicWaker
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani committed Nov 18, 2024
1 parent c6404fe commit f83bd19
Show file tree
Hide file tree
Showing 19 changed files with 61 additions and 26 deletions.
1 change: 1 addition & 0 deletions esp-hal/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- I8080: Added `set_8bits_order()` to set the byte order in 8-bit mode (#2487)
- `I2c::{apply_config(), with_sda(), with_scl()}` (#2477)
- ESP32-S2: Added missing GPIO alternate functions (#2512)
- `esp_hal::asynch::AtomicWaker` that does not hold a global critical section (#2555)

### Changed

Expand Down
2 changes: 1 addition & 1 deletion esp-hal/src/dma/gdma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ impl<const N: u8> GdmaChannel for SpecificGdmaChannel<N> {
#[doc(hidden)]
pub struct ChannelTxImpl<C: GdmaChannel>(C);

use embassy_sync::waitqueue::AtomicWaker;
use crate::asynch::AtomicWaker;

static TX_WAKERS: [AtomicWaker; CHANNEL_COUNT] = [const { AtomicWaker::new() }; CHANNEL_COUNT];
static RX_WAKERS: [AtomicWaker; CHANNEL_COUNT] = [const { AtomicWaker::new() }; CHANNEL_COUNT];
Expand Down
10 changes: 5 additions & 5 deletions esp-hal/src/dma/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1654,7 +1654,7 @@ pub trait Rx: crate::private::Sealed {

fn clear_interrupts(&self);

fn waker(&self) -> &'static embassy_sync::waitqueue::AtomicWaker;
fn waker(&self) -> &'static crate::asynch::AtomicWaker;
}

// DMA receive channel
Expand Down Expand Up @@ -1898,7 +1898,7 @@ where
self.rx_impl.clear_all();
}

fn waker(&self) -> &'static embassy_sync::waitqueue::AtomicWaker {
fn waker(&self) -> &'static crate::asynch::AtomicWaker {
self.rx_impl.waker()
}
}
Expand Down Expand Up @@ -1947,7 +1947,7 @@ pub trait Tx: crate::private::Sealed {

fn clear_interrupts(&self);

fn waker(&self) -> &'static embassy_sync::waitqueue::AtomicWaker;
fn waker(&self) -> &'static crate::asynch::AtomicWaker;

fn last_out_dscr_address(&self) -> usize;
}
Expand Down Expand Up @@ -2180,7 +2180,7 @@ where
self.tx_impl.pending_interrupts()
}

fn waker(&self) -> &'static embassy_sync::waitqueue::AtomicWaker {
fn waker(&self) -> &'static crate::asynch::AtomicWaker {
self.tx_impl.waker()
}

Expand Down Expand Up @@ -2276,7 +2276,7 @@ pub trait InterruptAccess<T: EnumSetType>: crate::private::Sealed {
fn is_listening(&self) -> EnumSet<T>;
fn clear(&self, interrupts: impl Into<EnumSet<T>>);
fn pending_interrupts(&self) -> EnumSet<T>;
fn waker(&self) -> &'static embassy_sync::waitqueue::AtomicWaker;
fn waker(&self) -> &'static crate::asynch::AtomicWaker;

fn is_async(&self) -> bool;
fn set_async(&self, is_async: bool);
Expand Down
2 changes: 1 addition & 1 deletion esp-hal/src/dma/pdma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
//! [SPI]: ../spi/index.html
//! [I2S]: ../i2s/index.html
use embassy_sync::waitqueue::AtomicWaker;
use portable_atomic::{AtomicBool, Ordering};

use crate::{
asynch::AtomicWaker,
dma::*,
peripheral::PeripheralRef,
peripherals::Interrupt,
Expand Down
3 changes: 1 addition & 2 deletions esp-hal/src/gpio/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1971,9 +1971,8 @@ fn handle_pin_interrupts(handle: impl Fn(u8)) {
mod asynch {
use core::task::{Context, Poll};

use embassy_sync::waitqueue::AtomicWaker;

use super::*;
use crate::asynch::AtomicWaker;

#[ram]
pub(super) static PIN_WAKERS: [AtomicWaker; NUM_PINS] =
Expand Down
2 changes: 1 addition & 1 deletion esp-hal/src/i2c/master/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ use core::{
};

use embassy_embedded_hal::SetConfig;
use embassy_sync::waitqueue::AtomicWaker;
use embedded_hal::i2c::Operation as EhalOperation;
use fugit::HertzU32;

use crate::{
asynch::AtomicWaker,
clock::Clocks,
gpio::{interconnect::PeripheralOutput, InputSignal, OutputSignal, Pull},
interrupt::InterruptHandler,
Expand Down
3 changes: 1 addition & 2 deletions esp-hal/src/lcd_cam/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ pub mod lcd;

use core::marker::PhantomData;

use embassy_sync::waitqueue::AtomicWaker;

use crate::{
asynch::AtomicWaker,
interrupt::InterruptHandler,
lcd_cam::{cam::Cam, lcd::Lcd},
macros::handler,
Expand Down
38 changes: 38 additions & 0 deletions esp-hal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -570,3 +570,41 @@ pub fn init(config: Config) -> Peripherals {

peripherals
}

/// Asynchronous utilities.
pub mod asynch {
use core::task::Waker;

use crate::sync::Locked;

/// Utility struct to register and wake a waker.
pub struct AtomicWaker {
waker: Locked<Option<Waker>>,
}

impl AtomicWaker {
/// Create a new `AtomicWaker`.
pub const fn new() -> Self {
Self {
waker: Locked::new(None),
}
}

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

/// Wake the registered waker, if any.
pub fn wake(&self) {
self.waker.with(|waker| {
if let Some(w) = waker {
w.wake_by_ref();
}
})
}
}
}
2 changes: 1 addition & 1 deletion esp-hal/src/parl_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1491,11 +1491,11 @@ where
pub mod asynch {
use core::task::Poll;

use embassy_sync::waitqueue::AtomicWaker;
use procmacros::handler;

use super::{private::Instance, Error, ParlIoRx, ParlIoTx, MAX_DMA_SIZE};
use crate::{
asynch::AtomicWaker,
dma::{asynch::DmaRxFuture, ReadBuffer, WriteBuffer},
peripherals::Interrupt,
};
Expand Down
2 changes: 1 addition & 1 deletion esp-hal/src/rmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ use core::{
task::{Context, Poll},
};

use embassy_sync::waitqueue::AtomicWaker;
use enumset::{EnumSet, EnumSetType};
use fugit::HertzU32;

use crate::{
asynch::AtomicWaker,
gpio::interconnect::{PeripheralInput, PeripheralOutput},
macros::handler,
peripheral::Peripheral,
Expand Down
2 changes: 1 addition & 1 deletion esp-hal/src/rsa/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,11 +376,11 @@ where
pub(crate) mod asynch {
use core::task::Poll;

use embassy_sync::waitqueue::AtomicWaker;
use portable_atomic::{AtomicBool, Ordering};
use procmacros::handler;

use crate::{
asynch::AtomicWaker,
rsa::{
Multi,
Rsa,
Expand Down
2 changes: 1 addition & 1 deletion esp-hal/src/timer/systimer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -975,10 +975,10 @@ mod asynch {
task::{Context, Poll},
};

use embassy_sync::waitqueue::AtomicWaker;
use procmacros::handler;

use super::*;
use crate::asynch::AtomicWaker;

const NUM_ALARMS: usize = 3;

Expand Down
2 changes: 1 addition & 1 deletion esp-hal/src/timer/timg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1161,10 +1161,10 @@ mod asynch {
task::{Context, Poll},
};

use embassy_sync::waitqueue::AtomicWaker;
use procmacros::handler;

use super::*;
use crate::asynch::AtomicWaker;

cfg_if::cfg_if! {
if #[cfg(all(timg1, timg_timer1))] {
Expand Down
4 changes: 1 addition & 3 deletions esp-hal/src/touch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,10 +527,8 @@ mod asynch {
task::{Context, Poll},
};

use embassy_sync::waitqueue::AtomicWaker;

use super::*;
use crate::{macros::ram, prelude::handler, Async};
use crate::{asynch::AtomicWaker, macros::ram, prelude::handler, Async};

const NUM_TOUCH_PINS: usize = 10;

Expand Down
2 changes: 1 addition & 1 deletion esp-hal/src/uart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,11 @@
use core::{marker::PhantomData, sync::atomic::Ordering, task::Poll};

use embassy_embedded_hal::SetConfig;
use embassy_sync::waitqueue::AtomicWaker;
use enumset::{EnumSet, EnumSetType};
use portable_atomic::AtomicBool;

use crate::{
asynch::AtomicWaker,
clock::Clocks,
gpio::{
interconnect::{PeripheralInput, PeripheralOutput},
Expand Down
2 changes: 1 addition & 1 deletion esp-hal/src/usb_serial_jtag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@
use core::{convert::Infallible, marker::PhantomData, task::Poll};

use embassy_sync::waitqueue::AtomicWaker;
use procmacros::handler;

use crate::{
asynch::AtomicWaker,
peripheral::{Peripheral, PeripheralRef},
peripherals::{usb_device::RegisterBlock, Interrupt, USB_DEVICE},
system::PeripheralClockControl,
Expand Down
2 changes: 1 addition & 1 deletion esp-wifi/src/ble/controller/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ pub(crate) mod asynch {
HostToControllerPacket,
WriteHci,
};
use embassy_sync::waitqueue::AtomicWaker;
use esp_hal::asynch::AtomicWaker;

use super::*;
use crate::ble::have_hci_read_data;
Expand Down
2 changes: 1 addition & 1 deletion esp-wifi/src/esp_now/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -884,7 +884,7 @@ pub use asynch::SendFuture;
mod asynch {
use core::task::{Context, Poll};

use embassy_sync::waitqueue::AtomicWaker;
use esp_hal::asynch::AtomicWaker;

use super::*;

Expand Down
4 changes: 2 additions & 2 deletions esp-wifi/src/wifi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3122,7 +3122,7 @@ macro_rules! esp_wifi_result {

pub(crate) mod embassy {
use embassy_net_driver::{Capabilities, Driver, HardwareAddress, RxToken, TxToken};
use embassy_sync::waitqueue::AtomicWaker;
use esp_hal::asynch::AtomicWaker;

use super::*;

Expand Down Expand Up @@ -3209,7 +3209,7 @@ pub(crate) fn apply_power_saving(ps: PowerSaveMode) -> Result<(), WifiError> {
mod asynch {
use core::task::Poll;

use embassy_sync::waitqueue::AtomicWaker;
use esp_hal::asynch::AtomicWaker;

use super::*;

Expand Down

0 comments on commit f83bd19

Please sign in to comment.