-
-
Notifications
You must be signed in to change notification settings - Fork 101
/
pool.rs
104 lines (97 loc) · 2.61 KB
/
pool.rs
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
//!
//! Pool of threads to run lightweight processes
//!
//! Pool management and tracking belongs here.
//! We spawn futures onto the pool with [spawn] method of global run queue or
//! with corresponding [Worker]'s spawn method.
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;
///
/// Spawn a process (which contains future + process stack) onto the executor from the global level.
///
/// # Example
/// ```rust
/// use bastion_executor::prelude::*;
/// use lightproc::prelude::*;
///
/// let pid = 1;
/// let stack = ProcStack::default().with_pid(pid);
///
/// let handle = spawn(
/// async {
/// panic!("test");
/// },
/// stack.clone(),
/// );
///
/// run(
/// async {
/// handle.await;
/// },
/// stack.clone(),
/// );
/// ```
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)
}
///
/// Pool that global run queue, stealers of the workers, and parked threads.
#[derive(Debug)]
pub struct Pool {
///
/// Global run queue implementation
pub injector: Injector<LightProc>,
///
/// Stealers of the workers
pub stealers: Vec<Stealer<LightProc>>,
///
/// Container of parked threads
pub sleepers: Sleepers,
}
impl Pool {
/// Error recovery for the fallen threads
pub fn recover_async_thread() {
// FIXME: Do recovery for fallen worker threads
unimplemented!()
}
///
/// Spawn a process (which contains future + process stack) onto the executor via [Pool] interface.
pub fn spawn<F, T>(&self, future: F, stack: ProcStack) -> RecoverableHandle<T>
where
F: Future<Output = T> + Send + 'static,
T: Send + 'static,
{
// Log this `spawn` operation.
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
}
}
///
/// Acquire the static Pool reference
#[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
}