mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-27 20:42:04 +00:00
Use a weak ptr to hold the send end of channel
This commit is contained in:
parent
b929d05c74
commit
f461dc48d1
1 changed files with 18 additions and 28 deletions
|
@ -12,32 +12,24 @@ use std::{
|
||||||
io::{self, Write},
|
io::{self, Write},
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
process::{Child, Command, Stdio},
|
process::{Child, Command, Stdio},
|
||||||
|
sync::{Arc, Weak},
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
pub(crate) struct ProcMacroProcessSrv {
|
pub(crate) struct ProcMacroProcessSrv {
|
||||||
inner: Option<Sender<Task>>,
|
inner: Option<Weak<Sender<Task>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub(crate) struct ProcMacroProcessThread {
|
pub(crate) struct ProcMacroProcessThread {
|
||||||
// XXX: drop order is significant
|
// XXX: drop order is significant
|
||||||
sender: SenderGuard,
|
sender: Arc<Sender<Task>>,
|
||||||
handle: jod_thread::JoinHandle<()>,
|
handle: jod_thread::JoinHandle<()>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
struct Task {
|
||||||
struct SenderGuard(pub Sender<Task>);
|
req: Request,
|
||||||
|
result_tx: Sender<Response>,
|
||||||
impl std::ops::Drop for SenderGuard {
|
|
||||||
fn drop(&mut self) {
|
|
||||||
self.0.send(Task::Close).unwrap();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
enum Task {
|
|
||||||
Request { req: Request, result_tx: Sender<Response> },
|
|
||||||
Close,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Process {
|
struct Process {
|
||||||
|
@ -88,8 +80,9 @@ impl ProcMacroProcessSrv {
|
||||||
client_loop(task_rx, process);
|
client_loop(task_rx, process);
|
||||||
});
|
});
|
||||||
|
|
||||||
let srv = ProcMacroProcessSrv { inner: Some(task_tx.clone()) };
|
let task_tx = Arc::new(task_tx);
|
||||||
let thread = ProcMacroProcessThread { handle, sender: SenderGuard(task_tx) };
|
let srv = ProcMacroProcessSrv { inner: Some(Arc::downgrade(&task_tx)) };
|
||||||
|
let thread = ProcMacroProcessThread { handle, sender: task_tx };
|
||||||
|
|
||||||
Ok((thread, srv))
|
Ok((thread, srv))
|
||||||
}
|
}
|
||||||
|
@ -131,8 +124,13 @@ impl ProcMacroProcessSrv {
|
||||||
};
|
};
|
||||||
|
|
||||||
let (result_tx, result_rx) = bounded(0);
|
let (result_tx, result_rx) = bounded(0);
|
||||||
|
let sender = match sender.upgrade() {
|
||||||
sender.send(Task::Request { req: req.into(), result_tx }).unwrap();
|
None => {
|
||||||
|
return Err(ra_tt::ExpansionError::Unknown("Proc macro process is closed.".into()))
|
||||||
|
}
|
||||||
|
Some(it) => it,
|
||||||
|
};
|
||||||
|
sender.send(Task { req: req.into(), result_tx }).unwrap();
|
||||||
|
|
||||||
let res = result_rx.recv().unwrap();
|
let res = result_rx.recv().unwrap();
|
||||||
match res {
|
match res {
|
||||||
|
@ -155,16 +153,8 @@ fn client_loop(task_rx: Receiver<Task>, mut process: Process) {
|
||||||
Some(it) => it,
|
Some(it) => it,
|
||||||
};
|
};
|
||||||
|
|
||||||
loop {
|
for task in task_rx {
|
||||||
let task = match task_rx.recv() {
|
let Task { req, result_tx } = task;
|
||||||
Ok(task) => task,
|
|
||||||
Err(_) => break,
|
|
||||||
};
|
|
||||||
|
|
||||||
let (req, result_tx) = match task {
|
|
||||||
Task::Request { req, result_tx } => (req, result_tx),
|
|
||||||
Task::Close => break,
|
|
||||||
};
|
|
||||||
|
|
||||||
let res = match send_request(&mut stdin, &mut stdout, req) {
|
let res = match send_request(&mut stdin, &mut stdout, req) {
|
||||||
Ok(res) => res,
|
Ok(res) => res,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue