diff options
| author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2022-01-13 16:44:33 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-01-13 16:44:33 +0000 |
| commit | f3b999c8b5c5b557ef6d3cd2953cd0bf3e1f94c1 (patch) | |
| tree | a1a3c0b6a287ff9298dcd7831ace472e2ffcf5ad | |
| parent | d07f6828ef605cd27dcebfcd13703b8f98194fb0 (diff) | |
| parent | ab24e5db5927cf487ffb233e108a78007f2f3a18 (diff) | |
Merge #577
577: embassy/util: Add yield_now() r=Dirbaio a=Dirbaio
Co-authored-by: Dario Nieuwenhuis <[email protected]>
| -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 | } | ||
