@@ -11,6 +11,50 @@ use crate::task::executor::Sleepers;
1111use crate :: task:: Runnable ;
1212use 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.
1559struct Pool {
1660 /// The global queue of tasks.
@@ -25,7 +69,8 @@ struct Pool {
2569
2670/// Global executor that runs spawned tasks.
2771static 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) ;
0 commit comments