aboutsummaryrefslogtreecommitdiff
path: root/embassy-sync/src
diff options
context:
space:
mode:
authorPeter Krull <[email protected]>2024-09-24 12:37:32 +0200
committerPeter Krull <[email protected]>2024-09-24 12:37:32 +0200
commit5e1912a2d3adea920039dae3622643f34289290b (patch)
treebcd288f159128da415d32d27329a2877678faa50 /embassy-sync/src
parent999807f226623669a9cfc8ca218d3c81f0c04a77 (diff)
Reverse generics order, remove spin_get
Diffstat (limited to 'embassy-sync/src')
-rw-r--r--embassy-sync/src/watch.rs28
1 files changed, 2 insertions, 26 deletions
diff --git a/embassy-sync/src/watch.rs b/embassy-sync/src/watch.rs
index 4b7ffa5fc..336e64ba9 100644
--- a/embassy-sync/src/watch.rs
+++ b/embassy-sync/src/watch.rs
@@ -66,10 +66,10 @@ use crate::waitqueue::MultiWakerRegistration;
66/// block_on(f); 66/// block_on(f);
67/// ``` 67/// ```
68pub struct Watch<M: RawMutex, T: Clone, const N: usize> { 68pub struct Watch<M: RawMutex, T: Clone, const N: usize> {
69 mutex: Mutex<M, RefCell<WatchState<N, T>>>, 69 mutex: Mutex<M, RefCell<WatchState<T, N>>>,
70} 70}
71 71
72struct WatchState<const N: usize, T: Clone> { 72struct WatchState<T: Clone, const N: usize> {
73 data: Option<T>, 73 data: Option<T>,
74 current_id: u64, 74 current_id: u64,
75 wakers: MultiWakerRegistration<N>, 75 wakers: MultiWakerRegistration<N>,
@@ -365,30 +365,6 @@ impl<M: RawMutex, T: Clone, const N: usize> Watch<M, T, N> {
365 self.mutex.lock(|state| state.borrow().current_id) 365 self.mutex.lock(|state| state.borrow().current_id)
366 } 366 }
367 367
368 /// Waits for the `Watch` to be initialized with a value using a busy-wait mechanism.
369 ///
370 /// This is useful for initialization code where receivers may only be interested in
371 /// awaiting the value once in the lifetime of the program. It is therefore a temporaryily
372 /// CPU-inefficient operation, while being more memory efficient than using a `Receiver`.
373 ///
374 /// **Note** Be careful about using this within an InterruptExecutor, as it will starve
375 /// tasks in lower-priority executors.
376 pub async fn spin_get(&self) -> T {
377 poll_fn(|cx| {
378 self.mutex.lock(|state| {
379 let s = state.borrow();
380 match &s.data {
381 Some(data) => Poll::Ready(data.clone()),
382 None => {
383 cx.waker().wake_by_ref();
384 Poll::Pending
385 }
386 }
387 })
388 })
389 .await
390 }
391
392 /// Tries to get the value of the `Watch`. 368 /// Tries to get the value of the `Watch`.
393 pub fn try_get(&self) -> Option<T> { 369 pub fn try_get(&self) -> Option<T> {
394 WatchBehavior::try_get(self, None) 370 WatchBehavior::try_get(self, None)