Auto merge of #50108 - Zoxc:sync-gcx, r=mw

Make GlobalCtxt thread-safe

r? @michaelwoerister
This commit is contained in:
bors 2018-06-01 22:49:43 +00:00
commit bfa41f2801
13 changed files with 62 additions and 7 deletions

View File

@ -2044,6 +2044,7 @@ dependencies = [
"parking_lot_core 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-rayon 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-rayon-core 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc_cratesio_shim 0.0.0",
"serialize 0.0.0",
"stable_deref_trait 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",

View File

@ -36,6 +36,8 @@
#![feature(lang_items)]
#![feature(optin_builtin_traits)]
#![recursion_limit="256"]
extern crate syntax;
extern crate syntax_pos;
extern crate rustc_errors;

View File

@ -845,10 +845,10 @@ impl Session {
/// We want to know if we're allowed to do an optimization for crate foo from -z fuel=foo=n.
/// This expends fuel if applicable, and records fuel if applicable.
pub fn consider_optimizing<T: Fn() -> String>(&self, crate_name: &str, msg: T) -> bool {
assert!(self.query_threads() == 1);
let mut ret = true;
match self.optimization_fuel_crate {
Some(ref c) if c == crate_name => {
assert!(self.query_threads() == 1);
let fuel = self.optimization_fuel_limit.get();
ret = fuel != 0;
if fuel == 0 && !self.out_of_fuel.get() {
@ -862,6 +862,7 @@ impl Session {
}
match self.print_fuel_crate {
Some(ref c) if c == crate_name => {
assert!(self.query_threads() == 1);
self.print_fuel.set(self.print_fuel.get() + 1);
}
_ => {}

View File

@ -58,7 +58,7 @@ use rustc_data_structures::stable_hasher::{HashStable, hash_stable_hashmap,
StableVec};
use arena::{TypedArena, SyncDroplessArena};
use rustc_data_structures::indexed_vec::IndexVec;
use rustc_data_structures::sync::{Lrc, Lock};
use rustc_data_structures::sync::{self, Lrc, Lock, WorkerLocal};
use std::any::Any;
use std::borrow::Borrow;
use std::cmp::Ordering;
@ -80,14 +80,14 @@ use syntax_pos::Span;
use hir;
pub struct AllArenas<'tcx> {
pub global: GlobalArenas<'tcx>,
pub global: WorkerLocal<GlobalArenas<'tcx>>,
pub interner: SyncDroplessArena,
}
impl<'tcx> AllArenas<'tcx> {
pub fn new() -> Self {
AllArenas {
global: GlobalArenas::new(),
global: WorkerLocal::new(|_| GlobalArenas::new()),
interner: SyncDroplessArena::new(),
}
}
@ -854,7 +854,7 @@ impl<'a, 'gcx, 'tcx> Deref for TyCtxt<'a, 'gcx, 'tcx> {
}
pub struct GlobalCtxt<'tcx> {
global_arenas: &'tcx GlobalArenas<'tcx>,
global_arenas: &'tcx WorkerLocal<GlobalArenas<'tcx>>,
global_interners: CtxtInterners<'tcx>,
cstore: &'tcx CrateStoreDyn,
@ -1179,6 +1179,8 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
output_filenames: Arc::new(output_filenames.clone()),
};
sync::assert_send_val(&gcx);
tls::enter_global(gcx, f)
}
@ -1704,7 +1706,7 @@ pub mod tls {
use ty::maps;
use errors::{Diagnostic, TRACK_DIAGNOSTICS};
use rustc_data_structures::OnDrop;
use rustc_data_structures::sync::Lrc;
use rustc_data_structures::sync::{self, Lrc};
use dep_graph::OpenTask;
/// This is the implicit state of rustc. It contains the current
@ -1832,6 +1834,10 @@ pub mod tls {
if context == 0 {
f(None)
} else {
// We could get a ImplicitCtxt pointer from another thread.
// Ensure that ImplicitCtxt is Sync
sync::assert_sync::<ImplicitCtxt>();
unsafe { f(Some(&*(context as *const ImplicitCtxt))) }
}
}

View File

@ -617,6 +617,8 @@ pub struct Slice<T> {
opaque: OpaqueSliceContents,
}
unsafe impl<T: Sync> Sync for Slice<T> {}
impl<T: Copy> Slice<T> {
#[inline]
fn from_arena<'tcx>(arena: &'tcx SyncDroplessArena, slice: &[T]) -> &'tcx Slice<T> {

View File

@ -23,6 +23,8 @@
#![feature(quote)]
#![feature(rustc_diagnostic_macros)]
#![recursion_limit="256"]
extern crate ar;
extern crate flate2;
#[macro_use]

View File

@ -17,6 +17,7 @@ cfg-if = "0.1.2"
stable_deref_trait = "1.0.0"
parking_lot_core = "0.2.8"
rustc-rayon = "0.1.0"
rustc-rayon-core = "0.1.0"
rustc-hash = "1.0.1"
[dependencies.parking_lot]

View File

@ -44,6 +44,7 @@ extern crate parking_lot;
extern crate cfg_if;
extern crate stable_deref_trait;
extern crate rustc_rayon as rayon;
extern crate rustc_rayon_core as rayon_core;
extern crate rustc_hash;
// See librustc_cratesio_shim/Cargo.toml for a comment explaining this.

View File

@ -36,7 +36,6 @@ use std::marker::PhantomData;
use std::fmt::Debug;
use std::fmt::Formatter;
use std::fmt;
use std;
use std::ops::{Deref, DerefMut};
use owning_ref::{Erased, OwningRef};
@ -100,6 +99,33 @@ cfg_if! {
use std::cell::Cell;
#[derive(Debug)]
pub struct WorkerLocal<T>(OneThread<T>);
impl<T> WorkerLocal<T> {
/// Creates a new worker local where the `initial` closure computes the
/// value this worker local should take for each thread in the thread pool.
#[inline]
pub fn new<F: FnMut(usize) -> T>(mut f: F) -> WorkerLocal<T> {
WorkerLocal(OneThread::new(f(0)))
}
/// Returns the worker-local value for each thread
#[inline]
pub fn into_inner(self) -> Vec<T> {
vec![OneThread::into_inner(self.0)]
}
}
impl<T> Deref for WorkerLocal<T> {
type Target = T;
#[inline(always)]
fn deref(&self) -> &T {
&*self.0
}
}
#[derive(Debug)]
pub struct MTLock<T>(T);
@ -200,9 +226,12 @@ cfg_if! {
use parking_lot::Mutex as InnerLock;
use parking_lot::RwLock as InnerRwLock;
use std;
use std::thread;
pub use rayon::{join, scope};
pub use rayon_core::WorkerLocal;
pub use rayon::iter::ParallelIterator;
use rayon::iter::IntoParallelIterator;
@ -638,7 +667,9 @@ pub struct OneThread<T> {
inner: T,
}
#[cfg(parallel_queries)]
unsafe impl<T> std::marker::Sync for OneThread<T> {}
#[cfg(parallel_queries)]
unsafe impl<T> std::marker::Send for OneThread<T> {}
impl<T> OneThread<T> {

View File

@ -23,6 +23,8 @@
#![feature(specialization)]
#![feature(rustc_private)]
#![recursion_limit="256"]
extern crate libc;
#[macro_use]
extern crate log;

View File

@ -14,6 +14,8 @@
#![feature(rustc_diagnostic_macros)]
#![recursion_limit="256"]
#[macro_use] extern crate rustc;
#[macro_use] extern crate syntax;
extern crate rustc_typeck;

View File

@ -15,6 +15,8 @@
#![cfg_attr(stage0, feature(macro_lifetime_matcher))]
#![allow(unused_attributes)]
#![recursion_limit="256"]
#[macro_use]
extern crate rustc;

View File

@ -17,6 +17,8 @@
#![feature(iterator_find_map)]
#![feature(in_band_lifetimes)]
#![recursion_limit="256"]
extern crate chalk_engine;
#[macro_use]
extern crate log;