mod pool;
mod queue;
use std::{sync::OnceLock, thread, time::Duration};
use queue::*;
pub use pool::*;
static POOL: OnceLock<ThreadPool> = OnceLock::new();
pub fn init(name_pattern: &str, num_threads: usize) -> Result<(), ()> {
let pool = ThreadPool::new(name_pattern, num_threads);
POOL.set(pool).map_err(|_| ())
}
pub fn loop_and_sleep_until<F, P>(f: F, ok: P, sleep_for: Duration)
where
F: FnMut() + Send + 'static,
P: FnMut() -> bool + Send + 'static,
{
submit(move || {
let mut f = f;
let mut ok = ok;
loop {
f();
if ok() {
break;
}
thread::sleep(sleep_for);
}
});
}
pub fn submit<F: FnOnce() + Send + 'static>(task: F) {
let pool = POOL.get_or_init(|| ThreadPool::new("ncd-dsptch", 4));
pool.submit(task);
}