use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
use crate::{get_task_from_context, local_executor};
pub struct Yield {
was_yielded: bool,
}
impl Future for Yield {
type Output = ();
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
let this = self.get_mut();
if this.was_yielded {
Poll::Ready(())
} else {
this.was_yielded = true;
let task = unsafe { get_task_from_context!(cx) };
if task.is_local() {
local_executor().add_task_at_the_start_of_lifo_local_queue(task);
return Poll::Pending;
}
local_executor().add_task_at_the_start_of_lifo_shared_queue(task);
Poll::Pending
}
}
}
pub fn yield_now() -> Yield {
Yield { was_yielded: false }
}
#[cfg(test)]
mod tests {
use crate::local::Local;
use crate::runtime::local_executor;
use super::*;
use crate as orengine;
#[orengine::test::test_local]
fn test_yield_now() {
let i = Local::new(false);
let i_clone = i.clone();
local_executor().spawn_local(async move {
assert!(!*i.borrow());
*i.borrow_mut() = true;
});
yield_now().await;
assert!(*i_clone.borrow());
}
}