Skip to content

Commit

Permalink
add thread_name_fn method to runtime::Builder, close #1907.
Browse files Browse the repository at this point in the history
  • Loading branch information
biluohc committed Dec 7, 2019
1 parent 80abff0 commit 261c9df
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
5 changes: 3 additions & 2 deletions tokio/src/runtime/blocking/pool.rs
Expand Up @@ -6,6 +6,7 @@ use crate::runtime::{self, io, time, Builder, Callback};
use crate::runtime::blocking::shutdown;
use crate::runtime::blocking::schedule::NoopSchedule;
use crate::runtime::blocking::task::BlockingTask;
use crate::runtime::builder::ThreadNameFn;
use crate::task::{self, JoinHandle};

use std::cell::Cell;
Expand All @@ -31,7 +32,7 @@ struct Inner {
condvar: Condvar,

/// Spawned threads use this name
thread_name: String,
thread_name: ThreadNameFn,

/// Spawned thread stack size
stack_size: Option<usize>,
Expand Down Expand Up @@ -236,7 +237,7 @@ impl Spawner {
}

fn spawn_thread(&self, shutdown_tx: shutdown::Sender) {
let mut builder = thread::Builder::new().name(self.inner.thread_name.clone());
let mut builder = thread::Builder::new().name((self.inner.thread_name)());

if let Some(stack_size) = self.inner.stack_size {
builder = builder.stack_size(stack_size);
Expand Down
41 changes: 36 additions & 5 deletions tokio/src/runtime/builder.rs
Expand Up @@ -52,8 +52,8 @@ pub struct Builder {
/// Only used when not using the current-thread executor.
num_threads: usize,

/// Name used for threads spawned by the runtime.
pub(super) thread_name: String,
/// Name fn used for threads spawned by the runtime.
pub(super) thread_name: ThreadNameFn,

/// Stack size used for threads spawned by the runtime.
pub(super) thread_stack_size: Option<usize>,
Expand All @@ -65,6 +65,8 @@ pub struct Builder {
pub(super) before_stop: Option<Callback>,
}

pub(crate) type ThreadNameFn = Arc<dyn Fn() -> String + Send + Sync + 'static>;

#[derive(Debug, Clone, Copy)]
enum Kind {
Shell,
Expand Down Expand Up @@ -94,7 +96,7 @@ impl Builder {
num_threads: crate::loom::sys::num_cpus(),

// Default thread name
thread_name: "tokio-runtime-worker".into(),
thread_name: Arc::new(|| "tokio-runtime-worker".into()),

// Do not set a stack size by default
thread_stack_size: None,
Expand Down Expand Up @@ -168,7 +170,36 @@ impl Builder {
/// # }
/// ```
pub fn thread_name(&mut self, val: impl Into<String>) -> &mut Self {
self.thread_name = val.into();
let val = val.into();
self.thread_name = Arc::new(move || val.clone());
self
}

/// Set the name fn of threads spawned by the `Runtime`'s thread pool.
///
/// The default name fn is `|| "tokio-runtime-worker".into()`.
///
/// # Examples
///
/// ```
/// # use tokio::runtime;
/// # use std::sync::atomic::{AtomicUsize, Ordering};
///
/// # pub fn main() {
/// let rt = runtime::Builder::new()
/// .thread_name_fn(|| {
/// static ATOMIC_ID: AtomicUsize = AtomicUsize::new(0);
/// let id = ATOMIC_ID.fetch_add(1, Ordering::SeqCst);
/// format!("my-pool-{}", id)
/// })
/// .build();
/// # }
/// ```
pub fn thread_name_fn<F>(&mut self, f: F) -> &mut Self
where
F: Fn() -> String + Send + Sync + 'static,
{
self.thread_name = Arc::new(f);
self
}

Expand Down Expand Up @@ -449,7 +480,7 @@ impl fmt::Debug for Builder {
fmt.debug_struct("Builder")
.field("kind", &self.kind)
.field("num_threads", &self.num_threads)
.field("thread_name", &self.thread_name)
.field("thread_name_fn", &"<dyn Fn()->String+Send+Sync+'static>")
.field("thread_stack_size", &self.thread_stack_size)
.field("after_start", &self.after_start.as_ref().map(|_| "..."))
.field("before_stop", &self.after_start.as_ref().map(|_| "..."))
Expand Down

0 comments on commit 261c9df

Please sign in to comment.