1
1
// ignore-windows: No libc on Windows
2
2
// ignore-macos: pthread_condattr_setclock is not supported on MacOS.
3
- // compile-flags: -Zmiri-disable-isolation
3
+ // compile-flags: -Zmiri-disable-isolation -Zmiri-check-number-validity
4
4
5
5
#![ feature( rustc_private) ]
6
6
7
7
/// Test that conditional variable timeouts are working properly with both
8
8
/// monotonic and system clocks.
9
9
extern crate libc;
10
10
11
- use std:: mem;
11
+ use std:: mem:: MaybeUninit ;
12
12
use std:: time:: Instant ;
13
13
14
14
fn test_timed_wait_timeout ( clock_id : i32 ) {
15
15
unsafe {
16
- let mut attr: libc:: pthread_condattr_t = mem :: zeroed ( ) ;
17
- assert_eq ! ( libc:: pthread_condattr_init( & mut attr as * mut _ ) , 0 ) ;
18
- assert_eq ! ( libc:: pthread_condattr_setclock( & mut attr as * mut _ , clock_id) , 0 ) ;
16
+ let mut attr: MaybeUninit < libc:: pthread_condattr_t > = MaybeUninit :: uninit ( ) ;
17
+ assert_eq ! ( libc:: pthread_condattr_init( attr. as_mut_ptr ( ) ) , 0 ) ;
18
+ assert_eq ! ( libc:: pthread_condattr_setclock( attr. as_mut_ptr ( ) , clock_id) , 0 ) ;
19
19
20
- let mut cond: libc:: pthread_cond_t = mem :: zeroed ( ) ;
21
- assert_eq ! ( libc:: pthread_cond_init( & mut cond as * mut _ , & attr as * const _ ) , 0 ) ;
22
- assert_eq ! ( libc:: pthread_condattr_destroy( & mut attr as * mut _ ) , 0 ) ;
20
+ let mut cond: MaybeUninit < libc:: pthread_cond_t > = MaybeUninit :: uninit ( ) ;
21
+ assert_eq ! ( libc:: pthread_cond_init( cond. as_mut_ptr ( ) , attr. as_ptr ( ) ) , 0 ) ;
22
+ assert_eq ! ( libc:: pthread_condattr_destroy( attr. as_mut_ptr ( ) ) , 0 ) ;
23
23
24
- let mut mutex: libc:: pthread_mutex_t = mem :: zeroed ( ) ;
24
+ let mut mutex: libc:: pthread_mutex_t = libc :: PTHREAD_MUTEX_INITIALIZER ;
25
25
26
- let mut now: libc:: timespec = mem:: zeroed ( ) ;
27
- assert_eq ! ( libc:: clock_gettime( clock_id, & mut now) , 0 ) ;
26
+ let mut now_mu: MaybeUninit < libc:: timespec > = MaybeUninit :: uninit ( ) ;
27
+ assert_eq ! ( libc:: clock_gettime( clock_id, now_mu. as_mut_ptr( ) ) , 0 ) ;
28
+ let now = now_mu. assume_init ( ) ;
28
29
// Waiting for a second... mostly because waiting less requires mich more tricky arithmetic.
29
30
// FIXME: wait less.
30
31
let timeout = libc:: timespec { tv_sec : now. tv_sec + 1 , tv_nsec : now. tv_nsec } ;
31
32
32
33
assert_eq ! ( libc:: pthread_mutex_lock( & mut mutex as * mut _) , 0 ) ;
33
34
let current_time = Instant :: now ( ) ;
34
35
assert_eq ! (
35
- libc:: pthread_cond_timedwait( & mut cond as * mut _ , & mut mutex as * mut _, & timeout) ,
36
+ libc:: pthread_cond_timedwait( cond. as_mut_ptr ( ) , & mut mutex as * mut _, & timeout) ,
36
37
libc:: ETIMEDOUT
37
38
) ;
38
39
let elapsed_time = current_time. elapsed ( ) . as_millis ( ) ;
39
40
assert ! ( 900 <= elapsed_time && elapsed_time <= 1300 ) ;
40
41
41
42
// Test calling `pthread_cond_timedwait` again with an already elapsed timeout.
42
43
assert_eq ! (
43
- libc:: pthread_cond_timedwait( & mut cond as * mut _ , & mut mutex as * mut _, & timeout) ,
44
+ libc:: pthread_cond_timedwait( cond. as_mut_ptr ( ) , & mut mutex as * mut _, & timeout) ,
44
45
libc:: ETIMEDOUT
45
46
) ;
46
47
@@ -49,7 +50,7 @@ fn test_timed_wait_timeout(clock_id: i32) {
49
50
let invalid_timeout_1 = libc:: timespec { tv_sec : now. tv_sec + 1 , tv_nsec : 1_000_000_000 } ;
50
51
assert_eq ! (
51
52
libc:: pthread_cond_timedwait(
52
- & mut cond as * mut _ ,
53
+ cond. as_mut_ptr ( ) ,
53
54
& mut mutex as * mut _,
54
55
& invalid_timeout_1
55
56
) ,
@@ -58,7 +59,7 @@ fn test_timed_wait_timeout(clock_id: i32) {
58
59
let invalid_timeout_2 = libc:: timespec { tv_sec : now. tv_sec + 1 , tv_nsec : -1 } ;
59
60
assert_eq ! (
60
61
libc:: pthread_cond_timedwait(
61
- & mut cond as * mut _ ,
62
+ cond. as_mut_ptr ( ) ,
62
63
& mut mutex as * mut _,
63
64
& invalid_timeout_2
64
65
) ,
@@ -68,7 +69,7 @@ fn test_timed_wait_timeout(clock_id: i32) {
68
69
let invalid_timeout_3 = libc:: timespec { tv_sec : -1 , tv_nsec : 0 } ;
69
70
assert_eq ! (
70
71
libc:: pthread_cond_timedwait(
71
- & mut cond as * mut _ ,
72
+ cond. as_mut_ptr ( ) ,
72
73
& mut mutex as * mut _,
73
74
& invalid_timeout_3
74
75
) ,
@@ -77,7 +78,7 @@ fn test_timed_wait_timeout(clock_id: i32) {
77
78
78
79
assert_eq ! ( libc:: pthread_mutex_unlock( & mut mutex as * mut _) , 0 ) ;
79
80
assert_eq ! ( libc:: pthread_mutex_destroy( & mut mutex as * mut _) , 0 ) ;
80
- assert_eq ! ( libc:: pthread_cond_destroy( & mut cond as * mut _ ) , 0 ) ;
81
+ assert_eq ! ( libc:: pthread_cond_destroy( cond. as_mut_ptr ( ) ) , 0 ) ;
81
82
}
82
83
}
83
84
0 commit comments