Skip to content

Commit a4b1158

Browse files
committed
Move handle_deadlock where it is used.
1 parent ea3d465 commit a4b1158

File tree

6 files changed

+31
-33
lines changed

6 files changed

+31
-33
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3878,6 +3878,7 @@ version = "0.0.0"
38783878
dependencies = [
38793879
"libc",
38803880
"rustc-rayon",
3881+
"rustc-rayon-core",
38813882
"rustc_ast",
38823883
"rustc_ast_lowering",
38833884
"rustc_ast_passes",

compiler/rustc_interface/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ doctest = false
1010
[dependencies]
1111
libc = "0.2"
1212
tracing = "0.1"
13+
rustc-rayon-core = "0.3.0"
1314
rayon = { version = "0.3.0", package = "rustc-rayon" }
1415
smallvec = { version = "1.6.1", features = ["union", "may_dangle"] }
1516
rustc_ast = { path = "../rustc_ast" }

compiler/rustc_interface/src/util.rs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ use rustc_data_structures::stable_hasher::StableHasher;
1010
use rustc_data_structures::sync::Lrc;
1111
use rustc_errors::registry::Registry;
1212
use rustc_metadata::dynamic_lib::DynamicLibrary;
13+
#[cfg(parallel_compiler)]
14+
use rustc_middle::ty::tls;
1315
use rustc_resolve::{self, Resolver};
1416
use rustc_session as session;
1517
use rustc_session::config::{self, CrateType};
@@ -29,11 +31,12 @@ use std::io;
2931
use std::lazy::SyncOnceCell;
3032
use std::mem;
3133
use std::ops::DerefMut;
34+
#[cfg(not(parallel_compiler))]
35+
use std::panic;
3236
use std::path::{Path, PathBuf};
3337
use std::sync::atomic::{AtomicBool, Ordering};
3438
use std::sync::{Arc, Mutex, Once};
35-
#[cfg(not(parallel_compiler))]
36-
use std::{panic, thread};
39+
use std::thread;
3740
use tracing::info;
3841

3942
/// Adds `target_feature = "..."` cfgs for a variety of platform
@@ -156,22 +159,43 @@ pub fn setup_callbacks_and_run_in_thread_pool_with_globals<F: FnOnce() -> R + Se
156159
scoped_thread(cfg, main_handler)
157160
}
158161

162+
/// Creates a new thread and forwards information in thread locals to it.
163+
/// The new thread runs the deadlock handler.
164+
/// Must only be called when a deadlock is about to happen.
165+
#[cfg(parallel_compiler)]
166+
unsafe fn handle_deadlock() {
167+
let registry = rustc_rayon_core::Registry::current();
168+
169+
let context = tls::get_tlv();
170+
assert!(context != 0);
171+
rustc_data_structures::sync::assert_sync::<tls::ImplicitCtxt<'_, '_>>();
172+
let icx: &tls::ImplicitCtxt<'_, '_> = &*(context as *const tls::ImplicitCtxt<'_, '_>);
173+
174+
let session_globals = rustc_span::SESSION_GLOBALS.with(|sg| sg as *const _);
175+
let session_globals = &*session_globals;
176+
thread::spawn(move || {
177+
tls::enter_context(icx, |_| {
178+
rustc_span::SESSION_GLOBALS
179+
.set(session_globals, || tls::with(|tcx| tcx.queries.deadlock(tcx, &registry)))
180+
});
181+
});
182+
}
183+
159184
#[cfg(parallel_compiler)]
160185
pub fn setup_callbacks_and_run_in_thread_pool_with_globals<F: FnOnce() -> R + Send, R: Send>(
161186
edition: Edition,
162187
threads: usize,
163188
stderr: &Option<Arc<Mutex<Vec<u8>>>>,
164189
f: F,
165190
) -> R {
166-
use rustc_middle::ty;
167191
crate::callbacks::setup_callbacks();
168192

169193
let mut config = rayon::ThreadPoolBuilder::new()
170194
.thread_name(|_| "rustc".to_string())
171195
.acquire_thread_handler(jobserver::acquire_thread)
172196
.release_thread_handler(jobserver::release_thread)
173197
.num_threads(threads)
174-
.deadlock_handler(|| unsafe { ty::query::handle_deadlock() });
198+
.deadlock_handler(|| unsafe { handle_deadlock() });
175199

176200
if let Some(size) = get_stack_size() {
177201
config = config.stack_size(size);

compiler/rustc_middle/src/ty/query/job.rs

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

compiler/rustc_middle/src/ty/query/mod.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,6 @@ use rustc_query_system::query::*;
6868
mod stats;
6969
pub use self::stats::print_stats;
7070

71-
#[cfg(parallel_compiler)]
72-
mod job;
73-
#[cfg(parallel_compiler)]
74-
pub use self::job::handle_deadlock;
7571
pub use rustc_query_system::query::{QueryInfo, QueryJob, QueryJobId};
7672

7773
mod keys;

compiler/rustc_middle/src/ty/query/plumbing.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ macro_rules! define_queries_struct {
592592
}
593593

594594
#[cfg(parallel_compiler)]
595-
unsafe fn deadlock(&'tcx self, tcx: TyCtxt<'tcx>, registry: &rustc_rayon_core::Registry) {
595+
pub unsafe fn deadlock(&'tcx self, tcx: TyCtxt<'tcx>, registry: &rustc_rayon_core::Registry) {
596596
let tcx = QueryCtxt { tcx, queries: self };
597597
rustc_query_system::query::deadlock(tcx, registry)
598598
}

0 commit comments

Comments
 (0)