1
0
Fork 0
forked from wry/wry

it: wait for async engine and cpu worker to become idle

This commit is contained in:
Julian Orth 2024-09-28 22:26:01 +02:00
parent 58f82d889b
commit a9aad0c613
5 changed files with 59 additions and 3 deletions

View file

@ -35,6 +35,8 @@ pub struct AsyncEngine {
yield_stash: RefCell<VecDeque<Waker>>,
stopped: Cell<bool>,
now: Cell<Option<Time>>,
#[cfg(feature = "it")]
idle: Cell<Option<Waker>>,
}
impl AsyncEngine {
@ -48,6 +50,8 @@ impl AsyncEngine {
yield_stash: Default::default(),
stopped: Cell::new(false),
now: Default::default(),
#[cfg(feature = "it")]
idle: Default::default(),
})
}
@ -91,7 +95,15 @@ impl AsyncEngine {
pub fn dispatch(&self) {
let mut stash = self.stash.borrow_mut();
let mut yield_stash = self.yield_stash.borrow_mut();
while self.num_queued.get() > 0 {
loop {
if self.num_queued.get() == 0 {
#[cfg(feature = "it")]
if let Some(idle) = self.idle.take() {
idle.wake();
continue;
}
break;
}
self.now.take();
self.iteration.fetch_add(1);
let mut phase = 0;
@ -116,6 +128,22 @@ impl AsyncEngine {
}
}
#[cfg(feature = "it")]
pub async fn idle(&self) {
use std::{future::poll_fn, task::Poll};
let mut register = true;
poll_fn(|ctx| {
if register {
self.idle.set(Some(ctx.waker().clone()));
register = false;
Poll::Pending
} else {
Poll::Ready(())
}
})
.await
}
fn push(&self, runnable: Runnable, phase: Phase) {
self.queues[phase as usize].push(runnable);
self.num_queued.fetch_add(1);