aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorEdwin Cheng <[email protected]>2020-03-28 12:52:45 +0000
committerEdwin Cheng <[email protected]>2020-03-31 15:20:19 +0100
commit7f7a16675d9c6f60141956eda27cf56b94a5d36c (patch)
treec1509f5f957988eb406e1ac933206123713726ef /crates
parent39706a5786522c6c62cee50974ce4052160f30a8 (diff)
Use jod_thread
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_proc_macro/Cargo.toml1
-rw-r--r--crates/ra_proc_macro/src/process.rs31
2 files changed, 15 insertions, 17 deletions
diff --git a/crates/ra_proc_macro/Cargo.toml b/crates/ra_proc_macro/Cargo.toml
index f4a1b6d9e..d009ceb82 100644
--- a/crates/ra_proc_macro/Cargo.toml
+++ b/crates/ra_proc_macro/Cargo.toml
@@ -14,3 +14,4 @@ serde = { version = "1.0", features = ["derive"] }
14serde_json = "1.0" 14serde_json = "1.0"
15log = "0.4.8" 15log = "0.4.8"
16crossbeam-channel = "0.4.0" 16crossbeam-channel = "0.4.0"
17jod-thread = "0.1.1"
diff --git a/crates/ra_proc_macro/src/process.rs b/crates/ra_proc_macro/src/process.rs
index c38c9ab2f..3978d1c9b 100644
--- a/crates/ra_proc_macro/src/process.rs
+++ b/crates/ra_proc_macro/src/process.rs
@@ -12,7 +12,6 @@ use std::{
12 io::{self, Write}, 12 io::{self, Write},
13 path::{Path, PathBuf}, 13 path::{Path, PathBuf},
14 process::{Child, Command, Stdio}, 14 process::{Child, Command, Stdio},
15 thread::{spawn, JoinHandle},
16}; 15};
17 16
18#[derive(Debug, Default)] 17#[derive(Debug, Default)]
@@ -22,8 +21,18 @@ pub(crate) struct ProcMacroProcessSrv {
22 21
23#[derive(Debug)] 22#[derive(Debug)]
24pub(crate) struct ProcMacroProcessThread { 23pub(crate) struct ProcMacroProcessThread {
25 handle: Option<JoinHandle<()>>, 24 // XXX: drop order is significant
26 sender: Sender<Task>, 25 sender: SenderGuard,
26 handle: jod_thread::JoinHandle<()>,
27}
28
29#[derive(Debug)]
30struct SenderGuard(pub Sender<Task>);
31
32impl std::ops::Drop for SenderGuard {
33 fn drop(&mut self) {
34 let _ = self.0.send(Task::Close);
35 }
27} 36}
28 37
29enum Task { 38enum Task {
@@ -62,18 +71,6 @@ impl Process {
62 } 71 }
63} 72}
64 73
65impl 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
77impl ProcMacroProcessSrv { 74impl ProcMacroProcessSrv {
78 pub fn run( 75 pub fn run(
79 process_path: &Path, 76 process_path: &Path,
@@ -81,12 +78,12 @@ impl ProcMacroProcessSrv {
81 let process = Process::run(process_path)?; 78 let process = Process::run(process_path)?;
82 79
83 let (task_tx, task_rx) = bounded(0); 80 let (task_tx, task_rx) = bounded(0);
84 let handle = spawn(move || { 81 let handle = jod_thread::spawn(move || {
85 client_loop(task_rx, process); 82 client_loop(task_rx, process);
86 }); 83 });
87 84
88 let srv = ProcMacroProcessSrv { inner: Some(task_tx.clone()) }; 85 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) };
90 87
91 Ok((thread, srv)) 88 Ok((thread, srv))
92 } 89 }