Move the WorkerLocal type from the rustc-rayon fork into rustc_data_structures

This commit is contained in:
John Kåre Alsaker 2020-10-31 12:01:54 +01:00
parent c6fb7b9815
commit 64474a40b0
5 changed files with 213 additions and 34 deletions

View file

@ -19,6 +19,7 @@ use rustc_session::Session;
use rustc_session::{early_error, CompilerIO};
use rustc_span::source_map::{FileLoader, FileName};
use rustc_span::symbol::sym;
use std::cell::OnceCell;
use std::path::PathBuf;
use std::result;
@ -58,9 +59,25 @@ impl Compiler {
}
}
fn registry_setup() {
thread_local! {
static ONCE: OnceCell<()> = OnceCell::new();
}
// Create a dummy registry to allow `WorkerLocal` construction.
// We use `OnceCell` so we only register one dummy registry per thread.
ONCE.with(|once| {
once.get_or_init(|| {
rustc_data_structures::sync::Registry::new(1).register();
});
});
}
/// Converts strings provided as `--cfg [cfgspec]` into a `crate_cfg`.
pub fn parse_cfgspecs(cfgspecs: Vec<String>) -> FxHashSet<(String, Option<String>)> {
rustc_span::create_default_session_if_not_set_then(move |_| {
registry_setup();
let cfg = cfgspecs
.into_iter()
.map(|s| {
@ -120,6 +137,8 @@ pub fn parse_cfgspecs(cfgspecs: Vec<String>) -> FxHashSet<(String, Option<String
/// Converts strings provided as `--check-cfg [specs]` into a `CheckCfg`.
pub fn parse_check_cfg(specs: Vec<String>) -> CheckCfg {
rustc_span::create_default_session_if_not_set_then(move |_| {
registry_setup();
let mut cfg = CheckCfg::default();
'specs: for s in specs {

View file

@ -4,6 +4,8 @@ use libloading::Library;
use rustc_ast as ast;
use rustc_codegen_ssa::traits::CodegenBackend;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
#[cfg(parallel_compiler)]
use rustc_data_structures::sync;
use rustc_errors::registry::Registry;
use rustc_parse::validate_attr;
use rustc_session as session;
@ -170,6 +172,7 @@ pub(crate) fn run_in_thread_pool_with_globals<F: FnOnce() -> R + Send, R: Send>(
use rustc_middle::ty::tls;
use rustc_query_impl::{deadlock, QueryContext, QueryCtxt};
let registry = sync::Registry::new(threads);
let mut builder = rayon::ThreadPoolBuilder::new()
.thread_name(|_| "rustc".to_string())
.acquire_thread_handler(jobserver::acquire_thread)
@ -200,6 +203,9 @@ pub(crate) fn run_in_thread_pool_with_globals<F: FnOnce() -> R + Send, R: Send>(
.build_scoped(
// Initialize each new worker thread when created.
move |thread: rayon::ThreadBuilder| {
// Register the thread for use with the `WorkerLocal` type.
registry.register();
rustc_span::set_session_globals_then(session_globals, || thread.run())
},
// Run `f` on the first thread in the thread pool.