1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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,
{
// trace_span!("spawn_task").in_scope(|| {
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,
{
// trace_span!("submit_task").in_scope(|| {
self.rt.spawn(async move { task.await });
// });
}
fn submit_task_thread<F>(&self, task: F, _: usize)
where
F: Future + Send + 'static,
F::Output: Send,
{
// trace_span!("submit_task").in_scope(|| {
self.rt.spawn(async move { task.await });
// });
}
fn submit_io_task<F>(&self, task: F)
where
F: Future + Send + 'static,
F::Output: Send,
{
// trace_span!("submit_task").in_scope(|| {
self.rt.spawn(async move { task.await });
// });
}
fn submit_immediate_task<F>(&self, task: F)
where
F: Future + Send + 'static,
F::Output: Send,
{
// trace_span!("submit_task").in_scope(|| {
self.rt.spawn(async move { task.await });
// });
}
fn block_on<F: Future>(&self, task: F) -> F::Output {
// trace_span!("block_on").in_scope(||
self.rt.block_on(task)
// )
}
// //#[tracing::instrument(skip_all)]
fn shutdown(&self) {
// i think we just let tokio do this on drop
// println!("shutting down tokio runtime");
}
// //#[tracing::instrument(skip_all)]
fn force_shutdown(&self) {
// i think we just let tokio do this on drop
}
// //#[tracing::instrument(skip_all)]
fn exec_task(&self) {
// I dont think tokio has a way to do this
}
// fn set_max_workers(&mut self, num_workers: usize) {
// self.max_num_threads = num_workers;
// }
fn num_workers(&self) -> usize {
self.max_num_threads
}
// fn active(&self) -> bool {
// self.status.load(Ordering::SeqCst) == SchedulerStatus::Active as u8
// }
}
impl TokioRt {
pub(crate) fn new(num_workers: usize) -> TokioRt {
// println!("New TokioRT with {} workers", num_workers);
TokioRt {
max_num_threads: num_workers, //LAMELLAR_THREADS = num_workers + 1,so for tokio runtime, we actually want num_workers + 1 worker threads as block_on will not do anywork on the main thread (i think)...
rt: tokio::runtime::Builder::new_multi_thread()
.worker_threads(num_workers)
.enable_all()
.build()
.unwrap(),
}
}
}