@@ -105,41 +105,56 @@ impl<'a> Future for RawLockFuture<'a> {
105
105
type Output = ( ) ;
106
106
107
107
fn poll ( mut self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Self :: Output > {
108
- if self . mutex . try_lock ( ) {
109
- self . deregister_waker ( true ) ;
110
- Poll :: Ready ( ( ) )
111
- } else {
112
- let mut blocked = self . mutex . blocked . lock ( ) ;
113
-
114
- // Try locking again because it's possible the mutex got unlocked before
115
- // we acquire the lock of `blocked`.
116
- let state = self . mutex . state . fetch_or ( LOCK | BLOCKED , Ordering :: Relaxed ) ;
117
- if state & LOCK == 0 {
118
- std:: mem:: drop ( blocked) ;
119
- self . deregister_waker ( true ) ;
120
- return Poll :: Ready ( ( ) )
121
- }
108
+ match self . opt_key {
109
+ None => {
110
+ if self . mutex . try_lock ( ) {
111
+ Poll :: Ready ( ( ) )
112
+ } else {
113
+ let mut blocked = self . mutex . blocked . lock ( ) ;
114
+
115
+ // Try locking again because it's possible the mutex got unlocked before
116
+ // we acquire the lock of `blocked`.
117
+ let state = self . mutex . state . fetch_or ( LOCK | BLOCKED , Ordering :: Relaxed ) ;
118
+ if state & LOCK == 0 {
119
+ return Poll :: Ready ( ( ) ) ;
120
+ }
122
121
123
- // Register the current task.
124
- match self . opt_key {
125
- None => {
122
+ // Register the current task.
126
123
// Insert a new entry into the list of blocked tasks.
127
124
let w = cx. waker ( ) . clone ( ) ;
128
125
let key = blocked. insert ( Some ( w) ) ;
129
126
self . opt_key = Some ( key) ;
127
+
128
+ Poll :: Pending
130
129
}
131
- Some ( key) => {
130
+ }
131
+ Some ( key) => {
132
+ if self . mutex . try_lock ( ) {
133
+ self . deregister_waker ( true ) ;
134
+ Poll :: Ready ( ( ) )
135
+ } else {
136
+ let mut blocked = self . mutex . blocked . lock ( ) ;
137
+
138
+ // Try locking again because it's possible the mutex got unlocked before
139
+ // we acquire the lock of `blocked`. On this path we know we have BLOCKED
140
+ // set, so don't bother to set it again.
141
+ if self . mutex . try_lock ( ) {
142
+ std:: mem:: drop ( blocked) ;
143
+ self . deregister_waker ( true ) ;
144
+ return Poll :: Ready ( ( ) ) ;
145
+ }
146
+
132
147
// There is already an entry in the list of blocked tasks. Just
133
148
// reset the waker if it was removed.
134
149
let opt_waker = unsafe { blocked. get ( key) } ;
135
150
if opt_waker. is_none ( ) {
136
151
let w = cx. waker ( ) . clone ( ) ;
137
152
* opt_waker = Some ( w) ;
138
153
}
154
+
155
+ Poll :: Pending
139
156
}
140
157
}
141
-
142
- Poll :: Pending
143
158
}
144
159
}
145
160
}
0 commit comments