-
-
Notifications
You must be signed in to change notification settings - Fork 101
/
proc_panic.rs
58 lines (49 loc) · 1.31 KB
/
proc_panic.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
use crossbeam::channel::{unbounded, Sender};
use futures::executor;
use lazy_static::lazy_static;
use lightproc::prelude::*;
use std::future::Future;
use std::thread;
fn spawn_on_thread<F, R>(future: F) -> RecoverableHandle<R>
where
F: Future<Output = R> + Send + 'static,
R: Send + 'static,
{
lazy_static! {
// A channel that holds scheduled tasks.
static ref QUEUE: Sender<LightProc> = {
let (sender, receiver) = unbounded::<LightProc>();
// Start the executor thread.
thread::spawn(move || {
for proc in receiver {
proc.run();
}
});
sender
};
}
let schedule = |t| QUEUE.send(t).unwrap();
let (proc, handle) = LightProc::recoverable(
future,
schedule,
ProcStack::default()
.with_pid(1)
.with_before_start(|| {
println!("Before start");
})
.with_after_complete(|| {
println!("After complete");
})
.with_after_panic(|| {
println!("After panic");
}),
);
proc.schedule();
handle
}
fn main() {
let handle = spawn_on_thread(async {
panic!("Panic here!");
});
executor::block_on(handle);
}