Skip to content

Commit d28b02b

Browse files
committed
correct parallel! and LockGuard
1 parent 67955e0 commit d28b02b

File tree

1 file changed

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

1 file changed

+17
-11
lines changed

compiler/rustc_data_structures/src/sync.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
//! [^2] `MTLockRef` is a typedef.
4444
4545
use crate::owning_ref::{Erased, OwningRef};
46-
use std::cell::RefCell;
46+
use std::cell::{RefCell, RefMut};
4747
use std::collections::HashMap;
4848
use std::hash::{BuildHasher, Hash};
4949
use std::mem::{transmute, MaybeUninit};
@@ -402,6 +402,13 @@ cfg_if! {
402402
// We catch panics here ensuring that all the blocks execute.
403403
// This makes behavior consistent with the parallel compiler.
404404
let mut panic = None;
405+
if let Err(p) = ::std::panic::catch_unwind(
406+
::std::panic::AssertUnwindSafe(|| $fblock)
407+
) {
408+
if panic.is_none() {
409+
panic = Some(p);
410+
}
411+
}
405412
$(
406413
if let Err(p) = ::std::panic::catch_unwind(
407414
::std::panic::AssertUnwindSafe(|| $blocks)
@@ -626,14 +633,11 @@ impl<T> Lock<T> {
626633
pub fn try_lock(&self) -> Option<LockGuard<'_, T>> {
627634
// SAFETY: the `&mut T` is accessible as long as self exists.
628635
if self.single_thread {
629-
self.inner.try_borrow_mut().map(|mut r| unsafe { transmute(r.deref_mut()) }).ok()
636+
let mut r = self.inner.try_borrow_mut().ok()?;
637+
Some(LockGuard(unsafe { transmute(r.deref_mut()) }, Some(r), None))
630638
} else {
631-
self.mt_inner
632-
.as_ref()
633-
.unwrap()
634-
.try_lock()
635-
.map(|mut l| unsafe { transmute(l.deref_mut()) })
636-
.ok()
639+
let mut l = self.mt_inner.as_ref().unwrap().try_lock().ok()?;
640+
Some(LockGuard(unsafe { transmute(l.deref_mut()) }, None, Some(l)))
637641
}
638642
}
639643

@@ -649,9 +653,11 @@ impl<T> Lock<T> {
649653
pub fn lock(&self) -> LockGuard<'_, T> {
650654
// SAFETY: the `&mut T` is accessible as long as self exists.
651655
if self.single_thread {
652-
unsafe { transmute(self.inner.borrow_mut().deref_mut()) }
656+
let mut r = self.inner.borrow_mut();
657+
LockGuard(unsafe { transmute(r.deref_mut()) }, Some(r), None)
653658
} else {
654-
unsafe { transmute(self.mt_lock().deref_mut()) }
659+
let mut l = self.mt_lock();
660+
LockGuard(unsafe { transmute(l.deref_mut()) }, None, Some(l))
655661
}
656662
}
657663

@@ -693,7 +699,7 @@ impl<T: Clone> Clone for Lock<T> {
693699
unsafe impl<T: Send> std::marker::Send for Lock<T> {}
694700
unsafe impl<T: Send> std::marker::Sync for Lock<T> {}
695701

696-
pub struct LockGuard<'a, T>(&'a mut T);
702+
pub struct LockGuard<'a, T>(&'a mut T, Option<RefMut<'a, T>>, Option<MutexGuard<'a, T>>);
697703

698704
impl<T> const Deref for LockGuard<'_, T> {
699705
type Target = T;

0 commit comments

Comments
 (0)