@@ -130,11 +130,11 @@ impl<T> Lock<T> {
130
130
// Lock acquired!
131
131
0 => return LockGuard ( self . clone ( ) ) ,
132
132
133
- // Unlocked and somebody is starved - notify the first waiter in line .
134
- s if s % 2 == 0 => self . 0 . lock_ops . notify_one ( ) ,
133
+ // Lock is held and nobody is starved .
134
+ 1 => { }
135
135
136
- // The lock is currently acquired .
137
- _ => { }
136
+ // Somebody is starved .
137
+ _ => break ,
138
138
}
139
139
140
140
// Wait for a notification.
@@ -145,11 +145,16 @@ impl<T> Lock<T> {
145
145
// Lock acquired!
146
146
0 => return LockGuard ( self . clone ( ) ) ,
147
147
148
- // Unlocked and somebody is starved - notify the first waiter in line .
149
- s if s % 2 == 0 => self . 0 . lock_ops . notify_one ( ) ,
148
+ // Lock is held and nobody is starved .
149
+ 1 => { }
150
150
151
- // The lock is currently acquired.
152
- _ => { }
151
+ // Somebody is starved.
152
+ _ => {
153
+ // Notify the first listener in line because we probably received a
154
+ // notification that was meant for a starved thread.
155
+ self . 0 . lock_ops . notify_one ( ) ;
156
+ break ;
157
+ }
153
158
}
154
159
155
160
// If waiting for too long, fall back to a fairer locking strategy that will prevent
@@ -177,13 +182,16 @@ impl<T> Lock<T> {
177
182
// Try locking if nobody else is being starved.
178
183
match self . 0 . state . compare_and_swap ( 2 , 2 | 1 , Ordering :: Acquire ) {
179
184
// Lock acquired!
180
- 0 => return LockGuard ( self . clone ( ) ) ,
185
+ 2 => return LockGuard ( self . clone ( ) ) ,
181
186
182
- // Unlocked and somebody is starved - notify the first waiter in line .
183
- s if s % 2 == 0 => self . 0 . lock_ops . notify_one ( ) ,
187
+ // Lock is held by someone .
188
+ s if s % 2 == 1 => { }
184
189
185
- // The lock is currently acquired.
186
- _ => { }
190
+ // Lock is available.
191
+ _ => {
192
+ // Be fair: notify the first listener and then go wait in line.
193
+ self . 0 . lock_ops . notify_one ( ) ;
194
+ }
187
195
}
188
196
189
197
// Wait for a notification.
0 commit comments