2022-06-17 13:54:34 +02:00
|
|
|
use core::task::Waker;
|
|
|
|
|
|
|
|
use super::WakerRegistration;
|
|
|
|
|
2022-06-17 15:29:42 +02:00
|
|
|
/// Utility struct to register and wake multiple wakers.
|
2022-06-17 13:54:34 +02:00
|
|
|
pub struct MultiWakerRegistration<const N: usize> {
|
|
|
|
wakers: [WakerRegistration; N],
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<const N: usize> MultiWakerRegistration<N> {
|
2022-06-17 15:29:42 +02:00
|
|
|
/// Create a new empty instance
|
2022-06-17 13:54:34 +02:00
|
|
|
pub const fn new() -> Self {
|
|
|
|
const WAKER: WakerRegistration = WakerRegistration::new();
|
|
|
|
Self { wakers: [WAKER; N] }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Register a waker. If the buffer is full the function returns it in the error
|
|
|
|
pub fn register<'a>(&mut self, w: &'a Waker) -> Result<(), &'a Waker> {
|
|
|
|
if let Some(waker_slot) = self.wakers.iter_mut().find(|waker_slot| !waker_slot.occupied()) {
|
|
|
|
waker_slot.register(w);
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(w)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Wake all registered wakers. This clears the buffer
|
|
|
|
pub fn wake(&mut self) {
|
|
|
|
for waker_slot in self.wakers.iter_mut() {
|
|
|
|
waker_slot.wake()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|