diff options
Diffstat (limited to 'embassy-util/src/waitqueue')
| -rw-r--r-- | embassy-util/src/waitqueue/mod.rs | 7 | ||||
| -rw-r--r-- | embassy-util/src/waitqueue/multi_waker.rs | 33 | ||||
| -rw-r--r-- | embassy-util/src/waitqueue/waker.rs | 92 |
3 files changed, 132 insertions, 0 deletions
diff --git a/embassy-util/src/waitqueue/mod.rs b/embassy-util/src/waitqueue/mod.rs new file mode 100644 index 000000000..6661a6b61 --- /dev/null +++ b/embassy-util/src/waitqueue/mod.rs | |||
| @@ -0,0 +1,7 @@ | |||
| 1 | //! Async low-level wait queues | ||
| 2 | |||
| 3 | mod waker; | ||
| 4 | pub use waker::*; | ||
| 5 | |||
| 6 | mod multi_waker; | ||
| 7 | pub use multi_waker::*; | ||
diff --git a/embassy-util/src/waitqueue/multi_waker.rs b/embassy-util/src/waitqueue/multi_waker.rs new file mode 100644 index 000000000..325d2cb3a --- /dev/null +++ b/embassy-util/src/waitqueue/multi_waker.rs | |||
| @@ -0,0 +1,33 @@ | |||
| 1 | use core::task::Waker; | ||
| 2 | |||
| 3 | use super::WakerRegistration; | ||
| 4 | |||
| 5 | /// Utility struct to register and wake multiple wakers. | ||
| 6 | pub struct MultiWakerRegistration<const N: usize> { | ||
| 7 | wakers: [WakerRegistration; N], | ||
| 8 | } | ||
| 9 | |||
| 10 | impl<const N: usize> MultiWakerRegistration<N> { | ||
| 11 | /// Create a new empty instance | ||
| 12 | pub const fn new() -> Self { | ||
| 13 | const WAKER: WakerRegistration = WakerRegistration::new(); | ||
| 14 | Self { wakers: [WAKER; N] } | ||
| 15 | } | ||
| 16 | |||
| 17 | /// Register a waker. If the buffer is full the function returns it in the error | ||
| 18 | pub fn register<'a>(&mut self, w: &'a Waker) -> Result<(), &'a Waker> { | ||
| 19 | if let Some(waker_slot) = self.wakers.iter_mut().find(|waker_slot| !waker_slot.occupied()) { | ||
| 20 | waker_slot.register(w); | ||
| 21 | Ok(()) | ||
| 22 | } else { | ||
| 23 | Err(w) | ||
| 24 | } | ||
| 25 | } | ||
| 26 | |||
| 27 | /// Wake all registered wakers. This clears the buffer | ||
| 28 | pub fn wake(&mut self) { | ||
| 29 | for waker_slot in self.wakers.iter_mut() { | ||
| 30 | waker_slot.wake() | ||
| 31 | } | ||
| 32 | } | ||
| 33 | } | ||
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 | } | ||
