Skip to content

AtomicWaker: add take method #4152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
4 changes: 1 addition & 3 deletions embassy-executor/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,7 @@ fn waking_after_completion_does_not_poll() {
let (executor, trace) = setup();
executor.spawner().spawn(task1(trace.clone(), waker)).unwrap();

unsafe { executor.poll() };
waker.wake();
// Task registers waker, then exits
unsafe { executor.poll() };

// Exited task may be waken but is not polled
Expand All @@ -176,7 +175,6 @@ fn waking_after_completion_does_not_poll() {
"pend", // spawning a task pends the executor
"poll task1", //
"pend", // manual wake, gets cleared by poll
"pend", // manual wake, single pend for two wakes
"pend", // respawning a task pends the executor
"poll task1", //
]
Expand Down
3 changes: 3 additions & 0 deletions embassy-sync/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

- AtomicWaker now drops the Waker after waking it.
- Added `AtomicWaker::take`.

## 0.6.2 - 2025-01-15

- Add dynamic dispatch variant of `Pipe`.
Expand Down
19 changes: 13 additions & 6 deletions embassy-sync/src/waitqueue/atomic_waker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,16 @@ impl<M: RawMutex> GenericAtomicWaker<M> {
})
}

/// Extracts the previously registered waker, leaving `None` in its place.
pub fn take(&self) -> Option<Waker> {
self.waker.lock(|cell| cell.take())
}

/// 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));
}
})
if let Some(waker) = self.take() {
waker.wake();
}
}
}

Expand All @@ -59,6 +61,11 @@ impl AtomicWaker {
self.waker.register(w);
}

/// Extracts the previously registered waker, leaving `None` in its place.
pub fn take(&self) -> Option<Waker> {
self.waker.take()
}

/// Wake the registered waker, if any.
pub fn wake(&self) {
self.waker.wake();
Expand Down