@@ -156,6 +156,9 @@ pub struct ThreadPoolBuilder<S = DefaultSpawn> {
156
156
/// Closure invoked to spawn threads.
157
157
spawn_handler : S ,
158
158
159
+ /// Closure invoked on worker thread start.
160
+ main_handler : Option < Box < MainHandler > > ,
161
+
159
162
/// If false, worker threads will execute spawned jobs in a
160
163
/// "depth-first" fashion. If true, they will do a "breadth-first"
161
164
/// fashion. Depth-first is the default.
@@ -194,12 +197,19 @@ impl Default for ThreadPoolBuilder {
194
197
stack_size : None ,
195
198
start_handler : None ,
196
199
exit_handler : None ,
200
+ main_handler : None ,
197
201
spawn_handler : DefaultSpawn ,
198
202
breadth_first : false ,
199
203
}
200
204
}
201
205
}
202
206
207
+ /// The type for a closure that gets invoked with a
208
+ /// function which runs rayon tasks.
209
+ /// The closure is passed the index of the thread on which it is invoked.
210
+ /// Note that this same closure may be invoked multiple times in parallel.
211
+ type MainHandler = Fn ( usize , & mut FnMut ( ) ) + Send + Sync ;
212
+
203
213
impl ThreadPoolBuilder {
204
214
/// Creates and returns a valid rayon thread pool builder, but does not initialize it.
205
215
pub fn new ( ) -> Self {
@@ -380,6 +390,7 @@ impl<S> ThreadPoolBuilder<S> {
380
390
stack_size : self . stack_size ,
381
391
start_handler : self . start_handler ,
382
392
exit_handler : self . exit_handler ,
393
+ main_handler : self . main_handler ,
383
394
breadth_first : self . breadth_first ,
384
395
}
385
396
}
@@ -575,6 +586,24 @@ impl<S> ThreadPoolBuilder<S> {
575
586
self . exit_handler = Some ( Box :: new ( exit_handler) ) ;
576
587
self
577
588
}
589
+
590
+ /// Takes the current thread main callback, leaving `None`.
591
+ fn take_main_handler ( & mut self ) -> Option < Box < MainHandler > > {
592
+ self . main_handler . take ( )
593
+ }
594
+
595
+ /// Set a callback to be invoked on thread main.
596
+ ///
597
+ /// The closure is passed the index of the thread on which it is invoked.
598
+ /// Note that this same closure may be invoked multiple times in parallel.
599
+ /// If this closure panics, the panic will be passed to the panic handler.
600
+ pub fn main_handler < H > ( mut self , main_handler : H ) -> Self
601
+ where
602
+ H : Fn ( usize , & mut FnMut ( ) ) + Send + Sync + ' static ,
603
+ {
604
+ self . main_handler = Some ( Box :: new ( main_handler) ) ;
605
+ self
606
+ }
578
607
}
579
608
580
609
#[ allow( deprecated) ]
@@ -692,6 +721,7 @@ impl<S> fmt::Debug for ThreadPoolBuilder<S> {
692
721
ref panic_handler,
693
722
ref stack_size,
694
723
ref start_handler,
724
+ ref main_handler,
695
725
ref exit_handler,
696
726
spawn_handler : _,
697
727
ref breadth_first,
@@ -709,6 +739,7 @@ impl<S> fmt::Debug for ThreadPoolBuilder<S> {
709
739
let panic_handler = panic_handler. as_ref ( ) . map ( |_| ClosurePlaceholder ) ;
710
740
let start_handler = start_handler. as_ref ( ) . map ( |_| ClosurePlaceholder ) ;
711
741
let exit_handler = exit_handler. as_ref ( ) . map ( |_| ClosurePlaceholder ) ;
742
+ let main_handler = main_handler. as_ref ( ) . map ( |_| ClosurePlaceholder ) ;
712
743
713
744
f. debug_struct ( "ThreadPoolBuilder" )
714
745
. field ( "num_threads" , num_threads)
@@ -717,6 +748,7 @@ impl<S> fmt::Debug for ThreadPoolBuilder<S> {
717
748
. field ( "stack_size" , & stack_size)
718
749
. field ( "start_handler" , & start_handler)
719
750
. field ( "exit_handler" , & exit_handler)
751
+ . field ( "main_handler" , & main_handler)
720
752
. field ( "breadth_first" , & breadth_first)
721
753
. finish ( )
722
754
}
0 commit comments