pub 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>>,
pub(crate) deadlock_handler: Option<Box<dyn Fn() + Send + Sync>>,
start_handler: Option<Box<dyn Fn(usize) + Send + Sync>>,
exit_handler: Option<Box<dyn Fn(usize) + Send + Sync>>,
pub(crate) acquire_thread_handler: Option<Box<dyn Fn() + Send + Sync>>,
pub(crate) release_thread_handler: Option<Box<dyn Fn() + 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>>
§deadlock_handler: Option<Box<dyn Fn() + Send + Sync>>
§start_handler: Option<Box<dyn Fn(usize) + Send + Sync>>
§exit_handler: Option<Box<dyn Fn(usize) + Send + Sync>>
§acquire_thread_handler: Option<Box<dyn Fn() + Send + Sync>>
§release_thread_handler: Option<Box<dyn Fn() + 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 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 wait_until_stopped(&self)
pub(crate) fn wait_until_stopped(&self)
Waits for the worker threads to stop. This is used for testing – so we can check that termination actually works.
pub(crate) fn acquire_thread(&self)
pub(crate) fn release_thread(&self)
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.
pub(crate) 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”.
Auto Trait Implementations§
impl !Freeze for Registry
impl !RefUnwindSafe for Registry
impl Send for Registry
impl Sync for Registry
impl Unpin for Registry
impl !UnwindSafe for Registry
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Pointable for T
impl<T> Pointable for T
Layout§
Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...)
attributes. Please see the Rust Reference's “Type Layout” chapter for details on type layout guarantees.
Size: 512 bytes