269 lines
10 KiB
Rust
269 lines
10 KiB
Rust
![]() |
cfg_if!(
|
||
|
if #[cfg(not(parallel_compiler))] {
|
||
|
pub auto trait DynSend {}
|
||
|
pub auto trait DynSync {}
|
||
|
|
||
|
impl<T> DynSend for T {}
|
||
|
impl<T> DynSync for T {}
|
||
|
} else {
|
||
|
#[rustc_on_unimplemented(
|
||
|
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`"
|
||
|
)]
|
||
|
// Ensure data structures is `Send` if `sync::active()` is true.
|
||
|
// `sync::active()` should be checked before using these data structures.
|
||
|
// Note: Ensure that the data structure **will not break**
|
||
|
// thread safety after being created.
|
||
|
//
|
||
|
// `sync::active()` should be checked when downcasting these data structures
|
||
|
// to `Send` via `FromDyn`.
|
||
|
pub unsafe auto trait DynSend {}
|
||
|
|
||
|
#[rustc_on_unimplemented(
|
||
|
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`"
|
||
|
)]
|
||
|
// Ensure data structures is `Sync` if `sync::active()` is true.
|
||
|
// Note: Ensure that the data structure **will not break**
|
||
|
// thread safety after being checked.
|
||
|
//
|
||
|
// `sync::active()` should be checked when downcasting these data structures
|
||
|
// to `Send` via `FromDyn`.
|
||
|
pub unsafe auto trait DynSync {}
|
||
|
|
||
|
// Same with `Sync` and `Send`.
|
||
|
unsafe impl<T: DynSync + ?Sized> DynSend for &T {}
|
||
|
|
||
|
macro_rules! impls_dyn_send_neg {
|
||
|
($([$t1: ty $(where $($generics1: tt)*)?])*) => {
|
||
|
$(impl$(<$($generics1)*>)? !DynSend for $t1 {})*
|
||
|
};
|
||
|
}
|
||
|
|
||
|
// Consistent with `std`
|
||
|
impls_dyn_send_neg!(
|
||
|
[std::env::Args]
|
||
|
[std::env::ArgsOs]
|
||
|
[*const T where T: ?Sized]
|
||
|
[*mut T where T: ?Sized]
|
||
|
[std::ptr::NonNull<T> where T: ?Sized]
|
||
|
[std::rc::Rc<T> where T: ?Sized]
|
||
|
[std::rc::Weak<T> where T: ?Sized]
|
||
|
[std::sync::MutexGuard<'_, T> where T: ?Sized]
|
||
|
[std::sync::RwLockReadGuard<'_, T> where T: ?Sized]
|
||
|
[std::sync::RwLockWriteGuard<'_, T> where T: ?Sized]
|
||
|
[std::io::StdoutLock<'_>]
|
||
|
[std::io::StderrLock<'_>]
|
||
|
);
|
||
|
cfg_if!(
|
||
|
// Consistent with `std`
|
||
|
// `os_imp::Env` is `!Send` in these platforms
|
||
|
if #[cfg(any(unix, target_os = "hermit", target_os = "wasi", target_os = "solid_asp3"))] {
|
||
|
impl !DynSend for std::env::VarsOs {}
|
||
|
}
|
||
|
);
|
||
|
|
||
|
macro_rules! already_send {
|
||
|
($([$ty: ty])*) => {
|
||
|
$(unsafe impl DynSend for $ty where $ty: Send {})*
|
||
|
};
|
||
|
}
|
||
|
|
||
|
// These structures are already `Send`.
|
||
|
already_send!(
|
||
|
[std::backtrace::Backtrace]
|
||
|
[std::io::Stdout]
|
||
|
[std::io::Stderr]
|
||
|
[std::io::Error]
|
||
|
[std::fs::File]
|
||
|
[rustc_arena::DroplessArena]
|
||
|
[crate::memmap::Mmap]
|
||
|
[crate::profiling::SelfProfiler]
|
||
|
);
|
||
|
|
||
|
macro_rules! impl_dyn_send {
|
||
|
($($($attr: meta)* [$ty: ty where $($generics2: tt)*])*) => {
|
||
|
$(unsafe impl<$($generics2)*> DynSend for $ty {})*
|
||
|
};
|
||
|
}
|
||
|
|
||
|
impl_dyn_send!(
|
||
|
[std::sync::atomic::AtomicPtr<T> where T]
|
||
|
[std::sync::Mutex<T> where T: ?Sized+ DynSend]
|
||
|
[std::sync::mpsc::Sender<T> where T: DynSend]
|
||
|
[std::sync::Arc<T> where T: ?Sized + DynSync + DynSend]
|
||
|
[std::sync::LazyLock<T, F> where T: DynSend, F: DynSend]
|
||
|
[std::collections::HashSet<K, S> where K: DynSend, S: DynSend]
|
||
|
[std::collections::HashMap<K, V, S> where K: DynSend, V: DynSend, S: DynSend]
|
||
|
[std::collections::BTreeMap<K, V, A> where K: DynSend, V: DynSend, A: std::alloc::Allocator + Clone + DynSend]
|
||
|
[Vec<T, A> where T: DynSend, A: std::alloc::Allocator + DynSend]
|
||
|
[Box<T, A> where T: ?Sized + DynSend, A: std::alloc::Allocator + DynSend]
|
||
|
[crate::sync::Lock<T> where T: DynSend]
|
||
|
[crate::sync::RwLock<T> where T: DynSend]
|
||
|
[rustc_arena::TypedArena<T> where T: DynSend]
|
||
|
[indexmap::IndexSet<V, S> where V: DynSend, S: DynSend]
|
||
|
[indexmap::IndexMap<K, V, S> where K: DynSend, V: DynSend, S: DynSend]
|
||
|
[thin_vec::ThinVec<T> where T: DynSend]
|
||
|
[smallvec::SmallVec<A> where A: smallvec::Array + DynSend]
|
||
|
|
||
|
// We use `Send` here to omit some extra code, since they are only
|
||
|
// used in `Send` situations now.
|
||
|
[crate::owning_ref::OwningRef<O, T> where O: Send, T: ?Sized + Send]
|
||
|
[crate::owning_ref::OwningRefMut<O, T> where O: Send, T: ?Sized + Send]
|
||
|
);
|
||
|
|
||
|
macro_rules! impls_dyn_sync_neg {
|
||
|
($([$t1: ty $(where $($generics1: tt)*)?])*) => {
|
||
|
$(impl$(<$($generics1)*>)? !DynSync for $t1 {})*
|
||
|
};
|
||
|
}
|
||
|
|
||
|
// Consistent with `std`
|
||
|
impls_dyn_sync_neg!(
|
||
|
[std::env::Args]
|
||
|
[std::env::ArgsOs]
|
||
|
[*const T where T: ?Sized]
|
||
|
[*mut T where T: ?Sized]
|
||
|
[std::cell::Cell<T> where T: ?Sized]
|
||
|
[std::cell::RefCell<T> where T: ?Sized]
|
||
|
[std::cell::UnsafeCell<T> where T: ?Sized]
|
||
|
[std::ptr::NonNull<T> where T: ?Sized]
|
||
|
[std::rc::Rc<T> where T: ?Sized]
|
||
|
[std::rc::Weak<T> where T: ?Sized]
|
||
|
[std::cell::OnceCell<T> where T]
|
||
|
[std::sync::mpsc::Receiver<T> where T]
|
||
|
[std::sync::mpsc::Sender<T> where T]
|
||
|
);
|
||
|
cfg_if!(
|
||
|
// Consistent with `std`
|
||
|
// `os_imp::Env` is `!Sync` in these platforms
|
||
|
if #[cfg(any(unix, target_os = "hermit", target_os = "wasi", target_os = "solid_asp3"))] {
|
||
|
impl !DynSync for std::env::VarsOs {}
|
||
|
}
|
||
|
);
|
||
|
|
||
|
macro_rules! already_sync {
|
||
|
($([$ty: ty])*) => {
|
||
|
$(unsafe impl DynSync for $ty where $ty: Sync {})*
|
||
|
};
|
||
|
}
|
||
|
|
||
|
// These structures are already `Sync`.
|
||
|
already_sync!(
|
||
|
[std::sync::atomic::AtomicBool]
|
||
|
[std::sync::atomic::AtomicUsize]
|
||
|
[std::sync::atomic::AtomicU8]
|
||
|
[std::sync::atomic::AtomicU32]
|
||
|
[std::sync::atomic::AtomicU64]
|
||
|
[std::backtrace::Backtrace]
|
||
|
[std::io::Error]
|
||
|
[std::fs::File]
|
||
|
[jobserver_crate::Client]
|
||
|
[crate::memmap::Mmap]
|
||
|
[crate::profiling::SelfProfiler]
|
||
|
);
|
||
|
|
||
|
macro_rules! impl_dyn_sync {
|
||
|
($($($attr: meta)* [$ty: ty where $($generics2: tt)*])*) => {
|
||
|
$(unsafe impl<$($generics2)*> DynSync for $ty {})*
|
||
|
};
|
||
|
}
|
||
|
|
||
|
impl_dyn_sync!(
|
||
|
[std::sync::atomic::AtomicPtr<T> where T]
|
||
|
[std::sync::OnceLock<T> where T: DynSend + DynSync]
|
||
|
[std::sync::Mutex<T> where T: ?Sized + DynSend]
|
||
|
[std::sync::Arc<T> where T: ?Sized + DynSync + DynSend]
|
||
|
[std::sync::LazyLock<T, F> where T: DynSend + DynSync, F: DynSend]
|
||
|
[std::collections::HashSet<K, S> where K: DynSync, S: DynSync]
|
||
|
[std::collections::HashMap<K, V, S> where K: DynSync, V: DynSync, S: DynSync]
|
||
|
[std::collections::BTreeMap<K, V, A> where K: DynSync, V: DynSync, A: std::alloc::Allocator + Clone + DynSync]
|
||
|
[Vec<T, A> where T: DynSync, A: std::alloc::Allocator + DynSync]
|
||
|
[Box<T, A> where T: ?Sized + DynSync, A: std::alloc::Allocator + DynSync]
|
||
|
[crate::sync::Lock<T> where T: DynSend]
|
||
|
[crate::sync::RwLock<T> where T: DynSend + DynSync]
|
||
|
[crate::sync::OneThread<T> where T]
|
||
|
[crate::sync::WorkerLocal<T> where T: DynSend]
|
||
|
[crate::intern::Interned<'a, T> where 'a, T: DynSync]
|
||
|
[parking_lot::lock_api::Mutex<R, T> where R: DynSync, T: ?Sized + DynSend]
|
||
|
[parking_lot::lock_api::RwLock<R, T> where R: DynSync, T: ?Sized + DynSend + DynSync]
|
||
|
[indexmap::IndexSet<V, S> where V: DynSync, S: DynSync]
|
||
|
[indexmap::IndexMap<K, V, S> where K: DynSync, V: DynSync, S: DynSync]
|
||
|
[smallvec::SmallVec<A> where A: smallvec::Array + DynSync]
|
||
|
[thin_vec::ThinVec<T> where T: DynSync]
|
||
|
|
||
|
// We use `Sync` here to omit some extra code, since they are only
|
||
|
// used in `Sync` situations now.
|
||
|
[crate::owning_ref::OwningRef<O, T> where O: Sync, T: ?Sized + Sync]
|
||
|
[crate::owning_ref::OwningRefMut<O, T> where O: Sync, T: ?Sized + Sync]
|
||
|
);
|
||
|
}
|
||
|
);
|
||
|
|
||
|
pub fn assert_dyn_sync<T: ?Sized + DynSync>() {}
|
||
|
pub fn assert_dyn_send<T: ?Sized + DynSend>() {}
|
||
|
pub fn assert_dyn_send_val<T: ?Sized + DynSend>(_t: &T) {}
|
||
|
pub fn assert_dyn_send_sync_val<T: ?Sized + DynSync + DynSend>(_t: &T) {}
|
||
|
|
||
|
#[derive(Copy, Clone)]
|
||
|
pub struct FromDyn<T>(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)]
|
||
|
pub fn from(val: T) -> Self {
|
||
|
#[cfg(parallel_compiler)]
|
||
|
assert!(crate::sync::active());
|
||
|
FromDyn(val)
|
||
|
}
|
||
|
|
||
|
#[inline(always)]
|
||
|
pub fn into_inner(self) -> T {
|
||
|
self.0
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// `FromDyn` is `Send` if `T` is `DynSend`, since it check when created.
|
||
|
#[cfg(parallel_compiler)]
|
||
|
unsafe impl<T: DynSend> Send for FromDyn<T> {}
|
||
|
|
||
|
// `FromDyn` is `Sync` if `T` is `DynSync`, since it check when created.
|
||
|
#[cfg(parallel_compiler)]
|
||
|
unsafe impl<T: DynSync> Sync for FromDyn<T> {}
|
||
|
|
||
|
impl<T> const std::ops::Deref for FromDyn<T> {
|
||
|
type Target = T;
|
||
|
|
||
|
fn deref(&self) -> &Self::Target {
|
||
|
&self.0
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#[derive(Copy, Clone)]
|
||
|
pub struct IntoDyn<T: ?Sized>(pub T);
|
||
|
|
||
|
#[cfg(parallel_compiler)]
|
||
|
unsafe impl<T: ?Sized + Send> DynSend for IntoDyn<T> {}
|
||
|
#[cfg(parallel_compiler)]
|
||
|
unsafe impl<T: ?Sized + Sync> DynSync for IntoDyn<T> {}
|
||
|
|
||
|
impl<T> const std::ops::Deref for IntoDyn<T> {
|
||
|
type Target = T;
|
||
|
|
||
|
fn deref(&self) -> &T {
|
||
|
&self.0
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl<T> const std::ops::DerefMut for IntoDyn<T> {
|
||
|
fn deref_mut(&mut self) -> &mut T {
|
||
|
&mut self.0
|
||
|
}
|
||
|
}
|