Skip to content

Commit e166006

Browse files
committed
use thread-safe RwLock
1 parent 6e30bda commit e166006

File tree

1 file changed

+11
-50
lines changed
  • compiler/rustc_data_structures/src

1 file changed

+11
-50
lines changed

compiler/rustc_data_structures/src/sync.rs

Lines changed: 11 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -246,16 +246,8 @@ cfg_if! {
246246

247247
pub type MetadataRef = OwnedSlice;
248248

249-
pub use std::cell::Ref as ReadGuard;
250-
pub use std::cell::Ref as MappedReadGuard;
251-
pub use std::cell::RefMut as WriteGuard;
252-
pub use std::cell::RefMut as MappedWriteGuard;
253-
pub use std::cell::RefMut as MappedLockGuard;
254-
255249
pub use std::cell::OnceCell;
256250

257-
use std::cell::RefCell as InnerRwLock;
258-
259251
pub type MTLockRef<'a, T> = &'a mut MTLock<T>;
260252

261253
#[derive(Debug, Default)]
@@ -299,13 +291,6 @@ cfg_if! {
299291
pub use std::marker::Send as Send;
300292
pub use std::marker::Sync as Sync;
301293

302-
pub use parking_lot::RwLockReadGuard as ReadGuard;
303-
pub use parking_lot::MappedRwLockReadGuard as MappedReadGuard;
304-
pub use parking_lot::RwLockWriteGuard as WriteGuard;
305-
pub use parking_lot::MappedRwLockWriteGuard as MappedWriteGuard;
306-
307-
pub use parking_lot::MappedMutexGuard as MappedLockGuard;
308-
309294
pub use std::sync::OnceLock as OnceCell;
310295

311296
pub use std::sync::atomic::{AtomicBool, AtomicUsize, AtomicU32, AtomicU64};
@@ -342,8 +327,6 @@ cfg_if! {
342327
}
343328
}
344329

345-
use parking_lot::RwLock as InnerRwLock;
346-
347330
use std::thread;
348331

349332
#[inline]
@@ -508,14 +491,22 @@ cfg_if! {
508491

509492
pub type MetadataRef = OwnedSlice;
510493

511-
/// This makes locks panic if they are already held.
512-
/// It is only useful when you are running in a single thread
513-
const ERROR_CHECKING: bool = false;
514494
}
515495
}
516496

517497
pub use std::sync::Arc as Lrc;
518498

499+
use parking_lot::RwLock as InnerRwLock;
500+
501+
pub use parking_lot::MappedRwLockReadGuard as MappedReadGuard;
502+
pub use parking_lot::MappedRwLockWriteGuard as MappedWriteGuard;
503+
pub use parking_lot::RwLockReadGuard as ReadGuard;
504+
pub use parking_lot::RwLockWriteGuard as WriteGuard;
505+
506+
/// This makes locks panic if they are already held.
507+
/// It is only useful when you are running in a single thread
508+
const ERROR_CHECKING: bool = false;
509+
519510
pub unsafe trait DynSend {}
520511
pub unsafe trait DynSync {}
521512

@@ -742,14 +733,6 @@ impl<T> RwLock<T> {
742733
self.0.get_mut()
743734
}
744735

745-
#[cfg(not(parallel_compiler))]
746-
#[inline(always)]
747-
#[track_caller]
748-
pub fn read(&self) -> ReadGuard<'_, T> {
749-
self.0.borrow()
750-
}
751-
752-
#[cfg(parallel_compiler)]
753736
#[inline(always)]
754737
pub fn read(&self) -> ReadGuard<'_, T> {
755738
if ERROR_CHECKING {
@@ -765,26 +748,11 @@ impl<T> RwLock<T> {
765748
f(&*self.read())
766749
}
767750

768-
#[cfg(not(parallel_compiler))]
769-
#[inline(always)]
770-
pub fn try_write(&self) -> Result<WriteGuard<'_, T>, ()> {
771-
self.0.try_borrow_mut().map_err(|_| ())
772-
}
773-
774-
#[cfg(parallel_compiler)]
775751
#[inline(always)]
776752
pub fn try_write(&self) -> Result<WriteGuard<'_, T>, ()> {
777753
self.0.try_write().ok_or(())
778754
}
779755

780-
#[cfg(not(parallel_compiler))]
781-
#[inline(always)]
782-
#[track_caller]
783-
pub fn write(&self) -> WriteGuard<'_, T> {
784-
self.0.borrow_mut()
785-
}
786-
787-
#[cfg(parallel_compiler)]
788756
#[inline(always)]
789757
pub fn write(&self) -> WriteGuard<'_, T> {
790758
if ERROR_CHECKING {
@@ -812,13 +780,6 @@ impl<T> RwLock<T> {
812780
self.write()
813781
}
814782

815-
#[cfg(not(parallel_compiler))]
816-
#[inline(always)]
817-
pub fn leak(&self) -> &T {
818-
ReadGuard::leak(self.read())
819-
}
820-
821-
#[cfg(parallel_compiler)]
822783
#[inline(always)]
823784
pub fn leak(&self) -> &T {
824785
let guard = self.read();

0 commit comments

Comments
 (0)