From 7f7a16675d9c6f60141956eda27cf56b94a5d36c Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Sat, 28 Mar 2020 20:52:45 +0800 Subject: Use jod_thread --- crates/ra_proc_macro/Cargo.toml | 1 + crates/ra_proc_macro/src/process.rs | 31 ++++++++++++++----------------- 2 files changed, 15 insertions(+), 17 deletions(-) (limited to 'crates') 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"] } serde_json = "1.0" log = "0.4.8" crossbeam-channel = "0.4.0" +jod-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::{ io::{self, Write}, path::{Path, PathBuf}, process::{Child, Command, Stdio}, - thread::{spawn, JoinHandle}, }; #[derive(Debug, Default)] @@ -22,8 +21,18 @@ pub(crate) struct ProcMacroProcessSrv { #[derive(Debug)] pub(crate) struct ProcMacroProcessThread { - handle: Option>, - sender: Sender, + // XXX: drop order is significant + sender: SenderGuard, + handle: jod_thread::JoinHandle<()>, +} + +#[derive(Debug)] +struct SenderGuard(pub Sender); + +impl std::ops::Drop for SenderGuard { + fn drop(&mut self) { + let _ = self.0.send(Task::Close); + } } enum Task { @@ -62,18 +71,6 @@ impl Process { } } -impl std::ops::Drop for ProcMacroProcessThread { - fn drop(&mut self) { - if let Some(handle) = self.handle.take() { - let _ = self.sender.send(Task::Close); - - // Join the thread, it should finish shortly. We don't really care - // whether it panicked, so it is safe to ignore the result - let _ = handle.join(); - } - } -} - impl ProcMacroProcessSrv { pub fn run( process_path: &Path, @@ -81,12 +78,12 @@ impl ProcMacroProcessSrv { let process = Process::run(process_path)?; let (task_tx, task_rx) = bounded(0); - let handle = spawn(move || { + let handle = jod_thread::spawn(move || { client_loop(task_rx, process); }); let srv = ProcMacroProcessSrv { inner: Some(task_tx.clone()) }; - let thread = ProcMacroProcessThread { handle: Some(handle), sender: task_tx }; + let thread = ProcMacroProcessThread { handle, sender: SenderGuard(task_tx) }; Ok((thread, srv)) } -- cgit v1.2.3