Skip to content

Commit ca7142b

Browse files
Alter wait wake test for better edge case handling + Cleanup comments
1 parent b09ce99 commit ca7142b

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

tests/pass-dep/concurrency/freebsd-futex.rs

+18-13
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ use std::{io, thread};
99

1010
fn wait_wake() {
1111
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.
1515
static mut FUTEX: u32 = 0;
1616

1717
let waker = thread::spawn(|| {
@@ -43,14 +43,14 @@ fn wait_wake() {
4343
),
4444
-1
4545
);
46-
// main thread did not get woken up, so it timed out
46+
// Main thread did not get woken up, so it timed out.
4747
assert_eq!(io::Error::last_os_error().raw_os_error().unwrap(), libc::ETIMEDOUT);
4848
}
4949

5050
waker.join().unwrap();
5151
}
5252

53-
fn wake_one() {
53+
fn wake_two_of_three() {
5454
// We create 2 threads that wait on a futex with a 500ms timeout.
5555
// The main thread wakes up 1 thread waiting on this futex and after this
5656
// checks that only 1 thread woke up and the other timed out.
@@ -68,35 +68,40 @@ fn wait_wake() {
6868
timeout_size_arg,
6969
&mut timeout as *mut _ as _,
7070
);
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
7373
}
7474
}
7575

7676
let t1 = thread::spawn(waiter);
7777
let t2 = thread::spawn(waiter);
78+
let t3 = thread::spawn(waiter);
7879

7980
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.
8182
unsafe {
8283
assert_eq!(
8384
libc::_umtx_op(
8485
addr_of!(FUTEX) as *mut _,
8586
libc::UMTX_OP_WAKE_PRIVATE,
86-
1,
87+
2,
8788
ptr::null_mut::<libc::c_void>(),
8889
ptr::null_mut::<libc::c_void>(),
8990
),
9091
0
9192
);
9293
}
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}");
96101
}
97102

98103
wake_nobody();
99-
wake_one();
104+
wake_two_of_three();
100105
}
101106

102107
fn wake_dangling() {

0 commit comments

Comments
 (0)