use std::sync::Arc;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;
use std::time::Duration;
use parking_lot::Mutex;
use smol::block_on;
use vortex_error::VortexExpect;
use vortex_utils::parallelism::get_available_parallelism;
use crate::runtime::smol::SmolExecutor;
#[derive(Clone)]
pub struct CurrentThreadWorkerPool {
executor: Arc<SmolExecutor>,
state: Arc<Mutex<PoolState>>,
}
impl CurrentThreadWorkerPool {
pub(super) fn new(executor: Arc<SmolExecutor>) -> Self {
Self {
executor,
state: Arc::new(Mutex::new(PoolState::default())),
}
}
pub fn set_workers_to_available_parallelism(&self) {
let n = get_available_parallelism()
.map(|n| n.saturating_sub(1).max(1))
.unwrap_or(1);
self.set_workers(n);
}
pub fn set_workers(&self, n: usize) {
let mut state = self.state.lock();
let current = state.workers.len();
if n > current {
for _ in current..n {
let shutdown = Arc::new(AtomicBool::new(false));
let executor = Arc::clone(&self.executor);
let shutdown_clone = Arc::clone(&shutdown);
std::thread::Builder::new()
.name("vortex-current-thread-worker".to_string())
.spawn(move || {
block_on(executor.run(async move {
while !shutdown_clone.load(Ordering::Relaxed) {
smol::Timer::after(Duration::from_millis(100)).await;
}
}))
})
.vortex_expect("Failed to spawn current thread worker");
state.workers.push(WorkerHandle { shutdown });
}
} else if n < current {
state.workers.truncate(n);
}
}
pub fn worker_count(&self) -> usize {
self.state.lock().workers.len()
}
}
#[derive(Default)]
struct PoolState {
workers: Vec<WorkerHandle>,
}
struct WorkerHandle {
shutdown: Arc<AtomicBool>,
}
impl Drop for WorkerHandle {
fn drop(&mut self) {
self.shutdown.store(true, Ordering::Relaxed);
}
}
#[cfg(test)]
mod tests {
use std::sync::mpsc;
use std::thread;
use std::time::Instant;
use super::*;
use crate::runtime::BlockingRuntime;
use crate::runtime::current::CurrentThreadRuntime;
fn run_task(runtime: &CurrentThreadRuntime) {
let (tx, rx) = mpsc::channel();
runtime
.handle()
.spawn(async move {
let _ = tx.send(());
})
.detach();
rx.recv_timeout(Duration::from_secs(2))
.expect("background worker should complete spawned task");
}
fn wait_for_worker_exit(shutdown: &Arc<AtomicBool>) {
let deadline = Instant::now() + Duration::from_secs(2);
while Arc::strong_count(shutdown) > 1 {
assert!(
Instant::now() < deadline,
"worker thread did not exit after shutdown"
);
thread::sleep(Duration::from_millis(10));
}
}
#[test]
fn test_worker_pool_clone_and_shutdown() {
let runtime = CurrentThreadRuntime::new();
let pool = runtime.new_pool();
pool.set_workers(1);
let shutdown = Arc::clone(&pool.state.lock().workers[0].shutdown);
run_task(&runtime);
drop(pool.clone());
assert_eq!(pool.worker_count(), 1);
assert!(!shutdown.load(Ordering::Relaxed));
run_task(&runtime);
drop(pool);
assert!(shutdown.load(Ordering::Relaxed));
wait_for_worker_exit(&shutdown);
}
}