mirror of
https://github.com/embassy-rs/embassy.git
synced 2024-11-22 06:42:32 +00:00
embassy/util: Add yield_now()
This commit is contained in:
parent
0d67ceb066
commit
ab24e5db59
@ -1,7 +1,11 @@
|
||||
//! Misc utilities
|
||||
|
||||
mod forever;
|
||||
mod yield_now;
|
||||
|
||||
pub use forever::*;
|
||||
pub use yield_now::*;
|
||||
|
||||
/// Unsafely unborrow an owned singleton out of a `&mut`.
|
||||
///
|
||||
/// It is intended to be implemented for owned peripheral singletons, such as `USART3` or `AnyPin`.
|
||||
|
25
embassy/src/util/yield_now.rs
Normal file
25
embassy/src/util/yield_now.rs
Normal 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
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user