Move handle_deadlock where it is used.

This commit is contained in:
Camille GILLOT 2020-11-18 19:02:31 +01:00
parent ea3d465c95
commit a4b1158f78
6 changed files with 31 additions and 33 deletions

View file

@ -3878,6 +3878,7 @@ version = "0.0.0"
dependencies = [ dependencies = [
"libc", "libc",
"rustc-rayon", "rustc-rayon",
"rustc-rayon-core",
"rustc_ast", "rustc_ast",
"rustc_ast_lowering", "rustc_ast_lowering",
"rustc_ast_passes", "rustc_ast_passes",

View file

@ -10,6 +10,7 @@ doctest = false
[dependencies] [dependencies]
libc = "0.2" libc = "0.2"
tracing = "0.1" tracing = "0.1"
rustc-rayon-core = "0.3.0"
rayon = { version = "0.3.0", package = "rustc-rayon" } rayon = { version = "0.3.0", package = "rustc-rayon" }
smallvec = { version = "1.6.1", features = ["union", "may_dangle"] } smallvec = { version = "1.6.1", features = ["union", "may_dangle"] }
rustc_ast = { path = "../rustc_ast" } rustc_ast = { path = "../rustc_ast" }

View file

@ -10,6 +10,8 @@ use rustc_data_structures::stable_hasher::StableHasher;
use rustc_data_structures::sync::Lrc; use rustc_data_structures::sync::Lrc;
use rustc_errors::registry::Registry; use rustc_errors::registry::Registry;
use rustc_metadata::dynamic_lib::DynamicLibrary; use rustc_metadata::dynamic_lib::DynamicLibrary;
#[cfg(parallel_compiler)]
use rustc_middle::ty::tls;
use rustc_resolve::{self, Resolver}; use rustc_resolve::{self, Resolver};
use rustc_session as session; use rustc_session as session;
use rustc_session::config::{self, CrateType}; use rustc_session::config::{self, CrateType};
@ -29,11 +31,12 @@ use std::io;
use std::lazy::SyncOnceCell; use std::lazy::SyncOnceCell;
use std::mem; use std::mem;
use std::ops::DerefMut; use std::ops::DerefMut;
#[cfg(not(parallel_compiler))]
use std::panic;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex, Once}; use std::sync::{Arc, Mutex, Once};
#[cfg(not(parallel_compiler))] use std::thread;
use std::{panic, thread};
use tracing::info; use tracing::info;
/// Adds `target_feature = "..."` cfgs for a variety of platform /// Adds `target_feature = "..."` cfgs for a variety of platform
@ -156,6 +159,28 @@ pub fn setup_callbacks_and_run_in_thread_pool_with_globals<F: FnOnce() -> R + Se
scoped_thread(cfg, main_handler) scoped_thread(cfg, main_handler)
} }
/// Creates a new thread and forwards information in thread locals to it.
/// The new thread runs the deadlock handler.
/// Must only be called when a deadlock is about to happen.
#[cfg(parallel_compiler)]
unsafe fn handle_deadlock() {
let registry = rustc_rayon_core::Registry::current();
let context = tls::get_tlv();
assert!(context != 0);
rustc_data_structures::sync::assert_sync::<tls::ImplicitCtxt<'_, '_>>();
let icx: &tls::ImplicitCtxt<'_, '_> = &*(context as *const tls::ImplicitCtxt<'_, '_>);
let session_globals = rustc_span::SESSION_GLOBALS.with(|sg| sg as *const _);
let session_globals = &*session_globals;
thread::spawn(move || {
tls::enter_context(icx, |_| {
rustc_span::SESSION_GLOBALS
.set(session_globals, || tls::with(|tcx| tcx.queries.deadlock(tcx, &registry)))
});
});
}
#[cfg(parallel_compiler)] #[cfg(parallel_compiler)]
pub fn setup_callbacks_and_run_in_thread_pool_with_globals<F: FnOnce() -> R + Send, R: Send>( pub fn setup_callbacks_and_run_in_thread_pool_with_globals<F: FnOnce() -> R + Send, R: Send>(
edition: Edition, edition: Edition,
@ -163,7 +188,6 @@ pub fn setup_callbacks_and_run_in_thread_pool_with_globals<F: FnOnce() -> R + Se
stderr: &Option<Arc<Mutex<Vec<u8>>>>, stderr: &Option<Arc<Mutex<Vec<u8>>>>,
f: F, f: F,
) -> R { ) -> R {
use rustc_middle::ty;
crate::callbacks::setup_callbacks(); crate::callbacks::setup_callbacks();
let mut config = rayon::ThreadPoolBuilder::new() let mut config = rayon::ThreadPoolBuilder::new()
@ -171,7 +195,7 @@ pub fn setup_callbacks_and_run_in_thread_pool_with_globals<F: FnOnce() -> R + Se
.acquire_thread_handler(jobserver::acquire_thread) .acquire_thread_handler(jobserver::acquire_thread)
.release_thread_handler(jobserver::release_thread) .release_thread_handler(jobserver::release_thread)
.num_threads(threads) .num_threads(threads)
.deadlock_handler(|| unsafe { ty::query::handle_deadlock() }); .deadlock_handler(|| unsafe { handle_deadlock() });
if let Some(size) = get_stack_size() { if let Some(size) = get_stack_size() {
config = config.stack_size(size); config = config.stack_size(size);

View file

@ -1,24 +0,0 @@
use crate::ty::tls;
use rustc_rayon_core as rayon_core;
use std::thread;
/// Creates a new thread and forwards information in thread locals to it.
/// The new thread runs the deadlock handler.
/// Must only be called when a deadlock is about to happen.
pub unsafe fn handle_deadlock() {
let registry = rayon_core::Registry::current();
let context = tls::get_tlv();
assert!(context != 0);
rustc_data_structures::sync::assert_sync::<tls::ImplicitCtxt<'_, '_>>();
let icx: &tls::ImplicitCtxt<'_, '_> = &*(context as *const tls::ImplicitCtxt<'_, '_>);
let session_globals = rustc_span::SESSION_GLOBALS.with(|sg| sg as *const _);
let session_globals = &*session_globals;
thread::spawn(move || {
tls::enter_context(icx, |_| {
rustc_span::SESSION_GLOBALS
.set(session_globals, || tls::with(|tcx| tcx.queries.deadlock(tcx, &registry)))
});
});
}

View file

@ -68,10 +68,6 @@ use rustc_query_system::query::*;
mod stats; mod stats;
pub use self::stats::print_stats; pub use self::stats::print_stats;
#[cfg(parallel_compiler)]
mod job;
#[cfg(parallel_compiler)]
pub use self::job::handle_deadlock;
pub use rustc_query_system::query::{QueryInfo, QueryJob, QueryJobId}; pub use rustc_query_system::query::{QueryInfo, QueryJob, QueryJobId};
mod keys; mod keys;

View file

@ -592,7 +592,7 @@ macro_rules! define_queries_struct {
} }
#[cfg(parallel_compiler)] #[cfg(parallel_compiler)]
unsafe fn deadlock(&'tcx self, tcx: TyCtxt<'tcx>, registry: &rustc_rayon_core::Registry) { pub unsafe fn deadlock(&'tcx self, tcx: TyCtxt<'tcx>, registry: &rustc_rayon_core::Registry) {
let tcx = QueryCtxt { tcx, queries: self }; let tcx = QueryCtxt { tcx, queries: self };
rustc_query_system::query::deadlock(tcx, registry) rustc_query_system::query::deadlock(tcx, registry)
} }