43
43
//! [^2] `MTLockRef` is a typedef.
44
44
45
45
use crate :: owning_ref:: { Erased , OwningRef } ;
46
- use std:: cell:: RefCell ;
46
+ use std:: cell:: { RefCell , RefMut } ;
47
47
use std:: collections:: HashMap ;
48
48
use std:: hash:: { BuildHasher , Hash } ;
49
49
use std:: mem:: { transmute, MaybeUninit } ;
@@ -402,6 +402,13 @@ cfg_if! {
402
402
// We catch panics here ensuring that all the blocks execute.
403
403
// This makes behavior consistent with the parallel compiler.
404
404
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
+ }
405
412
$(
406
413
if let Err ( p) = :: std:: panic:: catch_unwind(
407
414
:: std:: panic:: AssertUnwindSafe ( || $blocks)
@@ -626,14 +633,11 @@ impl<T> Lock<T> {
626
633
pub fn try_lock ( & self ) -> Option < LockGuard < ' _ , T > > {
627
634
// SAFETY: the `&mut T` is accessible as long as self exists.
628
635
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 ) )
630
638
} 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) ) )
637
641
}
638
642
}
639
643
@@ -649,9 +653,11 @@ impl<T> Lock<T> {
649
653
pub fn lock ( & self ) -> LockGuard < ' _ , T > {
650
654
// SAFETY: the `&mut T` is accessible as long as self exists.
651
655
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 )
653
658
} 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) )
655
661
}
656
662
}
657
663
@@ -693,7 +699,7 @@ impl<T: Clone> Clone for Lock<T> {
693
699
unsafe impl < T : Send > std:: marker:: Send for Lock < T > { }
694
700
unsafe impl < T : Send > std:: marker:: Sync for Lock < T > { }
695
701
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 > > ) ;
697
703
698
704
impl < T > const Deref for LockGuard < ' _ , T > {
699
705
type Target = T ;
0 commit comments