@@ -9,9 +9,9 @@ use std::{io, thread};
9
9
10
10
fn wait_wake ( ) {
11
11
fn wake_nobody ( ) {
12
- // Current thread waits on futex
13
- // New thread wakes up 0 threads waiting on that futex
14
- // Current thread should time out
12
+ // Current thread waits on futex.
13
+ // New thread wakes up 0 threads waiting on that futex.
14
+ // Current thread should time out.
15
15
static mut FUTEX : u32 = 0 ;
16
16
17
17
let waker = thread:: spawn ( || {
@@ -43,14 +43,14 @@ fn wait_wake() {
43
43
) ,
44
44
-1
45
45
) ;
46
- // main thread did not get woken up, so it timed out
46
+ // Main thread did not get woken up, so it timed out.
47
47
assert_eq ! ( io:: Error :: last_os_error( ) . raw_os_error( ) . unwrap( ) , libc:: ETIMEDOUT ) ;
48
48
}
49
49
50
50
waker. join ( ) . unwrap ( ) ;
51
51
}
52
52
53
- fn wake_one ( ) {
53
+ fn wake_two_of_three ( ) {
54
54
// We create 2 threads that wait on a futex with a 500ms timeout.
55
55
// The main thread wakes up 1 thread waiting on this futex and after this
56
56
// checks that only 1 thread woke up and the other timed out.
@@ -68,35 +68,40 @@ fn wait_wake() {
68
68
timeout_size_arg,
69
69
& mut timeout as * mut _ as _ ,
70
70
) ;
71
- // Return wether this threads wait timed out or not .
72
- io:: Error :: last_os_error ( ) . raw_os_error ( ) . unwrap ( ) = = libc:: ETIMEDOUT
71
+ // Return true if this thread woke up .
72
+ io:: Error :: last_os_error ( ) . raw_os_error ( ) . unwrap ( ) ! = libc:: ETIMEDOUT
73
73
}
74
74
}
75
75
76
76
let t1 = thread:: spawn ( waiter) ;
77
77
let t2 = thread:: spawn ( waiter) ;
78
+ let t3 = thread:: spawn ( waiter) ;
78
79
79
80
thread:: yield_now ( ) ;
80
- // Wake up 1 thread and make sure the other is still waiting
81
+ // Wake up 2 thread and make sure 1 is still waiting.
81
82
unsafe {
82
83
assert_eq ! (
83
84
libc:: _umtx_op(
84
85
addr_of!( FUTEX ) as * mut _,
85
86
libc:: UMTX_OP_WAKE_PRIVATE ,
86
- 1 ,
87
+ 2 ,
87
88
ptr:: null_mut:: <libc:: c_void>( ) ,
88
89
ptr:: null_mut:: <libc:: c_void>( ) ,
89
90
) ,
90
91
0
91
92
) ;
92
93
}
93
- let t1_woke_up = t1. join ( ) . unwrap ( ) ;
94
- let t2_woke_up = t2. join ( ) . unwrap ( ) ;
95
- assert ! ( t1_woke_up ^ t2_woke_up, "Expected 1 thread to wake up" ) ;
94
+
95
+ // Treat the booleans as numbers to simplify checking how many threads were woken up.
96
+ let t1 = t1. join ( ) . unwrap ( ) as usize ;
97
+ let t2 = t2. join ( ) . unwrap ( ) as usize ;
98
+ let t3 = t3. join ( ) . unwrap ( ) as usize ;
99
+ let woken_up_count = t1 + t2 + t3;
100
+ assert ! ( woken_up_count == 2 , "Expected 2 threads to wake up got: {woken_up_count}" ) ;
96
101
}
97
102
98
103
wake_nobody ( ) ;
99
- wake_one ( ) ;
104
+ wake_two_of_three ( ) ;
100
105
}
101
106
102
107
fn wake_dangling ( ) {
0 commit comments