quickjs_runtime/jsutils/
helper_tasks.rs

1use futures::Future;
2use hirofa_utils::task_manager::TaskManager;
3use lazy_static::lazy_static;
4use tokio::task::JoinError;
5
6lazy_static! {
7    /// a static Multithreaded task manager used to run rust ops async and multithreaded ( in at least 2 threads)
8    static ref HELPER_TASKS: TaskManager = TaskManager::new(std::cmp::max(2, num_cpus::get()));
9}
10
11/// add a task the the "helper" thread pool
12pub fn add_helper_task<T>(task: T)
13where
14    T: FnOnce() + Send + 'static,
15{
16    log::trace!("adding a helper task");
17    HELPER_TASKS.add_task(task);
18}
19
20/// add an async task the the "helper" thread pool
21pub fn add_helper_task_async<R: Send + 'static, T: Future<Output = R> + Send + 'static>(
22    task: T,
23) -> impl Future<Output = Result<R, JoinError>> {
24    log::trace!("adding an async helper task");
25    HELPER_TASKS.add_task_async(task)
26}