mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-27 10:30:34 +00:00
fwb2-fix: finish the pending jobs (workspace preview queue)
(cherry picked from commit f5356d632e5ccb5822b97270f051dd23e0915878)
This commit is contained in:
@@ -434,6 +434,38 @@ impl<K: Clone + Eq + Hash + Send + 'static, R: Send + 'static> JobQueue<K, R> {
|
||||
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.
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
@@ -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<dyn Jobs>) -> Arc<JobQueue<Id, ()>> {
|
||||
})
|
||||
}
|
||||
|
||||
/// 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::<PendingState>::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<T: Clone + Send + Sync + 'static, E: Clone + Send + Sync + 'static>(ctx: &Context, id: Id, owner: Id) {
|
||||
let viewport = pass::current_viewport(ctx);
|
||||
|
||||
Reference in New Issue
Block a user