use std::time::Duration;
use qubit_executor::service::ExecutorServiceLifecycle;
use super::thread_pool_config::ThreadPoolConfig;
pub(crate) struct ThreadPoolState {
pub(super) lifecycle: ExecutorServiceLifecycle,
pub(super) live_workers: usize,
pub(super) idle_workers: usize,
pub(super) core_pool_size: usize,
pub(super) maximum_pool_size: usize,
pub(super) keep_alive: Duration,
pub(super) allow_core_thread_timeout: bool,
pub(super) next_worker_index: usize,
}
impl ThreadPoolState {
pub(super) fn new(config: ThreadPoolConfig) -> Self {
Self {
lifecycle: ExecutorServiceLifecycle::Running,
live_workers: 0,
idle_workers: 0,
core_pool_size: config.core_pool_size,
maximum_pool_size: config.maximum_pool_size,
keep_alive: config.keep_alive,
allow_core_thread_timeout: config.allow_core_thread_timeout,
next_worker_index: 0,
}
}
pub(super) fn worker_wait_is_timed(&self) -> bool {
self.allow_core_thread_timeout || self.live_workers > self.core_pool_size
}
pub(super) fn idle_worker_can_retire(&self) -> bool {
self.live_workers > self.maximum_pool_size
|| (self.worker_wait_is_timed()
&& (self.live_workers > self.core_pool_size || self.allow_core_thread_timeout))
}
}