@@ -117,7 +117,11 @@ impl<T: ?Sized> Mutex<T> {
117
117
let listener = self . lock_ops . listen ( ) ;
118
118
119
119
// Try locking if nobody is being starved.
120
- match self . state . compare_and_swap ( 0 , 1 , Ordering :: Acquire ) {
120
+ match self
121
+ . state
122
+ . compare_exchange ( 0 , 1 , Ordering :: Acquire , Ordering :: Acquire )
123
+ . unwrap_or_else ( |x| x)
124
+ {
121
125
// Lock acquired!
122
126
0 => return ,
123
127
@@ -132,7 +136,11 @@ impl<T: ?Sized> Mutex<T> {
132
136
listener. await ;
133
137
134
138
// Try locking if nobody is being starved.
135
- match self . state . compare_and_swap ( 0 , 1 , Ordering :: Acquire ) {
139
+ match self
140
+ . state
141
+ . compare_exchange ( 0 , 1 , Ordering :: Acquire , Ordering :: Acquire )
142
+ . unwrap_or_else ( |x| x)
143
+ {
136
144
// Lock acquired!
137
145
0 => return ,
138
146
@@ -171,7 +179,11 @@ impl<T: ?Sized> Mutex<T> {
171
179
let listener = self . lock_ops . listen ( ) ;
172
180
173
181
// Try locking if nobody else is being starved.
174
- match self . state . compare_and_swap ( 2 , 2 | 1 , Ordering :: Acquire ) {
182
+ match self
183
+ . state
184
+ . compare_exchange ( 2 , 2 | 1 , Ordering :: Acquire , Ordering :: Acquire )
185
+ . unwrap_or_else ( |x| x)
186
+ {
175
187
// Lock acquired!
176
188
2 => return ,
177
189
@@ -213,7 +225,11 @@ impl<T: ?Sized> Mutex<T> {
213
225
/// ```
214
226
#[ inline]
215
227
pub fn try_lock ( & self ) -> Option < MutexGuard < ' _ , T > > {
216
- if self . state . compare_and_swap ( 0 , 1 , Ordering :: Acquire ) == 0 {
228
+ if self
229
+ . state
230
+ . compare_exchange ( 0 , 1 , Ordering :: Acquire , Ordering :: Acquire )
231
+ . is_ok ( )
232
+ {
217
233
Some ( MutexGuard ( self ) )
218
234
} else {
219
235
None
@@ -286,7 +302,11 @@ impl<T: ?Sized> Mutex<T> {
286
302
/// ```
287
303
#[ inline]
288
304
pub fn try_lock_arc ( self : & Arc < Self > ) -> Option < MutexGuardArc < T > > {
289
- if self . state . compare_and_swap ( 0 , 1 , Ordering :: Acquire ) == 0 {
305
+ if self
306
+ . state
307
+ . compare_exchange ( 0 , 1 , Ordering :: Acquire , Ordering :: Acquire )
308
+ . is_ok ( )
309
+ {
290
310
Some ( MutexGuardArc ( self . clone ( ) ) )
291
311
} else {
292
312
None
0 commit comments