1
- pub use :: alloc:: sync:: Arc ;
1
+ pub use alloc:: sync:: Arc ;
2
2
use core:: ops:: { Deref , DerefMut } ;
3
3
use core:: time:: Duration ;
4
4
5
5
use std:: cell:: RefCell ;
6
6
7
7
use std:: sync:: atomic:: { AtomicUsize , Ordering } ;
8
+ use std:: sync:: Condvar as StdCondvar ;
8
9
use std:: sync:: Mutex as StdMutex ;
9
10
use std:: sync:: MutexGuard as StdMutexGuard ;
10
11
use std:: sync:: RwLock as StdRwLock ;
11
12
use std:: sync:: RwLockReadGuard as StdRwLockReadGuard ;
12
13
use std:: sync:: RwLockWriteGuard as StdRwLockWriteGuard ;
13
- use std:: sync:: Condvar as StdCondvar ;
14
14
15
15
pub use std:: sync:: WaitTimeoutResult ;
16
16
17
17
use crate :: prelude:: * ;
18
18
19
- use super :: { LockTestExt , LockHeldState } ;
19
+ use super :: { LockHeldState , LockTestExt } ;
20
20
21
21
#[ cfg( feature = "backtrace" ) ]
22
22
use { crate :: prelude:: hash_map, backtrace:: Backtrace , std:: sync:: Once } ;
23
23
24
24
#[ cfg( not( feature = "backtrace" ) ) ]
25
- struct Backtrace { }
25
+ struct Backtrace { }
26
26
#[ cfg( not( feature = "backtrace" ) ) ]
27
- impl Backtrace { fn new ( ) -> Backtrace { Backtrace { } } }
27
+ impl Backtrace {
28
+ fn new ( ) -> Backtrace {
29
+ Backtrace { }
30
+ }
31
+ }
28
32
29
33
pub type LockResult < Guard > = Result < Guard , ( ) > ;
30
34
@@ -37,22 +41,30 @@ impl Condvar {
37
41
Condvar { inner : StdCondvar :: new ( ) }
38
42
}
39
43
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 > > {
42
47
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 } )
44
51
. map_err ( |_| ( ) )
45
52
}
46
53
47
54
#[ 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 ) > {
50
58
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 ( |_| ( ) )
52
62
. map ( |( lock, e) | ( MutexGuard { mutex, lock } , e) )
53
63
}
54
64
55
- pub fn notify_all ( & self ) { self . inner . notify_all ( ) ; }
65
+ pub fn notify_all ( & self ) {
66
+ self . inner . notify_all ( ) ;
67
+ }
56
68
}
57
69
58
70
thread_local ! {
@@ -99,14 +111,19 @@ fn locate_call_symbol(backtrace: &Backtrace) -> (String, Option<u32>) {
99
111
symbol_after_latest_debug_sync = Some ( symbol) ;
100
112
found_debug_sync = false ;
101
113
}
102
- } else { found_debug_sync = true ; }
114
+ } else {
115
+ found_debug_sync = true ;
116
+ }
103
117
}
104
118
}
105
119
}
106
120
let symbol = symbol_after_latest_debug_sync. unwrap_or_else ( || {
107
121
panic ! ( "Couldn't find lock call symbol in trace {:?}" , backtrace) ;
108
122
} ) ;
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
+ )
110
127
}
111
128
112
129
impl LockMetadata {
@@ -124,16 +141,20 @@ impl LockMetadata {
124
141
{
125
142
let ( lock_constr_location, lock_constr_colno) =
126
143
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
+ } ) ;
128
147
let mut locks = unsafe { LOCKS . as_ref ( ) } . unwrap ( ) . lock ( ) . unwrap ( ) ;
129
148
match locks. entry ( lock_constr_location) {
130
149
hash_map:: Entry :: Occupied ( e) => {
131
150
assert_eq ! ( lock_constr_colno,
132
151
locate_call_symbol( & e. get( ) . _lock_construction_bt) . 1 ,
133
152
"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) ) ;
135
157
} ,
136
- hash_map:: Entry :: Vacant ( e) => { e. insert ( Arc :: clone ( & res) ) ; } ,
137
158
}
138
159
}
139
160
res
@@ -213,7 +234,8 @@ impl LockMetadata {
213
234
let mut locked_before = this. locked_before . lock ( ) . unwrap ( ) ;
214
235
for ( locked_idx, locked) in held. borrow ( ) . iter ( ) {
215
236
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 ( ) } ;
217
239
locked_before. insert ( * locked_idx, lockdep) ;
218
240
}
219
241
}
@@ -282,7 +304,8 @@ impl<T> Mutex<T> {
282
304
}
283
305
284
306
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 ( |_| ( ) ) ;
286
309
if res. is_ok ( ) {
287
310
LockMetadata :: try_locked ( & self . deps ) ;
288
311
}
@@ -376,7 +399,11 @@ impl<T> RwLock<T> {
376
399
}
377
400
378
401
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 ( |_| ( ) ) ;
380
407
if res. is_ok ( ) {
381
408
LockMetadata :: try_locked ( & self . deps ) ;
382
409
}
0 commit comments