Skip to content

Commit 5e6eee5

Browse files
committed
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.
1 parent 2b6770c commit 5e6eee5

File tree

4 files changed

+64
-46
lines changed

4 files changed

+64
-46
lines changed

crates/flycheck/src/lib.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,10 @@ impl FlycheckHandle {
6767
) -> FlycheckHandle {
6868
let actor = FlycheckActor::new(id, sender, config, workspace_root);
6969
let (sender, receiver) = unbounded::<Restart>();
70-
let thread = jod_thread::spawn(move || actor.run(receiver));
70+
let thread = jod_thread::Builder::new()
71+
.name("FlycheckThread".to_owned())
72+
.spawn(move || actor.run(receiver))
73+
.expect("failed to spawn thread");
7174
FlycheckHandle { sender, thread }
7275
}
7376

@@ -266,7 +269,10 @@ impl CargoHandle {
266269
let child_stdout = child.stdout.take().unwrap();
267270
let (sender, receiver) = unbounded();
268271
let actor = CargoActor::new(child_stdout, sender);
269-
let thread = jod_thread::spawn(move || actor.run());
272+
let thread = jod_thread::Builder::new()
273+
.name("CargoHandleThread".to_owned())
274+
.spawn(move || actor.run())
275+
.expect("failed to spawn thread");
270276
CargoHandle { child, thread, receiver }
271277
}
272278
fn join(mut self) -> io::Result<()> {

crates/proc_macro_api/src/process.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,12 @@ impl ProcMacroProcessSrv {
3737
let process = Process::run(process_path, args)?;
3838

3939
let (task_tx, task_rx) = bounded(0);
40-
let handle = jod_thread::spawn(move || {
41-
client_loop(task_rx, process);
42-
});
40+
let handle = jod_thread::Builder::new()
41+
.name("ProcMacroClientThread".to_owned())
42+
.spawn(move || {
43+
client_loop(task_rx, process);
44+
})
45+
.expect("failed to spawn thread");
4346

4447
let task_tx = Arc::new(task_tx);
4548
let srv = ProcMacroProcessSrv { inner: Arc::downgrade(&task_tx) };

crates/proc_macro_srv/src/proc_macro/bridge/server.rs

Lines changed: 46 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -181,21 +181,24 @@ impl ExecutionStrategy for CrossThread1 {
181181
let (req_tx, req_rx) = channel();
182182
let (res_tx, res_rx) = channel();
183183

184-
let join_handle = thread::spawn(move || {
185-
let mut dispatch = |b| {
186-
req_tx.send(b).unwrap();
187-
res_rx.recv().unwrap()
188-
};
189-
190-
run_client(
191-
Bridge {
192-
cached_buffer: input,
193-
dispatch: (&mut dispatch).into(),
194-
force_show_panics,
195-
},
196-
client_data,
197-
)
198-
});
184+
let join_handle = thread::Builder::new()
185+
.name("DispatchThread".to_owned())
186+
.spawn(move || {
187+
let mut dispatch = |b| {
188+
req_tx.send(b).unwrap();
189+
res_rx.recv().unwrap()
190+
};
191+
192+
run_client(
193+
Bridge {
194+
cached_buffer: input,
195+
dispatch: (&mut dispatch).into(),
196+
force_show_panics,
197+
},
198+
client_data,
199+
)
200+
})
201+
.expect("failed to spawn thread");
199202

200203
for b in req_rx {
201204
res_tx.send(dispatcher.dispatch(b)).unwrap();
@@ -227,33 +230,36 @@ impl ExecutionStrategy for CrossThread2 {
227230

228231
let server_thread = thread::current();
229232
let state2 = state.clone();
230-
let join_handle = thread::spawn(move || {
231-
let mut dispatch = |b| {
232-
*state2.lock().unwrap() = State::Req(b);
233-
server_thread.unpark();
234-
loop {
235-
thread::park();
236-
if let State::Res(b) = &mut *state2.lock().unwrap() {
237-
break b.take();
233+
let join_handle = thread::Builder::new()
234+
.name("ServerThread".to_owned())
235+
.spawn(move || {
236+
let mut dispatch = |b| {
237+
*state2.lock().unwrap() = State::Req(b);
238+
server_thread.unpark();
239+
loop {
240+
thread::park();
241+
if let State::Res(b) = &mut *state2.lock().unwrap() {
242+
break b.take();
243+
}
238244
}
239-
}
240-
};
245+
};
246+
247+
let r = run_client(
248+
Bridge {
249+
cached_buffer: input,
250+
dispatch: (&mut dispatch).into(),
251+
force_show_panics,
252+
},
253+
client_data,
254+
);
255+
256+
// Wake up the server so it can exit the dispatch loop.
257+
drop(state2);
258+
server_thread.unpark();
241259

242-
let r = run_client(
243-
Bridge {
244-
cached_buffer: input,
245-
dispatch: (&mut dispatch).into(),
246-
force_show_panics,
247-
},
248-
client_data,
249-
);
250-
251-
// Wake up the server so it can exit the dispatch loop.
252-
drop(state2);
253-
server_thread.unpark();
254-
255-
r
256-
});
260+
r
261+
})
262+
.expect("failed to spawn thread");
257263

258264
// Check whether `state2` was dropped, to know when to stop.
259265
while Arc::get_mut(&mut state).is_none() {

crates/vfs-notify/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ impl loader::Handle for NotifyHandle {
3131
fn spawn(sender: loader::Sender) -> NotifyHandle {
3232
let actor = NotifyActor::new(sender);
3333
let (sender, receiver) = unbounded::<Message>();
34-
let thread = jod_thread::spawn(move || actor.run(receiver));
34+
let thread = jod_thread::Builder::new()
35+
.name("LoaderThread".to_owned())
36+
.spawn(move || actor.run(receiver))
37+
.expect("failed to spawn thread");
3538
NotifyHandle { sender, thread }
3639
}
3740
fn set_config(&mut self, config: loader::Config) {

0 commit comments

Comments
 (0)