Skip to content

Commit a77af1f

Browse files
committed
Replace WorkerLocal with the original
1 parent 4933094 commit a77af1f

5 files changed

Lines changed: 33 additions & 193 deletions

File tree

compiler/rustc_data_structures/src/marker.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ already_send!(
6363
[std::sync::atomic::AtomicU32][std::backtrace::Backtrace][std::io::Stdout][std::io::Stderr]
6464
[std::io::Error][std::fs::File][std::panic::Location<'_>][rustc_arena::DroplessArena]
6565
[jobserver_crate::Client][jobserver_crate::HelperThread][crate::memmap::Mmap]
66-
[crate::profiling::SelfProfiler][crate::owned_slice::OwnedSlice]
66+
[crate::profiling::SelfProfiler][crate::owned_slice::OwnedSlice][crate::sync::Registry]
6767
);
6868

6969
#[cfg(target_has_atomic = "64")]
@@ -142,7 +142,7 @@ already_sync!(
142142
[std::sync::atomic::AtomicBool][std::sync::atomic::AtomicUsize][std::sync::atomic::AtomicU8]
143143
[std::sync::atomic::AtomicU32][std::backtrace::Backtrace][std::io::Error][std::fs::File][std::panic::Location<'_>]
144144
[jobserver_crate::Client][jobserver_crate::HelperThread][crate::memmap::Mmap]
145-
[crate::profiling::SelfProfiler][crate::owned_slice::OwnedSlice]
145+
[crate::profiling::SelfProfiler][crate::owned_slice::OwnedSlice][crate::sync::Registry]
146146
);
147147

148148
// Use portable AtomicU64 for targets without native 64-bit atomics

compiler/rustc_data_structures/src/sync.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ pub use parking_lot::{
2929
MappedRwLockReadGuard as MappedReadGuard, MappedRwLockWriteGuard as MappedWriteGuard,
3030
RwLockReadGuard as ReadGuard, RwLockWriteGuard as WriteGuard,
3131
};
32+
pub use rustc_thread_pool::{Registry, WorkerLocal};
3233

3334
pub use self::atomic::AtomicU64;
3435
pub use self::freeze::{FreezeLock, FreezeReadGuard, FreezeWriteGuard};
@@ -42,14 +43,12 @@ pub use self::parallel::{
4243
try_par_for_each_in,
4344
};
4445
pub use self::vec::{AppendOnlyIndexVec, AppendOnlyVec};
45-
pub use self::worker_local::{Registry, WorkerLocal};
4646
pub use crate::marker::*;
4747

4848
mod freeze;
4949
mod lock;
5050
mod parallel;
5151
mod vec;
52-
mod worker_local;
5352

5453
/// Keep the conditional imports together in a submodule, so that import-sorting
5554
/// doesn't split them up.

compiler/rustc_data_structures/src/sync/worker_local.rs

Lines changed: 0 additions & 149 deletions
This file was deleted.

compiler/rustc_interface/src/util.rs

Lines changed: 24 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -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

172172
pub(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
})

compiler/rustc_thread_pool/src/worker_local.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,9 @@ impl<T> Deref for WorkerLocal<T> {
7373
self.current()
7474
}
7575
}
76+
77+
impl<T: Default> Default for WorkerLocal<T> {
78+
fn default() -> Self {
79+
WorkerLocal::new(|_| Default::default())
80+
}
81+
}

0 commit comments

Comments
 (0)