mirror of
https://seed.radicle.dev/rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5.git
synced 2026-11-21 18:40:39 +08:00
For the purposes of debugging running nodes, it's useful to know which threads are doing what. This allows tools like `htop` to reveal which threads are running.
47 lines
1.1 KiB
Rust
47 lines
1.1 KiB
Rust
use std::thread;
|
|
|
|
pub use thread::*;
|
|
|
|
use radicle::prelude::NodeId;
|
|
|
|
/// Spawn an OS thread.
|
|
pub fn spawn<D, F, T>(nid: &NodeId, label: D, f: F) -> thread::JoinHandle<T>
|
|
where
|
|
D: std::fmt::Display,
|
|
F: FnOnce() -> T,
|
|
F: Send + 'static,
|
|
T: Send + 'static,
|
|
{
|
|
thread::Builder::new()
|
|
.name(name(nid, label))
|
|
.spawn(f)
|
|
.expect("thread::spawn: thread label must not contain NULL bytes")
|
|
}
|
|
|
|
/// Spawn a scoped OS thread.
|
|
pub fn spawn_scoped<'scope, 'env, D, F, T>(
|
|
nid: &NodeId,
|
|
label: D,
|
|
scope: &'scope thread::Scope<'scope, 'env>,
|
|
f: F,
|
|
) -> thread::ScopedJoinHandle<'scope, T>
|
|
where
|
|
D: std::fmt::Display,
|
|
F: FnOnce() -> T,
|
|
F: Send + 'scope,
|
|
T: Send + 'scope,
|
|
{
|
|
thread::Builder::new()
|
|
.name(name(nid, label))
|
|
.spawn_scoped(scope, f)
|
|
.expect("thread::spawn_scoped: thread label must not contain NULL bytes")
|
|
}
|
|
|
|
pub fn name<D: std::fmt::Display>(nid: &NodeId, label: D) -> String {
|
|
if cfg!(debug_assertions) {
|
|
format!("{nid} {:<14}", format!("<{label}>"))
|
|
} else {
|
|
format!("{label}")
|
|
}
|
|
}
|