Skip to content

Commit

Permalink
Move handle_deadlock where it is used.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgillot committed Feb 19, 2021
1 parent ea3d465 commit a4b1158
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 33 deletions.
1 change: 1 addition & 0 deletions Cargo.lock
Expand Up @@ -3878,6 +3878,7 @@ version = "0.0.0"
dependencies = [
"libc",
"rustc-rayon",
"rustc-rayon-core",
"rustc_ast",
"rustc_ast_lowering",
"rustc_ast_passes",
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_interface/Cargo.toml
Expand Up @@ -10,6 +10,7 @@ doctest = false
[dependencies]
libc = "0.2"
tracing = "0.1"
rustc-rayon-core = "0.3.0"
rayon = { version = "0.3.0", package = "rustc-rayon" }
smallvec = { version = "1.6.1", features = ["union", "may_dangle"] }
rustc_ast = { path = "../rustc_ast" }
Expand Down
32 changes: 28 additions & 4 deletions compiler/rustc_interface/src/util.rs
Expand Up @@ -10,6 +10,8 @@ use rustc_data_structures::stable_hasher::StableHasher;
use rustc_data_structures::sync::Lrc;
use rustc_errors::registry::Registry;
use rustc_metadata::dynamic_lib::DynamicLibrary;
#[cfg(parallel_compiler)]
use rustc_middle::ty::tls;
use rustc_resolve::{self, Resolver};
use rustc_session as session;
use rustc_session::config::{self, CrateType};
Expand All @@ -29,11 +31,12 @@ use std::io;
use std::lazy::SyncOnceCell;
use std::mem;
use std::ops::DerefMut;
#[cfg(not(parallel_compiler))]
use std::panic;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex, Once};
#[cfg(not(parallel_compiler))]
use std::{panic, thread};
use std::thread;
use tracing::info;

/// Adds `target_feature = "..."` cfgs for a variety of platform
Expand Down Expand Up @@ -156,22 +159,43 @@ pub fn setup_callbacks_and_run_in_thread_pool_with_globals<F: FnOnce() -> R + Se
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)]
pub fn setup_callbacks_and_run_in_thread_pool_with_globals<F: FnOnce() -> R + Send, R: Send>(
edition: Edition,
threads: usize,
stderr: &Option<Arc<Mutex<Vec<u8>>>>,
f: F,
) -> R {
use rustc_middle::ty;
crate::callbacks::setup_callbacks();

let mut config = rayon::ThreadPoolBuilder::new()
.thread_name(|_| "rustc".to_string())
.acquire_thread_handler(jobserver::acquire_thread)
.release_thread_handler(jobserver::release_thread)
.num_threads(threads)
.deadlock_handler(|| unsafe { ty::query::handle_deadlock() });
.deadlock_handler(|| unsafe { handle_deadlock() });

if let Some(size) = get_stack_size() {
config = config.stack_size(size);
Expand Down
24 changes: 0 additions & 24 deletions compiler/rustc_middle/src/ty/query/job.rs

This file was deleted.

4 changes: 0 additions & 4 deletions compiler/rustc_middle/src/ty/query/mod.rs
Expand Up @@ -68,10 +68,6 @@ use rustc_query_system::query::*;
mod 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};

mod keys;
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/query/plumbing.rs
Expand Up @@ -592,7 +592,7 @@ macro_rules! define_queries_struct {
}

#[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 };
rustc_query_system::query::deadlock(tcx, registry)
}
Expand Down

0 comments on commit a4b1158

Please sign in to comment.