Skip to content

Commit 63011fe

Browse files
author
Michael Ward
committed
created RuntimeConfig to allow for configuring the number of threads, as well as thread name, used by POOL in task::executor
1 parent 125fa5b commit 63011fe

File tree

4 files changed

+55
-2
lines changed

4 files changed

+55
-2
lines changed

Diff for: examples/a-chat/server.rs

+6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use async_std::{
1010
net::{TcpListener, TcpStream, ToSocketAddrs},
1111
prelude::*,
1212
task,
13+
task::RuntimeConfig,
1314
};
1415

1516
type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;
@@ -20,6 +21,11 @@ type Receiver<T> = mpsc::UnboundedReceiver<T>;
2021
enum Void {}
2122

2223
pub(crate) fn main() -> Result<()> {
24+
let mut config = RuntimeConfig::new();
25+
let num_threads = num_cpus::get() / 2;
26+
config.num_thread(num_threads).thread_name("a-chat-server");
27+
assert!(config.finalize().is_ok());
28+
2329
task::block_on(accept_loop("127.0.0.1:8080"))
2430
}
2531

Diff for: src/task/executor/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//! * The only import is the `crate::task::Runnable` type.
77
88
pub(crate) use pool::schedule;
9+
pub use pool::RuntimeConfig;
910

1011
use sleepers::Sleepers;
1112

Diff for: src/task/executor/pool.rs

+47-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,50 @@ use crate::task::executor::Sleepers;
1111
use crate::task::Runnable;
1212
use crate::utils::{abort_on_panic, random};
1313

14+
type SyncOnceCell<T> = once_cell::sync::OnceCell<T>;
15+
static RUNTIME_CONFIG: SyncOnceCell<RuntimeConfig> = SyncOnceCell::new();
16+
17+
/// configuration parameters for executor
18+
#[derive(Debug)]
19+
pub struct RuntimeConfig {
20+
/// Name given to created worker threads
21+
pub thread_name: String,
22+
23+
/// Number of threads executor is allowed to create
24+
pub num_threads: usize,
25+
}
26+
impl Default for RuntimeConfig {
27+
fn default() -> Self {
28+
Self {
29+
thread_name: "async-std/executor".to_string(),
30+
num_threads: num_cpus::get(),
31+
}
32+
}
33+
}
34+
impl RuntimeConfig {
35+
/// Creates new config with predefined defaults
36+
pub fn new() -> RuntimeConfig {
37+
RuntimeConfig::default()
38+
}
39+
40+
/// Configures name given to worker threads
41+
pub fn thread_name(&mut self, thread_name: impl Into<String>) -> &mut Self {
42+
self.thread_name = thread_name.into();
43+
self
44+
}
45+
46+
/// Configures number of worker threads
47+
pub fn num_thread(&mut self, num_threads: usize) -> &mut Self {
48+
self.num_threads = num_threads;
49+
self
50+
}
51+
52+
/// Sets `RUNTIME_CONFIG` with self
53+
pub fn finalize(self) -> Result<(), RuntimeConfig> {
54+
RUNTIME_CONFIG.set(self)
55+
}
56+
}
57+
1458
/// The state of an executor.
1559
struct Pool {
1660
/// The global queue of tasks.
@@ -25,7 +69,8 @@ struct Pool {
2569

2670
/// Global executor that runs spawned tasks.
2771
static POOL: Lazy<Pool> = Lazy::new(|| {
28-
let num_threads = num_cpus::get().max(1);
72+
let runtime_config = RUNTIME_CONFIG.get_or_init(|| RuntimeConfig::default());
73+
let num_threads = runtime_config.num_threads.max(1);
2974
let mut stealers = Vec::new();
3075

3176
// Spawn worker threads.
@@ -40,7 +85,7 @@ static POOL: Lazy<Pool> = Lazy::new(|| {
4085
};
4186

4287
thread::Builder::new()
43-
.name("async-std/executor".to_string())
88+
.name(runtime_config.thread_name.clone())
4489
.spawn(|| {
4590
let _ = PROCESSOR.with(|p| p.set(proc));
4691
abort_on_panic(main_loop);

Diff for: src/task/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ cfg_default! {
140140
pub use sleep::sleep;
141141
pub use spawn::spawn;
142142
pub use task_local::{AccessError, LocalKey};
143+
pub use executor::RuntimeConfig;
143144

144145
use builder::Runnable;
145146
use task_local::LocalsMap;

0 commit comments

Comments
 (0)