6
6
use std:: sync:: { LockResult , TryLockError , TryLockResult } ;
7
7
use std:: time:: Duration ;
8
8
9
- use parking_lot as pl;
9
+ // Types that do not need wrapping
10
+ pub ( crate ) use parking_lot:: { MutexGuard , RwLockReadGuard , RwLockWriteGuard , WaitTimeoutResult } ;
10
11
11
12
/// Adapter for `parking_lot::Mutex` to the `std::sync::Mutex` interface.
12
13
#[ derive( Debug ) ]
13
- pub ( crate ) struct Mutex < T : ?Sized > ( pl:: Mutex < T > ) ;
14
+ pub ( crate ) struct Mutex < T : ?Sized > ( parking_lot:: Mutex < T > ) ;
15
+
16
+ #[ derive( Debug ) ]
17
+ pub ( crate ) struct RwLock < T > ( parking_lot:: RwLock < T > ) ;
18
+
19
+ /// Adapter for `parking_lot::Condvar` to the `std::sync::Condvar` interface.
20
+ #[ derive( Debug ) ]
21
+ pub ( crate ) struct Condvar ( parking_lot:: Condvar ) ;
14
22
15
23
impl < T > Mutex < T > {
16
24
#[ inline]
17
25
pub ( crate ) fn new ( t : T ) -> Mutex < T > {
18
- Mutex ( pl :: Mutex :: new ( t) )
26
+ Mutex ( parking_lot :: Mutex :: new ( t) )
19
27
}
20
28
21
29
#[ inline]
22
- pub ( crate ) fn lock ( & self ) -> LockResult < pl :: MutexGuard < ' _ , T > > {
30
+ pub ( crate ) fn lock ( & self ) -> LockResult < MutexGuard < ' _ , T > > {
23
31
Ok ( self . 0 . lock ( ) )
24
32
}
25
33
26
34
#[ inline]
27
- pub ( crate ) fn try_lock ( & self ) -> TryLockResult < pl :: MutexGuard < ' _ , T > > {
35
+ pub ( crate ) fn try_lock ( & self ) -> TryLockResult < MutexGuard < ' _ , T > > {
28
36
match self . 0 . try_lock ( ) {
29
37
Some ( guard) => Ok ( guard) ,
30
38
None => Err ( TryLockError :: WouldBlock ) ,
@@ -35,14 +43,24 @@ impl<T> Mutex<T> {
35
43
// provided here as needed.
36
44
}
37
45
38
- /// Adapter for `parking_lot::Condvar` to the `std::sync::Condvar` interface.
39
- #[ derive( Debug ) ]
40
- pub ( crate ) struct Condvar ( pl:: Condvar ) ;
46
+ impl < T > RwLock < T > {
47
+ pub ( crate ) fn new ( t : T ) -> RwLock < T > {
48
+ RwLock ( parking_lot:: RwLock :: new ( t) )
49
+ }
50
+
51
+ pub ( crate ) fn read ( & self ) -> LockResult < RwLockReadGuard < ' _ , T > > {
52
+ Ok ( self . 0 . read ( ) )
53
+ }
54
+
55
+ pub ( crate ) fn write ( & self ) -> LockResult < RwLockWriteGuard < ' _ , T > > {
56
+ Ok ( self . 0 . write ( ) )
57
+ }
58
+ }
41
59
42
60
impl Condvar {
43
61
#[ inline]
44
62
pub ( crate ) fn new ( ) -> Condvar {
45
- Condvar ( pl :: Condvar :: new ( ) )
63
+ Condvar ( parking_lot :: Condvar :: new ( ) )
46
64
}
47
65
48
66
#[ inline]
@@ -58,18 +76,18 @@ impl Condvar {
58
76
#[ inline]
59
77
pub ( crate ) fn wait < ' a , T > (
60
78
& self ,
61
- mut guard : pl :: MutexGuard < ' a , T > ,
62
- ) -> LockResult < pl :: MutexGuard < ' a , T > > {
79
+ mut guard : MutexGuard < ' a , T > ,
80
+ ) -> LockResult < MutexGuard < ' a , T > > {
63
81
self . 0 . wait ( & mut guard) ;
64
82
Ok ( guard)
65
83
}
66
84
67
85
#[ inline]
68
86
pub ( crate ) fn wait_timeout < ' a , T > (
69
87
& self ,
70
- mut guard : pl :: MutexGuard < ' a , T > ,
88
+ mut guard : MutexGuard < ' a , T > ,
71
89
timeout : Duration ,
72
- ) -> LockResult < ( pl :: MutexGuard < ' a , T > , pl :: WaitTimeoutResult ) > {
90
+ ) -> LockResult < ( MutexGuard < ' a , T > , WaitTimeoutResult ) > {
73
91
let wtr = self . 0 . wait_for ( & mut guard, timeout) ;
74
92
Ok ( ( guard, wtr) )
75
93
}
0 commit comments