diff options
| author | Dario Nieuwenhuis <[email protected]> | 2023-05-26 13:07:32 +0200 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2023-05-26 13:17:39 +0200 |
| commit | 3081ecf301a54f8ed3d0f72350dd21f8ac9e1b18 (patch) | |
| tree | 9b0e2fd56ba0affcd82b06e5141967000e73566e /embassy-sync/src/waitqueue | |
| parent | 31b364b9b0a18a9ebb341747861441b11f621ea0 (diff) | |
sync: do will_wake check in MultiWakerRegistration.
Diffstat (limited to 'embassy-sync/src/waitqueue')
| -rw-r--r-- | embassy-sync/src/waitqueue/multi_waker.rs | 49 |
1 files changed, 37 insertions, 12 deletions
diff --git a/embassy-sync/src/waitqueue/multi_waker.rs b/embassy-sync/src/waitqueue/multi_waker.rs index 325d2cb3a..824d192da 100644 --- a/embassy-sync/src/waitqueue/multi_waker.rs +++ b/embassy-sync/src/waitqueue/multi_waker.rs | |||
| @@ -1,33 +1,58 @@ | |||
| 1 | use core::task::Waker; | 1 | use core::task::Waker; |
| 2 | 2 | ||
| 3 | use super::WakerRegistration; | 3 | use heapless::Vec; |
| 4 | 4 | ||
| 5 | /// Utility struct to register and wake multiple wakers. | 5 | /// Utility struct to register and wake multiple wakers. |
| 6 | pub struct MultiWakerRegistration<const N: usize> { | 6 | pub struct MultiWakerRegistration<const N: usize> { |
| 7 | wakers: [WakerRegistration; N], | 7 | wakers: Vec<Waker, N>, |
| 8 | } | 8 | } |
| 9 | 9 | ||
| 10 | impl<const N: usize> MultiWakerRegistration<N> { | 10 | impl<const N: usize> MultiWakerRegistration<N> { |
| 11 | /// Create a new empty instance | 11 | /// Create a new empty instance |
| 12 | pub const fn new() -> Self { | 12 | pub const fn new() -> Self { |
| 13 | const WAKER: WakerRegistration = WakerRegistration::new(); | 13 | Self { wakers: Vec::new() } |
| 14 | Self { wakers: [WAKER; N] } | ||
| 15 | } | 14 | } |
| 16 | 15 | ||
| 17 | /// Register a waker. If the buffer is full the function returns it in the error | 16 | /// 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> { | 17 | pub fn register<'a>(&mut self, w: &'a Waker) { |
| 19 | if let Some(waker_slot) = self.wakers.iter_mut().find(|waker_slot| !waker_slot.occupied()) { | 18 | // If we already have some waker that wakes the same task as `w`, do nothing. |
| 20 | waker_slot.register(w); | 19 | // This avoids cloning wakers, and avoids unnecessary mass-wakes. |
| 21 | Ok(()) | 20 | for w2 in &self.wakers { |
| 22 | } else { | 21 | if w.will_wake(w2) { |
| 23 | Err(w) | 22 | return; |
| 23 | } | ||
| 24 | } | ||
| 25 | |||
| 26 | if self.wakers.is_full() { | ||
| 27 | // All waker slots were full. It's a bit inefficient, but we can wake everything. | ||
| 28 | // Any future that is still active will simply reregister. | ||
| 29 | // This won't happen a lot, so it's ok. | ||
| 30 | self.wake(); | ||
| 31 | } | ||
| 32 | |||
| 33 | if self.wakers.push(w.clone()).is_err() { | ||
| 34 | // This can't happen unless N=0 | ||
| 35 | // (Either `wakers` wasn't full, or it was in which case `wake()` empied it) | ||
| 36 | panic!("tried to push a waker to a zero-length MultiWakerRegistration") | ||
| 24 | } | 37 | } |
| 25 | } | 38 | } |
| 26 | 39 | ||
| 27 | /// Wake all registered wakers. This clears the buffer | 40 | /// Wake all registered wakers. This clears the buffer |
| 28 | pub fn wake(&mut self) { | 41 | pub fn wake(&mut self) { |
| 29 | for waker_slot in self.wakers.iter_mut() { | 42 | // heapless::Vec has no `drain()`, do it unsafely ourselves... |
| 30 | waker_slot.wake() | 43 | |
| 44 | // First set length to 0, without dropping the contents. | ||
| 45 | // This is necessary for soundness: if wake() panics and we're using panic=unwind. | ||
| 46 | // Setting len=0 upfront ensures other code can't observe the vec in an inconsistent state. | ||
| 47 | // (it'll leak wakers, but that's not UB) | ||
| 48 | let len = self.wakers.len(); | ||
| 49 | unsafe { self.wakers.set_len(0) } | ||
| 50 | |||
| 51 | for i in 0..len { | ||
| 52 | // Move a waker out of the vec. | ||
| 53 | let waker = unsafe { self.wakers.as_mut_ptr().add(i).read() }; | ||
| 54 | // Wake it by value, which consumes (drops) it. | ||
| 55 | waker.wake(); | ||
| 31 | } | 56 | } |
| 32 | } | 57 | } |
| 33 | } | 58 | } |
