wip: working on async diff
This commit is contained in:
1 parent
882d69571d
commit
f41cff571f
7 files changed
+198
-109
No files matched your search
@@ -0,0 +1,52 @@
|
||||
use std::future::Future;
|
||||
use std::pin::Pin;
|
||||
use std::task::{Context, Poll};
|
||||
|
||||
// use crate::task::{Context, Poll};
|
||||
|
||||
/// Cooperatively gives up a timeslice to the task scheduler.
|
||||
///
|
||||
/// Calling this function will move the currently executing future to the back
|
||||
/// of the execution queue, making room for other futures to execute. This is
|
||||
/// especially useful after running CPU-intensive operations inside a future.
|
||||
///
|
||||
/// See also [`task::spawn_blocking`].
|
||||
///
|
||||
/// [`task::spawn_blocking`]: fn.spawn_blocking.html
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// Basic usage:
|
||||
///
|
||||
/// ```
|
||||
/// # async_std::task::block_on(async {
|
||||
/// #
|
||||
/// use async_std::task;
|
||||
///
|
||||
/// task::yield_now().await;
|
||||
/// #
|
||||
/// # })
|
||||
/// ```
|
||||
#[inline]
|
||||
pub async fn yield_now() {
|
||||
YieldNow(false).await
|
||||
}
|
||||
|
||||
struct YieldNow(bool);
|
||||
|
||||
impl Future for YieldNow {
|
||||
type Output = ();
|
||||
|
||||
// The futures executor is implemented as a FIFO queue, so all this future
|
||||
// does is re-schedule the future back to the end of the queue, giving room
|
||||
// for other futures to progress.
|
||||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||
if !self.0 {
|
||||
self.0 = true;
|
||||
cx.waker().wake_by_ref();
|
||||
Poll::Pending
|
||||
} else {
|
||||
Poll::Ready(())
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user