forked from amqp-rs/lapin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththread.rs
37 lines (30 loc) · 887 Bytes
/
thread.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
use crate::Result;
use parking_lot::Mutex;
use std::{sync::Arc, thread};
pub type JoinHandle = thread::JoinHandle<Result<()>>;
#[derive(Clone)]
pub struct ThreadHandle(Arc<Mutex<Option<JoinHandle>>>);
impl Default for ThreadHandle {
fn default() -> Self {
Self(Arc::new(Mutex::new(None)))
}
}
impl ThreadHandle {
pub fn new(handle: JoinHandle) -> Self {
Self(Arc::new(Mutex::new(Some(handle))))
}
pub(crate) fn register(&self, handle: JoinHandle) {
*self.0.lock() = Some(handle);
}
fn take(&self) -> Option<JoinHandle> {
self.0.lock().take()
}
pub(crate) fn wait(&self, context: &'static str) -> Result<()> {
if let Some(handle) = self.take() {
if handle.thread().id() != thread::current().id() {
handle.join().expect(context)?;
}
}
Ok(())
}
}