1
1
#[ cfg( all( test, not( target_os = "emscripten" ) ) ) ]
2
2
mod tests;
3
3
4
- use crate :: fmt;
5
- use crate :: marker;
4
+ use crate :: marker:: PhantomPinned ;
6
5
use crate :: ops:: Deref ;
7
6
use crate :: panic:: { RefUnwindSafe , UnwindSafe } ;
7
+ use crate :: pin:: Pin ;
8
8
use crate :: sys:: mutex as sys;
9
9
10
10
/// A re-entrant mutual exclusion
@@ -15,6 +15,7 @@ use crate::sys::mutex as sys;
15
15
pub struct ReentrantMutex < T > {
16
16
inner : sys:: ReentrantMutex ,
17
17
data : T ,
18
+ _pinned : PhantomPinned ,
18
19
}
19
20
20
21
unsafe impl < T : Send > Send for ReentrantMutex < T > { }
@@ -37,10 +38,10 @@ impl<T> RefUnwindSafe for ReentrantMutex<T> {}
37
38
/// guarded data.
38
39
#[ must_use = "if unused the ReentrantMutex will immediately unlock" ]
39
40
pub struct ReentrantMutexGuard < ' a , T : ' a > {
40
- lock : & ' a ReentrantMutex < T > ,
41
+ lock : Pin < & ' a ReentrantMutex < T > > ,
41
42
}
42
43
43
- impl < T > !marker :: Send for ReentrantMutexGuard < ' _ , T > { }
44
+ impl < T > !Send for ReentrantMutexGuard < ' _ , T > { }
44
45
45
46
impl < T > ReentrantMutex < T > {
46
47
/// Creates a new reentrant mutex in an unlocked state.
@@ -51,7 +52,11 @@ impl<T> ReentrantMutex<T> {
51
52
/// once this mutex is in its final resting place, and only then are the
52
53
/// lock/unlock methods safe.
53
54
pub const unsafe fn new ( t : T ) -> ReentrantMutex < T > {
54
- ReentrantMutex { inner : sys:: ReentrantMutex :: uninitialized ( ) , data : t }
55
+ ReentrantMutex {
56
+ inner : sys:: ReentrantMutex :: uninitialized ( ) ,
57
+ data : t,
58
+ _pinned : PhantomPinned ,
59
+ }
55
60
}
56
61
57
62
/// Initializes this mutex so it's ready for use.
@@ -60,8 +65,8 @@ impl<T> ReentrantMutex<T> {
60
65
///
61
66
/// Unsafe to call more than once, and must be called after this will no
62
67
/// longer move in memory.
63
- pub unsafe fn init ( & self ) {
64
- self . inner . init ( ) ;
68
+ pub unsafe fn init ( self : Pin < & mut Self > ) {
69
+ self . get_unchecked_mut ( ) . inner . init ( )
65
70
}
66
71
67
72
/// Acquires a mutex, blocking the current thread until it is able to do so.
@@ -76,9 +81,9 @@ impl<T> ReentrantMutex<T> {
76
81
/// If another user of this mutex panicked while holding the mutex, then
77
82
/// this call will return failure if the mutex would otherwise be
78
83
/// acquired.
79
- pub fn lock ( & self ) -> ReentrantMutexGuard < ' _ , T > {
84
+ pub fn lock ( self : Pin < & Self > ) -> ReentrantMutexGuard < ' _ , T > {
80
85
unsafe { self . inner . lock ( ) }
81
- ReentrantMutexGuard :: new ( & self )
86
+ ReentrantMutexGuard { lock : self }
82
87
}
83
88
84
89
/// Attempts to acquire this lock.
@@ -93,8 +98,12 @@ impl<T> ReentrantMutex<T> {
93
98
/// If another user of this mutex panicked while holding the mutex, then
94
99
/// this call will return failure if the mutex would otherwise be
95
100
/// acquired.
96
- pub fn try_lock ( & self ) -> Option < ReentrantMutexGuard < ' _ , T > > {
97
- if unsafe { self . inner . try_lock ( ) } { Some ( ReentrantMutexGuard :: new ( & self ) ) } else { None }
101
+ pub fn try_lock ( self : Pin < & Self > ) -> Option < ReentrantMutexGuard < ' _ , T > > {
102
+ if unsafe { self . inner . try_lock ( ) } {
103
+ Some ( ReentrantMutexGuard { lock : self } )
104
+ } else {
105
+ None
106
+ }
98
107
}
99
108
}
100
109
@@ -107,30 +116,6 @@ impl<T> Drop for ReentrantMutex<T> {
107
116
}
108
117
}
109
118
110
- impl < T : fmt:: Debug + ' static > fmt:: Debug for ReentrantMutex < T > {
111
- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
112
- match self . try_lock ( ) {
113
- Some ( guard) => f. debug_struct ( "ReentrantMutex" ) . field ( "data" , & * guard) . finish ( ) ,
114
- None => {
115
- struct LockedPlaceholder ;
116
- impl fmt:: Debug for LockedPlaceholder {
117
- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
118
- f. write_str ( "<locked>" )
119
- }
120
- }
121
-
122
- f. debug_struct ( "ReentrantMutex" ) . field ( "data" , & LockedPlaceholder ) . finish ( )
123
- }
124
- }
125
- }
126
- }
127
-
128
- impl < ' mutex , T > ReentrantMutexGuard < ' mutex , T > {
129
- fn new ( lock : & ' mutex ReentrantMutex < T > ) -> ReentrantMutexGuard < ' mutex , T > {
130
- ReentrantMutexGuard { lock }
131
- }
132
- }
133
-
134
119
impl < T > Deref for ReentrantMutexGuard < ' _ , T > {
135
120
type Target = T ;
136
121
0 commit comments