pub(super) struct ShardedImpl {
shards: [Mutex<Shard>; 16],
non_empty_mask: AtomicUsize,
coord: Mutex<ShardedCoord>,
condvar: Condvar,
is_shutdown: AtomicBool,
}Fields§
§shards: [Mutex<Shard>; 16]§non_empty_mask: AtomicUsizeOne bit per shard, set when that shard’s queue is non-empty. Only
updated while holding that shard’s lock, so it is exact at every
lock release; unlocked loads may be stale (see pop).
coord: Mutex<ShardedCoord>§condvar: Condvar§is_shutdown: AtomicBoolMirror of ThreadManagementState::shutdown, to let spawners reject
tasks without taking coord.
Implementations§
Source§impl ShardedImpl
impl ShardedImpl
pub(super) fn new(thread_mgmt_state: ThreadManagementState) -> ShardedImpl
Sourcefn push_shard_index(&self) -> usize
fn push_shard_index(&self) -> usize
Pick a shard to push to. Use the thread-local RNG so that concurrent spawners spread across the shards.
Sourcefn push(&self, task: Task, metrics: &SpawnerMetrics) -> Result<(), Task>
fn push(&self, task: Task, metrics: &SpawnerMetrics) -> Result<(), Task>
Push a task onto one of the shards, or hand it back if the chosen shard has been sealed for shutdown.
The queue-depth metric is incremented under the shard lock so that the pop that consumes this task (whose decrement is ordered after this lock’s release) can never transiently wrap the counter.
Sourcefn pop(&self, preferred_shard: usize) -> Option<Task>
fn pop(&self, preferred_shard: usize) -> Option<Task>
Pop a task, checking the worker’s preferred shard first.
Sourcefn drain_and_seal(&self, metrics: &SpawnerMetrics, preferred_shard: usize)
fn drain_and_seal(&self, metrics: &SpawnerMetrics, preferred_shard: usize)
Drain every shard, sealing each so that later pushes are rejected,
and run-or-cancel the collected tasks. Called by workers during
shutdown (and by begin_shutdown when there are no workers left to
do it). Sealing is idempotent, so concurrent callers are fine.
Sourcepub(super) fn spawn_task<F>(
&self,
task: Task,
metrics: &SpawnerMetrics,
on_no_idle: F,
) -> Result<(), SpawnError>
pub(super) fn spawn_task<F>( &self, task: Task, metrics: &SpawnerMetrics, on_no_idle: F, ) -> Result<(), SpawnError>
Push a task and either notify an idle worker or invoke
on_no_idle (which is responsible for spawning a new worker if
possible).
Sourcepub(super) fn run_worker(
&self,
metrics: &SpawnerMetrics,
keep_alive: Duration,
worker_thread_id: usize,
) -> Option<JoinHandle<()>>
pub(super) fn run_worker( &self, metrics: &SpawnerMetrics, keep_alive: Duration, worker_thread_id: usize, ) -> Option<JoinHandle<()>>
Run a worker thread’s main loop.
Sourcepub(super) fn begin_shutdown(
&self,
metrics: &SpawnerMetrics,
) -> Option<(Option<JoinHandle<()>>, HashMap<usize, JoinHandle<()>>)>
pub(super) fn begin_shutdown( &self, metrics: &SpawnerMetrics, ) -> Option<(Option<JoinHandle<()>>, HashMap<usize, JoinHandle<()>>)>
Begin pool shutdown: set the shutdown flag, drop the shutdown
sender, wake all waiting workers, and hand back the worker
JoinHandles for the caller to join.