diff options
| author | Dario Nieuwenhuis <[email protected]> | 2022-01-12 16:30:37 +0100 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2022-01-12 16:30:37 +0100 |
| commit | ab24e5db5927cf487ffb233e108a78007f2f3a18 (patch) | |
| tree | f46d4d70d4699b0c3c40e91e750e9071631f4812 | |
| parent | 0d67ceb066f7b6d3b0163eaa3688b0499439a21c (diff) | |
embassy/util: Add yield_now()
| -rw-r--r-- | embassy/src/util/mod.rs | 4 | ||||
| -rw-r--r-- | embassy/src/util/yield_now.rs | 25 |
2 files changed, 29 insertions, 0 deletions
diff --git a/embassy/src/util/mod.rs b/embassy/src/util/mod.rs index f832fa2f6..7744778bd 100644 --- a/embassy/src/util/mod.rs +++ b/embassy/src/util/mod.rs | |||
| @@ -1,7 +1,11 @@ | |||
| 1 | //! Misc utilities | 1 | //! Misc utilities |
| 2 | |||
| 2 | mod forever; | 3 | mod forever; |
| 4 | mod yield_now; | ||
| 3 | 5 | ||
| 4 | pub use forever::*; | 6 | pub use forever::*; |
| 7 | pub use yield_now::*; | ||
| 8 | |||
| 5 | /// Unsafely unborrow an owned singleton out of a `&mut`. | 9 | /// Unsafely unborrow an owned singleton out of a `&mut`. |
| 6 | /// | 10 | /// |
| 7 | /// It is intended to be implemented for owned peripheral singletons, such as `USART3` or `AnyPin`. | 11 | /// It is intended to be implemented for owned peripheral singletons, such as `USART3` or `AnyPin`. |
diff --git a/embassy/src/util/yield_now.rs b/embassy/src/util/yield_now.rs new file mode 100644 index 000000000..1ebecb916 --- /dev/null +++ b/embassy/src/util/yield_now.rs | |||
| @@ -0,0 +1,25 @@ | |||
| 1 | use core::future::Future; | ||
| 2 | use core::pin::Pin; | ||
| 3 | use core::task::{Context, Poll}; | ||
| 4 | |||
| 5 | /// Yield from the current task once, allowing other tasks to run. | ||
| 6 | pub fn yield_now() -> impl Future<Output = ()> { | ||
| 7 | YieldNowFuture { yielded: false } | ||
| 8 | } | ||
| 9 | |||
| 10 | struct YieldNowFuture { | ||
| 11 | yielded: bool, | ||
| 12 | } | ||
| 13 | |||
| 14 | impl Future for YieldNowFuture { | ||
| 15 | type Output = (); | ||
| 16 | fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
| 17 | if self.yielded { | ||
| 18 | Poll::Ready(()) | ||
| 19 | } else { | ||
| 20 | self.yielded = true; | ||
| 21 | cx.waker().wake_by_ref(); | ||
| 22 | Poll::Pending | ||
| 23 | } | ||
| 24 | } | ||
| 25 | } | ||
