use crate::{com, std_impl};
use core::sync::atomic::{AtomicUsize, Ordering};
pub type Signal = std_impl::Signal;
static INITIAL_WORKERS: AtomicUsize = AtomicUsize::new(0);
pub struct GlobalContext {
imp: std_impl::Context,
}
static GLOBAL: spin::Lazy<GlobalContext> = spin::Lazy::new(|| GlobalContext {
imp: std_impl::Context::new({
let workers = INITIAL_WORKERS.load(Ordering::Acquire);
if workers == 0 {
num_cpus::get()
} else {
workers
}
}),
});
pub fn init_with_limit(limit: usize) {
INITIAL_WORKERS.store(limit, Ordering::Release);
spin::Lazy::force(&GLOBAL);
}
impl GlobalContext {
#[allow(missing_docs)]
pub fn get() -> &'static Self {
&GLOBAL
}
pub fn signal() -> Signal {
Signal::new()
}
}
impl crate::TaskContext for GlobalContext {
fn set_workers(&self, max: usize) {
self.imp.set_workers(max)
}
fn workers(&self) -> usize {
self.imp.workers()
}
fn create_task(&self, f: com::TaskFut) -> com::ComHandle {
self.imp.create_task(f)
}
}