Skip to content

Commit 9f8ab2a

Browse files
committed
rename relative names in sync
1 parent f196e27 commit 9f8ab2a

File tree

3 files changed

+29
-24
lines changed

3 files changed

+29
-24
lines changed

compiler/rustc_data_structures/src/marker.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ cfg_if!(
1010
message = "`{Self}` doesn't implement `DynSend`. \
1111
Add it to `rustc_data_structures::marker` or use `IntoDyn` if it's already `Send`"
1212
)]
13-
// This is an auto trait for types which can be sent across threads if `sync::active()`
13+
// This is an auto trait for types which can be sent across threads if `sync::is_dyn_thread_safe()`
1414
// is true. These types can be wrapped in a `FromDyn` to get a `Send` type. Wrapping a
1515
// `Send` type in `IntoDyn` will create a `DynSend` type.
1616
pub unsafe auto trait DynSend {}
@@ -19,7 +19,7 @@ cfg_if!(
1919
message = "`{Self}` doesn't implement `DynSync`. \
2020
Add it to `rustc_data_structures::marker` or use `IntoDyn` if it's already `Sync`"
2121
)]
22-
// This is an auto trait for types which can be shared across threads if `sync::active()`
22+
// This is an auto trait for types which can be shared across threads if `sync::is_dyn_thread_safe()`
2323
// is true. These types can be wrapped in a `FromDyn` to get a `Sync` type. Wrapping a
2424
// `Sync` type in `IntoDyn` will create a `DynSync` type.
2525
pub unsafe auto trait DynSync {}
@@ -204,11 +204,11 @@ pub struct FromDyn<T>(T);
204204
impl<T> FromDyn<T> {
205205
#[inline(always)]
206206
pub fn from(val: T) -> Self {
207-
// Check that `sync::active()` is true on creation so we can
207+
// Check that `sync::is_dyn_thread_safe()` is true on creation so we can
208208
// implement `Send` and `Sync` for this structure when `T`
209209
// implements `DynSend` and `DynSync` respectively.
210210
#[cfg(parallel_compiler)]
211-
assert!(crate::sync::active());
211+
assert!(crate::sync::is_dyn_thread_safe());
212212
FromDyn(val)
213213
}
214214

@@ -218,11 +218,11 @@ impl<T> FromDyn<T> {
218218
}
219219
}
220220

221-
// `FromDyn` is `Send` if `T` is `DynSend`, since it ensures that sync::active() is true.
221+
// `FromDyn` is `Send` if `T` is `DynSend`, since it ensures that sync::is_dyn_thread_safe() is true.
222222
#[cfg(parallel_compiler)]
223223
unsafe impl<T: DynSend> Send for FromDyn<T> {}
224224

225-
// `FromDyn` is `Sync` if `T` is `DynSync`, since it ensures that sync::active() is true.
225+
// `FromDyn` is `Sync` if `T` is `DynSync`, since it ensures that sync::is_dyn_thread_safe() is true.
226226
#[cfg(parallel_compiler)]
227227
unsafe impl<T: DynSync> Sync for FromDyn<T> {}
228228

compiler/rustc_data_structures/src/sync.rs

+22-17
Original file line numberDiff line numberDiff line change
@@ -61,32 +61,37 @@ mod mode {
6161
use std::sync::atomic::AtomicU8;
6262

6363
const UNINITIALIZED: u8 = 0;
64-
const INACTIVE: u8 = 1;
65-
const ACTIVE: u8 = 2;
64+
const DYN_NOT_SYNC: u8 = 1;
65+
const DYN_SYNC: u8 = 2;
6666

67-
static MODE: AtomicU8 = AtomicU8::new(UNINITIALIZED);
67+
static DYN_SYNC_MODE: AtomicU8 = AtomicU8::new(UNINITIALIZED);
6868

69+
// Weather control thread safety dynamically
6970
#[inline]
70-
pub fn active() -> bool {
71-
match MODE.load(Ordering::Relaxed) {
72-
INACTIVE => false,
73-
ACTIVE => true,
71+
pub fn is_dyn_thread_safe() -> bool {
72+
match DYN_SYNC_MODE.load(Ordering::Relaxed) {
73+
DYN_NOT_SYNC => false,
74+
DYN_SYNC => true,
7475
_ => panic!("uninitialized parallel mode!"),
7576
}
7677
}
7778

7879
// Only set by the `-Z threads` compile option
79-
pub fn set(parallel: bool) {
80-
let set: u8 = if parallel { ACTIVE } else { INACTIVE };
81-
let previous =
82-
MODE.compare_exchange(UNINITIALIZED, set, Ordering::Relaxed, Ordering::Relaxed);
80+
pub fn set_dyn_thread_safe_mode(parallel: bool) {
81+
let set: u8 = if parallel { DYN_SYNC } else { DYN_NOT_SYNC };
82+
let previous = DYN_SYNC_MODE.compare_exchange(
83+
UNINITIALIZED,
84+
set,
85+
Ordering::Relaxed,
86+
Ordering::Relaxed,
87+
);
8388

8489
// Check that the mode was either uninitialized or was already set to the requested mode.
8590
assert!(previous.is_ok() || previous == Err(set));
8691
}
8792
}
8893

89-
pub use mode::{active, set};
94+
pub use mode::{is_dyn_thread_safe, set_dyn_thread_safe_mode};
9095
cfg_if! {
9196
if #[cfg(not(parallel_compiler))] {
9297
pub unsafe auto trait Send {}
@@ -358,7 +363,7 @@ cfg_if! {
358363
A: FnOnce() -> RA + DynSend,
359364
B: FnOnce() -> RB + DynSend,
360365
{
361-
if mode::active() {
366+
if mode::is_dyn_thread_safe() {
362367
let oper_a = FromDyn::from(oper_a);
363368
let oper_b = FromDyn::from(oper_b);
364369
let (a, b) = rayon::join(move || FromDyn::from(oper_a.into_inner()()), move || FromDyn::from(oper_b.into_inner()()));
@@ -368,7 +373,7 @@ cfg_if! {
368373
}
369374
}
370375

371-
// This function only works when `mode::active()`.
376+
// This function only works when `mode::is_dyn_thread_safe()`.
372377
pub fn scope<'scope, OP, R>(op: OP) -> R
373378
where
374379
OP: FnOnce(&rayon::Scope<'scope>) -> R + DynSend,
@@ -393,7 +398,7 @@ cfg_if! {
393398
});
394399
};
395400
($fblock:block, $($blocks:block),*) => {
396-
if rustc_data_structures::sync::active() {
401+
if rustc_data_structures::sync::is_dyn_thread_safe() {
397402
// Reverse the order of the later blocks since Rayon executes them in reverse order
398403
// when using a single thread. This ensures the execution order matches that
399404
// of a single threaded rustc
@@ -431,7 +436,7 @@ cfg_if! {
431436
t: T,
432437
for_each: impl Fn(I) + DynSync + DynSend
433438
) {
434-
if mode::active() {
439+
if mode::is_dyn_thread_safe() {
435440
let for_each = FromDyn::from(for_each);
436441
let panic: Lock<Option<_>> = Lock::new(None);
437442
t.into_par_iter().for_each(|i| if let Err(p) = catch_unwind(AssertUnwindSafe(|| for_each(i))) {
@@ -470,7 +475,7 @@ cfg_if! {
470475
t: T,
471476
map: impl Fn(I) -> R + DynSync + DynSend
472477
) -> C {
473-
if mode::active() {
478+
if mode::is_dyn_thread_safe() {
474479
let panic: Lock<Option<_>> = Lock::new(None);
475480
let map = FromDyn::from(map);
476481
// We catch panics here ensuring that all the loop iterations execute.

compiler/rustc_interface/src/interface.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl Compiler {
6262

6363
#[allow(rustc::bad_opt_access)]
6464
pub fn set_parallel_mode(sopts: &config::UnstableOptions) {
65-
rustc_data_structures::sync::set(sopts.threads > 1);
65+
rustc_data_structures::sync::set_dyn_thread_safe_mode(sopts.threads > 1);
6666
}
6767

6868
/// Converts strings provided as `--cfg [cfgspec]` into a `crate_cfg`.

0 commit comments

Comments
 (0)