about summary refs log tree commit diff
path: root/compiler/rustc_interface/src
diff options
context:
space:
mode:
authorJohn Kåre Alsaker <john.kare.alsaker@gmail.com>2020-10-31 12:01:54 +0100
committerJohn Kåre Alsaker <john.kare.alsaker@gmail.com>2023-04-16 05:50:57 +0200
commit64474a40b0273557dbea58ca29af9245cbb4ad87 (patch)
tree2be6396311b795b12a8a603ab84b80349c3f91c8 /compiler/rustc_interface/src
parentc6fb7b9815aea87fb5ced1c683212871699c907c (diff)
downloadrust-64474a40b0273557dbea58ca29af9245cbb4ad87.tar.gz
rust-64474a40b0273557dbea58ca29af9245cbb4ad87.zip
Move the WorkerLocal type from the rustc-rayon fork into rustc_data_structures
Diffstat (limited to 'compiler/rustc_interface/src')
-rw-r--r--compiler/rustc_interface/src/interface.rs19
-rw-r--r--compiler/rustc_interface/src/util.rs6
2 files changed, 25 insertions, 0 deletions
diff --git a/compiler/rustc_interface/src/interface.rs b/compiler/rustc_interface/src/interface.rs
index be7fa9378ca..577a1034307 100644
--- a/compiler/rustc_interface/src/interface.rs
+++ b/compiler/rustc_interface/src/interface.rs
@@ -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 {
diff --git a/compiler/rustc_interface/src/util.rs b/compiler/rustc_interface/src/util.rs
index 612903810d2..a27a1e2978a 100644
--- a/compiler/rustc_interface/src/util.rs
+++ b/compiler/rustc_interface/src/util.rs
@@ -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.