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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
use super::actor::{Actor, ActorConstructable, Props};
use super::address::Address;
use super::cell::Cell;
use super::scheduler::Scheduler;
use std::fmt::{self, Display, Formatter};
use std::sync::Arc;
use std::thread;
use num_cpus;
use rand::{thread_rng, Rng, ThreadRng};
pub struct System {
thread_handles: Vec<thread::JoinHandle<()>>,
thread_schedulers: Vec<Arc<Scheduler>>,
config: Config,
state: RunningState,
rng: ThreadRng,
}
impl System {
pub fn new() -> Self {
System {
thread_handles: vec![],
thread_schedulers: vec![],
config: Config::default(),
state: RunningState::AwaitingStart,
rng: thread_rng(),
}
}
pub fn with_config(&mut self, config: Config) {
self.config = config;
}
pub fn new_actor<A, P>(&mut self, props: P) -> Address<A>
where
A: Actor + ActorConstructable<P> + 'static,
P: Props + 'static,
{
if self.state != RunningState::Running {
panic!(
"Cannot create actors on a runtime that is not started, currently in state {}",
self.state
);
}
let scheduler_index = Rng::gen_range(&mut self.rng, 0, self.thread_schedulers.len());
let producer = Box::new(move || A::new(&props));
let cell: Arc<Cell<A>> = Cell::new(producer,
Arc::downgrade(&self.thread_schedulers[scheduler_index]));
self.thread_schedulers[scheduler_index].register_new_cell(cell.clone());
Cell::address(cell)
}
pub fn spawn(&mut self) {
self.state = RunningState::Starting;
for thread_id in 0..self.config.threads {
let scheduler = Arc::new(Scheduler::new(thread_id));
self.thread_schedulers.push(scheduler.clone());
self.thread_handles.push(thread::spawn(move || {
println!("Spawning scheduler on new thread");
scheduler.start();
}));
}
self.state = RunningState::Running;
}
pub fn graceful_shutdown(self) {
for handle in self.thread_handles {
match handle.join() {
Ok(_) => (),
Err(_) => error!("Thread panicked during graceful shutdown while waiting to join"),
}
}
}
}
unsafe impl Send for System {}
#[derive(PartialEq, Eq)]
enum RunningState {
AwaitingStart = 0,
Starting,
Running,
Stopping,
Stopped,
}
impl Display for RunningState {
fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
if self == &RunningState::AwaitingStart {
write!(f, "awaiting-start")
} else if self == &RunningState::Starting {
write!(f, "starting")
} else if self == &RunningState::Running {
write!(f, "running")
} else if self == &RunningState::Stopping {
write!(f, "stopping")
} else if self == &RunningState::Stopped {
write!(f, "stopped")
} else {
Ok(())
}
}
}
pub struct Config {
threads: usize,
}
impl Default for Config {
fn default() -> Self {
Config {
threads: num_cpus::get(),
}
}
}