diff options
author | Aleksey Kladov <[email protected]> | 2018-09-02 12:46:15 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-09-02 12:46:15 +0100 |
commit | 80be61ed78e8410e013cb94879435d54a4907c30 (patch) | |
tree | 3ab33c902644a3aa14b490be8a7caa9120da7537 /crates/server/src/thread_watcher.rs | |
parent | 7fad13de73ded5b8a332c5f50c18671d612bd1e3 (diff) |
project model
Diffstat (limited to 'crates/server/src/thread_watcher.rs')
-rw-r--r-- | crates/server/src/thread_watcher.rs | 33 |
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 @@ | |||
1 | use std::thread; | ||
2 | use drop_bomb::DropBomb; | ||
3 | use Result; | ||
4 | |||
5 | pub struct ThreadWatcher { | ||
6 | name: &'static str, | ||
7 | thread: thread::JoinHandle<()>, | ||
8 | bomb: DropBomb, | ||
9 | } | ||
10 | |||
11 | impl 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 | } | ||