Skip to content

Commit

Permalink
Rollup merge of #114359 - ttsugriy:barrier-simpl, r=cuviper
Browse files Browse the repository at this point in the history
[library/std] Replace condv while loop with `cvar.wait_while`.

`wait_while` takes care of spurious wake-ups in centralized place, reducing chances for mistakes and potential future optimizations (who knows, maybe in future there will be no spurious wake-ups? :)
  • Loading branch information
compiler-errors committed Aug 11, 2023
2 parents 3791f6d + a090e97 commit 5da7f36
Showing 1 changed file with 2 additions and 5 deletions.
7 changes: 2 additions & 5 deletions library/std/src/sync/barrier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,8 @@ impl Barrier {
let local_gen = lock.generation_id;
lock.count += 1;
if lock.count < self.num_threads {
// We need a while loop to guard against spurious wakeups.
// https://en.wikipedia.org/wiki/Spurious_wakeup
while local_gen == lock.generation_id {
lock = self.cvar.wait(lock).unwrap();
}
let _guard =
self.cvar.wait_while(lock, |state| local_gen == state.generation_id).unwrap();
BarrierWaitResult(false)
} else {
lock.count = 0;
Expand Down

0 comments on commit 5da7f36

Please sign in to comment.