@@ -11,6 +11,50 @@ use crate::task::executor::Sleepers;
11
11
use crate :: task:: Runnable ;
12
12
use crate :: utils:: { abort_on_panic, random} ;
13
13
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
+
14
58
/// The state of an executor.
15
59
struct Pool {
16
60
/// The global queue of tasks.
@@ -25,7 +69,8 @@ struct Pool {
25
69
26
70
/// Global executor that runs spawned tasks.
27
71
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 ) ;
29
74
let mut stealers = Vec :: new ( ) ;
30
75
31
76
// Spawn worker threads.
@@ -40,7 +85,7 @@ static POOL: Lazy<Pool> = Lazy::new(|| {
40
85
} ;
41
86
42
87
thread:: Builder :: new ( )
43
- . name ( "async-std/executor" . to_string ( ) )
88
+ . name ( runtime_config . thread_name . clone ( ) )
44
89
. spawn ( || {
45
90
let _ = PROCESSOR . with ( |p| p. set ( proc) ) ;
46
91
abort_on_panic ( main_loop) ;
0 commit comments