Skip to main content

Pool

Struct Pool 

pub struct Pool { /* private fields */ }
Expand description

A fixed set of worker threads that Exec tokens place work on.

Pool is the only way to make a pool. It wraps the scheduler privately: no scheduler type appears in any signature, there is no constructor from an external pool and no accessor returning one. The pool’s owner keeps the Pool and mints tokens from it with Exec::new.

A Pool is a unique owner: it is not Clone and no method returns a second handle. An engine that wants reserved, always-awake members moves the pool into a Team with into_team; the team owns the pool from then on (and gives it back with Team::into_pool). Sharing a pool between several callers stays outside this crate (for example behind an Arc held by the caller), where each call acquires its members with Exec::team or uses plain tokens.

Dropping a Pool (or the Team that owns it) shuts its workers down and joins them: when drop returns, every worker thread has exited, so the thread-local destructors of the workers (including state set up by an on_start hook) have already run. Without the parallel feature there are no workers and nothing is joined.

A pool dropped from one of its own workers cannot wait for itself: that drop signals shutdown and returns without joining, so the workers exit shortly after instead of before the return. Drop the pool from a plain thread when the guarantee matters.

§Examples

use threadteam::Pool;

let pool = Pool::builder().threads(2).thread_name(|i| format!("w{i}")).build()?;
assert!(pool.width().get() <= 2);

Implementations§

§

impl Pool

pub fn builder() -> PoolBuilder

Starts configuring a pool.

§Examples
let pool = threadteam::Pool::builder().threads(1).build()?;
assert_eq!(pool.width().get(), 1);

pub fn width(&self) -> NonZero<usize>

Number of worker threads, the largest budget a token can have.

Without the parallel feature this is always 1.

§Examples
let pool = threadteam::Pool::builder().threads(1).build()?;
assert_eq!(pool.width().get(), 1);

pub fn wait_policy(&self) -> WaitPolicy

The pool-wide default [WaitPolicy] of idle team members.

§Examples
use std::time::Duration;

let pool = threadteam::Pool::builder()
    .threads(1)
    .idle_spin(Duration::from_millis(5))
    .build()?;
assert_eq!(pool.wait_policy().idle_spin(), Duration::from_millis(5));

pub fn release_idle(&self)

Asks idle team members to return their threads to Rayon now.

Members that are idle leave at once; members that are reserved leave when their reservation ends, so this never revokes a live TeamExec. The call does not wait for anything, so it is safe to make from inside a reservation. A team acquired later starts its members afresh.

§Examples
let pool = threadteam::Pool::builder().threads(2).build()?;
pool.release_idle(); // nothing resident: no effect

pub fn dispatch_costs(&self) -> DispatchCosts

The [DispatchCosts] of this pool’s tokens.

§Examples
use threadteam::{DispatchCosts, Pool};
let pool = Pool::builder().threads(1).build()?;
assert_eq!(pool.dispatch_costs(), DispatchCosts::host_default());

pub fn resident_members(&self) -> usize

Number of workers currently kept awake as team members, reserved or idle. Zero once idle members returned to Rayon. A diagnostic: the value can change at any moment.

§Examples
let pool = threadteam::Pool::builder().threads(2).build()?;
assert_eq!(pool.resident_members(), 0);
§

impl Pool

pub fn into_team(self, width: usize) -> Result<Team, IntoTeamError>

Moves the pool into a [Team] that reserves width - 1 members for as long as it lives.

width counts the leader and is clamped to the pool width; zero is treated as one. Acquisition never waits for a running Rayon job (see Exec::team). Members park after the pool’s idle_spin without work.

§Errors

[IntoTeamError] when the members could not all be reserved; it gives the pool back with the [Refused] reason so the caller falls back to plain tokens.

§Examples
let pool = threadteam::Pool::builder()
    .threads(3)
    .acquire_timeout(std::time::Duration::from_secs(10))
    .build()?;
let team = pool.into_team(3).expect("an idle pool");
assert_eq!(team.size().get(), team.pool().width().get().min(3));

pub fn into_team_with( self, width: usize, over: WaitOverride, ) -> Result<Team, IntoTeamError>

into_team with an override of the park delay (idle_spin) of the reserved members.

§Errors

As into_team.

Trait Implementations§

§

impl Debug for Pool

§

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

Formats the value using the given formatter. Read more
§

impl Drop for Pool

§

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)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for Pool

§

impl !UnwindSafe for Pool

§

impl Freeze for Pool

§

impl Send for Pool

§

impl Sync for Pool

§

impl Unpin for Pool

§

impl UnsafeUnpin for Pool

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<T> ByRef<T> for T

§

fn by_ref(&self) -> &T

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Imply<T> for U
where T: ?Sized, U: ?Sized,

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> 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, S> SimdFrom<T, S> for T
where S: Simd,

§

fn simd_from(_simd: S, value: T) -> T

§

impl<F, T, S> SimdInto<T, S> for F
where T: SimdFrom<F, S>, S: Simd,

§

fn simd_into(self, simd: S) -> T

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.