aboutsummaryrefslogtreecommitdiff
path: root/embassy-sync/src/waitqueue/multi_waker.rs
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2022-08-22 20:18:40 +0000
committerGitHub <[email protected]>2022-08-22 20:18:40 +0000
commitcb9f0ef5b800ce4a22cde1805e0eb88425f1e07b (patch)
tree0c7425dae57acb94cb6ddca27def7e77609369b3 /embassy-sync/src/waitqueue/multi_waker.rs
parent61356181b223e95f289ca3af3a038a699cde2112 (diff)
parent5677b13a86beca58aa57ecfd7cea0db7ceb189fa (diff)
Merge #922
922: split `embassy-util` into `embassy-futures`, `embassy-sync`. r=Dirbaio a=Dirbaio Co-authored-by: Dario Nieuwenhuis <[email protected]>
Diffstat (limited to 'embassy-sync/src/waitqueue/multi_waker.rs')
-rw-r--r--embassy-sync/src/waitqueue/multi_waker.rs33
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 @@
1use core::task::Waker;
2
3use super::WakerRegistration;
4
5/// Utility struct to register and wake multiple wakers.
6pub struct MultiWakerRegistration<const N: usize> {
7 wakers: [WakerRegistration; N],
8}
9
10impl<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}