2017-12-03 13:49:01 +01:00
|
|
|
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
//! This mdoule defines types which are thread safe if cfg!(parallel_queries) is true.
|
|
|
|
//!
|
|
|
|
//! `Lrc` is an alias of either Rc or Arc.
|
|
|
|
//!
|
|
|
|
//! `Lock` is a mutex.
|
|
|
|
//! It internally uses `parking_lot::Mutex` if cfg!(parallel_queries) is true,
|
|
|
|
//! `RefCell` otherwise.
|
|
|
|
//!
|
|
|
|
//! `RwLock` is a read-write lock.
|
|
|
|
//! It internally uses `parking_lot::RwLock` if cfg!(parallel_queries) is true,
|
|
|
|
//! `RefCell` otherwise.
|
|
|
|
//!
|
|
|
|
//! `LockCell` is a thread safe version of `Cell`, with `set` and `get` operations.
|
|
|
|
//! It can never deadlock. It uses `Cell` when
|
|
|
|
//! cfg!(parallel_queries) is false, otherwise it is a `Lock`.
|
|
|
|
//!
|
|
|
|
//! `MTLock` is a mutex which disappears if cfg!(parallel_queries) is false.
|
|
|
|
//!
|
|
|
|
//! `rustc_erase_owner!` erases a OwningRef owner into Erased or Erased + Send + Sync
|
|
|
|
//! depending on the value of cfg!(parallel_queries).
|
|
|
|
|
2018-04-01 10:25:16 +02:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::hash::{Hash, BuildHasher};
|
2017-12-03 13:49:01 +01:00
|
|
|
use std::cmp::Ordering;
|
|
|
|
use std::fmt::Debug;
|
|
|
|
use std::fmt::Formatter;
|
|
|
|
use std::fmt;
|
2018-04-01 09:43:19 +02:00
|
|
|
use std;
|
|
|
|
use std::ops::{Deref, DerefMut};
|
2017-12-03 13:49:01 +01:00
|
|
|
use owning_ref::{Erased, OwningRef};
|
|
|
|
|
|
|
|
cfg_if! {
|
|
|
|
if #[cfg(not(parallel_queries))] {
|
|
|
|
pub auto trait Send {}
|
|
|
|
pub auto trait Sync {}
|
|
|
|
|
|
|
|
impl<T: ?Sized> Send for T {}
|
|
|
|
impl<T: ?Sized> Sync for T {}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! rustc_erase_owner {
|
|
|
|
($v:expr) => {
|
|
|
|
$v.erase_owner()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub type MetadataRef = OwningRef<Box<Erased>, [u8]>;
|
|
|
|
|
|
|
|
pub use std::rc::Rc as Lrc;
|
|
|
|
pub use std::cell::Ref as ReadGuard;
|
|
|
|
pub use std::cell::RefMut as WriteGuard;
|
|
|
|
pub use std::cell::RefMut as LockGuard;
|
|
|
|
|
2018-03-08 04:50:43 +01:00
|
|
|
use std::cell::RefCell as InnerRwLock;
|
2017-12-03 13:49:01 +01:00
|
|
|
use std::cell::RefCell as InnerLock;
|
|
|
|
|
|
|
|
use std::cell::Cell;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn get_mut(&mut self) -> &mut T {
|
|
|
|
&mut self.0
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn lock(&self) -> &T {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn borrow(&self) -> &T {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn borrow_mut(&self) -> &T {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct LockCell<T>(Cell<T>);
|
|
|
|
|
|
|
|
impl<T> LockCell<T> {
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn new(inner: T) -> Self {
|
|
|
|
LockCell(Cell::new(inner))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn into_inner(self) -> T {
|
|
|
|
self.0.into_inner()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn set(&self, new_inner: T) {
|
|
|
|
self.0.set(new_inner);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn get(&self) -> T where T: Copy {
|
|
|
|
self.0.get()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn set_mut(&mut self, new_inner: T) {
|
|
|
|
self.0.set(new_inner);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn get_mut(&mut self) -> T where T: Copy {
|
|
|
|
self.0.get()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> LockCell<Option<T>> {
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn take(&self) -> Option<T> {
|
|
|
|
unsafe { (*self.0.as_ptr()).take() }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
pub use std::marker::Send as Send;
|
|
|
|
pub use std::marker::Sync as Sync;
|
|
|
|
|
|
|
|
pub use parking_lot::RwLockReadGuard as ReadGuard;
|
|
|
|
pub use parking_lot::RwLockWriteGuard as WriteGuard;
|
|
|
|
|
|
|
|
pub use parking_lot::MutexGuard as LockGuard;
|
|
|
|
|
|
|
|
pub use std::sync::Arc as Lrc;
|
|
|
|
|
|
|
|
pub use self::Lock as MTLock;
|
|
|
|
|
|
|
|
use parking_lot::Mutex as InnerLock;
|
2018-03-08 04:50:43 +01:00
|
|
|
use parking_lot::RwLock as InnerRwLock;
|
2017-12-03 13:49:01 +01:00
|
|
|
|
2018-04-01 09:43:19 +02:00
|
|
|
use std::thread;
|
|
|
|
|
2017-12-03 13:49:01 +01:00
|
|
|
pub type MetadataRef = OwningRef<Box<Erased + Send + Sync>, [u8]>;
|
|
|
|
|
|
|
|
/// 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 06:17:06 +01:00
|
|
|
::rustc_data_structures::sync::assert_send_val(&v);
|
2017-12-03 13:49:01 +01:00
|
|
|
v.erase_send_sync_owner()
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct LockCell<T>(Lock<T>);
|
|
|
|
|
|
|
|
impl<T> LockCell<T> {
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn new(inner: T) -> Self {
|
|
|
|
LockCell(Lock::new(inner))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn into_inner(self) -> T {
|
|
|
|
self.0.into_inner()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn set(&self, new_inner: T) {
|
|
|
|
*self.0.lock() = new_inner;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn get(&self) -> T where T: Copy {
|
|
|
|
*self.0.lock()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn set_mut(&mut self, new_inner: T) {
|
|
|
|
*self.0.get_mut() = new_inner;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn get_mut(&mut self) -> T where T: Copy {
|
|
|
|
*self.0.get_mut()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> LockCell<Option<T>> {
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn take(&self) -> Option<T> {
|
|
|
|
self.0.lock().take()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn assert_sync<T: ?Sized + Sync>() {}
|
2018-03-03 06:17:06 +01:00
|
|
|
pub fn assert_send_val<T: ?Sized + Send>(_t: &T) {}
|
2017-12-03 13:49:01 +01:00
|
|
|
pub fn assert_send_sync_val<T: ?Sized + Sync + Send>(_t: &T) {}
|
|
|
|
|
2018-04-01 10:25:16 +02: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 13:49:01 +01:00
|
|
|
impl<T: Copy + Debug> Debug for LockCell<T> {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
|
|
|
f.debug_struct("LockCell")
|
|
|
|
.field("value", &self.get())
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T:Default> Default for LockCell<T> {
|
|
|
|
/// Creates a `LockCell<T>`, with the `Default` value for T.
|
|
|
|
#[inline]
|
|
|
|
fn default() -> LockCell<T> {
|
|
|
|
LockCell::new(Default::default())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T:PartialEq + Copy> PartialEq for LockCell<T> {
|
|
|
|
#[inline]
|
|
|
|
fn eq(&self, other: &LockCell<T>) -> bool {
|
|
|
|
self.get() == other.get()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T:Eq + Copy> Eq for LockCell<T> {}
|
|
|
|
|
|
|
|
impl<T:PartialOrd + Copy> PartialOrd for LockCell<T> {
|
|
|
|
#[inline]
|
|
|
|
fn partial_cmp(&self, other: &LockCell<T>) -> Option<Ordering> {
|
|
|
|
self.get().partial_cmp(&other.get())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn lt(&self, other: &LockCell<T>) -> bool {
|
|
|
|
self.get() < other.get()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn le(&self, other: &LockCell<T>) -> bool {
|
|
|
|
self.get() <= other.get()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn gt(&self, other: &LockCell<T>) -> bool {
|
|
|
|
self.get() > other.get()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn ge(&self, other: &LockCell<T>) -> bool {
|
|
|
|
self.get() >= other.get()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T:Ord + Copy> Ord for LockCell<T> {
|
|
|
|
#[inline]
|
|
|
|
fn cmp(&self, other: &LockCell<T>) -> Ordering {
|
|
|
|
self.get().cmp(&other.get())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(parallel_queries)]
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn lock(&self) -> LockGuard<T> {
|
|
|
|
if ERROR_CHECKING {
|
|
|
|
self.0.try_lock().expect("lock was already held")
|
|
|
|
} else {
|
|
|
|
self.0.lock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(parallel_queries))]
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn lock(&self) -> LockGuard<T> {
|
|
|
|
self.0.borrow_mut()
|
|
|
|
}
|
|
|
|
|
2018-03-08 04:50:43 +01:00
|
|
|
#[inline(always)]
|
|
|
|
pub fn with_lock<F: FnOnce(&mut T) -> R, R>(&self, f: F) -> R {
|
|
|
|
f(&mut *self.lock())
|
|
|
|
}
|
|
|
|
|
2017-12-03 13:49:01 +01:00
|
|
|
#[inline(always)]
|
|
|
|
pub fn borrow(&self) -> LockGuard<T> {
|
|
|
|
self.lock()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn borrow_mut(&self) -> LockGuard<T> {
|
|
|
|
self.lock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-14 20:13:42 +01:00
|
|
|
impl<T: Default> Default for Lock<T> {
|
|
|
|
#[inline]
|
|
|
|
fn default() -> Self {
|
|
|
|
Lock::new(T::default())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-03 13:49:01 +01:00
|
|
|
// FIXME: Probably a bad idea
|
|
|
|
impl<T: Clone> Clone for Lock<T> {
|
|
|
|
#[inline]
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Lock::new(self.borrow().clone())
|
|
|
|
}
|
|
|
|
}
|
2018-03-08 04:50:43 +01:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct RwLock<T>(InnerRwLock<T>);
|
|
|
|
|
|
|
|
impl<T> RwLock<T> {
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn new(inner: T) -> Self {
|
|
|
|
RwLock(InnerRwLock::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()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(parallel_queries))]
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn read(&self) -> ReadGuard<T> {
|
|
|
|
self.0.borrow()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(parallel_queries)]
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn read(&self) -> ReadGuard<T> {
|
|
|
|
if ERROR_CHECKING {
|
|
|
|
self.0.try_read().expect("lock was already held")
|
|
|
|
} else {
|
|
|
|
self.0.read()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn with_read_lock<F: FnOnce(&T) -> R, R>(&self, f: F) -> R {
|
|
|
|
f(&*self.read())
|
|
|
|
}
|
|
|
|
|
2018-03-26 20:52:59 +02:00
|
|
|
#[cfg(not(parallel_queries))]
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn try_write(&self) -> Result<WriteGuard<T>, ()> {
|
|
|
|
self.0.try_borrow_mut().map_err(|_| ())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(parallel_queries)]
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn try_write(&self) -> Result<WriteGuard<T>, ()> {
|
|
|
|
self.0.try_write().ok_or(())
|
|
|
|
}
|
|
|
|
|
2018-03-08 04:50:43 +01:00
|
|
|
#[cfg(not(parallel_queries))]
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn write(&self) -> WriteGuard<T> {
|
|
|
|
self.0.borrow_mut()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(parallel_queries)]
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn write(&self) -> WriteGuard<T> {
|
|
|
|
if ERROR_CHECKING {
|
|
|
|
self.0.try_write().expect("lock was already held")
|
|
|
|
} else {
|
|
|
|
self.0.write()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn with_write_lock<F: FnOnce(&mut T) -> R, R>(&self, f: F) -> R {
|
|
|
|
f(&mut *self.write())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn borrow(&self) -> ReadGuard<T> {
|
|
|
|
self.read()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn borrow_mut(&self) -> WriteGuard<T> {
|
|
|
|
self.write()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 09:43:19 +02: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.
|
|
|
|
#[derive(Copy, Clone, Hash, Debug, Eq, PartialEq)]
|
|
|
|
pub struct OneThread<T> {
|
|
|
|
#[cfg(parallel_queries)]
|
|
|
|
thread: thread::ThreadId,
|
|
|
|
inner: T,
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe impl<T> std::marker::Sync for OneThread<T> {}
|
|
|
|
unsafe impl<T> std::marker::Send for OneThread<T> {}
|
|
|
|
|
|
|
|
impl<T> OneThread<T> {
|
|
|
|
#[inline(always)]
|
|
|
|
fn check(&self) {
|
|
|
|
#[cfg(parallel_queries)]
|
|
|
|
assert_eq!(thread::current().id(), self.thread);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn new(inner: T) -> Self {
|
|
|
|
OneThread {
|
|
|
|
#[cfg(parallel_queries)]
|
|
|
|
thread: thread::current().id(),
|
|
|
|
inner,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn into_inner(value: Self) -> T {
|
|
|
|
value.check();
|
|
|
|
value.inner
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|