plat/pool.rs#
Source: lib/plat/pool.rs
pool -- the worker thread pool: parallel loops and background jobs [src/core/thread.s]
thr_init(n) -> workers start the pool once: n workers (0 = cpu_count), at most
THR_MAX; `--threads N` on the command line wins. Later
calls return the count; every function below starts it
thr_count() -> workers thr_shutdown() stop and join the workers (no job running)
parallel_for(count, &f) -> Ok / Err(Cancelled)
f(index, job) for 0..count on the workers AND the calling thread; returns when every
item is done. f may call job.cancel() to skip the remaining items. Nested loops
(from inside f) are fine.
Job: a background job -- set(count, f), start() (false: still running), wait(),
cancel(), done(), items_done(). When it finishes the pool calls wake() (E_WAKE).
thr_self() -> slot 0..n-1 on worker k, n on any other thread: per-thread
arrays of thr_count() + 1 entries
Design (as in ASMLIB): one queue of runnable jobs under a short spin lock (loops at the
head, background jobs at the tail). A thread takes part in a job by counting itself in
`active` under the lock, then claims item indexes with fetch_add on `next`. The thread
that sees every index handed out (or the cancel flag) unlinks the job; the last one to
leave finishes it: state DONE (from then on the owner may drop the record), wake() for
background jobs, and every thread sleeping in a job wait is woken. Idle workers spin
briefly, then sleep on a semaphore; waiters spin, then sleep on a second one at most
WAIT_POLL_MS at a time (a semaphore post may be taken by another waiter). Work depends
only on the item index, so results are the same for any number of threads. On Android the
workers run below the UI thread (nice 10, THREAD_PRIORITY_BACKGROUND): a busy pool must not take
the turns of the thread that answers the Choreographer (docs/ANDROID.md AN1).
The loop record lives on the caller's stack and holds a pointer to the borrowed closure:
parallel_for returns only after the last participant left, which is what makes handing
the borrow to the workers sound (the unsafe below).
THR_MAX: usize = 64
struct Cancelleda loop or job ended through cancel()struct JobCtla job as its item functions see it: cancel the rest, look at the progress
impl JobCtl
fn cancel(&self)skip the items not started yet (callable from inside an item) [thr_job_cancel]fn cancelled(&self) -> boollong items poll thisfn count(&self) -> u64fn items_done(&self) -> u64items finished so far (progress = items_done / count)fn thr_init(n: u32) -> u32start the pool (once): the number of worker threads [thr_init]fn thr_count() -> u32the number of worker threads (starts the pool on first use) [thr_count]fn thr_shutdown()stop the workers once the queue is empty, join them; a later call starts a new poolfn parallel_for(count: usize, f: &(dyn Fn(usize, &JobCtl) + Sync)) -> Result<(), Cancelled>f(index, job) for every index in 0..count, on the workers and this thread [thr_parallel_for]struct Joba background job: count items of f on the workers; wake() when it is finished
impl Job
fn new() -> Jobfn set(&mut self, count: u64, f: impl Fn(usize, &JobCtl) + Send + Sync + 'static) -> boolthe work of the next start: count items of f (false while the job runs)fn set_count(&mut self, count: u64) -> boolanother item count for the same function (false while the job runs)fn start(&self) -> boolrun it in the background; false: it is still running [thr_job_start]fn wait(&self) -> Result<(), Cancelled>block until the job is finished (this thread helps with items not taken yet)fn cancel(&self)skip the items not started yet; the job still finishes (wake)fn done(&self) -> boolfinished (or never started)fn items_done(&self) -> u64fn count(&self) -> u64fn thr_self() -> usizethe calling thread's slot: k on worker k, the worker count on any other thread [thr_self]