@@ -137,36 +137,36 @@ fn run_in_thread_with_globals<F: FnOnce(CurrentGcx, Arc<Proxy>) -> R + Send, R:
137137 extra_symbols : & [ & ' static str ] ,
138138 f : F ,
139139) -> R {
140- // The "thread pool" is a single spawned thread in the non-parallel
141- // compiler. We run on a spawned thread instead of the main thread (a) to
140+ // For `WorkerLocal` to function properly we need to create a thread pool.
141+ // Also we run on a spawned thread instead of the main thread (a) to
142142 // provide control over the stack size, and (b) to increase similarity with
143143 // the parallel compiler, in particular to ensure there is no accidental
144144 // sharing of data between the main thread and the compilation thread
145145 // (which might cause problems for the parallel compiler).
146- let builder = thread:: Builder :: new ( ) . name ( "rustc" . to_string ( ) ) . stack_size ( thread_stack_size) ;
146+ let builder = rustc_thread_pool:: ThreadPoolBuilder :: new ( )
147+ . thread_name ( |_| "rustc" . to_string ( ) )
148+ . num_threads ( 1 )
149+ . stack_size ( thread_stack_size) ;
147150
148151 // We build the session globals and run `f` on the spawned thread, because
149152 // `SessionGlobals` does not impl `Send` in the non-parallel compiler.
150- thread:: scope ( |s| {
151- // `unwrap` is ok here because `spawn_scoped` only panics if the thread
152- // name contains null bytes.
153- let r = builder
154- . spawn_scoped ( s, move || {
155- rustc_span:: create_session_globals_then (
156- edition,
157- extra_symbols,
158- Some ( sm_inputs) ,
159- || f ( CurrentGcx :: new ( ) , Proxy :: new ( ) ) ,
160- )
161- } )
162- . unwrap ( )
163- . join ( ) ;
164-
165- match r {
166- Ok ( v) => v,
167- Err ( e) => std:: panic:: resume_unwind ( e) ,
168- }
169- } )
153+ // `unwrap` is ok here because `build_scoped` just as `std::thread`
154+ // only panics if the thread name contains null bytes.
155+ builder
156+ . build_scoped (
157+ |thread| thread. run ( ) ,
158+ move |pool| {
159+ pool. install ( || {
160+ rustc_span:: create_session_globals_then (
161+ edition,
162+ extra_symbols,
163+ Some ( sm_inputs) ,
164+ || f ( CurrentGcx :: new ( ) , Proxy :: new ( ) ) ,
165+ )
166+ } )
167+ } ,
168+ )
169+ . unwrap ( )
170170}
171171
172172pub ( crate ) fn run_in_thread_pool_with_globals <
@@ -188,21 +188,8 @@ pub(crate) fn run_in_thread_pool_with_globals<
188188
189189 let thread_stack_size = init_stack_size ( thread_builder_diag) ;
190190
191- let registry = sync:: Registry :: new ( std:: num:: NonZero :: new ( threads) . unwrap ( ) ) ;
192-
193191 let Some ( proof) = sync:: check_dyn_thread_safe ( ) else {
194- return run_in_thread_with_globals (
195- thread_stack_size,
196- edition,
197- sm_inputs,
198- extra_symbols,
199- |current_gcx, jobserver_proxy| {
200- // Register the thread for use with the `WorkerLocal` type.
201- registry. register ( ) ;
202-
203- f ( current_gcx, jobserver_proxy)
204- } ,
205- ) ;
192+ return run_in_thread_with_globals ( thread_stack_size, edition, sm_inputs, extra_symbols, f) ;
206193 } ;
207194
208195 let current_gcx = proof. derive ( CurrentGcx :: new ( ) ) ;
@@ -282,9 +269,6 @@ internal compiler error: query cycle handler thread panicked, aborting process";
282269 . build_scoped (
283270 // Initialize each new worker thread when created.
284271 move |thread : rustc_thread_pool:: ThreadBuilder | {
285- // Register the thread for use with the `WorkerLocal` type.
286- registry. register ( ) ;
287-
288272 rustc_span:: set_session_globals_then ( session_globals. into_inner ( ) , || {
289273 thread. run ( )
290274 } )
0 commit comments