Skip to content

Commit b656db2

Browse files
committed
Don't make writers spin when #readers changes in futex RwLock.
1 parent de4a290 commit b656db2

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

library/std/src/sys/unix/locks/futex_rwlock.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -161,23 +161,22 @@ impl RwLock {
161161

162162
// Set the waiting bit indicating that we're waiting on it.
163163
if !writers_waiting(state) {
164-
match self.state.compare_exchange(state, state | WRITERS_WAITING, Relaxed, Relaxed)
164+
if let Err(s) =
165+
self.state.compare_exchange(state, state | WRITERS_WAITING, Relaxed, Relaxed)
165166
{
166-
Ok(_) => state |= WRITERS_WAITING,
167-
Err(s) => {
168-
state = s;
169-
continue;
170-
}
167+
state = s;
168+
continue;
171169
}
172170
}
173171

174172
// Examine the notification counter before we check if `state` has changed,
175173
// to make sure we don't miss any notifications.
176174
let seq = self.writer_notify.load(Acquire);
177175

178-
// Don't go to sleep if the state has already changed.
176+
// Don't go to sleep if the lock has become available, or the
177+
// writers waiting bit is no longer set.
179178
let s = self.state.load(Relaxed);
180-
if state != s {
179+
if readers(s) == 0 || !writers_waiting(s) {
181180
state = s;
182181
continue;
183182
}

0 commit comments

Comments
 (0)