1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//! Task system / thread pool for parallel query execution.
//!
//! Uses `rayon` under the hood for work-stealing parallelism.
use rayon::ThreadPool;
use std::sync::Arc;
/// A handle to Akar's task execution system.
#[derive(Clone)]
pub struct TaskSystem {
pool: Arc<ThreadPool>,
num_threads: usize,
}
impl TaskSystem {
/// Create a new task system with the given number of threads.
/// If `num_threads` is 0, uses rayon's default (logical CPU count).
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
}
/// Execute a parallel operation across the thread pool.
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)
}
}