Skip to content

Commit

Permalink
Don't make writers spin when #readers changes in futex RwLock.
Browse files Browse the repository at this point in the history
  • Loading branch information
m-ou-se committed Apr 7, 2022
1 parent de4a290 commit b656db2
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions library/std/src/sys/unix/locks/futex_rwlock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,23 +161,22 @@ impl RwLock {

// Set the waiting bit indicating that we're waiting on it.
if !writers_waiting(state) {
match self.state.compare_exchange(state, state | WRITERS_WAITING, Relaxed, Relaxed)
if let Err(s) =
self.state.compare_exchange(state, state | WRITERS_WAITING, Relaxed, Relaxed)
{
Ok(_) => state |= WRITERS_WAITING,
Err(s) => {
state = s;
continue;
}
state = s;
continue;
}
}

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

// Don't go to sleep if the state has already changed.
// Don't go to sleep if the lock has become available, or the
// writers waiting bit is no longer set.
let s = self.state.load(Relaxed);
if state != s {
if readers(s) == 0 || !writers_waiting(s) {
state = s;
continue;
}
Expand Down

0 comments on commit b656db2

Please sign in to comment.