aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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}