embassy/util: Add yield_now()

This commit is contained in:
Dario Nieuwenhuis 2022-01-12 16:30:37 +01:00
parent 0d67ceb066
commit ab24e5db59
2 changed files with 29 additions and 0 deletions

View File

@ -1,7 +1,11 @@
//! Misc utilities //! Misc utilities
mod forever; mod forever;
mod yield_now;
pub use forever::*; pub use forever::*;
pub use yield_now::*;
/// Unsafely unborrow an owned singleton out of a `&mut`. /// Unsafely unborrow an owned singleton out of a `&mut`.
/// ///
/// It is intended to be implemented for owned peripheral singletons, such as `USART3` or `AnyPin`. /// It is intended to be implemented for owned peripheral singletons, such as `USART3` or `AnyPin`.

View File

@ -0,0 +1,25 @@
use core::future::Future;
use core::pin::Pin;
use core::task::{Context, Poll};
/// Yield from the current task once, allowing other tasks to run.
pub fn yield_now() -> impl Future<Output = ()> {
YieldNowFuture { yielded: false }
}
struct YieldNowFuture {
yielded: bool,
}
impl Future for YieldNowFuture {
type Output = ();
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
if self.yielded {
Poll::Ready(())
} else {
self.yielded = true;
cx.waker().wake_by_ref();
Poll::Pending
}
}
}