1- pub use :: alloc:: sync:: Arc ;
1+ pub use alloc:: sync:: Arc ;
22use core:: ops:: { Deref , DerefMut } ;
33use core:: time:: Duration ;
44
55use std:: cell:: RefCell ;
66
77use std:: sync:: atomic:: { AtomicUsize , Ordering } ;
8+ use std:: sync:: Condvar as StdCondvar ;
89use std:: sync:: Mutex as StdMutex ;
910use std:: sync:: MutexGuard as StdMutexGuard ;
1011use std:: sync:: RwLock as StdRwLock ;
1112use std:: sync:: RwLockReadGuard as StdRwLockReadGuard ;
1213use std:: sync:: RwLockWriteGuard as StdRwLockWriteGuard ;
13- use std:: sync:: Condvar as StdCondvar ;
1414
1515pub use std:: sync:: WaitTimeoutResult ;
1616
1717use crate :: prelude:: * ;
1818
19- use super :: { LockTestExt , LockHeldState } ;
19+ use super :: { LockHeldState , LockTestExt } ;
2020
2121#[ cfg( feature = "backtrace" ) ]
2222use { crate :: prelude:: hash_map, backtrace:: Backtrace , std:: sync:: Once } ;
2323
2424#[ cfg( not( feature = "backtrace" ) ) ]
25- struct Backtrace { }
25+ struct Backtrace { }
2626#[ cfg( not( feature = "backtrace" ) ) ]
27- impl Backtrace { fn new ( ) -> Backtrace { Backtrace { } } }
27+ impl Backtrace {
28+ fn new ( ) -> Backtrace {
29+ Backtrace { }
30+ }
31+ }
2832
2933pub type LockResult < Guard > = Result < Guard , ( ) > ;
3034
@@ -37,22 +41,30 @@ impl Condvar {
3741 Condvar { inner : StdCondvar :: new ( ) }
3842 }
3943
40- pub fn wait_while < ' a , T , F : FnMut ( & mut T ) -> bool > ( & ' a self , guard : MutexGuard < ' a , T > , condition : F )
41- -> LockResult < MutexGuard < ' a , T > > {
44+ pub fn wait_while < ' a , T , F : FnMut ( & mut T ) -> bool > (
45+ & ' a self , guard : MutexGuard < ' a , T > , condition : F ,
46+ ) -> LockResult < MutexGuard < ' a , T > > {
4247 let mutex: & ' a Mutex < T > = guard. mutex ;
43- self . inner . wait_while ( guard. into_inner ( ) , condition) . map ( |lock| MutexGuard { mutex, lock } )
48+ self . inner
49+ . wait_while ( guard. into_inner ( ) , condition)
50+ . map ( |lock| MutexGuard { mutex, lock } )
4451 . map_err ( |_| ( ) )
4552 }
4653
4754 #[ allow( unused) ]
48- pub fn wait_timeout_while < ' a , T , F : FnMut ( & mut T ) -> bool > ( & ' a self , guard : MutexGuard < ' a , T > , dur : Duration , condition : F )
49- -> LockResult < ( MutexGuard < ' a , T > , WaitTimeoutResult ) > {
55+ pub fn wait_timeout_while < ' a , T , F : FnMut ( & mut T ) -> bool > (
56+ & ' a self , guard : MutexGuard < ' a , T > , dur : Duration , condition : F ,
57+ ) -> LockResult < ( MutexGuard < ' a , T > , WaitTimeoutResult ) > {
5058 let mutex = guard. mutex ;
51- self . inner . wait_timeout_while ( guard. into_inner ( ) , dur, condition) . map_err ( |_| ( ) )
59+ self . inner
60+ . wait_timeout_while ( guard. into_inner ( ) , dur, condition)
61+ . map_err ( |_| ( ) )
5262 . map ( |( lock, e) | ( MutexGuard { mutex, lock } , e) )
5363 }
5464
55- pub fn notify_all ( & self ) { self . inner . notify_all ( ) ; }
65+ pub fn notify_all ( & self ) {
66+ self . inner . notify_all ( ) ;
67+ }
5668}
5769
5870thread_local ! {
@@ -99,14 +111,19 @@ fn locate_call_symbol(backtrace: &Backtrace) -> (String, Option<u32>) {
99111 symbol_after_latest_debug_sync = Some ( symbol) ;
100112 found_debug_sync = false ;
101113 }
102- } else { found_debug_sync = true ; }
114+ } else {
115+ found_debug_sync = true ;
116+ }
103117 }
104118 }
105119 }
106120 let symbol = symbol_after_latest_debug_sync. unwrap_or_else ( || {
107121 panic ! ( "Couldn't find lock call symbol in trace {:?}" , backtrace) ;
108122 } ) ;
109- ( format ! ( "{}:{}" , symbol. filename( ) . unwrap( ) . display( ) , symbol. lineno( ) . unwrap( ) ) , symbol. colno ( ) )
123+ (
124+ format ! ( "{}:{}" , symbol. filename( ) . unwrap( ) . display( ) , symbol. lineno( ) . unwrap( ) ) ,
125+ symbol. colno ( ) ,
126+ )
110127}
111128
112129impl LockMetadata {
@@ -124,16 +141,20 @@ impl LockMetadata {
124141 {
125142 let ( lock_constr_location, lock_constr_colno) =
126143 locate_call_symbol ( & res. _lock_construction_bt ) ;
127- LOCKS_INIT . call_once ( || { unsafe { LOCKS = Some ( StdMutex :: new ( new_hash_map ( ) ) ) ; } } ) ;
144+ LOCKS_INIT . call_once ( || unsafe {
145+ LOCKS = Some ( StdMutex :: new ( new_hash_map ( ) ) ) ;
146+ } ) ;
128147 let mut locks = unsafe { LOCKS . as_ref ( ) } . unwrap ( ) . lock ( ) . unwrap ( ) ;
129148 match locks. entry ( lock_constr_location) {
130149 hash_map:: Entry :: Occupied ( e) => {
131150 assert_eq ! ( lock_constr_colno,
132151 locate_call_symbol( & e. get( ) . _lock_construction_bt) . 1 ,
133152 "Because Windows doesn't support column number results in backtraces, we cannot construct two mutexes on the same line or we risk lockorder detection false positives." ) ;
134- return Arc :: clone ( e. get ( ) )
153+ return Arc :: clone ( e. get ( ) ) ;
154+ } ,
155+ hash_map:: Entry :: Vacant ( e) => {
156+ e. insert ( Arc :: clone ( & res) ) ;
135157 } ,
136- hash_map:: Entry :: Vacant ( e) => { e. insert ( Arc :: clone ( & res) ) ; } ,
137158 }
138159 }
139160 res
@@ -213,7 +234,8 @@ impl LockMetadata {
213234 let mut locked_before = this. locked_before . lock ( ) . unwrap ( ) ;
214235 for ( locked_idx, locked) in held. borrow ( ) . iter ( ) {
215236 if !locked_before. contains_key ( locked_idx) {
216- let lockdep = LockDep { lock : Arc :: clone ( locked) , _lockdep_trace : Backtrace :: new ( ) } ;
237+ let lockdep =
238+ LockDep { lock : Arc :: clone ( locked) , _lockdep_trace : Backtrace :: new ( ) } ;
217239 locked_before. insert ( * locked_idx, lockdep) ;
218240 }
219241 }
@@ -282,7 +304,8 @@ impl<T> Mutex<T> {
282304 }
283305
284306 pub fn try_lock < ' a > ( & ' a self ) -> LockResult < MutexGuard < ' a , T > > {
285- let res = self . inner . try_lock ( ) . map ( |lock| MutexGuard { mutex : self , lock } ) . map_err ( |_| ( ) ) ;
307+ let res =
308+ self . inner . try_lock ( ) . map ( |lock| MutexGuard { mutex : self , lock } ) . map_err ( |_| ( ) ) ;
286309 if res. is_ok ( ) {
287310 LockMetadata :: try_locked ( & self . deps ) ;
288311 }
@@ -376,7 +399,11 @@ impl<T> RwLock<T> {
376399 }
377400
378401 pub fn try_write < ' a > ( & ' a self ) -> LockResult < RwLockWriteGuard < ' a , T > > {
379- let res = self . inner . try_write ( ) . map ( |guard| RwLockWriteGuard { lock : self , guard } ) . map_err ( |_| ( ) ) ;
402+ let res = self
403+ . inner
404+ . try_write ( )
405+ . map ( |guard| RwLockWriteGuard { lock : self , guard } )
406+ . map_err ( |_| ( ) ) ;
380407 if res. is_ok ( ) {
381408 LockMetadata :: try_locked ( & self . deps ) ;
382409 }
0 commit comments