use rayon::{ThreadPool, ThreadPoolBuilder};
use std::sync::{Mutex, OnceLock, PoisonError};
pub(crate) fn cores() -> usize {
std::thread::available_parallelism().map_or(1, usize::from)
}
fn pool() -> Option<&'static ThreadPool> {
static POOL: OnceLock<Option<ThreadPool>> = OnceLock::new();
POOL.get_or_init(|| {
ThreadPoolBuilder::new()
.num_threads(cores())
.thread_name(|number| format!("scionic-{number}"))
.build()
.ok()
})
.as_ref()
}
pub(crate) fn with_helpers<T: Send, U>(
others: usize,
work: &(impl Fn() -> T + Sync),
meanwhile: impl FnOnce() -> U,
) -> (Vec<T>, U) {
let pool = match pool() {
Some(pool) if others > 0 => pool,
_ => {
let done = meanwhile();
return (vec![work()], done);
}
};
let given = Mutex::new(Vec::with_capacity(others));
let (own, done) = pool.in_place_scope(|scope| {
for _ in 0..others {
scope.spawn(|_| {
let got = work();
given
.lock()
.unwrap_or_else(PoisonError::into_inner)
.push(got);
});
}
let done = meanwhile();
(work(), done)
});
let mut all = given.into_inner().unwrap_or_else(PoisonError::into_inner);
all.push(own);
(all, done)
}
pub(crate) fn on_threads<T: Send>(threads: usize, work: &(impl Fn() -> T + Sync)) -> Vec<T> {
with_helpers(threads.saturating_sub(1), work, || ()).0
}
#[cfg(test)]
mod tests {
use super::*;
use std::sync::atomic::{AtomicUsize, Ordering};
#[test]
fn every_run_is_given_back_and_meanwhile_runs_on_this_thread() {
let runs = AtomicUsize::new(0);
let here = std::thread::current().id();
let (given, there) = with_helpers(7, &|| runs.fetch_add(1, Ordering::Relaxed), || {
std::thread::current().id()
});
let mut given = given;
given.sort_unstable();
assert_eq!(given, (0..8).collect::<Vec<_>>());
assert_eq!(there, here);
assert_eq!(on_threads(0, &|| 1), vec![1]);
assert_eq!(on_threads(3, &|| 1), vec![1, 1, 1]);
}
#[test]
fn a_panic_on_a_thread_of_the_pool_is_raised_here_once_all_are_done() {
let done = AtomicUsize::new(0);
let caught = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
on_threads(4, &|| {
let run = done.fetch_add(1, Ordering::SeqCst);
if run == 0 {
panic!("the first run fails");
}
})
}));
assert!(caught.is_err());
assert_eq!(done.load(Ordering::SeqCst), 4);
}
#[test]
fn the_threads_that_help_are_the_librarys_own() {
let names = on_threads(4, &|| std::thread::current().name().map(str::to_owned));
let ours = names
.iter()
.flatten()
.filter(|name| name.starts_with("scionic-"))
.count();
assert_eq!(ours, 3, "{names:?}");
}
}