Skip to content

Commit 0e45c3e

Browse files
committed
Replace LockCell with atomic types
1 parent 475cafe commit 0e45c3e

File tree

7 files changed

+172
-175
lines changed

7 files changed

+172
-175
lines changed

src/librustc/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
#![feature(slice_patterns)]
6060
#![feature(slice_sort_by_cached_key)]
6161
#![feature(specialization)]
62+
#![feature(stmt_expr_attributes)]
6263
#![feature(unboxed_closures)]
6364
#![feature(thread_local)]
6465
#![feature(trace_macros)]

src/librustc/session/mod.rs

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ use util::common::{duration_to_secs_str, ErrorReported};
2626
use util::common::ProfileQueriesMsg;
2727

2828
use rustc_data_structures::base_n;
29-
use rustc_data_structures::sync::{self, Lrc, Lock, LockCell, OneThread, Once, RwLock};
29+
use rustc_data_structures::sync::{
30+
self, Lrc, Lock, OneThread, Once, RwLock, AtomicU64, AtomicUsize, AtomicBool, Ordering,
31+
Ordering::SeqCst,
32+
};
3033

3134
use errors::{self, DiagnosticBuilder, DiagnosticId, Applicability};
3235
use errors::emitter::{Emitter, EmitterWriter};
@@ -51,7 +54,6 @@ use std::io::Write;
5154
use std::path::{Path, PathBuf};
5255
use std::time::Duration;
5356
use std::sync::mpsc;
54-
use std::sync::atomic::{AtomicUsize, Ordering};
5557

5658
mod code_stats;
5759
pub mod config;
@@ -142,15 +144,15 @@ pub struct Session {
142144
/// If -zfuel=crate=n is specified, Some(crate).
143145
optimization_fuel_crate: Option<String>,
144146
/// If -zfuel=crate=n is specified, initially set to n. Otherwise 0.
145-
optimization_fuel_limit: LockCell<u64>,
147+
optimization_fuel_limit: AtomicU64,
146148
/// We're rejecting all further optimizations.
147-
out_of_fuel: LockCell<bool>,
149+
out_of_fuel: AtomicBool,
148150

149151
// The next two are public because the driver needs to read them.
150152
/// If -zprint-fuel=crate, Some(crate).
151153
pub print_fuel_crate: Option<String>,
152154
/// Always set to zero and incremented so that we can print fuel expended by a crate.
153-
pub print_fuel: LockCell<u64>,
155+
pub print_fuel: AtomicU64,
154156

155157
/// Loaded up early on in the initialization of this `Session` to avoid
156158
/// false positives about a job server in our environment.
@@ -868,32 +870,43 @@ impl Session {
868870
self.perf_stats.normalize_projection_ty.load(Ordering::Relaxed));
869871
}
870872

871-
/// We want to know if we're allowed to do an optimization for crate foo from -z fuel=foo=n.
872-
/// This expends fuel if applicable, and records fuel if applicable.
873-
pub fn consider_optimizing<T: Fn() -> String>(&self, crate_name: &str, msg: T) -> bool {
873+
#[inline(never)]
874+
#[cold]
875+
pub fn consider_optimizing_cold<T: Fn() -> String>(&self, crate_name: &str, msg: T) -> bool {
874876
let mut ret = true;
875877
if let Some(ref c) = self.optimization_fuel_crate {
876878
if c == crate_name {
877879
assert_eq!(self.query_threads(), 1);
878-
let fuel = self.optimization_fuel_limit.get();
880+
let fuel = self.optimization_fuel_limit.load(SeqCst);
879881
ret = fuel != 0;
880-
if fuel == 0 && !self.out_of_fuel.get() {
882+
if fuel == 0 && !self.out_of_fuel.load(SeqCst) {
881883
eprintln!("optimization-fuel-exhausted: {}", msg());
882-
self.out_of_fuel.set(true);
884+
self.out_of_fuel.store(true, SeqCst);
883885
} else if fuel > 0 {
884-
self.optimization_fuel_limit.set(fuel - 1);
886+
self.optimization_fuel_limit.store(fuel - 1, SeqCst);
885887
}
886888
}
887889
}
888890
if let Some(ref c) = self.print_fuel_crate {
889891
if c == crate_name {
890892
assert_eq!(self.query_threads(), 1);
891-
self.print_fuel.set(self.print_fuel.get() + 1);
893+
self.print_fuel.store(self.print_fuel.load(SeqCst) + 1, SeqCst);
892894
}
893895
}
894896
ret
895897
}
896898

899+
/// We want to know if we're allowed to do an optimization for crate foo from -z fuel=foo=n.
900+
/// This expends fuel if applicable, and records fuel if applicable.
901+
#[inline(always)]
902+
pub fn consider_optimizing<T: Fn() -> String>(&self, crate_name: &str, msg: T) -> bool {
903+
if likely!(self.optimization_fuel_crate.is_none()) {
904+
true
905+
} else {
906+
self.consider_optimizing_cold(crate_name, msg)
907+
}
908+
}
909+
897910
/// Returns the number of query threads that should be used for this
898911
/// compilation
899912
pub fn query_threads_from_opts(opts: &config::Options) -> usize {
@@ -1130,9 +1143,9 @@ pub fn build_session_(
11301143

11311144
let optimization_fuel_crate = sopts.debugging_opts.fuel.as_ref().map(|i| i.0.clone());
11321145
let optimization_fuel_limit =
1133-
LockCell::new(sopts.debugging_opts.fuel.as_ref().map(|i| i.1).unwrap_or(0));
1146+
AtomicU64::new(sopts.debugging_opts.fuel.as_ref().map(|i| i.1).unwrap_or(0));
11341147
let print_fuel_crate = sopts.debugging_opts.print_fuel.clone();
1135-
let print_fuel = LockCell::new(0);
1148+
let print_fuel = AtomicU64::new(0);
11361149

11371150
let working_dir = env::current_dir().unwrap_or_else(|e|
11381151
p_s.span_diagnostic
@@ -1191,7 +1204,7 @@ pub fn build_session_(
11911204
optimization_fuel_limit,
11921205
print_fuel_crate,
11931206
print_fuel,
1194-
out_of_fuel: LockCell::new(false),
1207+
out_of_fuel: AtomicBool::new(false),
11951208
// Note that this is unsafe because it may misinterpret file descriptors
11961209
// on Unix as jobserver file descriptors. We hopefully execute this near
11971210
// the beginning of the process though to ensure we don't get false

src/librustc_data_structures/lib.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
#![feature(allow_internal_unstable)]
3131
#![feature(vec_resize_with)]
3232
#![feature(hash_raw_entry)]
33+
#![feature(stmt_expr_attributes)]
34+
#![feature(core_intrinsics)]
35+
#![feature(integer_atomics)]
3336

3437
#![cfg_attr(unix, feature(libc))]
3538
#![cfg_attr(test, feature(test))]
@@ -58,6 +61,26 @@ extern crate rustc_cratesio_shim;
5861

5962
pub use rustc_serialize::hex::ToHex;
6063

64+
#[macro_export]
65+
macro_rules! likely {
66+
($e:expr) => {
67+
#[allow(unused_unsafe)]
68+
{
69+
unsafe { std::intrinsics::likely($e) }
70+
}
71+
}
72+
}
73+
74+
#[macro_export]
75+
macro_rules! unlikely {
76+
($e:expr) => {
77+
#[allow(unused_unsafe)]
78+
{
79+
unsafe { std::intrinsics::unlikely($e) }
80+
}
81+
}
82+
}
83+
6184
pub mod macros;
6285
pub mod svh;
6386
pub mod base_n;
@@ -80,6 +103,7 @@ pub mod sorted_map;
80103
pub mod sync;
81104
pub mod tiny_list;
82105
pub mod thin_vec;
106+
pub mod vec;
83107
pub mod transitive_relation;
84108
pub mod tuple_slice;
85109
pub use ena::unify;

0 commit comments

Comments
 (0)