Reverse generics order, remove spin_get

This commit is contained in:
Peter Krull 2024-09-24 12:37:32 +02:00
parent 999807f226
commit 5e1912a2d3

View File

@ -66,10 +66,10 @@ use crate::waitqueue::MultiWakerRegistration;
/// block_on(f); /// block_on(f);
/// ``` /// ```
pub struct Watch<M: RawMutex, T: Clone, const N: usize> { pub struct Watch<M: RawMutex, T: Clone, const N: usize> {
mutex: Mutex<M, RefCell<WatchState<N, T>>>, mutex: Mutex<M, RefCell<WatchState<T, N>>>,
} }
struct WatchState<const N: usize, T: Clone> { struct WatchState<T: Clone, const N: usize> {
data: Option<T>, data: Option<T>,
current_id: u64, current_id: u64,
wakers: MultiWakerRegistration<N>, wakers: MultiWakerRegistration<N>,
@ -365,30 +365,6 @@ impl<M: RawMutex, T: Clone, const N: usize> Watch<M, T, N> {
self.mutex.lock(|state| state.borrow().current_id) self.mutex.lock(|state| state.borrow().current_id)
} }
/// Waits for the `Watch` to be initialized with a value using a busy-wait mechanism.
///
/// This is useful for initialization code where receivers may only be interested in
/// awaiting the value once in the lifetime of the program. It is therefore a temporaryily
/// CPU-inefficient operation, while being more memory efficient than using a `Receiver`.
///
/// **Note** Be careful about using this within an InterruptExecutor, as it will starve
/// tasks in lower-priority executors.
pub async fn spin_get(&self) -> T {
poll_fn(|cx| {
self.mutex.lock(|state| {
let s = state.borrow();
match &s.data {
Some(data) => Poll::Ready(data.clone()),
None => {
cx.waker().wake_by_ref();
Poll::Pending
}
}
})
})
.await
}
/// Tries to get the value of the `Watch`. /// Tries to get the value of the `Watch`.
pub fn try_get(&self) -> Option<T> { pub fn try_get(&self) -> Option<T> {
WatchBehavior::try_get(self, None) WatchBehavior::try_get(self, None)