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()
.name("ProcMacroClientThread".to_owned())
.spawn(move || {
client_loop(task_rx, process); 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,7 +181,9 @@ 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()
.name("DispatchThread".to_owned())
.spawn(move || {
let mut dispatch = |b| { let mut dispatch = |b| {
req_tx.send(b).unwrap(); req_tx.send(b).unwrap();
res_rx.recv().unwrap() res_rx.recv().unwrap()
@ -195,7 +197,8 @@ impl ExecutionStrategy for CrossThread1 {
}, },
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,7 +230,9 @@ 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()
.name("ServerThread".to_owned())
.spawn(move || {
let mut dispatch = |b| { let mut dispatch = |b| {
*state2.lock().unwrap() = State::Req(b); *state2.lock().unwrap() = State::Req(b);
server_thread.unpark(); server_thread.unpark();
@ -253,7 +258,8 @@ impl ExecutionStrategy for CrossThread2 {
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) {