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
impl PoolConfig
Sourcepub fn with_workers(self, workers: usize) -> PoolConfig
pub fn with_workers(self, workers: usize) -> PoolConfig
Sets the number of worker threads.
Sourcepub fn with_name_prefix(self, prefix: impl Into<String>) -> PoolConfig
pub fn with_name_prefix(self, prefix: impl Into<String>) -> PoolConfig
Thread name prefix (visible in the profiler and the debugger): <prefix>-<index>.
Sourcepub fn with_stack_size(self, bytes: Option<usize>) -> PoolConfig
pub fn with_stack_size(self, bytes: Option<usize>) -> PoolConfig
Worker thread stack size in bytes (None: the OS default).
Sourcepub fn with_background_limit(self, limit: Option<usize>) -> PoolConfig
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).
Sourcepub fn with_shutdown(self, policy: ShutdownPolicy) -> PoolConfig
pub fn with_shutdown(self, policy: ShutdownPolicy) -> PoolConfig
Shutdown policy when the pool is dropped.
Sourcepub fn with_watchdog(self, threshold: Option<Duration>) -> PoolConfig
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.
Sourcepub fn with_thread_priority(
self,
priority: Option<ThreadPriority>,
) -> PoolConfig
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.
Sourcepub fn with_affinity(self, cores: Option<Vec<usize>>) -> PoolConfig
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 aroundSourcepub fn with_idle_spin(self, rounds: u32) -> PoolConfig
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).
Sourcepub fn watchdog(&self) -> Option<Duration>
pub fn watchdog(&self) -> Option<Duration>
The watchdog threshold (effective in debug builds only).
Sourcepub fn thread_priority(&self) -> Option<ThreadPriority>
pub fn thread_priority(&self) -> Option<ThreadPriority>
Priority of the worker threads.
Sourcepub fn core_for(&self, index: usize) -> Option<usize>
pub fn core_for(&self, index: usize) -> Option<usize>
The core for worker index, if pinning is configured.
Sourcepub fn name_prefix(&self) -> &str
pub fn name_prefix(&self) -> &str
Thread name prefix.
Sourcepub fn stack_size(&self) -> Option<usize>
pub fn stack_size(&self) -> Option<usize>
Worker thread stack size.
Sourcepub fn background_limit(&self) -> usize
pub fn background_limit(&self) -> usize
The worker limit for background tasks, with the default applied.
Sourcepub fn shutdown(&self) -> ShutdownPolicy
pub fn shutdown(&self) -> ShutdownPolicy
The shutdown policy.
Sourcepub fn validate(self) -> Result<PoolConfig, JobsError>
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
impl Clone for PoolConfig
Source§fn clone(&self) -> PoolConfig
fn clone(&self) -> PoolConfig
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for PoolConfig
impl Debug for PoolConfig
Source§impl Default for PoolConfig
impl Default for PoolConfig
Source§fn default() -> PoolConfig
fn default() -> PoolConfig
Number of workers = logical cores − 1 (the main thread runs tasks too), at least 1.
impl Eq for PoolConfig
Source§impl PartialEq for PoolConfig
impl PartialEq for PoolConfig
impl StructuralPartialEq for PoolConfig
Auto Trait Implementations§
impl Freeze for PoolConfig
impl RefUnwindSafe for PoolConfig
impl Send for PoolConfig
impl Sync for PoolConfig
impl Unpin for PoolConfig
impl UnsafeUnpin for PoolConfig
impl UnwindSafe for PoolConfig
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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