Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 628b351

Browse files
committedApr 2, 2019
Use locks in single-threaded mode
1 parent e008e4f commit 628b351

File tree

1 file changed

+45
-90
lines changed
  • src/librustc_data_structures

1 file changed

+45
-90
lines changed
 

‎src/librustc_data_structures/sync.rs

+45-90
Original file line numberDiff line numberDiff line change
@@ -179,15 +179,49 @@ cfg_if! {
179179

180180
pub use std::rc::Rc as Lrc;
181181
pub use std::rc::Weak as Weak;
182-
pub use std::cell::Ref as ReadGuard;
183-
pub use std::cell::Ref as MappedReadGuard;
184-
pub use std::cell::RefMut as WriteGuard;
185-
pub use std::cell::RefMut as MappedWriteGuard;
186-
pub use std::cell::RefMut as LockGuard;
187-
pub use std::cell::RefMut as MappedLockGuard;
188182

189-
use std::cell::RefCell as InnerRwLock;
190-
use std::cell::RefCell as InnerLock;
183+
pub use parking_lot::RwLockReadGuard as ReadGuard;
184+
pub use parking_lot::MappedRwLockReadGuard as MappedReadGuard;
185+
pub use parking_lot::RwLockWriteGuard as WriteGuard;
186+
pub use parking_lot::MappedRwLockWriteGuard as MappedWriteGuard;
187+
188+
pub use parking_lot::MutexGuard as LockGuard;
189+
pub use parking_lot::MappedMutexGuard as MappedLockGuard;
190+
191+
pub type MTRef<'a, T> = &'a T;
192+
193+
#[derive(Debug, Default)]
194+
pub struct MTLock<T>(Lock<T>);
195+
196+
impl<T> MTLock<T> {
197+
#[inline(always)]
198+
pub fn new(inner: T) -> Self {
199+
MTLock(Lock::new(inner))
200+
}
201+
202+
#[inline(always)]
203+
pub fn into_inner(self) -> T {
204+
self.0.into_inner()
205+
}
206+
207+
#[inline(always)]
208+
pub fn get_mut(&mut self) -> &mut T {
209+
self.0.get_mut()
210+
}
211+
212+
#[inline(always)]
213+
pub fn lock(&self) -> LockGuard<'_, T> {
214+
self.0.lock()
215+
}
216+
217+
#[inline(always)]
218+
pub fn lock_mut(&self) -> LockGuard<'_, T> {
219+
self.lock()
220+
}
221+
}
222+
223+
use parking_lot::Mutex as InnerLock;
224+
use parking_lot::RwLock as InnerRwLock;
191225

192226
use std::cell::Cell;
193227

@@ -218,38 +252,6 @@ cfg_if! {
218252
}
219253
}
220254

221-
pub type MTRef<'a, T> = &'a mut T;
222-
223-
#[derive(Debug, Default)]
224-
pub struct MTLock<T>(T);
225-
226-
impl<T> MTLock<T> {
227-
#[inline(always)]
228-
pub fn new(inner: T) -> Self {
229-
MTLock(inner)
230-
}
231-
232-
#[inline(always)]
233-
pub fn into_inner(self) -> T {
234-
self.0
235-
}
236-
237-
#[inline(always)]
238-
pub fn get_mut(&mut self) -> &mut T {
239-
&mut self.0
240-
}
241-
242-
#[inline(always)]
243-
pub fn lock(&self) -> &T {
244-
&self.0
245-
}
246-
247-
#[inline(always)]
248-
pub fn lock_mut(&mut self) -> &mut T {
249-
&mut self.0
250-
}
251-
}
252-
253255
// FIXME: Probably a bad idea (in the threaded case)
254256
impl<T: Clone> Clone for MTLock<T> {
255257
#[inline]
@@ -535,32 +537,14 @@ impl<T> Lock<T> {
535537
self.0.get_mut()
536538
}
537539

538-
#[cfg(parallel_compiler)]
539540
#[inline(always)]
540541
pub fn try_lock(&self) -> Option<LockGuard<'_, T>> {
541542
self.0.try_lock()
542543
}
543544

544-
#[cfg(not(parallel_compiler))]
545-
#[inline(always)]
546-
pub fn try_lock(&self) -> Option<LockGuard<'_, T>> {
547-
self.0.try_borrow_mut().ok()
548-
}
549-
550-
#[cfg(parallel_compiler)]
551-
#[inline(always)]
552-
pub fn lock(&self) -> LockGuard<'_, T> {
553-
if ERROR_CHECKING {
554-
self.0.try_lock().expect("lock was already held")
555-
} else {
556-
self.0.lock()
557-
}
558-
}
559-
560-
#[cfg(not(parallel_compiler))]
561545
#[inline(always)]
562546
pub fn lock(&self) -> LockGuard<'_, T> {
563-
self.0.borrow_mut()
547+
self.0.lock()
564548
}
565549

566550
#[inline(always)]
@@ -613,53 +597,24 @@ impl<T> RwLock<T> {
613597
self.0.get_mut()
614598
}
615599

616-
#[cfg(not(parallel_compiler))]
617-
#[inline(always)]
618-
pub fn read(&self) -> ReadGuard<'_, T> {
619-
self.0.borrow()
620-
}
621-
622-
#[cfg(parallel_compiler)]
623600
#[inline(always)]
624601
pub fn read(&self) -> ReadGuard<'_, T> {
625-
if ERROR_CHECKING {
626-
self.0.try_read().expect("lock was already held")
627-
} else {
628-
self.0.read()
629-
}
602+
self.0.read()
630603
}
631604

632605
#[inline(always)]
633606
pub fn with_read_lock<F: FnOnce(&T) -> R, R>(&self, f: F) -> R {
634607
f(&*self.read())
635608
}
636609

637-
#[cfg(not(parallel_compiler))]
638-
#[inline(always)]
639-
pub fn try_write(&self) -> Result<WriteGuard<'_, T>, ()> {
640-
self.0.try_borrow_mut().map_err(|_| ())
641-
}
642-
643-
#[cfg(parallel_compiler)]
644610
#[inline(always)]
645611
pub fn try_write(&self) -> Result<WriteGuard<'_, T>, ()> {
646612
self.0.try_write().ok_or(())
647613
}
648614

649-
#[cfg(not(parallel_compiler))]
650-
#[inline(always)]
651-
pub fn write(&self) -> WriteGuard<'_, T> {
652-
self.0.borrow_mut()
653-
}
654-
655-
#[cfg(parallel_compiler)]
656615
#[inline(always)]
657616
pub fn write(&self) -> WriteGuard<'_, T> {
658-
if ERROR_CHECKING {
659-
self.0.try_write().expect("lock was already held")
660-
} else {
661-
self.0.write()
662-
}
617+
self.0.write()
663618
}
664619

665620
#[inline(always)]

0 commit comments

Comments
 (0)
Please sign in to comment.