2023-03-29 23:57:44 +00:00
|
|
|
//! This module defines various operations and types that are implemented in
|
|
|
|
//! one way for the serial compiler, and another way the parallel compiler.
|
2017-12-03 12:49:01 +00:00
|
|
|
//!
|
2023-03-29 23:57:44 +00:00
|
|
|
//! Operations
|
|
|
|
//! ----------
|
|
|
|
//! The parallel versions of operations use Rayon to execute code in parallel,
|
|
|
|
//! while the serial versions degenerate straightforwardly to serial execution.
|
|
|
|
//! The operations include `join`, `parallel`, `par_iter`, and `par_for_each`.
|
2017-12-03 12:49:01 +00:00
|
|
|
//!
|
2023-03-29 23:57:44 +00:00
|
|
|
//! `rustc_erase_owner!` erases an `OwningRef` owner into `Erased` for the
|
|
|
|
//! serial version and `Erased + Send + Sync` for the parallel version.
|
2017-12-03 12:49:01 +00:00
|
|
|
//!
|
2023-03-29 23:57:44 +00:00
|
|
|
//! Types
|
|
|
|
//! -----
|
|
|
|
//! The parallel versions of types provide various kinds of synchronization,
|
|
|
|
//! while the serial compiler versions do not.
|
2017-12-03 12:49:01 +00:00
|
|
|
//!
|
2023-03-29 23:57:44 +00:00
|
|
|
//! The following table shows how the types are implemented internally. Except
|
|
|
|
//! where noted otherwise, the type in column one is defined as a
|
|
|
|
//! newtype around the type from column two or three.
|
2017-12-03 12:49:01 +00:00
|
|
|
//!
|
2023-03-29 23:57:44 +00:00
|
|
|
//! | Type | Serial version | Parallel version |
|
|
|
|
//! | ----------------------- | ------------------- | ------------------------------- |
|
|
|
|
//! | `Lrc<T>` | `rc::Rc<T>` | `sync::Arc<T>` |
|
|
|
|
//! |` Weak<T>` | `rc::Weak<T>` | `sync::Weak<T>` |
|
|
|
|
//! | | | |
|
|
|
|
//! | `AtomicBool` | `Cell<bool>` | `atomic::AtomicBool` |
|
|
|
|
//! | `AtomicU32` | `Cell<u32>` | `atomic::AtomicU32` |
|
|
|
|
//! | `AtomicU64` | `Cell<u64>` | `atomic::AtomicU64` |
|
|
|
|
//! | `AtomicUsize` | `Cell<usize>` | `atomic::AtomicUsize` |
|
|
|
|
//! | | | |
|
|
|
|
//! | `Lock<T>` | `RefCell<T>` | `parking_lot::Mutex<T>` |
|
|
|
|
//! | `RwLock<T>` | `RefCell<T>` | `parking_lot::RwLock<T>` |
|
|
|
|
//! | `MTLock<T>` [^1] | `T` | `Lock<T>` |
|
|
|
|
//! | `MTLockRef<'a, T>` [^2] | `&'a mut MTLock<T>` | `&'a MTLock<T>` |
|
|
|
|
//! | | | |
|
|
|
|
//! | `ParallelIterator` | `Iterator` | `rayon::iter::ParallelIterator` |
|
2018-06-08 15:48:31 +00:00
|
|
|
//!
|
2023-03-29 23:57:44 +00:00
|
|
|
//! [^1] `MTLock` is similar to `Lock`, but the serial version avoids the cost
|
|
|
|
//! of a `RefCell`. This is appropriate when interior mutability is not
|
|
|
|
//! required.
|
|
|
|
//!
|
|
|
|
//! [^2] `MTLockRef` is a typedef.
|
2017-12-03 12:49:01 +00:00
|
|
|
|
2019-02-08 16:36:22 +00:00
|
|
|
use crate::owning_ref::{Erased, OwningRef};
|
2018-04-01 08:25:16 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::hash::{BuildHasher, Hash};
|
2018-04-01 07:43:19 +00:00
|
|
|
use std::ops::{Deref, DerefMut};
|
2022-06-27 08:39:10 +00:00
|
|
|
use std::panic::{catch_unwind, resume_unwind, AssertUnwindSafe};
|
2017-12-03 12:49:01 +00:00
|
|
|
|
2018-12-05 15:51:58 +00:00
|
|
|
pub use std::sync::atomic::Ordering;
|
|
|
|
pub use std::sync::atomic::Ordering::SeqCst;
|
|
|
|
|
2023-03-14 11:51:00 +00:00
|
|
|
pub use vec::{AppendOnlyIndexVec, AppendOnlyVec};
|
2023-02-21 08:37:10 +00:00
|
|
|
|
|
|
|
mod vec;
|
|
|
|
|
2017-12-03 12:49:01 +00:00
|
|
|
cfg_if! {
|
2019-01-28 14:51:47 +00:00
|
|
|
if #[cfg(not(parallel_compiler))] {
|
2017-12-03 12:49:01 +00:00
|
|
|
pub auto trait Send {}
|
|
|
|
pub auto trait Sync {}
|
|
|
|
|
2023-01-19 06:43:05 +00:00
|
|
|
impl<T> Send for T {}
|
|
|
|
impl<T> Sync for T {}
|
2017-12-03 12:49:01 +00:00
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! rustc_erase_owner {
|
|
|
|
($v:expr) => {
|
|
|
|
$v.erase_owner()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 15:51:58 +00:00
|
|
|
use std::ops::Add;
|
|
|
|
|
2019-03-15 11:17:11 +00:00
|
|
|
/// This is a single threaded variant of `AtomicU64`, `AtomicUsize`, etc.
|
2021-03-31 16:29:34 +00:00
|
|
|
/// It has explicit ordering arguments and is only intended for use with
|
|
|
|
/// the native atomic types.
|
2019-03-15 11:17:11 +00:00
|
|
|
/// You should use this type through the `AtomicU64`, `AtomicUsize`, etc, type aliases
|
|
|
|
/// as it's not intended to be used separately.
|
2022-08-30 00:30:25 +00:00
|
|
|
#[derive(Debug, Default)]
|
2018-12-05 15:51:58 +00:00
|
|
|
pub struct Atomic<T: Copy>(Cell<T>);
|
|
|
|
|
|
|
|
impl<T: Copy> Atomic<T> {
|
2018-12-22 17:03:40 +00:00
|
|
|
#[inline]
|
2018-12-05 15:51:58 +00:00
|
|
|
pub fn new(v: T) -> Self {
|
|
|
|
Atomic(Cell::new(v))
|
|
|
|
}
|
|
|
|
|
2021-03-31 16:29:34 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn into_inner(self) -> T {
|
|
|
|
self.0.into_inner()
|
|
|
|
}
|
|
|
|
|
2018-12-22 17:03:40 +00:00
|
|
|
#[inline]
|
2018-12-05 15:51:58 +00:00
|
|
|
pub fn load(&self, _: Ordering) -> T {
|
|
|
|
self.0.get()
|
|
|
|
}
|
|
|
|
|
2018-12-22 17:03:40 +00:00
|
|
|
#[inline]
|
2018-12-05 15:51:58 +00:00
|
|
|
pub fn store(&self, val: T, _: Ordering) {
|
|
|
|
self.0.set(val)
|
|
|
|
}
|
2021-03-31 16:29:34 +00:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn swap(&self, val: T, _: Ordering) -> T {
|
|
|
|
self.0.replace(val)
|
|
|
|
}
|
2019-03-15 11:17:11 +00:00
|
|
|
}
|
2018-12-05 15:51:58 +00:00
|
|
|
|
2019-03-15 11:17:11 +00:00
|
|
|
impl<T: Copy + PartialEq> Atomic<T> {
|
|
|
|
#[inline]
|
2018-12-05 15:51:58 +00:00
|
|
|
pub fn compare_exchange(&self,
|
|
|
|
current: T,
|
|
|
|
new: T,
|
|
|
|
_: Ordering,
|
|
|
|
_: Ordering)
|
|
|
|
-> Result<T, T> {
|
|
|
|
let read = self.0.get();
|
|
|
|
if read == current {
|
|
|
|
self.0.set(new);
|
|
|
|
Ok(read)
|
|
|
|
} else {
|
|
|
|
Err(read)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Add<Output=T> + Copy> Atomic<T> {
|
2019-03-15 11:17:11 +00:00
|
|
|
#[inline]
|
2018-12-05 15:51:58 +00:00
|
|
|
pub fn fetch_add(&self, val: T, _: Ordering) -> T {
|
|
|
|
let old = self.0.get();
|
|
|
|
self.0.set(old + val);
|
|
|
|
old
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub type AtomicUsize = Atomic<usize>;
|
|
|
|
pub type AtomicBool = Atomic<bool>;
|
2018-12-22 17:03:40 +00:00
|
|
|
pub type AtomicU32 = Atomic<u32>;
|
2018-12-05 15:51:58 +00:00
|
|
|
pub type AtomicU64 = Atomic<u64>;
|
|
|
|
|
2019-10-14 20:49:47 +00:00
|
|
|
pub fn join<A, B, RA, RB>(oper_a: A, oper_b: B) -> (RA, RB)
|
|
|
|
where A: FnOnce() -> RA,
|
|
|
|
B: FnOnce() -> RB
|
|
|
|
{
|
|
|
|
(oper_a(), oper_b())
|
|
|
|
}
|
|
|
|
|
2019-01-29 15:47:30 +00:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! parallel {
|
|
|
|
($($blocks:tt),*) => {
|
2019-03-06 03:46:46 +00:00
|
|
|
// We catch panics here ensuring that all the blocks execute.
|
|
|
|
// This makes behavior consistent with the parallel compiler.
|
2019-02-24 21:37:55 +00:00
|
|
|
let mut panic = None;
|
|
|
|
$(
|
|
|
|
if let Err(p) = ::std::panic::catch_unwind(
|
|
|
|
::std::panic::AssertUnwindSafe(|| $blocks)
|
|
|
|
) {
|
|
|
|
if panic.is_none() {
|
|
|
|
panic = Some(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)*
|
|
|
|
if let Some(panic) = panic {
|
|
|
|
::std::panic::resume_unwind(panic);
|
|
|
|
}
|
2019-01-29 15:47:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-09 00:14:43 +00:00
|
|
|
pub use Iterator as ParallelIterator;
|
2018-04-25 22:50:33 +00:00
|
|
|
|
|
|
|
pub fn par_iter<T: IntoIterator>(t: T) -> T::IntoIter {
|
|
|
|
t.into_iter()
|
|
|
|
}
|
|
|
|
|
2022-07-19 09:00:51 +00:00
|
|
|
pub fn par_for_each_in<T: IntoIterator>(t: T, mut for_each: impl FnMut(T::Item) + Sync + Send) {
|
2019-03-06 03:46:46 +00:00
|
|
|
// We catch panics here ensuring that all the loop iterations execute.
|
|
|
|
// This makes behavior consistent with the parallel compiler.
|
2019-02-24 21:37:55 +00:00
|
|
|
let mut panic = None;
|
|
|
|
t.into_iter().for_each(|i| {
|
|
|
|
if let Err(p) = catch_unwind(AssertUnwindSafe(|| for_each(i))) {
|
|
|
|
if panic.is_none() {
|
|
|
|
panic = Some(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if let Some(panic) = panic {
|
|
|
|
resume_unwind(panic);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-11 11:58:27 +00:00
|
|
|
pub type MetadataRef = OwningRef<Box<dyn Erased>, [u8]>;
|
2017-12-03 12:49:01 +00:00
|
|
|
|
|
|
|
pub use std::rc::Rc as Lrc;
|
2018-04-25 22:50:33 +00:00
|
|
|
pub use std::rc::Weak as Weak;
|
2017-12-03 12:49:01 +00:00
|
|
|
pub use std::cell::Ref as ReadGuard;
|
2018-08-04 22:24:39 +00:00
|
|
|
pub use std::cell::Ref as MappedReadGuard;
|
2017-12-03 12:49:01 +00:00
|
|
|
pub use std::cell::RefMut as WriteGuard;
|
2018-08-04 22:24:39 +00:00
|
|
|
pub use std::cell::RefMut as MappedWriteGuard;
|
2017-12-03 12:49:01 +00:00
|
|
|
pub use std::cell::RefMut as LockGuard;
|
2018-08-04 22:24:39 +00:00
|
|
|
pub use std::cell::RefMut as MappedLockGuard;
|
2017-12-03 12:49:01 +00:00
|
|
|
|
2022-06-16 15:41:40 +00:00
|
|
|
pub use std::cell::OnceCell;
|
2020-05-16 04:43:06 +00:00
|
|
|
|
2018-03-08 03:50:43 +00:00
|
|
|
use std::cell::RefCell as InnerRwLock;
|
2017-12-03 12:49:01 +00:00
|
|
|
use std::cell::RefCell as InnerLock;
|
|
|
|
|
|
|
|
use std::cell::Cell;
|
|
|
|
|
2018-05-11 14:28:28 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct WorkerLocal<T>(OneThread<T>);
|
|
|
|
|
|
|
|
impl<T> WorkerLocal<T> {
|
|
|
|
/// Creates a new worker local where the `initial` closure computes the
|
|
|
|
/// value this worker local should take for each thread in the thread pool.
|
|
|
|
#[inline]
|
|
|
|
pub fn new<F: FnMut(usize) -> T>(mut f: F) -> WorkerLocal<T> {
|
|
|
|
WorkerLocal(OneThread::new(f(0)))
|
|
|
|
}
|
2021-03-31 16:29:34 +00:00
|
|
|
|
|
|
|
/// Returns the worker-local value for each thread
|
|
|
|
#[inline]
|
|
|
|
pub fn into_inner(self) -> Vec<T> {
|
|
|
|
vec![OneThread::into_inner(self.0)]
|
|
|
|
}
|
2018-05-11 14:28:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Deref for WorkerLocal<T> {
|
|
|
|
type Target = T;
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn deref(&self) -> &T {
|
2022-11-29 11:01:17 +00:00
|
|
|
&self.0
|
2018-05-11 14:28:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-29 23:57:44 +00:00
|
|
|
pub type MTLockRef<'a, T> = &'a mut MTLock<T>;
|
2018-06-08 15:48:31 +00:00
|
|
|
|
2018-10-16 14:57:53 +00:00
|
|
|
#[derive(Debug, Default)]
|
2017-12-03 12:49:01 +00:00
|
|
|
pub struct MTLock<T>(T);
|
|
|
|
|
|
|
|
impl<T> MTLock<T> {
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn new(inner: T) -> Self {
|
|
|
|
MTLock(inner)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn into_inner(self) -> T {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
|
2021-03-31 16:29:34 +00:00
|
|
|
#[inline(always)]
|
|
|
|
pub fn get_mut(&mut self) -> &mut T {
|
|
|
|
&mut self.0
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn lock(&self) -> &T {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
|
2017-12-03 12:49:01 +00:00
|
|
|
#[inline(always)]
|
2018-06-08 15:48:31 +00:00
|
|
|
pub fn lock_mut(&mut self) -> &mut T {
|
|
|
|
&mut self.0
|
2017-12-03 12:49:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Probably a bad idea (in the threaded case)
|
|
|
|
impl<T: Clone> Clone for MTLock<T> {
|
|
|
|
#[inline]
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
MTLock(self.0.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
pub use std::marker::Send as Send;
|
|
|
|
pub use std::marker::Sync as Sync;
|
|
|
|
|
|
|
|
pub use parking_lot::RwLockReadGuard as ReadGuard;
|
2018-08-04 22:24:39 +00:00
|
|
|
pub use parking_lot::MappedRwLockReadGuard as MappedReadGuard;
|
2017-12-03 12:49:01 +00:00
|
|
|
pub use parking_lot::RwLockWriteGuard as WriteGuard;
|
2018-08-04 22:24:39 +00:00
|
|
|
pub use parking_lot::MappedRwLockWriteGuard as MappedWriteGuard;
|
2017-12-03 12:49:01 +00:00
|
|
|
|
|
|
|
pub use parking_lot::MutexGuard as LockGuard;
|
2018-08-04 22:24:39 +00:00
|
|
|
pub use parking_lot::MappedMutexGuard as MappedLockGuard;
|
2017-12-03 12:49:01 +00:00
|
|
|
|
2022-06-16 15:39:39 +00:00
|
|
|
pub use std::sync::OnceLock as OnceCell;
|
2020-05-16 04:43:06 +00:00
|
|
|
|
2019-12-17 21:28:33 +00:00
|
|
|
pub use std::sync::atomic::{AtomicBool, AtomicUsize, AtomicU32, AtomicU64};
|
2018-12-05 15:51:58 +00:00
|
|
|
|
2017-12-03 12:49:01 +00:00
|
|
|
pub use std::sync::Arc as Lrc;
|
2018-04-25 22:50:33 +00:00
|
|
|
pub use std::sync::Weak as Weak;
|
2017-12-03 12:49:01 +00:00
|
|
|
|
2023-03-29 23:57:44 +00:00
|
|
|
pub type MTLockRef<'a, T> = &'a MTLock<T>;
|
2018-06-08 15:48:31 +00:00
|
|
|
|
2018-10-16 14:57:53 +00:00
|
|
|
#[derive(Debug, Default)]
|
2018-06-08 15:48:31 +00:00
|
|
|
pub struct MTLock<T>(Lock<T>);
|
|
|
|
|
|
|
|
impl<T> MTLock<T> {
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn new(inner: T) -> Self {
|
|
|
|
MTLock(Lock::new(inner))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn into_inner(self) -> T {
|
|
|
|
self.0.into_inner()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn get_mut(&mut self) -> &mut T {
|
|
|
|
self.0.get_mut()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2019-02-08 16:36:22 +00:00
|
|
|
pub fn lock(&self) -> LockGuard<'_, T> {
|
2018-06-08 15:48:31 +00:00
|
|
|
self.0.lock()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2019-02-08 16:36:22 +00:00
|
|
|
pub fn lock_mut(&self) -> LockGuard<'_, T> {
|
2018-06-08 15:48:31 +00:00
|
|
|
self.lock()
|
|
|
|
}
|
|
|
|
}
|
2017-12-03 12:49:01 +00:00
|
|
|
|
|
|
|
use parking_lot::Mutex as InnerLock;
|
2018-03-08 03:50:43 +00:00
|
|
|
use parking_lot::RwLock as InnerRwLock;
|
2017-12-03 12:49:01 +00:00
|
|
|
|
2018-04-01 07:43:19 +00:00
|
|
|
use std::thread;
|
2018-04-25 22:50:33 +00:00
|
|
|
pub use rayon::{join, scope};
|
|
|
|
|
2019-03-06 03:46:46 +00:00
|
|
|
/// Runs a list of blocks in parallel. The first block is executed immediately on
|
|
|
|
/// the current thread. Use that for the longest running block.
|
2019-01-29 15:47:30 +00:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! parallel {
|
2019-02-23 15:47:13 +00:00
|
|
|
(impl $fblock:tt [$($c:tt,)*] [$block:tt $(, $rest:tt)*]) => {
|
|
|
|
parallel!(impl $fblock [$block, $($c,)*] [$($rest),*])
|
2019-01-29 15:47:30 +00:00
|
|
|
};
|
2019-02-23 15:47:13 +00:00
|
|
|
(impl $fblock:tt [$($blocks:tt,)*] []) => {
|
2019-01-29 15:47:30 +00:00
|
|
|
::rustc_data_structures::sync::scope(|s| {
|
|
|
|
$(
|
|
|
|
s.spawn(|_| $blocks);
|
|
|
|
)*
|
2019-02-23 15:47:13 +00:00
|
|
|
$fblock;
|
2019-01-29 15:47:30 +00:00
|
|
|
})
|
|
|
|
};
|
2019-02-23 15:47:13 +00:00
|
|
|
($fblock:tt, $($blocks:tt),*) => {
|
2019-03-06 03:46:46 +00:00
|
|
|
// Reverse the order of the later blocks since Rayon executes them in reverse order
|
2019-01-29 15:47:30 +00:00
|
|
|
// when using a single thread. This ensures the execution order matches that
|
|
|
|
// of a single threaded rustc
|
2019-02-23 15:47:13 +00:00
|
|
|
parallel!(impl $fblock [] [$($blocks),*]);
|
2019-01-29 15:47:30 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-05-11 14:28:28 +00:00
|
|
|
pub use rayon_core::WorkerLocal;
|
|
|
|
|
2018-04-25 22:50:33 +00:00
|
|
|
pub use rayon::iter::ParallelIterator;
|
|
|
|
use rayon::iter::IntoParallelIterator;
|
|
|
|
|
|
|
|
pub fn par_iter<T: IntoParallelIterator>(t: T) -> T::Iter {
|
|
|
|
t.into_par_iter()
|
|
|
|
}
|
2018-04-01 07:43:19 +00:00
|
|
|
|
2019-02-24 21:37:55 +00:00
|
|
|
pub fn par_for_each_in<T: IntoParallelIterator>(
|
|
|
|
t: T,
|
2020-02-26 23:08:21 +00:00
|
|
|
for_each: impl Fn(T::Item) + Sync + Send,
|
2019-02-24 21:37:55 +00:00
|
|
|
) {
|
2022-06-27 08:39:10 +00:00
|
|
|
let ps: Vec<_> = t.into_par_iter().map(|i| catch_unwind(AssertUnwindSafe(|| for_each(i)))).collect();
|
|
|
|
ps.into_iter().for_each(|p| if let Err(panic) = p {
|
|
|
|
resume_unwind(panic)
|
|
|
|
});
|
2019-02-24 21:37:55 +00:00
|
|
|
}
|
|
|
|
|
2018-07-11 14:08:38 +00:00
|
|
|
pub type MetadataRef = OwningRef<Box<dyn Erased + Send + Sync>, [u8]>;
|
2017-12-03 12:49:01 +00:00
|
|
|
|
|
|
|
/// This makes locks panic if they are already held.
|
|
|
|
/// It is only useful when you are running in a single thread
|
|
|
|
const ERROR_CHECKING: bool = false;
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! rustc_erase_owner {
|
|
|
|
($v:expr) => {{
|
|
|
|
let v = $v;
|
2018-03-03 05:17:06 +00:00
|
|
|
::rustc_data_structures::sync::assert_send_val(&v);
|
2017-12-03 12:49:01 +00:00
|
|
|
v.erase_send_sync_owner()
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn assert_sync<T: ?Sized + Sync>() {}
|
2019-01-03 17:14:01 +00:00
|
|
|
pub fn assert_send<T: ?Sized + Send>() {}
|
2018-03-03 05:17:06 +00:00
|
|
|
pub fn assert_send_val<T: ?Sized + Send>(_t: &T) {}
|
2017-12-03 12:49:01 +00:00
|
|
|
pub fn assert_send_sync_val<T: ?Sized + Sync + Send>(_t: &T) {}
|
|
|
|
|
2018-04-01 08:25:16 +00:00
|
|
|
pub trait HashMapExt<K, V> {
|
|
|
|
/// Same as HashMap::insert, but it may panic if there's already an
|
|
|
|
/// entry for `key` with a value not equal to `value`
|
|
|
|
fn insert_same(&mut self, key: K, value: V);
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<K: Eq + Hash, V: Eq, S: BuildHasher> HashMapExt<K, V> for HashMap<K, V, S> {
|
|
|
|
fn insert_same(&mut self, key: K, value: V) {
|
|
|
|
self.entry(key).and_modify(|old| assert!(*old == value)).or_insert(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-03 12:49:01 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Lock<T>(InnerLock<T>);
|
|
|
|
|
|
|
|
impl<T> Lock<T> {
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn new(inner: T) -> Self {
|
|
|
|
Lock(InnerLock::new(inner))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn into_inner(self) -> T {
|
|
|
|
self.0.into_inner()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn get_mut(&mut self) -> &mut T {
|
|
|
|
self.0.get_mut()
|
|
|
|
}
|
|
|
|
|
2019-01-28 14:51:47 +00:00
|
|
|
#[cfg(parallel_compiler)]
|
2018-05-27 05:46:19 +00:00
|
|
|
#[inline(always)]
|
2019-02-08 16:36:22 +00:00
|
|
|
pub fn try_lock(&self) -> Option<LockGuard<'_, T>> {
|
2018-05-27 05:46:19 +00:00
|
|
|
self.0.try_lock()
|
|
|
|
}
|
|
|
|
|
2019-01-28 14:51:47 +00:00
|
|
|
#[cfg(not(parallel_compiler))]
|
2018-05-27 05:46:19 +00:00
|
|
|
#[inline(always)]
|
2019-02-08 16:36:22 +00:00
|
|
|
pub fn try_lock(&self) -> Option<LockGuard<'_, T>> {
|
2018-05-27 05:46:19 +00:00
|
|
|
self.0.try_borrow_mut().ok()
|
|
|
|
}
|
|
|
|
|
2019-01-28 14:51:47 +00:00
|
|
|
#[cfg(parallel_compiler)]
|
2017-12-03 12:49:01 +00:00
|
|
|
#[inline(always)]
|
2022-11-01 17:24:51 +00:00
|
|
|
#[track_caller]
|
2019-02-08 16:36:22 +00:00
|
|
|
pub fn lock(&self) -> LockGuard<'_, T> {
|
2017-12-03 12:49:01 +00:00
|
|
|
if ERROR_CHECKING {
|
|
|
|
self.0.try_lock().expect("lock was already held")
|
|
|
|
} else {
|
|
|
|
self.0.lock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-28 14:51:47 +00:00
|
|
|
#[cfg(not(parallel_compiler))]
|
2017-12-03 12:49:01 +00:00
|
|
|
#[inline(always)]
|
2022-11-01 17:24:51 +00:00
|
|
|
#[track_caller]
|
2019-02-08 16:36:22 +00:00
|
|
|
pub fn lock(&self) -> LockGuard<'_, T> {
|
2017-12-03 12:49:01 +00:00
|
|
|
self.0.borrow_mut()
|
|
|
|
}
|
|
|
|
|
2018-03-08 03:50:43 +00:00
|
|
|
#[inline(always)]
|
2022-11-01 17:24:51 +00:00
|
|
|
#[track_caller]
|
2018-03-08 03:50:43 +00:00
|
|
|
pub fn with_lock<F: FnOnce(&mut T) -> R, R>(&self, f: F) -> R {
|
|
|
|
f(&mut *self.lock())
|
|
|
|
}
|
|
|
|
|
2017-12-03 12:49:01 +00:00
|
|
|
#[inline(always)]
|
2022-11-01 17:24:51 +00:00
|
|
|
#[track_caller]
|
2019-02-08 16:36:22 +00:00
|
|
|
pub fn borrow(&self) -> LockGuard<'_, T> {
|
2017-12-03 12:49:01 +00:00
|
|
|
self.lock()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2022-11-01 17:24:51 +00:00
|
|
|
#[track_caller]
|
2019-02-08 16:36:22 +00:00
|
|
|
pub fn borrow_mut(&self) -> LockGuard<'_, T> {
|
2017-12-03 12:49:01 +00:00
|
|
|
self.lock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-14 19:13:42 +00:00
|
|
|
impl<T: Default> Default for Lock<T> {
|
|
|
|
#[inline]
|
|
|
|
fn default() -> Self {
|
|
|
|
Lock::new(T::default())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-29 00:00:00 +00:00
|
|
|
#[derive(Debug, Default)]
|
2018-03-08 03:50:43 +00:00
|
|
|
pub struct RwLock<T>(InnerRwLock<T>);
|
|
|
|
|
|
|
|
impl<T> RwLock<T> {
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn new(inner: T) -> Self {
|
|
|
|
RwLock(InnerRwLock::new(inner))
|
|
|
|
}
|
|
|
|
|
2021-03-31 16:29:34 +00:00
|
|
|
#[inline(always)]
|
|
|
|
pub fn into_inner(self) -> T {
|
|
|
|
self.0.into_inner()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn get_mut(&mut self) -> &mut T {
|
|
|
|
self.0.get_mut()
|
|
|
|
}
|
|
|
|
|
2019-01-28 14:51:47 +00:00
|
|
|
#[cfg(not(parallel_compiler))]
|
2018-03-08 03:50:43 +00:00
|
|
|
#[inline(always)]
|
2022-11-01 17:24:51 +00:00
|
|
|
#[track_caller]
|
2019-02-08 16:36:22 +00:00
|
|
|
pub fn read(&self) -> ReadGuard<'_, T> {
|
2018-03-08 03:50:43 +00:00
|
|
|
self.0.borrow()
|
|
|
|
}
|
|
|
|
|
2019-01-28 14:51:47 +00:00
|
|
|
#[cfg(parallel_compiler)]
|
2018-03-08 03:50:43 +00:00
|
|
|
#[inline(always)]
|
2019-02-08 16:36:22 +00:00
|
|
|
pub fn read(&self) -> ReadGuard<'_, T> {
|
2018-03-08 03:50:43 +00:00
|
|
|
if ERROR_CHECKING {
|
|
|
|
self.0.try_read().expect("lock was already held")
|
|
|
|
} else {
|
|
|
|
self.0.read()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-31 16:29:34 +00:00
|
|
|
#[inline(always)]
|
2022-11-01 17:24:51 +00:00
|
|
|
#[track_caller]
|
2021-03-31 16:29:34 +00:00
|
|
|
pub fn with_read_lock<F: FnOnce(&T) -> R, R>(&self, f: F) -> R {
|
|
|
|
f(&*self.read())
|
|
|
|
}
|
|
|
|
|
2019-01-28 14:51:47 +00:00
|
|
|
#[cfg(not(parallel_compiler))]
|
2018-03-26 18:52:59 +00:00
|
|
|
#[inline(always)]
|
2019-02-08 16:36:22 +00:00
|
|
|
pub fn try_write(&self) -> Result<WriteGuard<'_, T>, ()> {
|
2018-03-26 18:52:59 +00:00
|
|
|
self.0.try_borrow_mut().map_err(|_| ())
|
|
|
|
}
|
|
|
|
|
2019-01-28 14:51:47 +00:00
|
|
|
#[cfg(parallel_compiler)]
|
2018-03-26 18:52:59 +00:00
|
|
|
#[inline(always)]
|
2019-02-08 16:36:22 +00:00
|
|
|
pub fn try_write(&self) -> Result<WriteGuard<'_, T>, ()> {
|
2018-03-26 18:52:59 +00:00
|
|
|
self.0.try_write().ok_or(())
|
|
|
|
}
|
|
|
|
|
2019-01-28 14:51:47 +00:00
|
|
|
#[cfg(not(parallel_compiler))]
|
2018-03-08 03:50:43 +00:00
|
|
|
#[inline(always)]
|
2022-11-01 17:24:51 +00:00
|
|
|
#[track_caller]
|
2019-02-08 16:36:22 +00:00
|
|
|
pub fn write(&self) -> WriteGuard<'_, T> {
|
2018-03-08 03:50:43 +00:00
|
|
|
self.0.borrow_mut()
|
|
|
|
}
|
|
|
|
|
2019-01-28 14:51:47 +00:00
|
|
|
#[cfg(parallel_compiler)]
|
2018-03-08 03:50:43 +00:00
|
|
|
#[inline(always)]
|
2019-02-08 16:36:22 +00:00
|
|
|
pub fn write(&self) -> WriteGuard<'_, T> {
|
2018-03-08 03:50:43 +00:00
|
|
|
if ERROR_CHECKING {
|
|
|
|
self.0.try_write().expect("lock was already held")
|
|
|
|
} else {
|
|
|
|
self.0.write()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-31 16:29:34 +00:00
|
|
|
#[inline(always)]
|
2022-11-01 17:24:51 +00:00
|
|
|
#[track_caller]
|
2021-03-31 16:29:34 +00:00
|
|
|
pub fn with_write_lock<F: FnOnce(&mut T) -> R, R>(&self, f: F) -> R {
|
|
|
|
f(&mut *self.write())
|
|
|
|
}
|
|
|
|
|
2018-03-08 03:50:43 +00:00
|
|
|
#[inline(always)]
|
2022-11-01 17:24:51 +00:00
|
|
|
#[track_caller]
|
2019-02-08 16:36:22 +00:00
|
|
|
pub fn borrow(&self) -> ReadGuard<'_, T> {
|
2018-03-08 03:50:43 +00:00
|
|
|
self.read()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2022-11-01 17:24:51 +00:00
|
|
|
#[track_caller]
|
2019-02-08 16:36:22 +00:00
|
|
|
pub fn borrow_mut(&self) -> WriteGuard<'_, T> {
|
2018-03-08 03:50:43 +00:00
|
|
|
self.write()
|
|
|
|
}
|
2021-07-12 20:19:25 +00:00
|
|
|
|
|
|
|
#[cfg(not(parallel_compiler))]
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn leak(&self) -> &T {
|
|
|
|
ReadGuard::leak(self.read())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(parallel_compiler)]
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn leak(&self) -> &T {
|
|
|
|
let guard = self.read();
|
|
|
|
let ret = unsafe { &*(&*guard as *const T) };
|
|
|
|
std::mem::forget(guard);
|
|
|
|
ret
|
|
|
|
}
|
2018-03-08 03:50:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Probably a bad idea
|
|
|
|
impl<T: Clone> Clone for RwLock<T> {
|
|
|
|
#[inline]
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
RwLock::new(self.borrow().clone())
|
|
|
|
}
|
|
|
|
}
|
2018-04-01 07:43:19 +00:00
|
|
|
|
|
|
|
/// A type which only allows its inner value to be used in one thread.
|
|
|
|
/// It will panic if it is used on multiple threads.
|
2019-10-20 04:54:53 +00:00
|
|
|
#[derive(Debug)]
|
2018-04-01 07:43:19 +00:00
|
|
|
pub struct OneThread<T> {
|
2019-01-28 14:51:47 +00:00
|
|
|
#[cfg(parallel_compiler)]
|
2018-04-01 07:43:19 +00:00
|
|
|
thread: thread::ThreadId,
|
|
|
|
inner: T,
|
|
|
|
}
|
|
|
|
|
2019-01-28 14:51:47 +00:00
|
|
|
#[cfg(parallel_compiler)]
|
2018-04-01 07:43:19 +00:00
|
|
|
unsafe impl<T> std::marker::Sync for OneThread<T> {}
|
2019-01-28 14:51:47 +00:00
|
|
|
#[cfg(parallel_compiler)]
|
2018-04-01 07:43:19 +00:00
|
|
|
unsafe impl<T> std::marker::Send for OneThread<T> {}
|
|
|
|
|
|
|
|
impl<T> OneThread<T> {
|
|
|
|
#[inline(always)]
|
|
|
|
fn check(&self) {
|
2019-01-28 14:51:47 +00:00
|
|
|
#[cfg(parallel_compiler)]
|
2018-04-01 07:43:19 +00:00
|
|
|
assert_eq!(thread::current().id(), self.thread);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn new(inner: T) -> Self {
|
|
|
|
OneThread {
|
2019-01-28 14:51:47 +00:00
|
|
|
#[cfg(parallel_compiler)]
|
2018-04-01 07:43:19 +00:00
|
|
|
thread: thread::current().id(),
|
|
|
|
inner,
|
|
|
|
}
|
|
|
|
}
|
2021-03-31 16:29:34 +00:00
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn into_inner(value: Self) -> T {
|
|
|
|
value.check();
|
|
|
|
value.inner
|
|
|
|
}
|
2018-04-01 07:43:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Deref for OneThread<T> {
|
|
|
|
type Target = T;
|
|
|
|
|
|
|
|
fn deref(&self) -> &T {
|
|
|
|
self.check();
|
|
|
|
&self.inner
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> DerefMut for OneThread<T> {
|
|
|
|
fn deref_mut(&mut self) -> &mut T {
|
|
|
|
self.check();
|
|
|
|
&mut self.inner
|
|
|
|
}
|
|
|
|
}
|