use crate::distributor::Distributor;
use crate::run_queue::{Injector, Stealer};
use crate::sleepers::Sleepers;
use crate::worker;
use lazy_static::lazy_static;
use lightproc::prelude::*;
use std::future::Future;
pub fn spawn<F, T>(future: F, stack: ProcStack) -> RecoverableHandle<T>
where
F: Future<Output = T> + Send + 'static,
T: Send + 'static,
{
self::get().spawn(future, stack)
}
#[derive(Debug)]
pub struct Pool {
pub(crate) injector: Injector<LightProc>,
pub(crate) stealers: Vec<Stealer<LightProc>>,
pub(crate) sleepers: Sleepers,
}
impl Pool {
pub fn spawn<F, T>(&self, future: F, stack: ProcStack) -> RecoverableHandle<T>
where
F: Future<Output = T> + Send + 'static,
T: Send + 'static,
{
let _child_id = stack.get_pid() as u64;
let _parent_id = worker::get_proc_stack(|t| t.get_pid() as u64).unwrap_or(0);
let (task, handle) = LightProc::recoverable(future, worker::schedule, stack);
task.schedule();
handle
}
}
#[inline]
pub fn get() -> &'static Pool {
lazy_static! {
static ref POOL: Pool = {
let distributor = Distributor::new();
let stealers = distributor.assign();
Pool {
injector: Injector::new(),
stealers,
sleepers: Sleepers::new(),
}
};
}
&*POOL
}