diff --git a/crates/host/src/queue.rs b/crates/host/src/queue.rs index 9ad5ee0682..06cd8d7448 100644 --- a/crates/host/src/queue.rs +++ b/crates/host/src/queue.rs @@ -434,6 +434,38 @@ impl JobQueue { ran } + /// Finishes the queue's work now: runs the waiting jobs on the calling thread, best first, and waits up to + /// `timeout` for the ones running on lanes (and for the jobs of their keys that wait behind them). Returns whether + /// nothing is left waiting or running; each result waits for [`try_recv`](Self::try_recv) as from a lane. For an + /// owner that must show finished work at once (automation's "wait for the preview", tests). Never waits for a + /// job running on the calling thread. + pub fn finish(&self, timeout: Duration) -> bool { + let me = thread::current().id(); + let deadline = Instant::now().checked_add(timeout); + loop { + let ran = self.run_inline(usize::MAX); + let state = self.shared.lock(); + let elsewhere = state.running.iter().any(|r| r.thread != me); + if !elsewhere { + // Nothing runs elsewhere: done, or the waiting jobs cannot start here (a stopped queue, or a key + // whose job runs on this thread). + if state.queued.is_empty() || ran == 0 { + return state.queued.is_empty(); + } + continue; + } + let left = match deadline { + Some(deadline) => deadline.saturating_duration_since(Instant::now()), + None => timeout, + }; + if left.is_zero() { + return false; + } + // A lane's job ending may free a key whose next job waits: run it on the next turn. + drop(self.shared.ended.wait_timeout(state, left).unwrap_or_else(|e| e.into_inner())); + } + } + /// Gives waiting work a lane when none is alive: a lane the host dropped unrun (a refused spawn) left jobs /// waiting that no later [`submit`](Self::submit) may come to start. An owner that keeps polling a /// waiting job calls it each pass; it does nothing while a lane is alive or nothing waits. diff --git a/crates/host/src/queue_tests.rs b/crates/host/src/queue_tests.rs index 03892cdeb7..b819cc3ea9 100644 --- a/crates/host/src/queue_tests.rs +++ b/crates/host/src/queue_tests.rs @@ -98,3 +98,18 @@ fn queue_gives_back_a_lane_the_host_dropped() { } // `ManualJobs::run_next` runs the oldest job only; a job spawned while it runs waits behind the others. + +// `finish` runs every waiting job on the calling thread, the next job of a key included, and reports the queue done; +// the lanes the host was handed find nothing left. +#[test] +fn queue_finish_runs_the_waiting_jobs_inline() { + let (queue, jobs, _) = queue(1); + queue.submit("a", Priority::VISIBLE, |_| "a"); + queue.submit("b", Priority::BACKGROUND, |_| "b"); + assert!(queue.finish(std::time::Duration::ZERO), "nothing runs elsewhere, so nothing is left"); + assert_eq!(queue.counts().queued + queue.counts().running, 0); + jobs.run_all(); + let mut done = drain(&queue); + done.sort_unstable(); + assert_eq!(done, ["a", "b"], "each job ran once"); +} diff --git a/crates/ui-contract/src/runtime/pending.rs b/crates/ui-contract/src/runtime/pending.rs index d7dbd6677b..43f343e0a0 100644 --- a/crates/ui-contract/src/runtime/pending.rs +++ b/crates/ui-contract/src/runtime/pending.rs @@ -12,6 +12,7 @@ use std::any::TypeId; use std::collections::HashMap; use std::sync::atomic::{AtomicU32, Ordering}; use std::sync::{Arc, Mutex, PoisonError}; +use std::time::Duration; /// A result that may not have arrived. #[derive(Clone, Debug, PartialEq)] @@ -214,6 +215,16 @@ fn queue_of(ctx: &Context, jobs: &Arc) -> Arc> { }) } +/// Finishes the jobs of every pending result on `ctx` now: the waiting ones run on the calling thread, and the call +/// waits up to `timeout` for the ones running on lanes. Returns whether none is left. The results show from the next +/// pass, which takes them into their slots. This is the queue every `cx.pending` job runs on, the full-window +/// workspaces' previews included: an app that must reply with a finished preview (automation's `waitPreview`) runs +/// the pass that asks for it, then this, then one pass more. +pub fn finish_jobs(ctx: &Context, timeout: Duration) -> bool { + let queue = Service::::run(ctx, |state| state.queue.as_ref().map(|(_, queue)| queue.clone())); + queue.is_none_or(|queue| queue.finish(timeout)) +} + /// Notes that `id` was asked for in this pass of the running viewport. fn track(ctx: &Context, id: Id, owner: Id) { let viewport = pass::current_viewport(ctx);