1
- //! Helper module that wraps some Mutex types with different implementations .
1
+ //! Helper module that adds extra checks when the `deadlock_detection` feature is turned on .
2
2
3
3
// ----------------------------------------------------------------------------
4
4
5
- #[ cfg( not( target_arch = "wasm32" ) ) ]
6
- #[ cfg( not( debug_assertions) ) ]
5
+ #[ cfg( not( feature = "deadlock_detection" ) ) ]
7
6
mod mutex_impl {
8
7
/// Provides interior mutability.
9
8
///
10
- /// Uses `parking_lot` crate on native targets, and `atomic_refcell` on `wasm32` targets.
9
+ /// This is a thin wrapper around [`parking_lot::Mutex`], except if
10
+ /// the feature `deadlock_detection` is turned enabled, in which case
11
+ /// extra checks are added to detect deadlocks.
11
12
#[ derive( Default ) ]
12
13
pub struct Mutex < T > ( parking_lot:: Mutex < T > ) ;
13
14
@@ -27,12 +28,13 @@ mod mutex_impl {
27
28
}
28
29
}
29
30
30
- #[ cfg( not( target_arch = "wasm32" ) ) ]
31
- #[ cfg( debug_assertions) ]
31
+ #[ cfg( feature = "deadlock_detection" ) ]
32
32
mod mutex_impl {
33
33
/// Provides interior mutability.
34
34
///
35
- /// Uses `parking_lot` crate on native targets, and `atomic_refcell` on `wasm32` targets.
35
+ /// This is a thin wrapper around [`parking_lot::Mutex`], except if
36
+ /// the feature `deadlock_detection` is turned enabled, in which case
37
+ /// extra checks are added to detect deadlocks.
36
38
#[ derive( Default ) ]
37
39
pub struct Mutex < T > ( parking_lot:: Mutex < T > ) ;
38
40
@@ -115,7 +117,8 @@ mod mutex_impl {
115
117
}
116
118
}
117
119
118
- #[ cfg( not( target_arch = "wasm32" ) ) ]
120
+ // ----------------------------------------------------------------------------
121
+
119
122
#[ cfg( not( feature = "deadlock_detection" ) ) ]
120
123
mod rw_lock_impl {
121
124
/// The lock you get from [`RwLock::read`].
@@ -126,7 +129,9 @@ mod rw_lock_impl {
126
129
127
130
/// Provides interior mutability.
128
131
///
129
- /// Uses `parking_lot` crate on native targets, and `atomic_refcell` on `wasm32` targets.
132
+ /// This is a thin wrapper around [`parking_lot::RwLock`], except if
133
+ /// the feature `deadlock_detection` is turned enabled, in which case
134
+ /// extra checks are added to detect deadlocks.
130
135
#[ derive( Default ) ]
131
136
pub struct RwLock < T > ( parking_lot:: RwLock < T > ) ;
132
137
@@ -148,7 +153,6 @@ mod rw_lock_impl {
148
153
}
149
154
}
150
155
151
- #[ cfg( not( target_arch = "wasm32" ) ) ]
152
156
#[ cfg( feature = "deadlock_detection" ) ]
153
157
mod rw_lock_impl {
154
158
use std:: {
@@ -246,7 +250,9 @@ mod rw_lock_impl {
246
250
247
251
/// Provides interior mutability.
248
252
///
249
- /// Uses `parking_lot` crate on native targets, and `atomic_refcell` on `wasm32` targets.
253
+ /// This is a thin wrapper around [`parking_lot::RwLock`], except if
254
+ /// the feature `deadlock_detection` is turned enabled, in which case
255
+ /// extra checks are added to detect deadlocks.
250
256
#[ derive( Default ) ]
251
257
pub struct RwLock < T > {
252
258
lock : parking_lot:: RwLock < T > ,
@@ -352,80 +358,6 @@ mod rw_lock_impl {
352
358
353
359
// ----------------------------------------------------------------------------
354
360
355
- #[ cfg( target_arch = "wasm32" ) ]
356
- mod mutex_impl {
357
- // `atomic_refcell` will panic if multiple threads try to access the same value
358
-
359
- /// Provides interior mutability.
360
- ///
361
- /// Uses `parking_lot` crate on native targets, and `atomic_refcell` on `wasm32` targets.
362
- #[ derive( Default ) ]
363
- pub struct Mutex < T > ( atomic_refcell:: AtomicRefCell < T > ) ;
364
-
365
- /// The lock you get from [`Mutex`].
366
- pub use atomic_refcell:: AtomicRefMut as MutexGuard ;
367
-
368
- impl < T > Mutex < T > {
369
- #[ inline( always) ]
370
- pub fn new ( val : T ) -> Self {
371
- Self ( atomic_refcell:: AtomicRefCell :: new ( val) )
372
- }
373
-
374
- /// Panics if already locked.
375
- #[ inline( always) ]
376
- pub fn lock ( & self ) -> MutexGuard < ' _ , T > {
377
- self . 0 . borrow_mut ( )
378
- }
379
-
380
- #[ inline( always) ]
381
- pub fn into_inner ( self ) -> T {
382
- self . 0 . into_inner ( )
383
- }
384
- }
385
- }
386
-
387
- #[ cfg( target_arch = "wasm32" ) ]
388
- mod rw_lock_impl {
389
- // `atomic_refcell` will panic if multiple threads try to access the same value
390
-
391
- /// The lock you get from [`RwLock::read`].
392
- pub use atomic_refcell:: AtomicRef as RwLockReadGuard ;
393
-
394
- /// The lock you get from [`RwLock::write`].
395
- pub use atomic_refcell:: AtomicRefMut as RwLockWriteGuard ;
396
-
397
- /// Provides interior mutability.
398
- ///
399
- /// Uses `parking_lot` crate on native targets, and `atomic_refcell` on `wasm32` targets.
400
- #[ derive( Default ) ]
401
- pub struct RwLock < T > ( atomic_refcell:: AtomicRefCell < T > ) ;
402
-
403
- impl < T > RwLock < T > {
404
- #[ inline( always) ]
405
- pub fn new ( val : T ) -> Self {
406
- Self ( atomic_refcell:: AtomicRefCell :: new ( val) )
407
- }
408
-
409
- #[ inline( always) ]
410
- pub fn read ( & self ) -> RwLockReadGuard < ' _ , T > {
411
- self . 0 . borrow ( )
412
- }
413
-
414
- /// Panics if already locked.
415
- #[ inline( always) ]
416
- pub fn write ( & self ) -> RwLockWriteGuard < ' _ , T > {
417
- self . 0 . borrow_mut ( )
418
- }
419
-
420
- #[ inline( always) ]
421
- pub fn into_inner ( self ) -> T {
422
- self . 0 . into_inner ( )
423
- }
424
- }
425
- }
426
-
427
- // ----------------------------------------------------------------------------
428
-
429
361
pub use mutex_impl:: { Mutex , MutexGuard } ;
430
362
pub use rw_lock_impl:: { RwLock , RwLockReadGuard , RwLockWriteGuard } ;
431
363
@@ -469,7 +401,7 @@ mod tests {
469
401
let other_thread = {
470
402
let one = Arc :: clone ( & one) ;
471
403
std:: thread:: spawn ( move || {
472
- let _ = one. lock ( ) ;
404
+ let _lock = one. lock ( ) ;
473
405
} )
474
406
} ;
475
407
std:: thread:: sleep ( Duration :: from_millis ( 200 ) ) ;
0 commit comments