Skip to main content

ShardedImpl

Struct ShardedImpl 

Source
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: AtomicUsize

One 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: AtomicBool

Mirror of ThreadManagementState::shutdown, to let spawners reject tasks without taking coord.

Implementations§

Source§

impl ShardedImpl

Source

pub(super) fn new(thread_mgmt_state: ThreadManagementState) -> ShardedImpl

Source

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.

Source

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.

Source

fn pop(&self, preferred_shard: usize) -> Option<Task>

Pop a task, checking the worker’s preferred shard first.

Source

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.

Source

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).

Source

pub(super) fn run_worker( &self, metrics: &SpawnerMetrics, keep_alive: Duration, worker_thread_id: usize, ) -> Option<JoinHandle<()>>

Run a worker thread’s main loop.

Source

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.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.