use rayon::ThreadPool;
use std::sync::Arc;
#[derive(Clone)]
pub struct TaskSystem {
pool: Arc<ThreadPool>,
num_threads: usize,
}
impl TaskSystem {
pub fn new(num_threads: usize) -> Self {
let num = if num_threads == 0 {
rayon::current_num_threads()
} else {
num_threads
};
let pool = Arc::new(
rayon::ThreadPoolBuilder::new()
.num_threads(num)
.thread_name(|i| format!("akar-worker-{i}"))
.build()
.expect("Failed to build rayon thread pool"),
);
Self { pool, num_threads: num }
}
pub fn num_threads(&self) -> usize {
self.num_threads
}
pub fn install<F, R>(&self, op: F) -> R
where
F: FnOnce() -> R + Send,
R: Send,
{
self.pool.install(op)
}
}
impl Default for TaskSystem {
fn default() -> Self {
Self::new(0)
}
}