1
Fork 0

fix some nits

This commit is contained in:
SparrowLii 2023-03-13 10:04:56 +08:00
parent b9746ce039
commit 261b727d76
2 changed files with 27 additions and 39 deletions

View file

@ -8,31 +8,20 @@ cfg_if!(
} else { } else {
#[rustc_on_unimplemented( #[rustc_on_unimplemented(
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`",
label = "`{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`"
)] )]
// Ensure data structures is `Send` if `sync::active()` is true. // This is an auto trait for types which can be sent across threads if `sync::active()`
// `sync::active()` should be checked before using these data structures. // is true. These types can be wrapped in a `FromDyn` to get a `Send` type. Wrapping a
// Note: Ensure that the data structure **will not break** // `Send` type in `IntoDyn` will create a `DynSend` type.
// thread safety after being created.
//
// `sync::active()` should be checked when downcasting these data structures
// to `Send` via `FromDyn`.
pub unsafe auto trait DynSend {} pub unsafe auto trait DynSend {}
#[rustc_on_unimplemented( #[rustc_on_unimplemented(
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`",
label = "`{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`"
)] )]
// Ensure data structures is `Sync` if `sync::active()` is true. // This is an auto trait for types which can be shared across threads if `sync::active()`
// Note: Ensure that the data structure **will not break** // is true. These types can be wrapped in a `FromDyn` to get a `Sync` type. Wrapping a
// thread safety after being checked. // `Sync` type in `IntoDyn` will create a `DynSync` type.
//
// `sync::active()` should be checked when downcasting these data structures
// to `Send` via `FromDyn`.
pub unsafe auto trait DynSync {} pub unsafe auto trait DynSync {}
// Same with `Sync` and `Send`. // Same with `Sync` and `Send`.
@ -110,8 +99,8 @@ cfg_if!(
[thin_vec::ThinVec<T> where T: DynSend] [thin_vec::ThinVec<T> where T: DynSend]
[smallvec::SmallVec<A> where A: smallvec::Array + DynSend] [smallvec::SmallVec<A> where A: smallvec::Array + DynSend]
// We use `Send` here to omit some extra code, since they are only // We use `Send` here, since they are only used in `Send` situations now.
// used in `Send` situations now. // In this case we don't need copy or change the codes in `crate::owning_ref`.
[crate::owning_ref::OwningRef<O, T> where O: Send, T: ?Sized + Send] [crate::owning_ref::OwningRef<O, T> where O: Send, T: ?Sized + Send]
[crate::owning_ref::OwningRefMut<O, T> where O: Send, T: ?Sized + Send] [crate::owning_ref::OwningRefMut<O, T> where O: Send, T: ?Sized + Send]
); );
@ -196,8 +185,8 @@ cfg_if!(
[smallvec::SmallVec<A> where A: smallvec::Array + DynSync] [smallvec::SmallVec<A> where A: smallvec::Array + DynSync]
[thin_vec::ThinVec<T> where T: DynSync] [thin_vec::ThinVec<T> where T: DynSync]
// We use `Sync` here to omit some extra code, since they are only // We use `Sync` here, since they are only used in `Sync` situations now.
// used in `Sync` situations now. // In this case we don't need copy or change the codes in `crate::owning_ref`.
[crate::owning_ref::OwningRef<O, T> where O: Sync, T: ?Sized + Sync] [crate::owning_ref::OwningRef<O, T> where O: Sync, T: ?Sized + Sync]
[crate::owning_ref::OwningRefMut<O, T> where O: Sync, T: ?Sized + Sync] [crate::owning_ref::OwningRefMut<O, T> where O: Sync, T: ?Sized + Sync]
); );
@ -213,11 +202,11 @@ pub fn assert_dyn_send_sync_val<T: ?Sized + DynSync + DynSend>(_t: &T) {}
pub struct FromDyn<T>(T); pub struct FromDyn<T>(T);
impl<T> FromDyn<T> { impl<T> FromDyn<T> {
// Check `sync::active()` when creating this structure
// and downcasting to `Send`. So we can ensure it is
// thread-safe.
#[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
// implement `Send` and `Sync` for this structure when `T`
// implements `DynSend` and `DynSync` respectively.
#[cfg(parallel_compiler)] #[cfg(parallel_compiler)]
assert!(crate::sync::active()); assert!(crate::sync::active());
FromDyn(val) FromDyn(val)
@ -229,11 +218,11 @@ impl<T> FromDyn<T> {
} }
} }
// `FromDyn` is `Send` if `T` is `DynSend`, since it check when created. // `FromDyn` is `Send` if `T` is `DynSend`, since it ensures that sync::active() 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 check when created. // `FromDyn` is `Sync` if `T` is `DynSync`, since it ensures that sync::active() 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

@ -181,7 +181,7 @@ cfg_if! {
#[macro_export] #[macro_export]
macro_rules! parallel { macro_rules! parallel {
($($blocks:block),*) => {{ ($($blocks:block),*) => {
// We catch panics here ensuring that all the blocks execute. // We catch panics here ensuring that all the blocks execute.
// This makes behavior consistent with the parallel compiler. // This makes behavior consistent with the parallel compiler.
let mut panic = None; let mut panic = None;
@ -197,7 +197,7 @@ cfg_if! {
if let Some(panic) = panic { if let Some(panic) = panic {
::std::panic::resume_unwind(panic); ::std::panic::resume_unwind(panic);
} }
}} }
} }
pub fn par_for_each_in<T: IntoIterator>(t: T, mut for_each: impl FnMut(T::Item) + Sync + Send) { pub fn par_for_each_in<T: IntoIterator>(t: T, mut for_each: impl FnMut(T::Item) + Sync + Send) {
@ -368,6 +368,7 @@ cfg_if! {
} }
} }
// This function only works when `mode::active()`.
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,
@ -381,24 +382,22 @@ cfg_if! {
/// the current thread. Use that for the longest running block. /// the current thread. Use that for the longest running block.
#[macro_export] #[macro_export]
macro_rules! parallel { macro_rules! parallel {
($fblock:block [$($c:expr,)*] [$block:expr $(, $rest:expr)*]) => { (impl $fblock:block [$($c:expr,)*] [$block:expr $(, $rest:expr)*]) => {
parallel!($fblock [$block, $($c,)*] [$($rest),*]) parallel!(impl $fblock [$block, $($c,)*] [$($rest),*])
}; };
($fblock:block [$($blocks:expr,)*] []) => { (impl $fblock:block [$($blocks:expr,)*] []) => {
{
::rustc_data_structures::sync::scope(|s| { ::rustc_data_structures::sync::scope(|s| {
$(let block = rustc_data_structures::sync::FromDyn::from(|| $blocks); $(let block = rustc_data_structures::sync::FromDyn::from(|| $blocks);
s.spawn(move |_| block.into_inner()());)* s.spawn(move |_| block.into_inner()());)*
(|| $fblock)(); (|| $fblock)();
}); });
}
}; };
($fblock:block, $($blocks:block),*) => { ($fblock:block, $($blocks:block),*) => {
if rustc_data_structures::sync::active() { if rustc_data_structures::sync::active() {
// 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
parallel!($fblock [] [$($blocks),*]); parallel!(impl $fblock [] [$($blocks),*]);
} else { } else {
// We catch panics here ensuring that all the blocks execute. // We catch panics here ensuring that all the blocks execute.
// This makes behavior consistent with the parallel compiler. // This makes behavior consistent with the parallel compiler.