diff options
| author | Dario Nieuwenhuis <[email protected]> | 2023-03-30 17:55:55 +0200 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2023-03-30 17:55:55 +0200 |
| commit | 80972f1e0e6b6d409cc4d86202608c22e5ee3e5a (patch) | |
| tree | 3465f890980bdcb8b296cadb8d0aa0fc4c0cace4 /embassy-sync/src/waitqueue/waker_registration.rs | |
| parent | 754bb802ba377c19be97d092c4b2afe542de20b5 (diff) | |
executor,sync: add support for turbo-wakers.
This is a `core` patch to make wakers 1 word (the task pointer) instead of 2 (task pointer + vtable). It allows having the "waker optimization" we had a while back on `WakerRegistration/AtomicWaker`, but EVERYWHERE, without patching all crates.
Advantages:
- Less memory usage.
- Faster.
- `AtomicWaker` can actually use atomics to load/store the waker, No critical section needed.
- No `dyn` call, which means `cargo-call-stack` can now see through wakes.
Disadvantages:
- You have to patch `core`...
- Breaks all executors and other things that create wakers, unless they opt in to using the new `from_ptr` API.
How to use:
- Run this shell script to patch `core`. https://gist.github.com/Dirbaio/c67da7cf318515181539122c9d32b395
- Enable `build-std`
- Enable `build-std-features = core/turbowakers`
- Enable feature `turbowakers` in `embassy-executor`, `embassy-sync`.
- Make sure you have no other crate creating wakers other than `embassy-executor`. These will panic at runtime.
Note that the patched `core` is equivalent to the unpached one when the `turbowakers` feature is not enabled, so it should be fine to leave it there.
Diffstat (limited to 'embassy-sync/src/waitqueue/waker_registration.rs')
| -rw-r--r-- | embassy-sync/src/waitqueue/waker_registration.rs | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/embassy-sync/src/waitqueue/waker_registration.rs b/embassy-sync/src/waitqueue/waker_registration.rs new file mode 100644 index 000000000..9b666e7c4 --- /dev/null +++ b/embassy-sync/src/waitqueue/waker_registration.rs | |||
| @@ -0,0 +1,52 @@ | |||
| 1 | use core::mem; | ||
| 2 | use core::task::Waker; | ||
| 3 | |||
| 4 | /// Utility struct to register and wake a waker. | ||
| 5 | #[derive(Debug, Default)] | ||
| 6 | pub struct WakerRegistration { | ||
| 7 | waker: Option<Waker>, | ||
| 8 | } | ||
| 9 | |||
| 10 | impl WakerRegistration { | ||
| 11 | /// Create a new `WakerRegistration`. | ||
| 12 | pub const fn new() -> Self { | ||
| 13 | Self { waker: None } | ||
| 14 | } | ||
| 15 | |||
| 16 | /// Register a waker. Overwrites the previous waker, if any. | ||
| 17 | pub fn register(&mut self, w: &Waker) { | ||
| 18 | match self.waker { | ||
| 19 | // Optimization: If both the old and new Wakers wake the same task, we can simply | ||
| 20 | // keep the old waker, skipping the clone. (In most executor implementations, | ||
| 21 | // cloning a waker is somewhat expensive, comparable to cloning an Arc). | ||
| 22 | Some(ref w2) if (w2.will_wake(w)) => {} | ||
| 23 | _ => { | ||
| 24 | // clone the new waker and store it | ||
| 25 | if let Some(old_waker) = mem::replace(&mut self.waker, Some(w.clone())) { | ||
| 26 | // We had a waker registered for another task. Wake it, so the other task can | ||
| 27 | // reregister itself if it's still interested. | ||
| 28 | // | ||
| 29 | // If two tasks are waiting on the same thing concurrently, this will cause them | ||
| 30 | // to wake each other in a loop fighting over this WakerRegistration. This wastes | ||
| 31 | // CPU but things will still work. | ||
| 32 | // | ||
| 33 | // If the user wants to have two tasks waiting on the same thing they should use | ||
| 34 | // a more appropriate primitive that can store multiple wakers. | ||
| 35 | old_waker.wake() | ||
| 36 | } | ||
| 37 | } | ||
| 38 | } | ||
| 39 | } | ||
| 40 | |||
| 41 | /// Wake the registered waker, if any. | ||
| 42 | pub fn wake(&mut self) { | ||
| 43 | if let Some(w) = self.waker.take() { | ||
| 44 | w.wake() | ||
| 45 | } | ||
| 46 | } | ||
| 47 | |||
| 48 | /// Returns true if a waker is currently registered | ||
| 49 | pub fn occupied(&self) -> bool { | ||
| 50 | self.waker.is_some() | ||
| 51 | } | ||
| 52 | } | ||
