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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
use std::thread::{Scope, ScopedJoinHandle};
use std::time::Duration;
use crate::settings::Settings;
use crate::utils::Shutdown;
use crate::worker::Worker;
use crate::{Context, Error, RespawnableContext};
/* ---------- */
/// A runtime to manage [`Workers`] scoped threads.
///
/// [`Workers`]: crate::Worker
pub struct ScopedRuntime<'scope, 'env> {
scope: &'scope Scope<'scope, 'env>,
shutdown: Shutdown,
threads: Vec<ScopedJoinHandle<'scope, ()>>,
respawnables: Vec<RespawnableScopedHandle<'scope, 'env>>,
nested: bool,
}
impl<'scope, 'env> ScopedRuntime<'scope, 'env> {
/// Returns a new runtime bound to the `scope`.
#[inline]
pub fn new(scope: &'scope Scope<'scope, 'env>) -> Self {
Self {
scope,
threads: Vec::new(),
respawnables: Vec::new(),
shutdown: Shutdown::new(),
nested: false,
}
}
/// Enables this runtime to be gracefully shutdown with a `Ctrl+C` signal.
///
/// If the gracefull shutdown doesn't have any effects, users can still
/// send a second `Ctrl+C` signal to forcefully kill the runtime.
#[inline]
pub fn enable_graceful_shutdown(&self) {
crate::utils::enable_graceful_shutdown(&self.shutdown)
}
/// Returns a new scoped runtime whose stopping condition is controlled by the "parent" runtime
/// from which `shutdown` is originates.
///
/// This allows users to spawn runtimes in workers without caring about the shutdown.
pub fn nested(scope: &'scope Scope<'scope, 'env>, shutdown: Shutdown) -> Self {
Self {
scope,
shutdown,
threads: Vec::new(),
respawnables: Vec::new(),
nested: true,
}
}
/// Runs an [`Worker`] in a new thread.
///
/// Similar to the [`Runtime::launch`] function, see its documentation for more details.
///
/// [`Runtime::launch`]: crate::Runtime::launch
#[inline]
pub fn launch<W: Worker + 'env>(&mut self, worker: W) -> Result<(), Error> {
self.inner_spawn_thread(worker, Settings::default(), None::<Vec<_>>)
}
/// Runs an [`Worker`] in a new thread.
///
/// Similar to the [`Runtime::launch_with_settings`] function, see its documentation for more details.
///
/// [`Runtime::launch_with_settings`]: crate::Runtime::launch_with_settings
#[inline]
pub fn launch_with_settings<W: Worker + 'env>(
&mut self,
worker: W,
settings: Settings,
) -> Result<(), Error> {
self.inner_spawn_thread(worker, settings, None::<Vec<_>>)
}
/// Runs an [`Worker`] in a new thread.
///
/// Similar to the [`Runtime::launch_pinned`] function, see its documentation for more details.
///
/// [`Runtime::launch_pinned`]: crate::Runtime::launch_pinned
#[inline]
pub fn launch_pinned<W, C>(&mut self, worker: W, cores: C) -> Result<(), Error>
where
W: Worker + 'env,
C: AsRef<[usize]> + Send + 'env,
{
self.launch_pinned_with_settings(worker, cores, Settings::default())
}
/// Runs an [`Worker`] in a new thread.
///
/// Similar to the [`Runtime::launch_pinned_with_settings`] function, see its documentation for more details.
///
/// [`Runtime::launch_pinned_with_settings`]: crate::Runtime::launch_pinned_with_settings
#[inline]
pub fn launch_pinned_with_settings<W, C>(
&mut self,
worker: W,
cores: C,
settings: Settings,
) -> Result<(), Error>
where
W: Worker + 'env,
C: AsRef<[usize]> + Send + 'env,
{
self.inner_spawn_thread(worker, settings, Some(cores))
}
/// Runs a [`Worker`] built from a [`Context`] that can be respawned if it panics.
///
/// Similar to the [`Runtime::launch_from_context`] function, see its documentation for more details.
///
/// [`Runtime::launch_from_context`]: crate::Runtime::launch_from_context
#[inline]
pub fn launch_from_context<W, C>(&mut self, ctx: C) -> Result<(), Error>
where
W: Worker + 'env,
C: Context<Target = W>,
{
let settings = ctx.settings();
let cores = ctx.core_pinning();
let worker = ctx.into_worker().inspect_err(|_| self.shutdown.stop())?;
self.inner_spawn_thread(worker, settings, cores)
}
/// Runs a [`Worker`] built from a [`RespawnableContext`] that can be respawned if it panics.
///
/// Similar to the [`Runtime::launch_respawnable`] function, see its documentation for more details.
///
/// [`Runtime::launch_respawnable`]: crate::Runtime::launch_respawnable
#[inline]
pub fn launch_respawnable<R>(&mut self, ctx: R) -> Result<(), Error>
where
R: RespawnableContext<'env> + 'env,
{
let managed = RespawnableScopedHandle::spawn_managed(self.scope, ctx, &self.shutdown)
.inspect_err(|_| self.shutdown.stop())?;
self.respawnables.push(managed);
Ok(())
}
/// Blocks the calling thread until all the runtime's workers stop.
///
/// Similar to the [`Runtime::wait`] function, see its documentation for more details.
///
/// [`Workers`]: crate::Worker
/// [`Runtime::wait`]: crate::Runtime::wait
#[inline]
pub fn wait(&mut self) {
// We need to manage respawnable workers until there's none left.
while !self.respawnables.is_empty() {
self.health_check();
std::thread::sleep(Duration::from_micros(1));
}
// Then we join the other workers
for thread in self.threads.drain(..) {
let _ = thread.join();
}
}
/// Checks all respawnable [`Workers`], respawning the ones that panicked.
///
/// Similar to the [`Runtime::health_check`] function, see its documentation for more details.
///
/// [`Workers`]: crate::Worker
/// [`Runtime::health_check`]: crate::Runtime::health_check
#[inline]
pub fn health_check(&mut self) {
self.respawnables.iter_mut().for_each(|managed| {
// TODO: Do something with the errors
let _ = managed.respawn_if_panicked(&self.shutdown);
});
// Filter the handles that actually finished without panicking.
self.respawnables = self
.respawnables
.drain(..)
.filter(|handle| !handle.is_finished())
.collect::<Vec<_>>();
// Filter the threads that finished.
self.threads = self
.threads
.drain(..)
.filter(|handle| !handle.is_finished())
.collect::<Vec<_>>();
}
#[inline]
fn inner_spawn_thread<W, C>(
&mut self,
worker: W,
settings: Settings,
cores: Option<C>,
) -> Result<(), Error>
where
W: Worker + 'env,
C: AsRef<[usize]> + Send + 'env,
{
let thread =
crate::utils::spawn_scoped_thread(self.scope, worker, settings, cores, &self.shutdown)
.inspect_err(|_| self.shutdown.stop())?;
self.threads.push(thread);
Ok(())
}
}
impl Drop for ScopedRuntime<'_, '_> {
fn drop(&mut self) {
if !self.nested {
self.shutdown.stop()
}
self.wait()
}
}
/* ---------- */
struct RespawnableScopedHandle<'scope, 'env> {
scope: &'scope Scope<'scope, 'env>,
handle: Option<ScopedJoinHandle<'scope, ()>>,
context: Box<dyn RespawnableContext<'env> + 'env>,
}
impl<'scope, 'env> RespawnableScopedHandle<'scope, 'env> {
#[inline]
fn spawn_managed(
scope: &'scope Scope<'scope, 'env>,
ctx: impl RespawnableContext<'env> + 'env,
shutdown: &Shutdown,
) -> Result<Self, Error> {
let cores = ctx.core_pinning();
let settings = ctx.settings();
let worker = ctx.boxed_worker()?;
let thread = crate::utils::spawn_scoped_thread(scope, worker, settings, cores, shutdown)?;
Ok(Self {
scope,
handle: Some(thread),
context: Box::new(ctx),
})
}
#[inline]
fn is_finished(&self) -> bool {
self.handle
.as_ref()
.map(|handle| handle.is_finished())
.unwrap_or(true)
}
fn respawn_if_panicked(&mut self, shutdown: &Shutdown) -> Result<(), Error> {
if !self.is_finished() || self.handle.is_none() {
return Ok(());
}
// SAFETY:
// At this point, self.handle is always Some.
let handle = unsafe { self.handle.take().unwrap_unchecked() };
if handle.join().is_err() {
let cores = self.context.core_pinning();
let settings = self.context.settings();
let worker = self.context.boxed_worker()?;
let thread =
crate::utils::spawn_scoped_thread(self.scope, worker, settings, cores, shutdown)?;
self.handle = Some(thread);
}
Ok(())
}
}
/* ---------- */
#[cfg(test)]
mod tests {
use std::time::{Duration, Instant};
use rand::Rng;
use super::*;
use crate::test_utils::*;
#[test]
fn start_stop() {
std::thread::scope(|scope| {
let mut rt = ScopedRuntime::new(scope);
rt.launch(TestWorker)
.expect("failed to launch the test actor");
std::thread::sleep(Duration::from_millis(500));
})
}
#[test]
fn wait() {
std::thread::scope(|scope| {
let mut rt = ScopedRuntime::new(scope);
let now = Instant::now();
let timeout = Duration::from_millis(500);
rt.launch(TestTimedWorker::new(timeout))
.expect("failed to launch the test actor");
rt.wait();
assert!(now.elapsed() > timeout);
})
}
#[test]
fn pinned_actor() {
std::thread::scope(|scope| {
let mut rt = ScopedRuntime::new(scope);
let core_id = rand::thread_rng().gen_range(0..5);
rt.launch_pinned(TestPinnedWorker::new(core_id), [core_id])
.expect("failed to launch the test actor");
std::thread::sleep(Duration::from_millis(1));
})
}
#[test]
fn stop_on_err() {
std::thread::scope(|scope| {
let mut rt = ScopedRuntime::new(scope);
let now = Instant::now();
rt.launch_from_context(BadWorkerContext)
.expect_err("launching this worker should fail");
rt.wait();
assert!(now.elapsed() < Duration::from_millis(500));
})
}
}