Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[do not merge] Use locks in single-threaded mode #59644

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 45 additions & 90 deletions src/librustc_data_structures/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,15 +179,49 @@ cfg_if! {

pub use std::rc::Rc as Lrc;
pub use std::rc::Weak as Weak;
pub use std::cell::Ref as ReadGuard;
pub use std::cell::Ref as MappedReadGuard;
pub use std::cell::RefMut as WriteGuard;
pub use std::cell::RefMut as MappedWriteGuard;
pub use std::cell::RefMut as LockGuard;
pub use std::cell::RefMut as MappedLockGuard;

use std::cell::RefCell as InnerRwLock;
use std::cell::RefCell as InnerLock;
pub use parking_lot::RwLockReadGuard as ReadGuard;
pub use parking_lot::MappedRwLockReadGuard as MappedReadGuard;
pub use parking_lot::RwLockWriteGuard as WriteGuard;
pub use parking_lot::MappedRwLockWriteGuard as MappedWriteGuard;

pub use parking_lot::MutexGuard as LockGuard;
pub use parking_lot::MappedMutexGuard as MappedLockGuard;

pub type MTRef<'a, T> = &'a T;

#[derive(Debug, Default)]
pub struct MTLock<T>(Lock<T>);

impl<T> MTLock<T> {
#[inline(always)]
pub fn new(inner: T) -> Self {
MTLock(Lock::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()
}

#[inline(always)]
pub fn lock(&self) -> LockGuard<'_, T> {
self.0.lock()
}

#[inline(always)]
pub fn lock_mut(&self) -> LockGuard<'_, T> {
self.lock()
}
}

use parking_lot::Mutex as InnerLock;
use parking_lot::RwLock as InnerRwLock;

use std::cell::Cell;

Expand Down Expand Up @@ -218,38 +252,6 @@ cfg_if! {
}
}

pub type MTRef<'a, T> = &'a mut T;

#[derive(Debug, Default)]
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 lock_mut(&mut self) -> &mut T {
&mut self.0
}
}

// FIXME: Probably a bad idea (in the threaded case)
impl<T: Clone> Clone for MTLock<T> {
#[inline]
Expand Down Expand Up @@ -535,32 +537,14 @@ impl<T> Lock<T> {
self.0.get_mut()
}

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

#[cfg(not(parallel_compiler))]
#[inline(always)]
pub fn try_lock(&self) -> Option<LockGuard<'_, T>> {
self.0.try_borrow_mut().ok()
}

#[cfg(parallel_compiler)]
#[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_compiler))]
#[inline(always)]
pub fn lock(&self) -> LockGuard<'_, T> {
self.0.borrow_mut()
self.0.lock()
}

#[inline(always)]
Expand Down Expand Up @@ -613,53 +597,24 @@ impl<T> RwLock<T> {
self.0.get_mut()
}

#[cfg(not(parallel_compiler))]
#[inline(always)]
pub fn read(&self) -> ReadGuard<'_, T> {
self.0.borrow()
}

#[cfg(parallel_compiler)]
#[inline(always)]
pub fn read(&self) -> ReadGuard<'_, T> {
if ERROR_CHECKING {
self.0.try_read().expect("lock was already held")
} else {
self.0.read()
}
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_compiler))]
#[inline(always)]
pub fn try_write(&self) -> Result<WriteGuard<'_, T>, ()> {
self.0.try_borrow_mut().map_err(|_| ())
}

#[cfg(parallel_compiler)]
#[inline(always)]
pub fn try_write(&self) -> Result<WriteGuard<'_, T>, ()> {
self.0.try_write().ok_or(())
}

#[cfg(not(parallel_compiler))]
#[inline(always)]
pub fn write(&self) -> WriteGuard<'_, T> {
self.0.borrow_mut()
}

#[cfg(parallel_compiler)]
#[inline(always)]
pub fn write(&self) -> WriteGuard<'_, T> {
if ERROR_CHECKING {
self.0.try_write().expect("lock was already held")
} else {
self.0.write()
}
self.0.write()
}

#[inline(always)]
Expand Down