diff options
Diffstat (limited to 'embassy-sync')
| -rw-r--r-- | embassy-sync/src/mutex.rs | 5 | ||||
| -rw-r--r-- | embassy-sync/src/once_lock.rs | 5 | ||||
| -rw-r--r-- | embassy-sync/src/watch.rs | 6 | ||||
| -rw-r--r-- | embassy-sync/src/zerocopy_channel.rs | 24 |
4 files changed, 20 insertions, 20 deletions
diff --git a/embassy-sync/src/mutex.rs b/embassy-sync/src/mutex.rs index 08f66e374..f25f74336 100644 --- a/embassy-sync/src/mutex.rs +++ b/embassy-sync/src/mutex.rs | |||
| @@ -2,7 +2,7 @@ | |||
| 2 | //! | 2 | //! |
| 3 | //! This module provides a mutex that can be used to synchronize data between asynchronous tasks. | 3 | //! This module provides a mutex that can be used to synchronize data between asynchronous tasks. |
| 4 | use core::cell::{RefCell, UnsafeCell}; | 4 | use core::cell::{RefCell, UnsafeCell}; |
| 5 | use core::future::poll_fn; | 5 | use core::future::{poll_fn, Future}; |
| 6 | use core::ops::{Deref, DerefMut}; | 6 | use core::ops::{Deref, DerefMut}; |
| 7 | use core::task::Poll; | 7 | use core::task::Poll; |
| 8 | use core::{fmt, mem}; | 8 | use core::{fmt, mem}; |
| @@ -73,7 +73,7 @@ where | |||
| 73 | /// Lock the mutex. | 73 | /// Lock the mutex. |
| 74 | /// | 74 | /// |
| 75 | /// This will wait for the mutex to be unlocked if it's already locked. | 75 | /// This will wait for the mutex to be unlocked if it's already locked. |
| 76 | pub async fn lock(&self) -> MutexGuard<'_, M, T> { | 76 | pub fn lock(&self) -> impl Future<Output = MutexGuard<'_, M, T>> { |
| 77 | poll_fn(|cx| { | 77 | poll_fn(|cx| { |
| 78 | let ready = self.state.lock(|s| { | 78 | let ready = self.state.lock(|s| { |
| 79 | let mut s = s.borrow_mut(); | 79 | let mut s = s.borrow_mut(); |
| @@ -92,7 +92,6 @@ where | |||
| 92 | Poll::Pending | 92 | Poll::Pending |
| 93 | } | 93 | } |
| 94 | }) | 94 | }) |
| 95 | .await | ||
| 96 | } | 95 | } |
| 97 | 96 | ||
| 98 | /// Attempt to immediately lock the mutex. | 97 | /// Attempt to immediately lock the mutex. |
diff --git a/embassy-sync/src/once_lock.rs b/embassy-sync/src/once_lock.rs index 55608ba32..cd05b986d 100644 --- a/embassy-sync/src/once_lock.rs +++ b/embassy-sync/src/once_lock.rs | |||
| @@ -1,7 +1,7 @@ | |||
| 1 | //! Synchronization primitive for initializing a value once, allowing others to await a reference to the value. | 1 | //! Synchronization primitive for initializing a value once, allowing others to await a reference to the value. |
| 2 | 2 | ||
| 3 | use core::cell::Cell; | 3 | use core::cell::Cell; |
| 4 | use core::future::poll_fn; | 4 | use core::future::{poll_fn, Future}; |
| 5 | use core::mem::MaybeUninit; | 5 | use core::mem::MaybeUninit; |
| 6 | use core::sync::atomic::{AtomicBool, Ordering}; | 6 | use core::sync::atomic::{AtomicBool, Ordering}; |
| 7 | use core::task::Poll; | 7 | use core::task::Poll; |
| @@ -55,7 +55,7 @@ impl<T> OnceLock<T> { | |||
| 55 | 55 | ||
| 56 | /// Get a reference to the underlying value, waiting for it to be set. | 56 | /// Get a reference to the underlying value, waiting for it to be set. |
| 57 | /// If the value is already set, this will return immediately. | 57 | /// If the value is already set, this will return immediately. |
| 58 | pub async fn get(&self) -> &T { | 58 | pub fn get(&self) -> impl Future<Output = &T> { |
| 59 | poll_fn(|cx| match self.try_get() { | 59 | poll_fn(|cx| match self.try_get() { |
| 60 | Some(data) => Poll::Ready(data), | 60 | Some(data) => Poll::Ready(data), |
| 61 | None => { | 61 | None => { |
| @@ -63,7 +63,6 @@ impl<T> OnceLock<T> { | |||
| 63 | Poll::Pending | 63 | Poll::Pending |
| 64 | } | 64 | } |
| 65 | }) | 65 | }) |
| 66 | .await | ||
| 67 | } | 66 | } |
| 68 | 67 | ||
| 69 | /// Try to get a reference to the underlying value if it exists. | 68 | /// Try to get a reference to the underlying value if it exists. |
diff --git a/embassy-sync/src/watch.rs b/embassy-sync/src/watch.rs index 404e31714..e76646c0b 100644 --- a/embassy-sync/src/watch.rs +++ b/embassy-sync/src/watch.rs | |||
| @@ -1,7 +1,7 @@ | |||
| 1 | //! A synchronization primitive for passing the latest value to **multiple** receivers. | 1 | //! A synchronization primitive for passing the latest value to **multiple** receivers. |
| 2 | 2 | ||
| 3 | use core::cell::RefCell; | 3 | use core::cell::RefCell; |
| 4 | use core::future::poll_fn; | 4 | use core::future::{poll_fn, Future}; |
| 5 | use core::marker::PhantomData; | 5 | use core::marker::PhantomData; |
| 6 | use core::ops::{Deref, DerefMut}; | 6 | use core::ops::{Deref, DerefMut}; |
| 7 | use core::task::{Context, Poll}; | 7 | use core::task::{Context, Poll}; |
| @@ -547,8 +547,8 @@ impl<'a, T: Clone, W: WatchBehavior<T> + ?Sized> Rcv<'a, T, W> { | |||
| 547 | /// Returns the current value of the `Watch` once it is initialized, marking it as seen. | 547 | /// Returns the current value of the `Watch` once it is initialized, marking it as seen. |
| 548 | /// | 548 | /// |
| 549 | /// **Note**: Futures do nothing unless you `.await` or poll them. | 549 | /// **Note**: Futures do nothing unless you `.await` or poll them. |
| 550 | pub async fn get(&mut self) -> T { | 550 | pub fn get(&mut self) -> impl Future<Output = T> + '_ { |
| 551 | poll_fn(|cx| self.watch.poll_get(&mut self.at_id, cx)).await | 551 | poll_fn(|cx| self.watch.poll_get(&mut self.at_id, cx)) |
| 552 | } | 552 | } |
| 553 | 553 | ||
| 554 | /// Tries to get the current value of the `Watch` without waiting, marking it as seen. | 554 | /// Tries to get the current value of the `Watch` without waiting, marking it as seen. |
diff --git a/embassy-sync/src/zerocopy_channel.rs b/embassy-sync/src/zerocopy_channel.rs index fabb69bf6..56433cd8a 100644 --- a/embassy-sync/src/zerocopy_channel.rs +++ b/embassy-sync/src/zerocopy_channel.rs | |||
| @@ -15,7 +15,7 @@ | |||
| 15 | //! another message will result in an error being returned. | 15 | //! another message will result in an error being returned. |
| 16 | 16 | ||
| 17 | use core::cell::RefCell; | 17 | use core::cell::RefCell; |
| 18 | use core::future::poll_fn; | 18 | use core::future::{poll_fn, Future}; |
| 19 | use core::marker::PhantomData; | 19 | use core::marker::PhantomData; |
| 20 | use core::task::{Context, Poll}; | 20 | use core::task::{Context, Poll}; |
| 21 | 21 | ||
| @@ -131,12 +131,15 @@ impl<'a, M: RawMutex, T> Sender<'a, M, T> { | |||
| 131 | } | 131 | } |
| 132 | 132 | ||
| 133 | /// Asynchronously send a value over the channel. | 133 | /// Asynchronously send a value over the channel. |
| 134 | pub async fn send(&mut self) -> &mut T { | 134 | pub fn send(&mut self) -> impl Future<Output = &mut T> { |
| 135 | let i = poll_fn(|cx| { | 135 | poll_fn(|cx| { |
| 136 | self.channel.state.lock(|s| { | 136 | self.channel.state.lock(|s| { |
| 137 | let s = &mut *s.borrow_mut(); | 137 | let s = &mut *s.borrow_mut(); |
| 138 | match s.push_index() { | 138 | match s.push_index() { |
| 139 | Some(i) => Poll::Ready(i), | 139 | Some(i) => { |
| 140 | let r = unsafe { &mut *self.channel.buf.add(i) }; | ||
| 141 | Poll::Ready(r) | ||
| 142 | } | ||
| 140 | None => { | 143 | None => { |
| 141 | s.receive_waker.register(cx.waker()); | 144 | s.receive_waker.register(cx.waker()); |
| 142 | Poll::Pending | 145 | Poll::Pending |
| @@ -144,8 +147,6 @@ impl<'a, M: RawMutex, T> Sender<'a, M, T> { | |||
| 144 | } | 147 | } |
| 145 | }) | 148 | }) |
| 146 | }) | 149 | }) |
| 147 | .await; | ||
| 148 | unsafe { &mut *self.channel.buf.add(i) } | ||
| 149 | } | 150 | } |
| 150 | 151 | ||
| 151 | /// Notify the channel that the sending of the value has been finalized. | 152 | /// Notify the channel that the sending of the value has been finalized. |
| @@ -213,12 +214,15 @@ impl<'a, M: RawMutex, T> Receiver<'a, M, T> { | |||
| 213 | } | 214 | } |
| 214 | 215 | ||
| 215 | /// Asynchronously receive a value over the channel. | 216 | /// Asynchronously receive a value over the channel. |
| 216 | pub async fn receive(&mut self) -> &mut T { | 217 | pub fn receive(&mut self) -> impl Future<Output = &mut T> { |
| 217 | let i = poll_fn(|cx| { | 218 | poll_fn(|cx| { |
| 218 | self.channel.state.lock(|s| { | 219 | self.channel.state.lock(|s| { |
| 219 | let s = &mut *s.borrow_mut(); | 220 | let s = &mut *s.borrow_mut(); |
| 220 | match s.pop_index() { | 221 | match s.pop_index() { |
| 221 | Some(i) => Poll::Ready(i), | 222 | Some(i) => { |
| 223 | let r = unsafe { &mut *self.channel.buf.add(i) }; | ||
| 224 | Poll::Ready(r) | ||
| 225 | } | ||
| 222 | None => { | 226 | None => { |
| 223 | s.send_waker.register(cx.waker()); | 227 | s.send_waker.register(cx.waker()); |
| 224 | Poll::Pending | 228 | Poll::Pending |
| @@ -226,8 +230,6 @@ impl<'a, M: RawMutex, T> Receiver<'a, M, T> { | |||
| 226 | } | 230 | } |
| 227 | }) | 231 | }) |
| 228 | }) | 232 | }) |
| 229 | .await; | ||
| 230 | unsafe { &mut *self.channel.buf.add(i) } | ||
| 231 | } | 233 | } |
| 232 | 234 | ||
| 233 | /// Notify the channel that the receiving of the value has been finalized. | 235 | /// Notify the channel that the receiving of the value has been finalized. |
