aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2022-01-13 16:44:33 +0000
committerGitHub <[email protected]>2022-01-13 16:44:33 +0000
commitf3b999c8b5c5b557ef6d3cd2953cd0bf3e1f94c1 (patch)
treea1a3c0b6a287ff9298dcd7831ace472e2ffcf5ad
parentd07f6828ef605cd27dcebfcd13703b8f98194fb0 (diff)
parentab24e5db5927cf487ffb233e108a78007f2f3a18 (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.rs4
-rw-r--r--embassy/src/util/yield_now.rs25
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
2mod forever; 3mod forever;
4mod yield_now;
3 5
4pub use forever::*; 6pub use forever::*;
7pub 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 @@
1use core::future::Future;
2use core::pin::Pin;
3use core::task::{Context, Poll};
4
5/// Yield from the current task once, allowing other tasks to run.
6pub fn yield_now() -> impl Future<Output = ()> {
7 YieldNowFuture { yielded: false }
8}
9
10struct YieldNowFuture {
11 yielded: bool,
12}
13
14impl 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}