Skip to main content

PoolConfig

Struct PoolConfig 

Source
pub struct PoolConfig { /* private fields */ }
Expand description

Thread pool configuration.

A builder: start from Default (cores − 1 workers, at least 1) and override what the engine needs. Errors are reported by validate, which ThreadPool::new calls for you.

use std::time::Duration;
use pragma_jobs::{PoolConfig, ShutdownPolicy, ThreadPool};
use pragma_jobs::sys::ThreadPriority;

let config = PoolConfig::default()
    .with_workers(4)
    .with_name_prefix("game-worker")
    .with_background_limit(Some(1)) // streaming never takes more than one worker
    .with_shutdown(ShutdownPolicy::Cancel)
    .with_watchdog(Some(Duration::from_millis(500)))
    .with_thread_priority(Some(ThreadPriority::Normal));
assert_eq!(config.background_limit(), 1);

let pool = ThreadPool::new(config)?;
assert_eq!(pool.workers(), 4);

Implementations§

Source§

impl PoolConfig

Source

pub fn with_workers(self, workers: usize) -> PoolConfig

Sets the number of worker threads.

Source

pub fn with_name_prefix(self, prefix: impl Into<String>) -> PoolConfig

Thread name prefix (visible in the profiler and the debugger): <prefix>-<index>.

Source

pub fn with_stack_size(self, bytes: Option<usize>) -> PoolConfig

Worker thread stack size in bytes (None: the OS default).

Source

pub fn with_background_limit(self, limit: Option<usize>) -> PoolConfig

How many workers may run Priority::Background tasks at the same time (None: half of the workers, rounded up).

Source

pub fn with_shutdown(self, policy: ShutdownPolicy) -> PoolConfig

Shutdown policy when the pool is dropped.

Source

pub fn with_watchdog(self, threshold: Option<Duration>) -> PoolConfig

Watchdog for stuck tasks (debug builds only): a labeled task (labeled: graph tasks, spawn, async) running on a pool thread longer than threshold produces one warning in the log (tracing::warn!, target pragma_jobs::watchdog) with the task and thread names. None disables it.

Defaults to DEFAULT_WATCHDOG. In a release build the setting is ignored: there is neither a watchdog thread nor task bookkeeping.

Source

pub fn with_thread_priority( self, priority: Option<ThreadPriority>, ) -> PoolConfig

OS priority of the worker threads (None: leave unchanged). If the OS refuses (for example raising it on Linux without CAP_SYS_NICE), the worker logs a warning and runs with its previous priority.

Source

pub fn with_affinity(self, cores: Option<Vec<usize>>) -> PoolConfig

Pins workers to logical cores: worker i is pinned to core cores[i % cores.len()] (None: no pinning, the OS decides). A pinning error is a warning in the log; the worker runs without it.

use pragma_jobs::PoolConfig;

// Leave core 0 to the main thread, spread 4 workers over cores 1..=3.
let config = PoolConfig::default().with_workers(4).with_affinity(Some(vec![1, 2, 3]));
assert_eq!(config.core_for(0), Some(1));
assert_eq!(config.core_for(3), Some(1)); // wraps around
Source

pub fn with_idle_spin(self, rounds: u32) -> PoolConfig

How many yield rounds an idle worker searches for work before going to sleep (after a short spin). More rounds pick up the next wave of work (a frame, a nested join) faster, but keep the CPU busy doing nothing for longer; 0 parks right after the spin. The default DEFAULT_IDLE_SPIN was tuned with benchmarks (docs/perf.md).

Source

pub fn idle_spin(&self) -> u32

yield rounds before parking.

Source

pub fn watchdog(&self) -> Option<Duration>

The watchdog threshold (effective in debug builds only).

Source

pub fn thread_priority(&self) -> Option<ThreadPriority>

Priority of the worker threads.

Source

pub fn core_for(&self, index: usize) -> Option<usize>

The core for worker index, if pinning is configured.

Source

pub fn workers(&self) -> usize

Number of worker threads.

Source

pub fn name_prefix(&self) -> &str

Thread name prefix.

Source

pub fn stack_size(&self) -> Option<usize>

Worker thread stack size.

Source

pub fn background_limit(&self) -> usize

The worker limit for background tasks, with the default applied.

Source

pub fn shutdown(&self) -> ShutdownPolicy

The shutdown policy.

Source

pub fn validate(self) -> Result<PoolConfig, JobsError>

Validates the configuration before threads are started.

Rules: 1..=512 workers, a non-zero stack size, a background limit in 1..=workers, a non-zero watchdog threshold, a non-empty affinity list.

use pragma_jobs::{JobsError, PoolConfig};

let err = PoolConfig::default()
    .with_workers(2)
    .with_background_limit(Some(3))
    .validate()
    .unwrap_err();
assert!(matches!(err, JobsError::InvalidConfig { .. }));
assert!(PoolConfig::default().validate().is_ok());

Trait Implementations§

Source§

impl Clone for PoolConfig

Source§

fn clone(&self) -> PoolConfig

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for PoolConfig

Source§

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

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

impl Default for PoolConfig

Source§

fn default() -> PoolConfig

Number of workers = logical cores − 1 (the main thread runs tasks too), at least 1.

Source§

impl Eq for PoolConfig

Source§

impl PartialEq for PoolConfig

Source§

fn eq(&self, other: &PoolConfig) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for PoolConfig

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

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

Initializes a with the given initializer. Read more
Source§

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

Dereferences the given pointer. Read more
Source§

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

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

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

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
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