Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/proc-macro-api/src/process.rs')
| -rw-r--r-- | crates/proc-macro-api/src/process.rs | 65 |
1 files changed, 22 insertions, 43 deletions
diff --git a/crates/proc-macro-api/src/process.rs b/crates/proc-macro-api/src/process.rs index 30877c5cf4..a41bb58e74 100644 --- a/crates/proc-macro-api/src/process.rs +++ b/crates/proc-macro-api/src/process.rs @@ -5,7 +5,10 @@ use std::{ io::{self, BufRead, BufReader, Read, Write}, panic::AssertUnwindSafe, process::{Child, ChildStdin, ChildStdout, Command, Stdio}, - sync::{Arc, Mutex, OnceLock}, + sync::{ + Arc, Mutex, OnceLock, + atomic::{AtomicBool, Ordering}, + }, }; use paths::AbsPath; @@ -29,7 +32,7 @@ pub(crate) struct ProcMacroServerProcess { protocol: Protocol, /// Populated when the server exits. exited: OnceLock<AssertUnwindSafe<ServerError>>, - single_use: bool, + can_use: AtomicBool, } impl std::fmt::Debug for ProcMacroServerProcess { @@ -149,11 +152,7 @@ impl ProcMacroServerProcess { let (process, stdin, stdout) = spawn(format)?; io::Result::Ok(ProcMacroServerProcess { - state: Mutex::new(ProcessSrvState { - process, - stdin, - stdout, - }), + state: Mutex::new(ProcessSrvState { process, stdin, stdout }), version: 0, protocol: match format { Some(ProtocolFormat::BidirectionalPostcardPrototype) => { @@ -164,7 +163,7 @@ impl ProcMacroServerProcess { } }, exited: OnceLock::new(), - single_use, + can_use: AtomicBool::new(true), }) }; let mut srv = create_srv()?; @@ -204,34 +203,20 @@ impl ProcMacroServerProcess { Err(err.unwrap()) } - pub(crate) fn load_dylib( + /// Finds proc-macros in a given dynamic library. + pub(crate) fn find_proc_macros( &self, dylib_path: &AbsPath, callback: Option<SubCallback<'_>>, - ) -> Result<Vec<(String, ProcMacroKind)>, ServerError> { - let _state = self.state.lock().unwrap(); - - // if state.loaded_dylibs.contains(dylib_path) { - // // Already loaded in this worker - // return Ok(Vec::new()); - // } - - let result = match self.protocol { + ) -> Result<Result<Vec<(String, ProcMacroKind)>, String>, ServerError> { + match self.protocol { Protocol::LegacyJson { .. } | Protocol::LegacyPostcard { .. } => { - legacy_protocol::find_proc_macros(self, dylib_path)? + legacy_protocol::find_proc_macros(self, dylib_path) } Protocol::BidirectionalPostcardPrototype { .. } => { - let cb = callback.expect("callback required"); - bidirectional_protocol::find_proc_macros(self, dylib_path, cb)? - } - }; - - match result { - Ok(macros) => { - // state.loaded_dylibs.insert(dylib_path.to_owned()); - Ok(macros) + let cb = callback.expect("callback required for bidirectional protocol"); + bidirectional_protocol::find_proc_macros(self, dylib_path, cb) } - Err(message) => Err(ServerError { message, io: None }), } } @@ -290,9 +275,11 @@ impl ProcMacroServerProcess { current_dir: String, callback: Option<SubCallback<'_>>, ) -> Result<Result<tt::TopSubtree, String>, ServerError> { + self.can_use.store(false, Ordering::Release); let result = match self.protocol { Protocol::LegacyJson { .. } => legacy_protocol::expand( proc_macro, + self, subtree, attr, env, @@ -303,6 +290,7 @@ impl ProcMacroServerProcess { ), Protocol::BidirectionalPostcardPrototype { .. } => bidirectional_protocol::expand( proc_macro, + self, subtree, attr, env, @@ -314,23 +302,10 @@ impl ProcMacroServerProcess { ), }; - if !self.is_reusable() { - self.terminate(); - } - + self.can_use.store(true, Ordering::Release); result } - fn is_reusable(&self) -> bool { - self.single_use - } - - fn terminate(&self) { - if let Ok(mut state) = self.state.lock() { - let _ = state.process.child.kill(); - } - } - pub(crate) fn send_task<Request, Response, C: Codec>( &self, send: impl FnOnce( @@ -391,6 +366,10 @@ impl ProcMacroServerProcess { bidirectional_protocol::run_conversation::<C>(writer, reader, buf, initial, callback) }) } + + pub(crate) fn can_use(&self) -> bool { + self.can_use.load(Ordering::Acquire) + } } /// Manages the execution of the proc-macro server process. |