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.
This commit is contained in:
Manas 2021-07-07 22:18:36 +05:30
parent 2b6770c936
commit 5e6eee5f63
4 changed files with 61 additions and 43 deletions

View file

@ -67,7 +67,10 @@ impl FlycheckHandle {
) -> FlycheckHandle { ) -> FlycheckHandle {
let actor = FlycheckActor::new(id, sender, config, workspace_root); let actor = FlycheckActor::new(id, sender, config, workspace_root);
let (sender, receiver) = unbounded::<Restart>(); let (sender, receiver) = unbounded::<Restart>();
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 } FlycheckHandle { sender, thread }
} }
@ -266,7 +269,10 @@ impl CargoHandle {
let child_stdout = child.stdout.take().unwrap(); let child_stdout = child.stdout.take().unwrap();
let (sender, receiver) = unbounded(); let (sender, receiver) = unbounded();
let actor = CargoActor::new(child_stdout, sender); 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 } CargoHandle { child, thread, receiver }
} }
fn join(mut self) -> io::Result<()> { fn join(mut self) -> io::Result<()> {

View file

@ -37,9 +37,12 @@ impl ProcMacroProcessSrv {
let process = Process::run(process_path, args)?; let process = Process::run(process_path, args)?;
let (task_tx, task_rx) = bounded(0); let (task_tx, task_rx) = bounded(0);
let handle = jod_thread::spawn(move || { let handle = jod_thread::Builder::new()
client_loop(task_rx, process); .name("ProcMacroClientThread".to_owned())
}); .spawn(move || {
client_loop(task_rx, process);
})
.expect("failed to spawn thread");
let task_tx = Arc::new(task_tx); let task_tx = Arc::new(task_tx);
let srv = ProcMacroProcessSrv { inner: Arc::downgrade(&task_tx) }; let srv = ProcMacroProcessSrv { inner: Arc::downgrade(&task_tx) };

View file

@ -181,21 +181,24 @@ impl ExecutionStrategy for CrossThread1 {
let (req_tx, req_rx) = channel(); let (req_tx, req_rx) = channel();
let (res_tx, res_rx) = channel(); let (res_tx, res_rx) = channel();
let join_handle = thread::spawn(move || { let join_handle = thread::Builder::new()
let mut dispatch = |b| { .name("DispatchThread".to_owned())
req_tx.send(b).unwrap(); .spawn(move || {
res_rx.recv().unwrap() let mut dispatch = |b| {
}; req_tx.send(b).unwrap();
res_rx.recv().unwrap()
};
run_client( run_client(
Bridge { Bridge {
cached_buffer: input, cached_buffer: input,
dispatch: (&mut dispatch).into(), dispatch: (&mut dispatch).into(),
force_show_panics, force_show_panics,
}, },
client_data, client_data,
) )
}); })
.expect("failed to spawn thread");
for b in req_rx { for b in req_rx {
res_tx.send(dispatcher.dispatch(b)).unwrap(); res_tx.send(dispatcher.dispatch(b)).unwrap();
@ -227,33 +230,36 @@ impl ExecutionStrategy for CrossThread2 {
let server_thread = thread::current(); let server_thread = thread::current();
let state2 = state.clone(); let state2 = state.clone();
let join_handle = thread::spawn(move || { let join_handle = thread::Builder::new()
let mut dispatch = |b| { .name("ServerThread".to_owned())
*state2.lock().unwrap() = State::Req(b); .spawn(move || {
server_thread.unpark(); let mut dispatch = |b| {
loop { *state2.lock().unwrap() = State::Req(b);
thread::park(); server_thread.unpark();
if let State::Res(b) = &mut *state2.lock().unwrap() { loop {
break b.take(); thread::park();
if let State::Res(b) = &mut *state2.lock().unwrap() {
break b.take();
}
} }
} };
};
let r = run_client( let r = run_client(
Bridge { Bridge {
cached_buffer: input, cached_buffer: input,
dispatch: (&mut dispatch).into(), dispatch: (&mut dispatch).into(),
force_show_panics, force_show_panics,
}, },
client_data, client_data,
); );
// Wake up the server so it can exit the dispatch loop. // Wake up the server so it can exit the dispatch loop.
drop(state2); drop(state2);
server_thread.unpark(); server_thread.unpark();
r r
}); })
.expect("failed to spawn thread");
// Check whether `state2` was dropped, to know when to stop. // Check whether `state2` was dropped, to know when to stop.
while Arc::get_mut(&mut state).is_none() { while Arc::get_mut(&mut state).is_none() {

View file

@ -31,7 +31,10 @@ impl loader::Handle for NotifyHandle {
fn spawn(sender: loader::Sender) -> NotifyHandle { fn spawn(sender: loader::Sender) -> NotifyHandle {
let actor = NotifyActor::new(sender); let actor = NotifyActor::new(sender);
let (sender, receiver) = unbounded::<Message>(); let (sender, receiver) = unbounded::<Message>();
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 } NotifyHandle { sender, thread }
} }
fn set_config(&mut self, config: loader::Config) { fn set_config(&mut self, config: loader::Config) {