1 // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! This module defines types which are thread safe if cfg!(parallel_queries) is true.
13 //! `Lrc` is an alias of either Rc or Arc.
15 //! `Lock` is a mutex.
16 //! It internally uses `parking_lot::Mutex` if cfg!(parallel_queries) is true,
17 //! `RefCell` otherwise.
19 //! `RwLock` is a read-write lock.
20 //! It internally uses `parking_lot::RwLock` if cfg!(parallel_queries) is true,
21 //! `RefCell` otherwise.
23 //! `LockCell` is a thread safe version of `Cell`, with `set` and `get` operations.
24 //! It can never deadlock. It uses `Cell` when
25 //! cfg!(parallel_queries) is false, otherwise it is a `Lock`.
27 //! `MTLock` is a mutex which disappears if cfg!(parallel_queries) is false.
29 //! `MTRef` is a immutable reference if cfg!(parallel_queries), and an mutable reference otherwise.
31 //! `rustc_erase_owner!` erases a OwningRef owner into Erased or Erased + Send + Sync
32 //! depending on the value of cfg!(parallel_queries).
34 use std::collections::HashMap;
35 use std::hash::{Hash, BuildHasher};
36 use std::cmp::Ordering;
37 use std::marker::PhantomData;
39 use std::fmt::Formatter;
41 use std::ops::{Deref, DerefMut};
42 use owning_ref::{Erased, OwningRef};
44 pub fn serial_join<A, B, RA, RB>(oper_a: A, oper_b: B) -> (RA, RB)
45 where A: FnOnce() -> RA,
51 pub struct SerialScope;
54 pub fn spawn<F>(&self, f: F)
55 where F: FnOnce(&SerialScope)
61 pub fn serial_scope<F, R>(f: F) -> R
62 where F: FnOnce(&SerialScope) -> R
68 if #[cfg(not(parallel_queries))] {
69 pub auto trait Send {}
70 pub auto trait Sync {}
72 impl<T: ?Sized> Send for T {}
73 impl<T: ?Sized> Sync for T {}
76 macro_rules! rustc_erase_owner {
82 pub use self::serial_join as join;
83 pub use self::serial_scope as scope;
85 pub use std::iter::Iterator as ParallelIterator;
87 pub fn par_iter<T: IntoIterator>(t: T) -> T::IntoIter {
91 pub type MetadataRef = OwningRef<Box<dyn Erased>, [u8]>;
93 pub use std::rc::Rc as Lrc;
94 pub use std::rc::Weak as Weak;
95 pub use std::cell::Ref as ReadGuard;
96 pub use std::cell::Ref as MappedReadGuard;
97 pub use std::cell::RefMut as WriteGuard;
98 pub use std::cell::RefMut as MappedWriteGuard;
99 pub use std::cell::RefMut as LockGuard;
100 pub use std::cell::RefMut as MappedLockGuard;
102 use std::cell::RefCell as InnerRwLock;
103 use std::cell::RefCell as InnerLock;
108 pub struct WorkerLocal<T>(OneThread<T>);
110 impl<T> WorkerLocal<T> {
111 /// Creates a new worker local where the `initial` closure computes the
112 /// value this worker local should take for each thread in the thread pool.
114 pub fn new<F: FnMut(usize) -> T>(mut f: F) -> WorkerLocal<T> {
115 WorkerLocal(OneThread::new(f(0)))
118 /// Returns the worker-local value for each thread
120 pub fn into_inner(self) -> Vec<T> {
121 vec![OneThread::into_inner(self.0)]
125 impl<T> Deref for WorkerLocal<T> {
129 fn deref(&self) -> &T {
134 pub type MTRef<'a, T> = &'a mut T;
137 pub struct MTLock<T>(T);
141 pub fn new(inner: T) -> Self {
146 pub fn into_inner(self) -> T {
151 pub fn get_mut(&mut self) -> &mut T {
156 pub fn lock(&self) -> &T {
161 pub fn lock_mut(&mut self) -> &mut T {
166 // FIXME: Probably a bad idea (in the threaded case)
167 impl<T: Clone> Clone for MTLock<T> {
169 fn clone(&self) -> Self {
170 MTLock(self.0.clone())
174 pub struct LockCell<T>(Cell<T>);
176 impl<T> LockCell<T> {
178 pub fn new(inner: T) -> Self {
179 LockCell(Cell::new(inner))
183 pub fn into_inner(self) -> T {
188 pub fn set(&self, new_inner: T) {
189 self.0.set(new_inner);
193 pub fn get(&self) -> T where T: Copy {
198 pub fn set_mut(&mut self, new_inner: T) {
199 self.0.set(new_inner);
203 pub fn get_mut(&mut self) -> T where T: Copy {
208 impl<T> LockCell<Option<T>> {
210 pub fn take(&self) -> Option<T> {
211 unsafe { (*self.0.as_ptr()).take() }
215 pub use std::marker::Send as Send;
216 pub use std::marker::Sync as Sync;
218 pub use parking_lot::RwLockReadGuard as ReadGuard;
219 pub use parking_lot::MappedRwLockReadGuard as MappedReadGuard;
220 pub use parking_lot::RwLockWriteGuard as WriteGuard;
221 pub use parking_lot::MappedRwLockWriteGuard as MappedWriteGuard;
223 pub use parking_lot::MutexGuard as LockGuard;
224 pub use parking_lot::MappedMutexGuard as MappedLockGuard;
226 pub use std::sync::Arc as Lrc;
227 pub use std::sync::Weak as Weak;
229 pub type MTRef<'a, T> = &'a T;
232 pub struct MTLock<T>(Lock<T>);
236 pub fn new(inner: T) -> Self {
237 MTLock(Lock::new(inner))
241 pub fn into_inner(self) -> T {
246 pub fn get_mut(&mut self) -> &mut T {
251 pub fn lock(&self) -> LockGuard<T> {
256 pub fn lock_mut(&self) -> LockGuard<T> {
261 use parking_lot::Mutex as InnerLock;
262 use parking_lot::RwLock as InnerRwLock;
266 pub use rayon::{join, scope};
268 pub use rayon_core::WorkerLocal;
270 pub use rayon::iter::ParallelIterator;
271 use rayon::iter::IntoParallelIterator;
273 pub fn par_iter<T: IntoParallelIterator>(t: T) -> T::Iter {
277 pub type MetadataRef = OwningRef<Box<dyn Erased + Send + Sync>, [u8]>;
279 /// This makes locks panic if they are already held.
280 /// It is only useful when you are running in a single thread
281 const ERROR_CHECKING: bool = false;
284 macro_rules! rustc_erase_owner {
287 ::rustc_data_structures::sync::assert_send_val(&v);
288 v.erase_send_sync_owner()
292 pub struct LockCell<T>(Lock<T>);
294 impl<T> LockCell<T> {
296 pub fn new(inner: T) -> Self {
297 LockCell(Lock::new(inner))
301 pub fn into_inner(self) -> T {
306 pub fn set(&self, new_inner: T) {
307 *self.0.lock() = new_inner;
311 pub fn get(&self) -> T where T: Copy {
316 pub fn set_mut(&mut self, new_inner: T) {
317 *self.0.get_mut() = new_inner;
321 pub fn get_mut(&mut self) -> T where T: Copy {
326 impl<T> LockCell<Option<T>> {
328 pub fn take(&self) -> Option<T> {
335 pub fn assert_sync<T: ?Sized + Sync>() {}
336 pub fn assert_send_val<T: ?Sized + Send>(_t: &T) {}
337 pub fn assert_send_sync_val<T: ?Sized + Sync + Send>(_t: &T) {}
339 pub trait HashMapExt<K, V> {
340 /// Same as HashMap::insert, but it may panic if there's already an
341 /// entry for `key` with a value not equal to `value`
342 fn insert_same(&mut self, key: K, value: V);
345 impl<K: Eq + Hash, V: Eq, S: BuildHasher> HashMapExt<K, V> for HashMap<K, V, S> {
346 fn insert_same(&mut self, key: K, value: V) {
347 self.entry(key).and_modify(|old| assert!(*old == value)).or_insert(value);
351 /// A type whose inner value can be written once and then will stay read-only
352 // This contains a PhantomData<T> since this type conceptually owns a T outside the Mutex once
353 // initialized. This ensures that Once<T> is Sync only if T is. If we did not have PhantomData<T>
354 // we could send a &Once<Cell<bool>> to multiple threads and call `get` on it to get access
355 // to &Cell<bool> on those threads.
356 pub struct Once<T>(Lock<Option<T>>, PhantomData<T>);
359 /// Creates an Once value which is uninitialized
361 pub fn new() -> Self {
362 Once(Lock::new(None), PhantomData)
365 /// Consumes the value and returns Some(T) if it was initialized
367 pub fn into_inner(self) -> Option<T> {
371 /// Tries to initialize the inner value to `value`.
372 /// Returns `None` if the inner value was uninitialized and `value` was consumed setting it
373 /// otherwise if the inner value was already set it returns `value` back to the caller
375 pub fn try_set(&self, value: T) -> Option<T> {
376 let mut lock = self.0.lock();
384 /// Tries to initialize the inner value to `value`.
385 /// Returns `None` if the inner value was uninitialized and `value` was consumed setting it
386 /// otherwise if the inner value was already set it asserts that `value` is equal to the inner
387 /// value and then returns `value` back to the caller
389 pub fn try_set_same(&self, value: T) -> Option<T> where T: Eq {
390 let mut lock = self.0.lock();
391 if let Some(ref inner) = *lock {
392 assert!(*inner == value);
399 /// Tries to initialize the inner value to `value` and panics if it was already initialized
401 pub fn set(&self, value: T) {
402 assert!(self.try_set(value).is_none());
405 /// Tries to initialize the inner value by calling the closure while ensuring that no-one else
406 /// can access the value in the mean time by holding a lock for the duration of the closure.
407 /// If the value was already initialized the closure is not called and `false` is returned,
408 /// otherwise if the value from the closure initializes the inner value, `true` is returned
410 pub fn init_locking<F: FnOnce() -> T>(&self, f: F) -> bool {
411 let mut lock = self.0.lock();
419 /// Tries to initialize the inner value by calling the closure without ensuring that no-one
420 /// else can access it. This mean when this is called from multiple threads, multiple
421 /// closures may concurrently be computing a value which the inner value should take.
422 /// Only one of these closures are used to actually initialize the value.
423 /// If some other closure already set the value,
424 /// we return the value our closure computed wrapped in a `Option`.
425 /// If our closure set the value, `None` is returned.
426 /// If the value is already initialized, the closure is not called and `None` is returned.
428 pub fn init_nonlocking<F: FnOnce() -> T>(&self, f: F) -> Option<T> {
429 if self.0.lock().is_some() {
436 /// Tries to initialize the inner value by calling the closure without ensuring that no-one
437 /// else can access it. This mean when this is called from multiple threads, multiple
438 /// closures may concurrently be computing a value which the inner value should take.
439 /// Only one of these closures are used to actually initialize the value.
440 /// If some other closure already set the value, we assert that it our closure computed
441 /// a value equal to the value already set and then
442 /// we return the value our closure computed wrapped in a `Option`.
443 /// If our closure set the value, `None` is returned.
444 /// If the value is already initialized, the closure is not called and `None` is returned.
446 pub fn init_nonlocking_same<F: FnOnce() -> T>(&self, f: F) -> Option<T> where T: Eq {
447 if self.0.lock().is_some() {
450 self.try_set_same(f())
454 /// Tries to get a reference to the inner value, returns `None` if it is not yet initialized
456 pub fn try_get(&self) -> Option<&T> {
457 let lock = &*self.0.lock();
458 if let Some(ref inner) = *lock {
459 // This is safe since we won't mutate the inner value
460 unsafe { Some(&*(inner as *const T)) }
466 /// Gets reference to the inner value, panics if it is not yet initialized
468 pub fn get(&self) -> &T {
469 self.try_get().expect("value was not set")
472 /// Gets reference to the inner value, panics if it is not yet initialized
474 pub fn borrow(&self) -> &T {
479 impl<T: Copy + Debug> Debug for LockCell<T> {
480 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
481 f.debug_struct("LockCell")
482 .field("value", &self.get())
487 impl<T:Default> Default for LockCell<T> {
488 /// Creates a `LockCell<T>`, with the `Default` value for T.
490 fn default() -> LockCell<T> {
491 LockCell::new(Default::default())
495 impl<T:PartialEq + Copy> PartialEq for LockCell<T> {
497 fn eq(&self, other: &LockCell<T>) -> bool {
498 self.get() == other.get()
502 impl<T:Eq + Copy> Eq for LockCell<T> {}
504 impl<T:PartialOrd + Copy> PartialOrd for LockCell<T> {
506 fn partial_cmp(&self, other: &LockCell<T>) -> Option<Ordering> {
507 self.get().partial_cmp(&other.get())
511 fn lt(&self, other: &LockCell<T>) -> bool {
512 self.get() < other.get()
516 fn le(&self, other: &LockCell<T>) -> bool {
517 self.get() <= other.get()
521 fn gt(&self, other: &LockCell<T>) -> bool {
522 self.get() > other.get()
526 fn ge(&self, other: &LockCell<T>) -> bool {
527 self.get() >= other.get()
531 impl<T:Ord + Copy> Ord for LockCell<T> {
533 fn cmp(&self, other: &LockCell<T>) -> Ordering {
534 self.get().cmp(&other.get())
539 pub struct Lock<T>(InnerLock<T>);
543 pub fn new(inner: T) -> Self {
544 Lock(InnerLock::new(inner))
548 pub fn into_inner(self) -> T {
553 pub fn get_mut(&mut self) -> &mut T {
557 #[cfg(parallel_queries)]
559 pub fn try_lock(&self) -> Option<LockGuard<T>> {
563 #[cfg(not(parallel_queries))]
565 pub fn try_lock(&self) -> Option<LockGuard<T>> {
566 self.0.try_borrow_mut().ok()
569 #[cfg(parallel_queries)]
571 pub fn lock(&self) -> LockGuard<T> {
573 self.0.try_lock().expect("lock was already held")
579 #[cfg(not(parallel_queries))]
581 pub fn lock(&self) -> LockGuard<T> {
586 pub fn with_lock<F: FnOnce(&mut T) -> R, R>(&self, f: F) -> R {
591 pub fn borrow(&self) -> LockGuard<T> {
596 pub fn borrow_mut(&self) -> LockGuard<T> {
601 impl<T: Default> Default for Lock<T> {
603 fn default() -> Self {
604 Lock::new(T::default())
608 // FIXME: Probably a bad idea
609 impl<T: Clone> Clone for Lock<T> {
611 fn clone(&self) -> Self {
612 Lock::new(self.borrow().clone())
617 pub struct RwLock<T>(InnerRwLock<T>);
621 pub fn new(inner: T) -> Self {
622 RwLock(InnerRwLock::new(inner))
626 pub fn into_inner(self) -> T {
631 pub fn get_mut(&mut self) -> &mut T {
635 #[cfg(not(parallel_queries))]
637 pub fn read(&self) -> ReadGuard<T> {
641 #[cfg(parallel_queries)]
643 pub fn read(&self) -> ReadGuard<T> {
645 self.0.try_read().expect("lock was already held")
652 pub fn with_read_lock<F: FnOnce(&T) -> R, R>(&self, f: F) -> R {
656 #[cfg(not(parallel_queries))]
658 pub fn try_write(&self) -> Result<WriteGuard<T>, ()> {
659 self.0.try_borrow_mut().map_err(|_| ())
662 #[cfg(parallel_queries)]
664 pub fn try_write(&self) -> Result<WriteGuard<T>, ()> {
665 self.0.try_write().ok_or(())
668 #[cfg(not(parallel_queries))]
670 pub fn write(&self) -> WriteGuard<T> {
674 #[cfg(parallel_queries)]
676 pub fn write(&self) -> WriteGuard<T> {
678 self.0.try_write().expect("lock was already held")
685 pub fn with_write_lock<F: FnOnce(&mut T) -> R, R>(&self, f: F) -> R {
686 f(&mut *self.write())
690 pub fn borrow(&self) -> ReadGuard<T> {
695 pub fn borrow_mut(&self) -> WriteGuard<T> {
700 // FIXME: Probably a bad idea
701 impl<T: Clone> Clone for RwLock<T> {
703 fn clone(&self) -> Self {
704 RwLock::new(self.borrow().clone())
708 /// A type which only allows its inner value to be used in one thread.
709 /// It will panic if it is used on multiple threads.
710 #[derive(Copy, Clone, Hash, Debug, Eq, PartialEq)]
711 pub struct OneThread<T> {
712 #[cfg(parallel_queries)]
713 thread: thread::ThreadId,
717 #[cfg(parallel_queries)]
718 unsafe impl<T> std::marker::Sync for OneThread<T> {}
719 #[cfg(parallel_queries)]
720 unsafe impl<T> std::marker::Send for OneThread<T> {}
722 impl<T> OneThread<T> {
725 #[cfg(parallel_queries)]
726 assert_eq!(thread::current().id(), self.thread);
730 pub fn new(inner: T) -> Self {
732 #[cfg(parallel_queries)]
733 thread: thread::current().id(),
739 pub fn into_inner(value: Self) -> T {
745 impl<T> Deref for OneThread<T> {
748 fn deref(&self) -> &T {
754 impl<T> DerefMut for OneThread<T> {
755 fn deref_mut(&mut self) -> &mut T {