Struct rayon_core::registry::Registry
source · pub(crate) struct Registry {
thread_infos: Vec<ThreadInfo>,
sleep: Sleep,
injected_jobs: Injector<JobRef>,
broadcasts: Mutex<Vec<Worker<JobRef>>>,
panic_handler: Option<Box<dyn Fn(Box<dyn Any + Send>) + Send + Sync>>,
start_handler: Option<Box<dyn Fn(usize) + Send + Sync>>,
exit_handler: Option<Box<dyn Fn(usize) + Send + Sync>>,
terminate_count: AtomicUsize,
}
Fields§
§thread_infos: Vec<ThreadInfo>
§sleep: Sleep
§injected_jobs: Injector<JobRef>
§broadcasts: Mutex<Vec<Worker<JobRef>>>
§panic_handler: Option<Box<dyn Fn(Box<dyn Any + Send>) + Send + Sync>>
§start_handler: Option<Box<dyn Fn(usize) + Send + Sync>>
§exit_handler: Option<Box<dyn Fn(usize) + Send + Sync>>
§terminate_count: AtomicUsize
Implementations§
source§impl Registry
impl Registry
pub(crate) fn new<S>(
builder: ThreadPoolBuilder<S>,
) -> Result<Arc<Self>, ThreadPoolBuildError>where
S: ThreadSpawn,
pub(crate) fn current() -> Arc<Registry>
sourcepub(crate) fn current_num_threads() -> usize
pub(crate) fn current_num_threads() -> usize
Returns the number of threads in the current registry. This
is better than Registry::current().num_threads()
because it
avoids incrementing the Arc
.
sourcepub(crate) fn current_thread(&self) -> Option<&WorkerThread>
pub(crate) fn current_thread(&self) -> Option<&WorkerThread>
Returns the current WorkerThread
if it’s part of this Registry
.
sourcepub(crate) fn id(&self) -> RegistryId
pub(crate) fn id(&self) -> RegistryId
Returns an opaque identifier for this registry.
pub(crate) fn num_threads(&self) -> usize
pub(crate) fn catch_unwind(&self, f: impl FnOnce())
sourcepub(crate) fn wait_until_primed(&self)
pub(crate) fn wait_until_primed(&self)
Waits for the worker threads to get up and running. This is meant to be used for benchmarking purposes, primarily, so that you can get more consistent numbers by having everything “ready to go”.
sourcepub(crate) fn inject_or_push(&self, job_ref: JobRef)
pub(crate) fn inject_or_push(&self, job_ref: JobRef)
//////////////////////////////////////////////////////////////////////// MAIN LOOP
So long as all of the worker threads are hanging out in their
top-level loop, there is no work to be done.
Push a job into the given registry
. If we are running on a
worker thread for the registry, this will push onto the
deque. Else, it will inject from the outside (which is slower).
sourcepub(crate) fn inject(&self, injected_job: JobRef)
pub(crate) fn inject(&self, injected_job: JobRef)
Push a job into the “external jobs” queue; it will be taken by whatever worker has nothing to do. Use this if you know that you are not on a worker of this registry.
fn has_injected_job(&self) -> bool
fn pop_injected_job(&self) -> Option<JobRef>
sourcepub(crate) fn inject_broadcast(
&self,
injected_jobs: impl ExactSizeIterator<Item = JobRef>,
)
pub(crate) fn inject_broadcast( &self, injected_jobs: impl ExactSizeIterator<Item = JobRef>, )
Push a job into each thread’s own “external jobs” queue; it will be executed only on that thread, when it has nothing else to do locally, before it tries to steal other work.
Panics if not given exactly as many jobs as there are threads.
sourcepub(crate) fn in_worker<OP, R>(&self, op: OP) -> R
pub(crate) fn in_worker<OP, R>(&self, op: OP) -> R
If already in a worker-thread of this registry, just execute op
.
Otherwise, inject op
in this thread-pool. Either way, block until op
completes and return its return value. If op
panics, that panic will
be propagated as well. The second argument indicates true
if injection
was performed, false
if executed directly.
unsafe fn in_worker_cold<OP, R>(&self, op: OP) -> R
unsafe fn in_worker_cross<OP, R>( &self, current_thread: &WorkerThread, op: OP, ) -> R
sourcepub(crate) fn increment_terminate_count(&self)
pub(crate) fn increment_terminate_count(&self)
Increments the terminate counter. This increment should be
balanced by a call to terminate
, which will decrement. This
is used when spawning asynchronous work, which needs to
prevent the registry from terminating so long as it is active.
Note that blocking functions such as join
and scope
do not
need to concern themselves with this fn; their context is
responsible for ensuring the current thread-pool will not
terminate until they return.
The global thread-pool always has an outstanding reference
(the initial one). Custom thread-pools have one outstanding
reference that is dropped when the ThreadPool
is dropped:
since installing the thread-pool blocks until any joins/scopes
complete, this ensures that joins/scopes are covered.
The exception is ::spawn()
, which can create a job outside
of any blocking scope. In that case, the job itself holds a
terminate count and is responsible for invoking terminate()
when finished.
sourcepub(crate) fn terminate(&self)
pub(crate) fn terminate(&self)
Signals that the thread-pool which owns this registry has been dropped. The worker threads will gradually terminate, once any extant work is completed.
sourcepub(crate) fn notify_worker_latch_is_set(&self, target_worker_index: usize)
pub(crate) fn notify_worker_latch_is_set(&self, target_worker_index: usize)
Notify the worker that the latch they are sleeping on has been “set”.