mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-25 12:33:33 +00:00
Remove proc macro management thread
This commit is contained in:
parent
552b50de9e
commit
5a9ca311e3
3 changed files with 44 additions and 71 deletions
|
@ -20,7 +20,7 @@ use std::{
|
||||||
|
|
||||||
use tt::{SmolStr, Subtree};
|
use tt::{SmolStr, Subtree};
|
||||||
|
|
||||||
use crate::process::{ProcMacroProcessSrv, ProcMacroProcessThread};
|
use crate::process::ProcMacroProcessSrv;
|
||||||
|
|
||||||
pub use rpc::{ExpansionResult, ExpansionTask, ListMacrosResult, ListMacrosTask, ProcMacroKind};
|
pub use rpc::{ExpansionResult, ExpansionTask, ListMacrosResult, ListMacrosTask, ProcMacroKind};
|
||||||
pub use version::{read_dylib_info, RustCInfo};
|
pub use version::{read_dylib_info, RustCInfo};
|
||||||
|
@ -64,16 +64,16 @@ impl base_db::ProcMacroExpander for ProcMacroProcessExpander {
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct ProcMacroClient {
|
pub struct ProcMacroClient {
|
||||||
process: Arc<ProcMacroProcessSrv>,
|
process: Arc<ProcMacroProcessSrv>,
|
||||||
thread: ProcMacroProcessThread,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ProcMacroClient {
|
impl ProcMacroClient {
|
||||||
|
/// Spawns an external process as the proc macro server and returns a client connected to it.
|
||||||
pub fn extern_process(
|
pub fn extern_process(
|
||||||
process_path: PathBuf,
|
process_path: PathBuf,
|
||||||
args: impl IntoIterator<Item = impl AsRef<OsStr>>,
|
args: impl IntoIterator<Item = impl AsRef<OsStr>>,
|
||||||
) -> io::Result<ProcMacroClient> {
|
) -> io::Result<ProcMacroClient> {
|
||||||
let (thread, process) = ProcMacroProcessSrv::run(process_path, args)?;
|
let process = ProcMacroProcessSrv::run(process_path, args)?;
|
||||||
Ok(ProcMacroClient { process: Arc::new(process), thread })
|
Ok(ProcMacroClient { process: Arc::new(process) })
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn by_dylib_path(&self, dylib_path: &Path) -> Vec<ProcMacro> {
|
pub fn by_dylib_path(&self, dylib_path: &Path) -> Vec<ProcMacro> {
|
||||||
|
|
|
@ -3,13 +3,13 @@
|
||||||
use std::{
|
use std::{
|
||||||
convert::{TryFrom, TryInto},
|
convert::{TryFrom, TryInto},
|
||||||
ffi::{OsStr, OsString},
|
ffi::{OsStr, OsString},
|
||||||
|
fmt,
|
||||||
io::{self, BufRead, BufReader, Write},
|
io::{self, BufRead, BufReader, Write},
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
process::{Child, Command, Stdio},
|
process::{Child, ChildStdin, ChildStdout, Command, Stdio},
|
||||||
sync::{Arc, Weak},
|
sync::Mutex,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crossbeam_channel::{bounded, Receiver, Sender};
|
|
||||||
use stdx::JodChild;
|
use stdx::JodChild;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
@ -17,38 +17,31 @@ use crate::{
|
||||||
rpc::{ListMacrosResult, ListMacrosTask, ProcMacroKind},
|
rpc::{ListMacrosResult, ListMacrosTask, ProcMacroKind},
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
|
||||||
pub(crate) struct ProcMacroProcessSrv {
|
pub(crate) struct ProcMacroProcessSrv {
|
||||||
inner: Weak<Sender<Task>>,
|
process: Mutex<Process>,
|
||||||
|
stdio: Mutex<(ChildStdin, BufReader<ChildStdout>)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
impl fmt::Debug for ProcMacroProcessSrv {
|
||||||
pub(crate) struct ProcMacroProcessThread {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
// XXX: drop order is significant
|
f.debug_struct("ProcMacroProcessSrv").field("process", &self.process).finish()
|
||||||
sender: Arc<Sender<Task>>,
|
}
|
||||||
handle: jod_thread::JoinHandle<()>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ProcMacroProcessSrv {
|
impl ProcMacroProcessSrv {
|
||||||
pub(crate) fn run(
|
pub(crate) fn run(
|
||||||
process_path: PathBuf,
|
process_path: PathBuf,
|
||||||
args: impl IntoIterator<Item = impl AsRef<OsStr>>,
|
args: impl IntoIterator<Item = impl AsRef<OsStr>>,
|
||||||
) -> io::Result<(ProcMacroProcessThread, ProcMacroProcessSrv)> {
|
) -> io::Result<ProcMacroProcessSrv> {
|
||||||
let process = Process::run(process_path, args)?;
|
let mut process = Process::run(process_path, args)?;
|
||||||
|
let (stdin, stdout) = process.stdio().expect("couldn't access child stdio");
|
||||||
|
|
||||||
let (task_tx, task_rx) = bounded(0);
|
let srv = ProcMacroProcessSrv {
|
||||||
let handle = jod_thread::Builder::new()
|
process: Mutex::new(process),
|
||||||
.name("ProcMacroClient".to_owned())
|
stdio: Mutex::new((stdin, stdout)),
|
||||||
.spawn(move || {
|
};
|
||||||
client_loop(task_rx, process);
|
|
||||||
})
|
|
||||||
.expect("failed to spawn thread");
|
|
||||||
|
|
||||||
let task_tx = Arc::new(task_tx);
|
Ok(srv)
|
||||||
let srv = ProcMacroProcessSrv { inner: Arc::downgrade(&task_tx) };
|
|
||||||
let thread = ProcMacroProcessThread { handle, sender: task_tx };
|
|
||||||
|
|
||||||
Ok((thread, srv))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn find_proc_macros(
|
pub(crate) fn find_proc_macros(
|
||||||
|
@ -65,18 +58,27 @@ impl ProcMacroProcessSrv {
|
||||||
where
|
where
|
||||||
R: TryFrom<Response, Error = &'static str>,
|
R: TryFrom<Response, Error = &'static str>,
|
||||||
{
|
{
|
||||||
let (result_tx, result_rx) = bounded(0);
|
let mut guard = self.stdio.lock().unwrap_or_else(|e| e.into_inner());
|
||||||
let sender = match self.inner.upgrade() {
|
let stdio = &mut *guard;
|
||||||
None => return Err(tt::ExpansionError::Unknown("proc macro process is closed".into())),
|
let (stdin, stdout) = (&mut stdio.0, &mut stdio.1);
|
||||||
Some(it) => it,
|
|
||||||
};
|
|
||||||
sender
|
|
||||||
.send(Task { req, result_tx })
|
|
||||||
.map_err(|_| tt::ExpansionError::Unknown("proc macro server crashed".into()))?;
|
|
||||||
|
|
||||||
let res = result_rx
|
let mut buf = String::new();
|
||||||
.recv()
|
let res = match send_request(stdin, stdout, req, &mut buf) {
|
||||||
.map_err(|_| tt::ExpansionError::Unknown("proc macro server crashed".into()))?;
|
Ok(res) => res,
|
||||||
|
Err(err) => {
|
||||||
|
let mut process = self.process.lock().unwrap_or_else(|e| e.into_inner());
|
||||||
|
log::error!(
|
||||||
|
"proc macro server crashed, server process state: {:?}, server request error: {:?}",
|
||||||
|
process.child.try_wait(),
|
||||||
|
err
|
||||||
|
);
|
||||||
|
let res = Response::Error(ResponseError {
|
||||||
|
code: ErrorCode::ServerErrorEnd,
|
||||||
|
message: "proc macro server crashed".into(),
|
||||||
|
});
|
||||||
|
Some(res)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
match res {
|
match res {
|
||||||
Some(Response::Error(err)) => Err(tt::ExpansionError::ExpansionError(err.message)),
|
Some(Response::Error(err)) => Err(tt::ExpansionError::ExpansionError(err.message)),
|
||||||
|
@ -88,37 +90,7 @@ impl ProcMacroProcessSrv {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn client_loop(task_rx: Receiver<Task>, mut process: Process) {
|
#[derive(Debug)]
|
||||||
let (mut stdin, mut stdout) = process.stdio().expect("couldn't access child stdio");
|
|
||||||
|
|
||||||
let mut buf = String::new();
|
|
||||||
|
|
||||||
for Task { req, result_tx } in task_rx {
|
|
||||||
match send_request(&mut stdin, &mut stdout, req, &mut buf) {
|
|
||||||
Ok(res) => result_tx.send(res).unwrap(),
|
|
||||||
Err(err) => {
|
|
||||||
log::error!(
|
|
||||||
"proc macro server crashed, server process state: {:?}, server request error: {:?}",
|
|
||||||
process.child.try_wait(),
|
|
||||||
err
|
|
||||||
);
|
|
||||||
let res = Response::Error(ResponseError {
|
|
||||||
code: ErrorCode::ServerErrorEnd,
|
|
||||||
message: "proc macro server crashed".into(),
|
|
||||||
});
|
|
||||||
result_tx.send(res.into()).unwrap();
|
|
||||||
// Exit the thread.
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Task {
|
|
||||||
req: Request,
|
|
||||||
result_tx: Sender<Option<Response>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Process {
|
struct Process {
|
||||||
child: JodChild,
|
child: JodChild,
|
||||||
}
|
}
|
||||||
|
@ -133,7 +105,7 @@ impl Process {
|
||||||
Ok(Process { child })
|
Ok(Process { child })
|
||||||
}
|
}
|
||||||
|
|
||||||
fn stdio(&mut self) -> Option<(impl Write, impl BufRead)> {
|
fn stdio(&mut self) -> Option<(ChildStdin, BufReader<ChildStdout>)> {
|
||||||
let stdin = self.child.stdin.take()?;
|
let stdin = self.child.stdin.take()?;
|
||||||
let stdout = self.child.stdout.take()?;
|
let stdout = self.child.stdout.take()?;
|
||||||
let read = BufReader::new(stdout);
|
let read = BufReader::new(stdout);
|
||||||
|
|
|
@ -111,6 +111,7 @@ pub fn defer<F: FnOnce()>(f: F) -> impl Drop {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg_attr(not(target_arch = "wasm32"), repr(transparent))]
|
#[cfg_attr(not(target_arch = "wasm32"), repr(transparent))]
|
||||||
|
#[derive(Debug)]
|
||||||
pub struct JodChild(pub std::process::Child);
|
pub struct JodChild(pub std::process::Child);
|
||||||
|
|
||||||
impl ops::Deref for JodChild {
|
impl ops::Deref for JodChild {
|
||||||
|
|
Loading…
Reference in a new issue