diff options
| author | Dario Nieuwenhuis <[email protected]> | 2022-08-22 21:46:09 +0200 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2022-08-22 22:18:13 +0200 |
| commit | 21072bee48ff6ec19b79e0d9527ad8cc34a4e9e0 (patch) | |
| tree | b5b8c0f4b3571989b5fd15152be5639f4334c282 /embassy-sync/src/waitqueue/multi_waker.rs | |
| parent | 61356181b223e95f289ca3af3a038a699cde2112 (diff) | |
split `embassy-util` into `embassy-futures`, `embassy-sync`.
Diffstat (limited to 'embassy-sync/src/waitqueue/multi_waker.rs')
| -rw-r--r-- | embassy-sync/src/waitqueue/multi_waker.rs | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/embassy-sync/src/waitqueue/multi_waker.rs b/embassy-sync/src/waitqueue/multi_waker.rs new file mode 100644 index 000000000..325d2cb3a --- /dev/null +++ b/embassy-sync/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 | } | ||
