aboutsummaryrefslogtreecommitdiff
path: root/crates/server/src/thread_watcher.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/server/src/thread_watcher.rs')
-rw-r--r--crates/server/src/thread_watcher.rs33
1 files changed, 33 insertions, 0 deletions
diff --git a/crates/server/src/thread_watcher.rs b/crates/server/src/thread_watcher.rs
new file mode 100644
index 000000000..98bcdfd6c
--- /dev/null
+++ b/crates/server/src/thread_watcher.rs
@@ -0,0 +1,33 @@
1use std::thread;
2use drop_bomb::DropBomb;
3use Result;
4
5pub struct ThreadWatcher {
6 name: &'static str,
7 thread: thread::JoinHandle<()>,
8 bomb: DropBomb,
9}
10
11impl ThreadWatcher {
12 pub fn spawn(name: &'static str, f: impl FnOnce() + Send + 'static) -> ThreadWatcher {
13 let thread = thread::spawn(f);
14 ThreadWatcher {
15 name,
16 thread,
17 bomb: DropBomb::new(format!("ThreadWatcher {} was not stopped", name)),
18 }
19 }
20
21 pub fn stop(mut self) -> Result<()> {
22 info!("waiting for {} to finish ...", self.name);
23 let name = self.name;
24 self.bomb.defuse();
25 let res = self.thread.join()
26 .map_err(|_| format_err!("ThreadWatcher {} died", name));
27 match &res {
28 Ok(()) => info!("... {} terminated with ok", name),
29 Err(_) => error!("... {} terminated with err", name)
30 }
31 res
32 }
33}