Skip to main content

WorkerStatefulPool

Struct WorkerStatefulPool 

Source
pub struct WorkerStatefulPool { /* private fields */ }
Available on crate feature rayon only.
Expand description

Owns one run’s thread-local state on a shared WorkerPool.

Share this value with jobs through an Arc. Dropping the last handle clears this run’s state on every pool thread, leaving other runs’ state untouched. Worker slots retain only a weak reference to this owner and do not keep it alive. Dereferences to the underlying pool for scheduling jobs; each job accessing state must retain an owner handle.

Access closures borrow the current thread’s worker and must not yield to Rayon or recursively access worker state. Use them only in jobs executing on this pool. Cleanup broadcasts to the pool and blocks until complete; the last handle must be dropped outside worker access closures.

Implementations§

Source§

impl WorkerStatefulPool

Source

pub fn new(pool: Arc<WorkerPool>) -> Arc<Self> ⓘ

Creates a shared state owner without initializing the pool or its workers.

Source

pub fn init<T: 'static>(&self, f: impl Fn(Option<&mut T>) -> T + Sync)

Initializes this run’s state on every pool thread.

Offers existing state to f only when its run and type match. Otherwise, drops the previous state before calling f with None.

Source

pub fn get<T: 'static, R>(&self, f: impl FnOnce(&T) -> R) -> R

Reads this run’s state on the current worker thread.

§Panics

Panics if the worker is uninitialized, its run or type differs, or it is mutably borrowed.

Source

pub fn get_or_init<T: 'static, R>( &self, init: impl FnOnce() -> T, f: impl FnOnce(&mut T) -> R, ) -> R

Mutably accesses this run’s state, initializing it on the current worker if needed.

Reuses state without calling init when both its run and type match. Otherwise, drops the previous state before calling init. If init panics, the worker remains uninitialized.

§Panics

Panics if the current worker is already borrowed.

Methods from Deref<Target = WorkerPool>§

Source

pub fn is_initialized(&self) -> bool

Returns true if the underlying rayon pool has been initialized.

Source

pub fn num_threads(&self) -> usize

Returns the number of threads the pool is configured with, without creating it.

Source

pub fn current_num_threads(&self) -> usize

Returns the total number of threads in the underlying rayon pool.

Source

pub fn broadcast(&self, num_threads: usize, f: impl Fn(&mut Worker) + Sync)

Runs a closure on num_threads threads in the pool, giving mutable access to each thread’s Worker.

The closure runs with exclusive access to each selected thread’s worker. Only num_threads threads execute the closure; the rest skip it.

Source

pub fn install<R: Send>(&self, f: impl FnOnce(&Worker) -> R + Send) -> R

Runs a closure on the pool with access to the calling thread’s Worker.

All rayon parallelism (e.g. par_iter) spawned inside the closure executes on this pool. Each thread can access its own Worker via the provided reference or through additional WorkerPool::with_worker calls.

Source

pub fn install_fn<R: Send>(&self, f: impl FnOnce() -> R + Send) -> R

Runs a closure on the pool without worker state access.

Like install but for closures that don’t need per-thread Worker state.

Source

pub fn spawn_and_wait<R: Send + 'static>( &self, f: impl FnOnce() -> R + Send + 'static, ) -> R

Runs a closure on this pool, waiting for its result.

Unlike install_fn, this always queues the closure onto this pool when called from another rayon pool. This avoids Rayon running the closure on the caller’s worker through its cross-pool install path.

Source

pub fn spawn(&self, f: impl FnOnce() + Send + 'static)

Spawns a closure on the pool.

Source

pub fn in_place_scope<'scope, R>( &self, f: impl FnOnce(&Scope<'scope>) -> R, ) -> R

Executes f on this pool using [rayon::in_place_scope], which converts the calling thread into a worker for the duration — tasks spawned inside the scope run on the pool and the call blocks until all of them complete.

Trait Implementations§

Source§

impl Debug for WorkerStatefulPool

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Deref for WorkerStatefulPool

Source§

type Target = WorkerPool

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl Drop for WorkerStatefulPool

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics #130494)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

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
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

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

§

fn type_id(&self) -> TypeId

Returns the lifetime-erased type ID of self.
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types #44874)
The target type on which the method may be called.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more

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: 16 bytes