From 5e6eee5f631e354ba3323e7237bf35d24d7741e9 Mon Sep 17 00:00:00 2001 From: Manas Date: Wed, 7 Jul 2021 22:18:36 +0530 Subject: [PATCH] Explicitly name all spawned threads The thread name is shown in debugger as well as panic messages and this patch makes it easier to follow a thread instead of looking through full backtrace, by naming all spawned threads according to their functioning. --- crates/flycheck/src/lib.rs | 10 ++- crates/proc_macro_api/src/process.rs | 9 ++- .../src/proc_macro/bridge/server.rs | 80 ++++++++++--------- crates/vfs-notify/src/lib.rs | 5 +- 4 files changed, 61 insertions(+), 43 deletions(-) diff --git a/crates/flycheck/src/lib.rs b/crates/flycheck/src/lib.rs index 93cf6a3d68..e55f74bc45 100644 --- a/crates/flycheck/src/lib.rs +++ b/crates/flycheck/src/lib.rs @@ -67,7 +67,10 @@ impl FlycheckHandle { ) -> FlycheckHandle { let actor = FlycheckActor::new(id, sender, config, workspace_root); let (sender, receiver) = unbounded::(); - let thread = jod_thread::spawn(move || actor.run(receiver)); + let thread = jod_thread::Builder::new() + .name("FlycheckThread".to_owned()) + .spawn(move || actor.run(receiver)) + .expect("failed to spawn thread"); FlycheckHandle { sender, thread } } @@ -266,7 +269,10 @@ impl CargoHandle { let child_stdout = child.stdout.take().unwrap(); let (sender, receiver) = unbounded(); let actor = CargoActor::new(child_stdout, sender); - let thread = jod_thread::spawn(move || actor.run()); + let thread = jod_thread::Builder::new() + .name("CargoHandleThread".to_owned()) + .spawn(move || actor.run()) + .expect("failed to spawn thread"); CargoHandle { child, thread, receiver } } fn join(mut self) -> io::Result<()> { diff --git a/crates/proc_macro_api/src/process.rs b/crates/proc_macro_api/src/process.rs index 38eac6c17f..19f854dba1 100644 --- a/crates/proc_macro_api/src/process.rs +++ b/crates/proc_macro_api/src/process.rs @@ -37,9 +37,12 @@ impl ProcMacroProcessSrv { let process = Process::run(process_path, args)?; let (task_tx, task_rx) = bounded(0); - let handle = jod_thread::spawn(move || { - client_loop(task_rx, process); - }); + let handle = jod_thread::Builder::new() + .name("ProcMacroClientThread".to_owned()) + .spawn(move || { + client_loop(task_rx, process); + }) + .expect("failed to spawn thread"); let task_tx = Arc::new(task_tx); let srv = ProcMacroProcessSrv { inner: Arc::downgrade(&task_tx) }; diff --git a/crates/proc_macro_srv/src/proc_macro/bridge/server.rs b/crates/proc_macro_srv/src/proc_macro/bridge/server.rs index cc9afd84b1..29b340445e 100644 --- a/crates/proc_macro_srv/src/proc_macro/bridge/server.rs +++ b/crates/proc_macro_srv/src/proc_macro/bridge/server.rs @@ -181,21 +181,24 @@ impl ExecutionStrategy for CrossThread1 { let (req_tx, req_rx) = channel(); let (res_tx, res_rx) = channel(); - let join_handle = thread::spawn(move || { - let mut dispatch = |b| { - req_tx.send(b).unwrap(); - res_rx.recv().unwrap() - }; + let join_handle = thread::Builder::new() + .name("DispatchThread".to_owned()) + .spawn(move || { + let mut dispatch = |b| { + req_tx.send(b).unwrap(); + res_rx.recv().unwrap() + }; - run_client( - Bridge { - cached_buffer: input, - dispatch: (&mut dispatch).into(), - force_show_panics, - }, - client_data, - ) - }); + run_client( + Bridge { + cached_buffer: input, + dispatch: (&mut dispatch).into(), + force_show_panics, + }, + client_data, + ) + }) + .expect("failed to spawn thread"); for b in req_rx { res_tx.send(dispatcher.dispatch(b)).unwrap(); @@ -227,33 +230,36 @@ impl ExecutionStrategy for CrossThread2 { let server_thread = thread::current(); let state2 = state.clone(); - let join_handle = thread::spawn(move || { - let mut dispatch = |b| { - *state2.lock().unwrap() = State::Req(b); - server_thread.unpark(); - loop { - thread::park(); - if let State::Res(b) = &mut *state2.lock().unwrap() { - break b.take(); + let join_handle = thread::Builder::new() + .name("ServerThread".to_owned()) + .spawn(move || { + let mut dispatch = |b| { + *state2.lock().unwrap() = State::Req(b); + server_thread.unpark(); + loop { + thread::park(); + if let State::Res(b) = &mut *state2.lock().unwrap() { + break b.take(); + } } - } - }; + }; - let r = run_client( - Bridge { - cached_buffer: input, - dispatch: (&mut dispatch).into(), - force_show_panics, - }, - client_data, - ); + let r = run_client( + Bridge { + cached_buffer: input, + dispatch: (&mut dispatch).into(), + force_show_panics, + }, + client_data, + ); - // Wake up the server so it can exit the dispatch loop. - drop(state2); - server_thread.unpark(); + // Wake up the server so it can exit the dispatch loop. + drop(state2); + server_thread.unpark(); - r - }); + r + }) + .expect("failed to spawn thread"); // Check whether `state2` was dropped, to know when to stop. while Arc::get_mut(&mut state).is_none() { diff --git a/crates/vfs-notify/src/lib.rs b/crates/vfs-notify/src/lib.rs index f7ae0577d2..ca341ddae9 100644 --- a/crates/vfs-notify/src/lib.rs +++ b/crates/vfs-notify/src/lib.rs @@ -31,7 +31,10 @@ impl loader::Handle for NotifyHandle { fn spawn(sender: loader::Sender) -> NotifyHandle { let actor = NotifyActor::new(sender); let (sender, receiver) = unbounded::(); - let thread = jod_thread::spawn(move || actor.run(receiver)); + let thread = jod_thread::Builder::new() + .name("LoaderThread".to_owned()) + .spawn(move || actor.run(receiver)) + .expect("failed to spawn thread"); NotifyHandle { sender, thread } } fn set_config(&mut self, config: loader::Config) {