Expand description
A work-stealing pool of worker threads.
A ThreadPool owns a fixed set of worker threads. Each worker keeps a deque of jobs that it
pushes and pops at one end while idle workers steal from the other, and a pool-wide injector
takes jobs submitted from threads outside the pool. A worker with nothing to run searches for
work for a while and then sleeps. Publishing a job or setting a latch wakes sleeping workers
as needed. A worker waiting for a job’s latch keeps running its pool’s jobs until it is set.
Internal join calls run two closures potentially in parallel. ThreadPool::spawn runs a
detached task, and ThreadPool::broadcast and ThreadPool::spawn_broadcast run a task on
every worker. The bridge maps, folds, and collects vectors by splitting them adaptively.
Sorting uses par_mergesort for stable sorts and par_quicksort for unstable sorts.
§Changes vs. Upstream
This module vendors the scheduler of rayon-core 1.13.0 and the producer/consumer bridge and
slice sorts of rayon 1.12.0. The upstream LICENSE-APACHE, LICENSE-MIT, and sleep protocol
README.md accompany the code.
- Moved rayon-core into this module and rewrote crate paths. Flattened the
broadcast,join,spawn, andthread_poolmodules beside their test directories. - Exports
ThreadPool,ThreadPoolBuilder,ThreadPoolBuildError, andYieldthroughcommonware_parallel::rayon. Retained free functions are crate-private or test-only. - Removed the global pool,
build_global,initialize,Registry::current, theprimedlatch, andGlobalPoolAlreadyInitialized. Internaljoinandcurrent_num_threadsrequire a calling worker. - Removed free
spawn,broadcast, andspawn_broadcast. - Removed scopes and their
CountLatchoperations, FIFO scheduling,yield_local,current_thread_has_pending_tasks,max_num_threads, andThreadPool::join. - Removed
Configuration,ThreadPool::new, and the deprecated error description override. - Removed builder options
spawn_handler,build_scoped,exit_handler,panic_handler, andbreadth_first. The registry starts threads directly. Uncaught detached-job and start-handler panics abort. - Removed the fallback for targets without threads,
ThreadPoolBuildError::is_unsupported,web_spin_lock,wasm_sync, the build script, and thelinkskey. bridge.rsdirectly composes the producer and consumers. Removed iterator traits and wrappers,ProducerCallback, producer-side map adapters, unindexed plumbing, ranged draining, andNoopConsumer. Whole-vector draining uses inherent methods andIntoIterator. Fallible folds and reductions useResultin place ofTryandControlFlow.- Merge-sort chunks run through recursive
joincalls in the same indexed split order as the upstreampar_chunks_mutiterator. - Failed collections drop partial outputs before returning the error, without assembling a final vector.
- Added
ThreadPool::block_on_workerto poll futures while servicing pool jobs and waiting through the scheduler’s latch and sleep protocol. - Added
ThreadPoolBuilder::unstarted_workersfor test mocks and a test-only notification when a worker commits to sleeping. - Tests use explicit pools and
commonware_utils::test_rng. Removed compile-fail tests and tests of omitted APIs. Added worker, collection, sorting, and wakeup regressions. - Adapted unsafe blocks, borrowing, and syntax to Rust 2024 and workspace lints. Added
SAFETYcomments and local lint allowances for upstreamstd::syncprimitives. List collection usesVec::with_capacityin place ofnewfollowed byreserve. - Adapted docs to the retained API, clarified queued join execution, removed inaccessible doctests and section banners, and named the scheduler in its abort message.
Structs§
- Thread
Pool - Represents a user-created thread pool.
- Thread
Pool Build Error - Error when initializing a thread pool.
- Thread
Pool Builder - Used to create a new
ThreadPool.
Enums§
- Yield
- Result of a worker’s cooperative yield to its thread pool.