Skip to content

Commit bfa41f2

Browse files
committed
Auto merge of #50108 - Zoxc:sync-gcx, r=mw
Make GlobalCtxt thread-safe r? @michaelwoerister
2 parents aa094a4 + 2119d04 commit bfa41f2

File tree

13 files changed

+62
-7
lines changed

13 files changed

+62
-7
lines changed

Diff for: src/Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -2044,6 +2044,7 @@ dependencies = [
20442044
"parking_lot_core 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)",
20452045
"rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
20462046
"rustc-rayon 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
2047+
"rustc-rayon-core 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
20472048
"rustc_cratesio_shim 0.0.0",
20482049
"serialize 0.0.0",
20492050
"stable_deref_trait 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",

Diff for: src/libproc_macro/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
#![feature(lang_items)]
3737
#![feature(optin_builtin_traits)]
3838

39+
#![recursion_limit="256"]
40+
3941
extern crate syntax;
4042
extern crate syntax_pos;
4143
extern crate rustc_errors;

Diff for: src/librustc/session/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -845,10 +845,10 @@ impl Session {
845845
/// We want to know if we're allowed to do an optimization for crate foo from -z fuel=foo=n.
846846
/// This expends fuel if applicable, and records fuel if applicable.
847847
pub fn consider_optimizing<T: Fn() -> String>(&self, crate_name: &str, msg: T) -> bool {
848-
assert!(self.query_threads() == 1);
849848
let mut ret = true;
850849
match self.optimization_fuel_crate {
851850
Some(ref c) if c == crate_name => {
851+
assert!(self.query_threads() == 1);
852852
let fuel = self.optimization_fuel_limit.get();
853853
ret = fuel != 0;
854854
if fuel == 0 && !self.out_of_fuel.get() {
@@ -862,6 +862,7 @@ impl Session {
862862
}
863863
match self.print_fuel_crate {
864864
Some(ref c) if c == crate_name => {
865+
assert!(self.query_threads() == 1);
865866
self.print_fuel.set(self.print_fuel.get() + 1);
866867
}
867868
_ => {}

Diff for: src/librustc/ty/context.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ use rustc_data_structures::stable_hasher::{HashStable, hash_stable_hashmap,
5858
StableVec};
5959
use arena::{TypedArena, SyncDroplessArena};
6060
use rustc_data_structures::indexed_vec::IndexVec;
61-
use rustc_data_structures::sync::{Lrc, Lock};
61+
use rustc_data_structures::sync::{self, Lrc, Lock, WorkerLocal};
6262
use std::any::Any;
6363
use std::borrow::Borrow;
6464
use std::cmp::Ordering;
@@ -80,14 +80,14 @@ use syntax_pos::Span;
8080
use hir;
8181

8282
pub struct AllArenas<'tcx> {
83-
pub global: GlobalArenas<'tcx>,
83+
pub global: WorkerLocal<GlobalArenas<'tcx>>,
8484
pub interner: SyncDroplessArena,
8585
}
8686

8787
impl<'tcx> AllArenas<'tcx> {
8888
pub fn new() -> Self {
8989
AllArenas {
90-
global: GlobalArenas::new(),
90+
global: WorkerLocal::new(|_| GlobalArenas::new()),
9191
interner: SyncDroplessArena::new(),
9292
}
9393
}
@@ -854,7 +854,7 @@ impl<'a, 'gcx, 'tcx> Deref for TyCtxt<'a, 'gcx, 'tcx> {
854854
}
855855

856856
pub struct GlobalCtxt<'tcx> {
857-
global_arenas: &'tcx GlobalArenas<'tcx>,
857+
global_arenas: &'tcx WorkerLocal<GlobalArenas<'tcx>>,
858858
global_interners: CtxtInterners<'tcx>,
859859

860860
cstore: &'tcx CrateStoreDyn,
@@ -1179,6 +1179,8 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
11791179
output_filenames: Arc::new(output_filenames.clone()),
11801180
};
11811181

1182+
sync::assert_send_val(&gcx);
1183+
11821184
tls::enter_global(gcx, f)
11831185
}
11841186

@@ -1704,7 +1706,7 @@ pub mod tls {
17041706
use ty::maps;
17051707
use errors::{Diagnostic, TRACK_DIAGNOSTICS};
17061708
use rustc_data_structures::OnDrop;
1707-
use rustc_data_structures::sync::Lrc;
1709+
use rustc_data_structures::sync::{self, Lrc};
17081710
use dep_graph::OpenTask;
17091711

17101712
/// This is the implicit state of rustc. It contains the current
@@ -1832,6 +1834,10 @@ pub mod tls {
18321834
if context == 0 {
18331835
f(None)
18341836
} else {
1837+
// We could get a ImplicitCtxt pointer from another thread.
1838+
// Ensure that ImplicitCtxt is Sync
1839+
sync::assert_sync::<ImplicitCtxt>();
1840+
18351841
unsafe { f(Some(&*(context as *const ImplicitCtxt))) }
18361842
}
18371843
}

Diff for: src/librustc/ty/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,8 @@ pub struct Slice<T> {
617617
opaque: OpaqueSliceContents,
618618
}
619619

620+
unsafe impl<T: Sync> Sync for Slice<T> {}
621+
620622
impl<T: Copy> Slice<T> {
621623
#[inline]
622624
fn from_arena<'tcx>(arena: &'tcx SyncDroplessArena, slice: &[T]) -> &'tcx Slice<T> {

Diff for: src/librustc_codegen_utils/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#![feature(quote)]
2424
#![feature(rustc_diagnostic_macros)]
2525

26+
#![recursion_limit="256"]
27+
2628
extern crate ar;
2729
extern crate flate2;
2830
#[macro_use]

Diff for: src/librustc_data_structures/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ cfg-if = "0.1.2"
1717
stable_deref_trait = "1.0.0"
1818
parking_lot_core = "0.2.8"
1919
rustc-rayon = "0.1.0"
20+
rustc-rayon-core = "0.1.0"
2021
rustc-hash = "1.0.1"
2122

2223
[dependencies.parking_lot]

Diff for: src/librustc_data_structures/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ extern crate parking_lot;
4444
extern crate cfg_if;
4545
extern crate stable_deref_trait;
4646
extern crate rustc_rayon as rayon;
47+
extern crate rustc_rayon_core as rayon_core;
4748
extern crate rustc_hash;
4849

4950
// See librustc_cratesio_shim/Cargo.toml for a comment explaining this.

Diff for: src/librustc_data_structures/sync.rs

+32-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ use std::marker::PhantomData;
3636
use std::fmt::Debug;
3737
use std::fmt::Formatter;
3838
use std::fmt;
39-
use std;
4039
use std::ops::{Deref, DerefMut};
4140
use owning_ref::{Erased, OwningRef};
4241

@@ -100,6 +99,33 @@ cfg_if! {
10099

101100
use std::cell::Cell;
102101

102+
#[derive(Debug)]
103+
pub struct WorkerLocal<T>(OneThread<T>);
104+
105+
impl<T> WorkerLocal<T> {
106+
/// Creates a new worker local where the `initial` closure computes the
107+
/// value this worker local should take for each thread in the thread pool.
108+
#[inline]
109+
pub fn new<F: FnMut(usize) -> T>(mut f: F) -> WorkerLocal<T> {
110+
WorkerLocal(OneThread::new(f(0)))
111+
}
112+
113+
/// Returns the worker-local value for each thread
114+
#[inline]
115+
pub fn into_inner(self) -> Vec<T> {
116+
vec![OneThread::into_inner(self.0)]
117+
}
118+
}
119+
120+
impl<T> Deref for WorkerLocal<T> {
121+
type Target = T;
122+
123+
#[inline(always)]
124+
fn deref(&self) -> &T {
125+
&*self.0
126+
}
127+
}
128+
103129
#[derive(Debug)]
104130
pub struct MTLock<T>(T);
105131

@@ -200,9 +226,12 @@ cfg_if! {
200226
use parking_lot::Mutex as InnerLock;
201227
use parking_lot::RwLock as InnerRwLock;
202228

229+
use std;
203230
use std::thread;
204231
pub use rayon::{join, scope};
205232

233+
pub use rayon_core::WorkerLocal;
234+
206235
pub use rayon::iter::ParallelIterator;
207236
use rayon::iter::IntoParallelIterator;
208237

@@ -638,7 +667,9 @@ pub struct OneThread<T> {
638667
inner: T,
639668
}
640669

670+
#[cfg(parallel_queries)]
641671
unsafe impl<T> std::marker::Sync for OneThread<T> {}
672+
#[cfg(parallel_queries)]
642673
unsafe impl<T> std::marker::Send for OneThread<T> {}
643674

644675
impl<T> OneThread<T> {

Diff for: src/librustc_metadata/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#![feature(specialization)]
2424
#![feature(rustc_private)]
2525

26+
#![recursion_limit="256"]
27+
2628
extern crate libc;
2729
#[macro_use]
2830
extern crate log;

Diff for: src/librustc_privacy/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
#![feature(rustc_diagnostic_macros)]
1616

17+
#![recursion_limit="256"]
18+
1719
#[macro_use] extern crate rustc;
1820
#[macro_use] extern crate syntax;
1921
extern crate rustc_typeck;

Diff for: src/librustc_save_analysis/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#![cfg_attr(stage0, feature(macro_lifetime_matcher))]
1616
#![allow(unused_attributes)]
1717

18+
#![recursion_limit="256"]
19+
1820
#[macro_use]
1921
extern crate rustc;
2022

Diff for: src/librustc_traits/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#![feature(iterator_find_map)]
1818
#![feature(in_band_lifetimes)]
1919

20+
#![recursion_limit="256"]
21+
2022
extern crate chalk_engine;
2123
#[macro_use]
2224
extern crate log;

0 commit comments

Comments
 (0)