Skip to content

Commit

Permalink
experiment: turbo-wakers.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dirbaio committed Mar 6, 2023
1 parent 18fe398 commit 7490021
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 44 deletions.
2 changes: 2 additions & 0 deletions embassy-executor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ wasm = ["dep:wasm-bindgen", "dep:js-sys"]
# Enable nightly-only features
nightly = []

turbowakers = []

integrated-timers = ["dep:embassy-time"]

# Trace interrupt invocations with rtos-trace.
Expand Down
1 change: 1 addition & 0 deletions embassy-executor/src/raw/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mod run_queue;
#[cfg(feature = "integrated-timers")]
mod timer_queue;
pub(crate) mod util;
#[cfg_attr(feature = "turbowakers", path = "waker_turbo.rs")]
mod waker;

use core::cell::Cell;
Expand Down
35 changes: 35 additions & 0 deletions embassy-executor/src/raw/waker_turbo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use core::mem;
use core::ptr::NonNull;
use core::task::{RawWaker, RawWakerVTable, Waker};

use super::{wake_task, TaskHeader, TaskRef};

pub(crate) unsafe fn from_task(p: TaskRef) -> Waker {
Waker::from_ptr(NonNull::new_unchecked(p.as_ptr() as _))
}

/// Get a task pointer from a waker.
///
/// This can be used as an optimization in wait queues to store task pointers
/// (1 word) instead of full Wakers (2 words). This saves a bit of RAM and helps
/// avoid dynamic dispatch.
///
/// You can use the returned task pointer to wake the task with [`wake_task`](super::wake_task).
///
/// # Panics
///
/// Panics if the waker is not created by the Embassy executor.
pub fn task_from_waker(waker: &Waker) -> TaskRef {
let ptr = waker.as_ptr().as_ptr();

// safety: our wakers are always created with `TaskRef::as_ptr`
unsafe { TaskRef::from_ptr(ptr as *const TaskHeader) }
}

#[inline(never)]
#[no_mangle]
fn _wake(ptr: NonNull<()>) {
// safety: our wakers are always created with `TaskRef::as_ptr`
let task = unsafe { TaskRef::from_ptr(ptr.as_ptr() as *const TaskHeader) };
wake_task(task)
}
1 change: 1 addition & 0 deletions embassy-sync/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ features = ["nightly"]
[features]
nightly = ["embedded-io/async"]
std = []
turbowakers = []

[dependencies]
defmt = { version = "0.3", optional = true }
Expand Down
41 changes: 41 additions & 0 deletions embassy-sync/src/waitqueue/atomic_waker.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use core::cell::Cell;
use core::task::Waker;

use crate::blocking_mutex::raw::CriticalSectionRawMutex;
use crate::blocking_mutex::Mutex;

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

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

/// Register a waker. Overwrites the previous waker, if any.
pub fn register(&self, w: &Waker) {
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),
_ => Some(w.clone()),
})
})
}

/// Wake the registered waker, if any.
pub fn wake(&self) {
critical_section::with(|cs| {
let cell = self.waker.borrow(cs);
if let Some(w) = cell.replace(None) {
w.wake_by_ref();
cell.set(Some(w));
}
})
}
}
34 changes: 34 additions & 0 deletions embassy-sync/src/waitqueue/atomic_waker_turbo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use core::cell::Cell;
use core::ptr::NonNull;
use core::sync::atomic::{AtomicPtr, Ordering};
use core::task::Waker;
use core::{mem, ptr};

use crate::blocking_mutex::raw::CriticalSectionRawMutex;
use crate::blocking_mutex::Mutex;

/// Utility struct to register and wake a waker.
pub struct AtomicWaker {
waker: AtomicPtr<()>,
}

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

/// Register a waker. Overwrites the previous waker, if any.
pub fn register(&self, w: &Waker) {
self.waker.store(w.as_ptr().as_ptr() as _, Ordering::Release);
}

/// Wake the registered waker, if any.
pub fn wake(&self) {
if let Some(ptr) = NonNull::new(self.waker.load(Ordering::Acquire)) {
unsafe { Waker::from_ptr(ptr) }.wake();
}
}
}
8 changes: 6 additions & 2 deletions embassy-sync/src/waitqueue/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
//! Async low-level wait queues

mod waker;
pub use waker::*;
#[cfg_attr(feature = "turbowakers", path = "atomic_waker_turbo.rs")]
mod atomic_waker;
pub use atomic_waker::*;

mod waker_registration;
pub use waker_registration::*;

mod multi_waker;
pub use multi_waker::*;
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
use core::cell::Cell;
use core::mem;
use core::task::Waker;

use crate::blocking_mutex::raw::CriticalSectionRawMutex;
use crate::blocking_mutex::Mutex;

/// Utility struct to register and wake a waker.
#[derive(Debug, Default)]
pub struct WakerRegistration {
Expand Down Expand Up @@ -54,39 +50,3 @@ impl WakerRegistration {
self.waker.is_some()
}
}

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

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

/// Register a waker. Overwrites the previous waker, if any.
pub fn register(&self, w: &Waker) {
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),
_ => Some(w.clone()),
})
})
}

/// Wake the registered waker, if any.
pub fn wake(&self) {
critical_section::with(|cs| {
let cell = self.waker.borrow(cs);
if let Some(w) = cell.replace(None) {
w.wake_by_ref();
cell.set(Some(w));
}
})
}
}
4 changes: 4 additions & 0 deletions examples/nrf52840/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
[unstable]
build-std = ["core"]
build-std-features = ["panic_immediate_abort", "core/turbowakers"]

[target.'cfg(all(target_arch = "arm", target_os = "none"))']
# replace nRF82840_xxAA with your chip as listed in `probe-run --list-chips`
runner = "probe-run --chip nRF52840_xxAA"
Expand Down
4 changes: 2 additions & 2 deletions examples/nrf52840/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ nightly = ["embassy-executor/nightly", "embassy-nrf/nightly", "embassy-net/night

[dependencies]
embassy-futures = { version = "0.1.0", path = "../../embassy-futures" }
embassy-sync = { version = "0.1.0", path = "../../embassy-sync", features = ["defmt"] }
embassy-executor = { version = "0.1.0", path = "../../embassy-executor", features = ["defmt", "integrated-timers"] }
embassy-sync = { version = "0.1.0", path = "../../embassy-sync", features = ["defmt", "turbowakers"] }
embassy-executor = { version = "0.1.0", path = "../../embassy-executor", features = ["defmt", "integrated-timers", "turbowakers"] }
embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime"] }
embassy-nrf = { version = "0.1.0", path = "../../embassy-nrf", features = ["defmt", "nrf52840", "time-driver-rtc1", "gpiote", "unstable-pac", "time"] }
embassy-net = { version = "0.1.0", path = "../../embassy-net", features = ["defmt", "tcp", "dhcpv4", "medium-ethernet"], optional = true }
Expand Down

0 comments on commit 7490021

Please sign in to comment.