Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions library/std/tests/sync/condvar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,17 +285,31 @@ nonpoison_and_poison_unwrap_test!(

thread::scope(|s| {
s.spawn(|| {
// Sleep so that the other thread has a chance to encounter the
// timeout.
thread::sleep(Duration::from_secs(2));
maybe_unwrap(sent.set(true));
cond.notify_all();
});

let guard = maybe_unwrap(sent.lock());
// If there is internal overflow, this call will return almost
// immediately, before the other thread has reached the `notify_all`
let (guard, res) = maybe_unwrap(cond.wait_timeout(guard, Duration::from_secs(u64::MAX.div_ceil(1_000_000_000))));
assert!(!res.timed_out());
assert!(*guard);
let mut guard = maybe_unwrap(sent.lock());
// Loop until `sent` is set by the thread to guard against spurious
// wakeups. If the `wait_timeout` happens just before the signal by
// the other thread, such a spurious wakeup might prevent the
// miscalculated timeout from occurring, but this is basically just
// a smoke test anyway.
loop {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a comment about why the loop?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

if *guard {
break;
}

// If there is internal overflow, this call will return almost
// immediately, before the other thread has reached the `notify_all`,
// and indicate a timeout.
let (g, res) = maybe_unwrap(cond.wait_timeout(guard, Duration::from_secs(u64::MAX.div_ceil(1_000_000_000))));
assert!(!res.timed_out());
guard = g;
}
})
}
);
Loading