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/multi_waker.rs | |
| parent | 8745d646f0976791b7098456aa61adb983fb1c18 (diff) | |
Split embassy crate into embassy-executor, embassy-util.
Diffstat (limited to 'embassy-util/src/waitqueue/multi_waker.rs')
| -rw-r--r-- | embassy-util/src/waitqueue/multi_waker.rs | 33 |
1 files changed, 33 insertions, 0 deletions
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 | } | ||
