diff options
| author | Dario Nieuwenhuis <[email protected]> | 2022-07-29 21:58:35 +0200 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2022-07-29 23:40:36 +0200 |
| commit | a0f1b0ee01d461607660d2d56b5b1bdc57e0d3fb (patch) | |
| tree | e60fc8f8db8ec07e55d655c1a830b07f4db0b7d2 /embassy-util/src/waitqueue/waker.rs | |
| parent | 8745d646f0976791b7098456aa61adb983fb1c18 (diff) | |
Split embassy crate into embassy-executor, embassy-util.
Diffstat (limited to 'embassy-util/src/waitqueue/waker.rs')
| -rw-r--r-- | embassy-util/src/waitqueue/waker.rs | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/embassy-util/src/waitqueue/waker.rs b/embassy-util/src/waitqueue/waker.rs new file mode 100644 index 000000000..64e300eb8 --- /dev/null +++ b/embassy-util/src/waitqueue/waker.rs | |||
| @@ -0,0 +1,92 @@ | |||
| 1 | use core::cell::Cell; | ||
| 2 | use core::mem; | ||
| 3 | use core::task::Waker; | ||
| 4 | |||
| 5 | use crate::blocking_mutex::raw::CriticalSectionRawMutex; | ||
| 6 | use crate::blocking_mutex::Mutex; | ||
| 7 | |||
| 8 | /// Utility struct to register and wake a waker. | ||
| 9 | #[derive(Debug)] | ||
| 10 | pub struct WakerRegistration { | ||
| 11 | waker: Option<Waker>, | ||
| 12 | } | ||
| 13 | |||
| 14 | impl WakerRegistration { | ||
| 15 | /// Create a new `WakerRegistration`. | ||
| 16 | pub const fn new() -> Self { | ||
| 17 | Self { waker: None } | ||
| 18 | } | ||
| 19 | |||
| 20 | /// Register a waker. Overwrites the previous waker, if any. | ||
| 21 | pub fn register(&mut self, w: &Waker) { | ||
| 22 | match self.waker { | ||
| 23 | // Optimization: If both the old and new Wakers wake the same task, we can simply | ||
| 24 | // keep the old waker, skipping the clone. (In most executor implementations, | ||
| 25 | // cloning a waker is somewhat expensive, comparable to cloning an Arc). | ||
| 26 | Some(ref w2) if (w2.will_wake(w)) => {} | ||
| 27 | _ => { | ||
| 28 | // clone the new waker and store it | ||
| 29 | if let Some(old_waker) = mem::replace(&mut self.waker, Some(w.clone())) { | ||
| 30 | // We had a waker registered for another task. Wake it, so the other task can | ||
| 31 | // reregister itself if it's still interested. | ||
| 32 | // | ||
| 33 | // If two tasks are waiting on the same thing concurrently, this will cause them | ||
| 34 | // to wake each other in a loop fighting over this WakerRegistration. This wastes | ||
| 35 | // CPU but things will still work. | ||
| 36 | // | ||
| 37 | // If the user wants to have two tasks waiting on the same thing they should use | ||
| 38 | // a more appropriate primitive that can store multiple wakers. | ||
| 39 | old_waker.wake() | ||
| 40 | } | ||
| 41 | } | ||
| 42 | } | ||
| 43 | } | ||
| 44 | |||
| 45 | /// Wake the registered waker, if any. | ||
| 46 | pub fn wake(&mut self) { | ||
| 47 | if let Some(w) = self.waker.take() { | ||
| 48 | w.wake() | ||
| 49 | } | ||
| 50 | } | ||
| 51 | |||
| 52 | /// Returns true if a waker is currently registered | ||
| 53 | pub fn occupied(&self) -> bool { | ||
| 54 | self.waker.is_some() | ||
| 55 | } | ||
| 56 | } | ||
| 57 | |||
| 58 | /// Utility struct to register and wake a waker. | ||
| 59 | pub struct AtomicWaker { | ||
| 60 | waker: Mutex<CriticalSectionRawMutex, Cell<Option<Waker>>>, | ||
| 61 | } | ||
| 62 | |||
| 63 | impl AtomicWaker { | ||
| 64 | /// Create a new `AtomicWaker`. | ||
| 65 | pub const fn new() -> Self { | ||
| 66 | Self { | ||
| 67 | waker: Mutex::const_new(CriticalSectionRawMutex::new(), Cell::new(None)), | ||
| 68 | } | ||
| 69 | } | ||
| 70 | |||
| 71 | /// Register a waker. Overwrites the previous waker, if any. | ||
| 72 | pub fn register(&self, w: &Waker) { | ||
| 73 | critical_section::with(|cs| { | ||
| 74 | let cell = self.waker.borrow(cs); | ||
| 75 | cell.set(match cell.replace(None) { | ||
| 76 | Some(w2) if (w2.will_wake(w)) => Some(w2), | ||
| 77 | _ => Some(w.clone()), | ||
| 78 | }) | ||
| 79 | }) | ||
| 80 | } | ||
| 81 | |||
| 82 | /// Wake the registered waker, if any. | ||
| 83 | pub fn wake(&self) { | ||
| 84 | critical_section::with(|cs| { | ||
| 85 | let cell = self.waker.borrow(cs); | ||
| 86 | if let Some(w) = cell.replace(None) { | ||
| 87 | w.wake_by_ref(); | ||
| 88 | cell.set(Some(w)); | ||
| 89 | } | ||
| 90 | }) | ||
| 91 | } | ||
| 92 | } | ||
