File tree Expand file tree Collapse file tree 2 files changed +37
-1
lines changed Expand file tree Collapse file tree 2 files changed +37
-1
lines changed Original file line number Diff line number Diff line change @@ -352,7 +352,6 @@ impl Registry {
352
352
353
353
/// Waits for the worker threads to stop. This is used for testing
354
354
/// -- so we can check that termination actually works.
355
- #[ cfg( test) ]
356
355
pub ( super ) fn wait_until_stopped ( & self ) {
357
356
for info in & self . thread_infos {
358
357
info. stopped . wait ( ) ;
Original file line number Diff line number Diff line change @@ -92,6 +92,43 @@ impl ThreadPool {
92
92
& DEFAULT_THREAD_POOL
93
93
}
94
94
95
+ /// Creates a scoped thread pool
96
+ pub fn scoped_pool < F , R , H > (
97
+ builder : ThreadPoolBuilder ,
98
+ main_handler : H ,
99
+ with_pool : F ,
100
+ ) -> Result < R , ThreadPoolBuildError >
101
+ where
102
+ F : FnOnce ( & ThreadPool ) -> R ,
103
+ H : Fn ( & mut FnMut ( ) ) + Send + Sync ,
104
+ {
105
+ struct Handler ( * const ( ) ) ;
106
+ unsafe impl Send for Handler { }
107
+ unsafe impl Sync for Handler { }
108
+
109
+ let handler = Handler ( & main_handler as * const _ as * const ( ) ) ;
110
+
111
+ let builder = builder. main_handler ( move |_, worker| {
112
+ let handler = unsafe { & * ( handler. 0 as * const H ) } ;
113
+ handler ( worker) ;
114
+ } ) ;
115
+
116
+ let pool = builder. build ( ) ?;
117
+
118
+ struct JoinRegistry ( Arc < Registry > ) ;
119
+
120
+ impl Drop for JoinRegistry {
121
+ fn drop ( & mut self ) {
122
+ self . 0 . terminate ( ) ;
123
+ self . 0 . wait_until_stopped ( ) ;
124
+ }
125
+ }
126
+
127
+ let _join_registry = JoinRegistry ( pool. registry . clone ( ) ) ;
128
+
129
+ Ok ( with_pool ( & pool) )
130
+ }
131
+
95
132
/// Executes `op` within the threadpool. Any attempts to use
96
133
/// `join`, `scope`, or parallel iterators will then operate
97
134
/// within that threadpool.
You can’t perform that action at this time.
0 commit comments