Skip to content

Commit 811280e

Browse files
committed
Add test for wait_timeout with no lock held
1 parent 550e24f commit 811280e

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

src/sync/condvar.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ impl<'a, 'b, T> Future for AwaitNotify<'a, 'b, T> {
384384
}
385385
None => {
386386
if let Some(key) = self.key {
387-
if self.cond.wakers.complete_if_notified(key, cx) {
387+
if self.cond.wakers.remove_if_notified(key, cx) {
388388
self.key = None;
389389
Poll::Ready(())
390390
} else {

src/sync/waker_set.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,12 @@ impl WakerSet {
8484
/// the waker for the entry, and return false. If the waker has been notified,
8585
/// treat the entry as completed and return true.
8686
#[cfg(feature = "unstable")]
87-
pub fn complete_if_notified(&self, key: usize, cx: &Context<'_>) -> bool {
87+
pub fn remove_if_notified(&self, key: usize, cx: &Context<'_>) -> bool {
8888
let mut inner = self.lock();
8989

9090
match &mut inner.entries[key] {
9191
None => {
9292
inner.entries.remove(key);
93-
inner.none_count -= 1;
9493
true
9594
}
9695
Some(w) => {

tests/condvar.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use async_std::sync::{Condvar, Mutex};
66
use async_std::task::{self, JoinHandle};
77

88
#[test]
9-
fn wait_timeout() {
9+
fn wait_timeout_with_lock() {
1010
task::block_on(async {
1111
let pair = Arc::new((Mutex::new(false), Condvar::new()));
1212
let pair2 = pair.clone();
@@ -26,6 +26,19 @@ fn wait_timeout() {
2626
})
2727
}
2828

29+
#[test]
30+
fn wait_timeout_without_lock() {
31+
task::block_on(async {
32+
let m = Mutex::new(false);
33+
let c = Condvar::new();
34+
35+
let (_, wait_result) = c
36+
.wait_timeout(m.lock().await, Duration::from_millis(10))
37+
.await;
38+
assert!(wait_result.timed_out());
39+
})
40+
}
41+
2942
#[test]
3043
fn wait_timeout_until_timed_out() {
3144
task::block_on(async {

0 commit comments

Comments
 (0)