Skip to content

Commit 7f7a166

Browse files
committed
Use jod_thread
1 parent 39706a5 commit 7f7a166

File tree

3 files changed

+16
-17
lines changed

3 files changed

+16
-17
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/ra_proc_macro/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ serde = { version = "1.0", features = ["derive"] }
1414
serde_json = "1.0"
1515
log = "0.4.8"
1616
crossbeam-channel = "0.4.0"
17+
jod-thread = "0.1.1"

crates/ra_proc_macro/src/process.rs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use std::{
1212
io::{self, Write},
1313
path::{Path, PathBuf},
1414
process::{Child, Command, Stdio},
15-
thread::{spawn, JoinHandle},
1615
};
1716

1817
#[derive(Debug, Default)]
@@ -22,8 +21,18 @@ pub(crate) struct ProcMacroProcessSrv {
2221

2322
#[derive(Debug)]
2423
pub(crate) struct ProcMacroProcessThread {
25-
handle: Option<JoinHandle<()>>,
26-
sender: Sender<Task>,
24+
// XXX: drop order is significant
25+
sender: SenderGuard,
26+
handle: jod_thread::JoinHandle<()>,
27+
}
28+
29+
#[derive(Debug)]
30+
struct SenderGuard(pub Sender<Task>);
31+
32+
impl std::ops::Drop for SenderGuard {
33+
fn drop(&mut self) {
34+
let _ = self.0.send(Task::Close);
35+
}
2736
}
2837

2938
enum Task {
@@ -62,31 +71,19 @@ impl Process {
6271
}
6372
}
6473

65-
impl std::ops::Drop for ProcMacroProcessThread {
66-
fn drop(&mut self) {
67-
if let Some(handle) = self.handle.take() {
68-
let _ = self.sender.send(Task::Close);
69-
70-
// Join the thread, it should finish shortly. We don't really care
71-
// whether it panicked, so it is safe to ignore the result
72-
let _ = handle.join();
73-
}
74-
}
75-
}
76-
7774
impl ProcMacroProcessSrv {
7875
pub fn run(
7976
process_path: &Path,
8077
) -> Result<(ProcMacroProcessThread, ProcMacroProcessSrv), io::Error> {
8178
let process = Process::run(process_path)?;
8279

8380
let (task_tx, task_rx) = bounded(0);
84-
let handle = spawn(move || {
81+
let handle = jod_thread::spawn(move || {
8582
client_loop(task_rx, process);
8683
});
8784

8885
let srv = ProcMacroProcessSrv { inner: Some(task_tx.clone()) };
89-
let thread = ProcMacroProcessThread { handle: Some(handle), sender: task_tx };
86+
let thread = ProcMacroProcessThread { handle, sender: SenderGuard(task_tx) };
9087

9188
Ok((thread, srv))
9289
}

0 commit comments

Comments
 (0)