|
1 |
| -//! This module defines types which are thread safe if cfg!(parallel_compiler) is true. |
| 1 | +//! This module defines various operations and types that are implemented in |
| 2 | +//! one way for the serial compiler, and another way the parallel compiler. |
2 | 3 | //!
|
3 |
| -//! `Lrc` is an alias of `Arc` if cfg!(parallel_compiler) is true, `Rc` otherwise. |
| 4 | +//! Operations |
| 5 | +//! ---------- |
| 6 | +//! The parallel versions of operations use Rayon to execute code in parallel, |
| 7 | +//! while the serial versions degenerate straightforwardly to serial execution. |
| 8 | +//! The operations include `join`, `parallel`, `par_iter`, and `par_for_each`. |
4 | 9 | //!
|
5 |
| -//! `Lock` is a mutex. |
6 |
| -//! It internally uses `parking_lot::Mutex` if cfg!(parallel_compiler) is true, |
7 |
| -//! `RefCell` otherwise. |
| 10 | +//! `rustc_erase_owner!` erases an `OwningRef` owner into `Erased` for the |
| 11 | +//! serial version and `Erased + Send + Sync` for the parallel version. |
8 | 12 | //!
|
9 |
| -//! `RwLock` is a read-write lock. |
10 |
| -//! It internally uses `parking_lot::RwLock` if cfg!(parallel_compiler) is true, |
11 |
| -//! `RefCell` otherwise. |
| 13 | +//! Types |
| 14 | +//! ----- |
| 15 | +//! The parallel versions of types provide various kinds of synchronization, |
| 16 | +//! while the serial compiler versions do not. |
12 | 17 | //!
|
13 |
| -//! `MTLock` is a mutex which disappears if cfg!(parallel_compiler) is false. |
| 18 | +//! The following table shows how the types are implemented internally. Except |
| 19 | +//! where noted otherwise, the type in column one is defined as a |
| 20 | +//! newtype around the type from column two or three. |
14 | 21 | //!
|
15 |
| -//! `MTRef` is an immutable reference if cfg!(parallel_compiler), and a mutable reference otherwise. |
| 22 | +//! | Type | Serial version | Parallel version | |
| 23 | +//! | ----------------------- | ------------------- | ------------------------------- | |
| 24 | +//! | `Lrc<T>` | `rc::Rc<T>` | `sync::Arc<T>` | |
| 25 | +//! |` Weak<T>` | `rc::Weak<T>` | `sync::Weak<T>` | |
| 26 | +//! | | | | |
| 27 | +//! | `AtomicBool` | `Cell<bool>` | `atomic::AtomicBool` | |
| 28 | +//! | `AtomicU32` | `Cell<u32>` | `atomic::AtomicU32` | |
| 29 | +//! | `AtomicU64` | `Cell<u64>` | `atomic::AtomicU64` | |
| 30 | +//! | `AtomicUsize` | `Cell<usize>` | `atomic::AtomicUsize` | |
| 31 | +//! | | | | |
| 32 | +//! | `Lock<T>` | `RefCell<T>` | `parking_lot::Mutex<T>` | |
| 33 | +//! | `RwLock<T>` | `RefCell<T>` | `parking_lot::RwLock<T>` | |
| 34 | +//! | `MTLock<T>` [^1] | `T` | `Lock<T>` | |
| 35 | +//! | `MTLockRef<'a, T>` [^2] | `&'a mut MTLock<T>` | `&'a MTLock<T>` | |
| 36 | +//! | | | | |
| 37 | +//! | `ParallelIterator` | `Iterator` | `rayon::iter::ParallelIterator` | |
16 | 38 | //!
|
17 |
| -//! `rustc_erase_owner!` erases an OwningRef owner into Erased or Erased + Send + Sync |
18 |
| -//! depending on the value of cfg!(parallel_compiler). |
| 39 | +//! [^1] `MTLock` is similar to `Lock`, but the serial version avoids the cost |
| 40 | +//! of a `RefCell`. This is appropriate when interior mutability is not |
| 41 | +//! required. |
| 42 | +//! |
| 43 | +//! [^2] `MTLockRef` is a typedef. |
19 | 44 |
|
20 | 45 | use crate::owning_ref::{Erased, OwningRef};
|
21 | 46 | use std::collections::HashMap;
|
@@ -209,7 +234,7 @@ cfg_if! {
|
209 | 234 | }
|
210 | 235 | }
|
211 | 236 |
|
212 |
| - pub type MTRef<'a, T> = &'a mut T; |
| 237 | + pub type MTLockRef<'a, T> = &'a mut MTLock<T>; |
213 | 238 |
|
214 | 239 | #[derive(Debug, Default)]
|
215 | 240 | pub struct MTLock<T>(T);
|
@@ -267,7 +292,7 @@ cfg_if! {
|
267 | 292 | pub use std::sync::Arc as Lrc;
|
268 | 293 | pub use std::sync::Weak as Weak;
|
269 | 294 |
|
270 |
| - pub type MTRef<'a, T> = &'a T; |
| 295 | + pub type MTLockRef<'a, T> = &'a MTLock<T>; |
271 | 296 |
|
272 | 297 | #[derive(Debug, Default)]
|
273 | 298 | pub struct MTLock<T>(Lock<T>);
|
@@ -553,18 +578,6 @@ impl<T> RwLock<T> {
|
553 | 578 | self.write()
|
554 | 579 | }
|
555 | 580 |
|
556 |
| - #[cfg(not(parallel_compiler))] |
557 |
| - #[inline(always)] |
558 |
| - pub fn clone_guard<'a>(rg: &ReadGuard<'a, T>) -> ReadGuard<'a, T> { |
559 |
| - ReadGuard::clone(rg) |
560 |
| - } |
561 |
| - |
562 |
| - #[cfg(parallel_compiler)] |
563 |
| - #[inline(always)] |
564 |
| - pub fn clone_guard<'a>(rg: &ReadGuard<'a, T>) -> ReadGuard<'a, T> { |
565 |
| - ReadGuard::rwlock(&rg).read() |
566 |
| - } |
567 |
| - |
568 | 581 | #[cfg(not(parallel_compiler))]
|
569 | 582 | #[inline(always)]
|
570 | 583 | pub fn leak(&self) -> &T {
|
|
0 commit comments