Skip to content

Commit 4a303b1

Browse files
Vytautas Astrauskasvakaras
authored andcommitted
Add a timeout test for conditional variables.
1 parent 6e774de commit 4a303b1

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

tests/run-pass/concurrency/sync.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
// ignore-windows: Concurrency on Windows is not supported yet.
2+
// compile-flags: -Zmiri-disable-isolation
23

34
use std::sync::mpsc::{channel, sync_channel};
45
use std::sync::{Arc, Barrier, Condvar, Mutex, Once, RwLock};
56
use std::thread;
7+
use std::time::{Duration, Instant};
68

79
// Check if Rust barriers are working.
810

@@ -50,6 +52,17 @@ fn check_conditional_variables() {
5052
}
5153
}
5254

55+
/// Test that waiting on a conditional variable with a timeout does not
56+
/// deadlock.
57+
fn check_conditional_variables_timeout() {
58+
let lock = Mutex::new(());
59+
let cvar = Condvar::new();
60+
let guard = lock.lock().unwrap();
61+
let now = Instant::now();
62+
let _guard = cvar.wait_timeout(guard, Duration::from_millis(100)).unwrap().0;
63+
assert!(now.elapsed().as_millis() >= 100);
64+
}
65+
5366
// Check if locks are working.
5467

5568
fn check_mutex() {
@@ -206,6 +219,7 @@ fn check_once() {
206219
fn main() {
207220
check_barriers();
208221
check_conditional_variables();
222+
check_conditional_variables_timeout();
209223
check_mutex();
210224
check_rwlock_write();
211225
check_rwlock_read_no_deadlock();

0 commit comments

Comments
 (0)