diff options
| author | Manish Goregaokar <manishsmail@gmail.com> | 2018-03-08 11:26:02 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-03-08 11:26:02 -0800 |
| commit | 457975369b5cdca62a7708f46f684dfec67b3c67 (patch) | |
| tree | 526c8b74176fe716194e4f5c420ca3b8e08e4fcc /src/librustc_data_structures/sync.rs | |
| parent | d17eb8f68edd459ddd4cc3a9d92125f9e6e9df46 (diff) | |
| parent | 728c16c88f8db0c914cecc8b20b7f851d936fd5a (diff) | |
| download | rust-457975369b5cdca62a7708f46f684dfec67b3c67.tar.gz rust-457975369b5cdca62a7708f46f684dfec67b3c67.zip | |
Rollup merge of #48808 - Zoxc:reg-diag, r=michaelwoerister
Move REGISTERED_DIAGNOSTICS to a ParseSess field r? @michaelwoerister
Diffstat (limited to 'src/librustc_data_structures/sync.rs')
| -rw-r--r-- | src/librustc_data_structures/sync.rs | 126 |
1 files changed, 87 insertions, 39 deletions
diff --git a/src/librustc_data_structures/sync.rs b/src/librustc_data_structures/sync.rs index 69fc9ef785e..d7cd459e577 100644 --- a/src/librustc_data_structures/sync.rs +++ b/src/librustc_data_structures/sync.rs @@ -62,7 +62,7 @@ cfg_if! { pub use std::cell::RefMut as WriteGuard; pub use std::cell::RefMut as LockGuard; - pub use std::cell::RefCell as RwLock; + use std::cell::RefCell as InnerRwLock; use std::cell::RefCell as InnerLock; use std::cell::Cell; @@ -159,13 +159,12 @@ cfg_if! { pub use parking_lot::MutexGuard as LockGuard; - use parking_lot; - pub use std::sync::Arc as Lrc; pub use self::Lock as MTLock; use parking_lot::Mutex as InnerLock; + use parking_lot::RwLock as InnerRwLock; pub type MetadataRef = OwningRef<Box<Erased + Send + Sync>, [u8]>; @@ -222,42 +221,6 @@ cfg_if! { self.0.lock().take() } } - - #[derive(Debug)] - pub struct RwLock<T>(parking_lot::RwLock<T>); - - impl<T> RwLock<T> { - #[inline(always)] - pub fn new(inner: T) -> Self { - RwLock(parking_lot::RwLock::new(inner)) - } - - #[inline(always)] - pub fn borrow(&self) -> ReadGuard<T> { - if ERROR_CHECKING { - self.0.try_read().expect("lock was already held") - } else { - self.0.read() - } - } - - #[inline(always)] - pub fn borrow_mut(&self) -> WriteGuard<T> { - if ERROR_CHECKING { - self.0.try_write().expect("lock was already held") - } else { - self.0.write() - } - } - } - - // FIXME: Probably a bad idea - impl<T: Clone> Clone for RwLock<T> { - #[inline] - fn clone(&self) -> Self { - RwLock::new(self.borrow().clone()) - } - } } } @@ -385,6 +348,11 @@ impl<T> Lock<T> { } #[inline(always)] + pub fn with_lock<F: FnOnce(&mut T) -> R, R>(&self, f: F) -> R { + f(&mut *self.lock()) + } + + #[inline(always)] pub fn borrow(&self) -> LockGuard<T> { self.lock() } @@ -402,3 +370,83 @@ impl<T: Clone> Clone for Lock<T> { Lock::new(self.borrow().clone()) } } + +#[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()) + } + + #[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()) + } +} |
