rename relative names in sync

This commit is contained in:
SparrowLii 2023-04-04 16:26:00 +08:00
parent f196e27d87
commit 9f8ab2a8d3
3 changed files with 29 additions and 24 deletions

View File

@ -10,7 +10,7 @@ cfg_if!(
message = "`{Self}` doesn't implement `DynSend`. \ message = "`{Self}` doesn't implement `DynSend`. \
Add it to `rustc_data_structures::marker` or use `IntoDyn` if it's already `Send`" Add it to `rustc_data_structures::marker` or use `IntoDyn` if it's already `Send`"
)] )]
// This is an auto trait for types which can be sent across threads if `sync::active()` // This is an auto trait for types which can be sent across threads if `sync::is_dyn_thread_safe()`
// is true. These types can be wrapped in a `FromDyn` to get a `Send` type. Wrapping a // is true. These types can be wrapped in a `FromDyn` to get a `Send` type. Wrapping a
// `Send` type in `IntoDyn` will create a `DynSend` type. // `Send` type in `IntoDyn` will create a `DynSend` type.
pub unsafe auto trait DynSend {} pub unsafe auto trait DynSend {}
@ -19,7 +19,7 @@ cfg_if!(
message = "`{Self}` doesn't implement `DynSync`. \ message = "`{Self}` doesn't implement `DynSync`. \
Add it to `rustc_data_structures::marker` or use `IntoDyn` if it's already `Sync`" Add it to `rustc_data_structures::marker` or use `IntoDyn` if it's already `Sync`"
)] )]
// This is an auto trait for types which can be shared across threads if `sync::active()` // This is an auto trait for types which can be shared across threads if `sync::is_dyn_thread_safe()`
// is true. These types can be wrapped in a `FromDyn` to get a `Sync` type. Wrapping a // is true. These types can be wrapped in a `FromDyn` to get a `Sync` type. Wrapping a
// `Sync` type in `IntoDyn` will create a `DynSync` type. // `Sync` type in `IntoDyn` will create a `DynSync` type.
pub unsafe auto trait DynSync {} pub unsafe auto trait DynSync {}
@ -204,11 +204,11 @@ pub struct FromDyn<T>(T);
impl<T> FromDyn<T> { impl<T> FromDyn<T> {
#[inline(always)] #[inline(always)]
pub fn from(val: T) -> Self { pub fn from(val: T) -> Self {
// Check that `sync::active()` is true on creation so we can // Check that `sync::is_dyn_thread_safe()` is true on creation so we can
// implement `Send` and `Sync` for this structure when `T` // implement `Send` and `Sync` for this structure when `T`
// implements `DynSend` and `DynSync` respectively. // implements `DynSend` and `DynSync` respectively.
#[cfg(parallel_compiler)] #[cfg(parallel_compiler)]
assert!(crate::sync::active()); assert!(crate::sync::is_dyn_thread_safe());
FromDyn(val) FromDyn(val)
} }
@ -218,11 +218,11 @@ impl<T> FromDyn<T> {
} }
} }
// `FromDyn` is `Send` if `T` is `DynSend`, since it ensures that sync::active() is true. // `FromDyn` is `Send` if `T` is `DynSend`, since it ensures that sync::is_dyn_thread_safe() is true.
#[cfg(parallel_compiler)] #[cfg(parallel_compiler)]
unsafe impl<T: DynSend> Send for FromDyn<T> {} unsafe impl<T: DynSend> Send for FromDyn<T> {}
// `FromDyn` is `Sync` if `T` is `DynSync`, since it ensures that sync::active() is true. // `FromDyn` is `Sync` if `T` is `DynSync`, since it ensures that sync::is_dyn_thread_safe() is true.
#[cfg(parallel_compiler)] #[cfg(parallel_compiler)]
unsafe impl<T: DynSync> Sync for FromDyn<T> {} unsafe impl<T: DynSync> Sync for FromDyn<T> {}

View File

@ -61,32 +61,37 @@ mod mode {
use std::sync::atomic::AtomicU8; use std::sync::atomic::AtomicU8;
const UNINITIALIZED: u8 = 0; const UNINITIALIZED: u8 = 0;
const INACTIVE: u8 = 1; const DYN_NOT_SYNC: u8 = 1;
const ACTIVE: u8 = 2; const DYN_SYNC: u8 = 2;
static MODE: AtomicU8 = AtomicU8::new(UNINITIALIZED); static DYN_SYNC_MODE: AtomicU8 = AtomicU8::new(UNINITIALIZED);
// Weather control thread safety dynamically
#[inline] #[inline]
pub fn active() -> bool { pub fn is_dyn_thread_safe() -> bool {
match MODE.load(Ordering::Relaxed) { match DYN_SYNC_MODE.load(Ordering::Relaxed) {
INACTIVE => false, DYN_NOT_SYNC => false,
ACTIVE => true, DYN_SYNC => true,
_ => panic!("uninitialized parallel mode!"), _ => panic!("uninitialized parallel mode!"),
} }
} }
// Only set by the `-Z threads` compile option // Only set by the `-Z threads` compile option
pub fn set(parallel: bool) { pub fn set_dyn_thread_safe_mode(parallel: bool) {
let set: u8 = if parallel { ACTIVE } else { INACTIVE }; let set: u8 = if parallel { DYN_SYNC } else { DYN_NOT_SYNC };
let previous = let previous = DYN_SYNC_MODE.compare_exchange(
MODE.compare_exchange(UNINITIALIZED, set, Ordering::Relaxed, Ordering::Relaxed); UNINITIALIZED,
set,
Ordering::Relaxed,
Ordering::Relaxed,
);
// Check that the mode was either uninitialized or was already set to the requested mode. // Check that the mode was either uninitialized or was already set to the requested mode.
assert!(previous.is_ok() || previous == Err(set)); assert!(previous.is_ok() || previous == Err(set));
} }
} }
pub use mode::{active, set}; pub use mode::{is_dyn_thread_safe, set_dyn_thread_safe_mode};
cfg_if! { cfg_if! {
if #[cfg(not(parallel_compiler))] { if #[cfg(not(parallel_compiler))] {
pub unsafe auto trait Send {} pub unsafe auto trait Send {}
@ -358,7 +363,7 @@ cfg_if! {
A: FnOnce() -> RA + DynSend, A: FnOnce() -> RA + DynSend,
B: FnOnce() -> RB + DynSend, B: FnOnce() -> RB + DynSend,
{ {
if mode::active() { if mode::is_dyn_thread_safe() {
let oper_a = FromDyn::from(oper_a); let oper_a = FromDyn::from(oper_a);
let oper_b = FromDyn::from(oper_b); let oper_b = FromDyn::from(oper_b);
let (a, b) = rayon::join(move || FromDyn::from(oper_a.into_inner()()), move || FromDyn::from(oper_b.into_inner()())); 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! {
} }
} }
// This function only works when `mode::active()`. // This function only works when `mode::is_dyn_thread_safe()`.
pub fn scope<'scope, OP, R>(op: OP) -> R pub fn scope<'scope, OP, R>(op: OP) -> R
where where
OP: FnOnce(&rayon::Scope<'scope>) -> R + DynSend, OP: FnOnce(&rayon::Scope<'scope>) -> R + DynSend,
@ -393,7 +398,7 @@ cfg_if! {
}); });
}; };
($fblock:block, $($blocks:block),*) => { ($fblock:block, $($blocks:block),*) => {
if rustc_data_structures::sync::active() { if rustc_data_structures::sync::is_dyn_thread_safe() {
// Reverse the order of the later blocks since Rayon executes them in reverse order // Reverse the order of the later blocks since Rayon executes them in reverse order
// when using a single thread. This ensures the execution order matches that // when using a single thread. This ensures the execution order matches that
// of a single threaded rustc // of a single threaded rustc
@ -431,7 +436,7 @@ cfg_if! {
t: T, t: T,
for_each: impl Fn(I) + DynSync + DynSend for_each: impl Fn(I) + DynSync + DynSend
) { ) {
if mode::active() { if mode::is_dyn_thread_safe() {
let for_each = FromDyn::from(for_each); let for_each = FromDyn::from(for_each);
let panic: Lock<Option<_>> = Lock::new(None); let panic: Lock<Option<_>> = Lock::new(None);
t.into_par_iter().for_each(|i| if let Err(p) = catch_unwind(AssertUnwindSafe(|| for_each(i))) { t.into_par_iter().for_each(|i| if let Err(p) = catch_unwind(AssertUnwindSafe(|| for_each(i))) {
@ -470,7 +475,7 @@ cfg_if! {
t: T, t: T,
map: impl Fn(I) -> R + DynSync + DynSend map: impl Fn(I) -> R + DynSync + DynSend
) -> C { ) -> C {
if mode::active() { if mode::is_dyn_thread_safe() {
let panic: Lock<Option<_>> = Lock::new(None); let panic: Lock<Option<_>> = Lock::new(None);
let map = FromDyn::from(map); let map = FromDyn::from(map);
// We catch panics here ensuring that all the loop iterations execute. // We catch panics here ensuring that all the loop iterations execute.

View File

@ -62,7 +62,7 @@ impl Compiler {
#[allow(rustc::bad_opt_access)] #[allow(rustc::bad_opt_access)]
pub fn set_parallel_mode(sopts: &config::UnstableOptions) { pub fn set_parallel_mode(sopts: &config::UnstableOptions) {
rustc_data_structures::sync::set(sopts.threads > 1); rustc_data_structures::sync::set_dyn_thread_safe_mode(sopts.threads > 1);
} }
/// Converts strings provided as `--cfg [cfgspec]` into a `crate_cfg`. /// Converts strings provided as `--cfg [cfgspec]` into a `crate_cfg`.