use crate::scheduler::{Executor, LamellarExecutor, LamellarTask, LamellarTaskInner};
use tokio::runtime::Runtime;
use futures_util::Future;
use std::sync::Arc;
#[derive(Debug)]
pub(crate) struct TokioRt {
max_num_threads: usize,
rt: Runtime,
}
impl LamellarExecutor for TokioRt {
fn spawn_task<F>(&self, task: F, executor: Arc<Executor>) -> LamellarTask<F::Output>
where
F: Future + Send + 'static,
F::Output: Send,
{
let task = self.rt.spawn(task);
LamellarTask {
task: LamellarTaskInner::TokioTask(task),
executor,
task_id: 0,
}
}
fn submit_task<F>(&self, task: F)
where
F: Future + Send + 'static,
F::Output: Send,
{
self.rt.spawn(async move { task.await });
}
fn submit_task_thread<F>(&self, task: F, _: usize)
where
F: Future + Send + 'static,
F::Output: Send,
{
self.rt.spawn(async move { task.await });
}
fn submit_io_task<F>(&self, task: F)
where
F: Future + Send + 'static,
F::Output: Send,
{
self.rt.spawn(async move { task.await });
}
fn submit_immediate_task<F>(&self, task: F)
where
F: Future + Send + 'static,
F::Output: Send,
{
self.rt.spawn(async move { task.await });
}
fn block_on<F: Future>(&self, task: F) -> F::Output {
self.rt.block_on(task)
}
fn shutdown(&self) {
}
fn force_shutdown(&self) {
}
fn exec_task(&self) {
}
fn num_workers(&self) -> usize {
self.max_num_threads
}
}
impl TokioRt {
pub(crate) fn new(num_workers: usize) -> TokioRt {
TokioRt {
max_num_threads: num_workers, rt: tokio::runtime::Builder::new_multi_thread()
.worker_threads(num_workers)
.enable_all()
.build()
.unwrap(),
}
}
}