Skip to content

Commit 109c299

Browse files
committed
Simplify thread blocking tests
1 parent d125945 commit 109c299

File tree

5 files changed

+18
-49
lines changed

5 files changed

+18
-49
lines changed

src/tools/miri/tests/fail-dep/libc/eventfd_block_read_twice.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ fn main() {
2323
let fd = unsafe { libc::eventfd(0, flags) };
2424

2525
let thread1 = thread::spawn(move || {
26-
thread::park();
2726
let mut buf: [u8; 8] = [0; 8];
2827
// This read will block initially.
2928
let res: i64 = unsafe { libc::read(fd, buf.as_mut_ptr().cast(), 8).try_into().unwrap() };
@@ -33,7 +32,6 @@ fn main() {
3332
});
3433

3534
let thread2 = thread::spawn(move || {
36-
thread::park();
3735
let mut buf: [u8; 8] = [0; 8];
3836
// This read will block initially, then get unblocked by thread3, then get blocked again
3937
// because the `read` in thread1 executes first and set the counter to 0 again.
@@ -45,7 +43,6 @@ fn main() {
4543
});
4644

4745
let thread3 = thread::spawn(move || {
48-
thread::park();
4946
let sized_8_data = 1_u64.to_ne_bytes();
5047
// Write 1 to the counter, so both thread1 and thread2 will unblock.
5148
let res: i64 = unsafe {
@@ -55,10 +52,6 @@ fn main() {
5552
assert_eq!(res, 8);
5653
});
5754

58-
thread1.thread().unpark();
59-
thread2.thread().unpark();
60-
thread3.thread().unpark();
61-
6255
thread1.join().unwrap();
6356
thread2.join().unwrap();
6457
thread3.join().unwrap();

src/tools/miri/tests/fail-dep/libc/eventfd_block_write_twice.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ fn main() {
2828
assert_eq!(res, 8);
2929

3030
let thread1 = thread::spawn(move || {
31-
thread::park();
3231
let sized_8_data = (u64::MAX - 1).to_ne_bytes();
3332
let res: i64 = unsafe {
3433
libc::write(fd, sized_8_data.as_ptr() as *const libc::c_void, 8).try_into().unwrap()
@@ -38,7 +37,6 @@ fn main() {
3837
});
3938

4039
let thread2 = thread::spawn(move || {
41-
thread::park();
4240
let sized_8_data = (u64::MAX - 1).to_ne_bytes();
4341
// Write u64::MAX - 1, so the all subsequent write will block.
4442
let res: i64 = unsafe {
@@ -52,7 +50,6 @@ fn main() {
5250
});
5351

5452
let thread3 = thread::spawn(move || {
55-
thread::park();
5653
let mut buf: [u8; 8] = [0; 8];
5754
// This will unblock both `write` in thread1 and thread2.
5855
let res: i64 = unsafe { libc::read(fd, buf.as_mut_ptr().cast(), 8).try_into().unwrap() };
@@ -61,10 +58,6 @@ fn main() {
6158
assert_eq!(counter, (u64::MAX - 1));
6259
});
6360

64-
thread1.thread().unpark();
65-
thread2.thread().unpark();
66-
thread3.thread().unpark();
67-
6861
thread1.join().unwrap();
6962
thread2.join().unwrap();
7063
thread3.join().unwrap();

src/tools/miri/tests/fail-dep/libc/libc_epoll_block_two_thread.rs

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
//@error-in-other-file: deadlock
66

77
use std::convert::TryInto;
8-
use std::thread;
98
use std::thread::spawn;
109

1110
// Using `as` cast since `EPOLLET` wraps around
@@ -41,10 +40,10 @@ fn check_epoll_wait<const N: usize>(
4140

4241
// Test if only one thread is unblocked if multiple threads blocked on same epfd.
4342
// Expected execution:
44-
// 1. Thread 2 blocks.
45-
// 2. Thread 3 blocks.
46-
// 3. Thread 1 unblocks thread 3.
47-
// 4. Thread 2 deadlocks.
43+
// 1. Thread 1 blocks.
44+
// 2. Thread 2 blocks.
45+
// 3. Thread 3 unblocks thread 2.
46+
// 4. Thread 1 deadlocks.
4847
fn main() {
4948
// Create an epoll instance.
5049
let epfd = unsafe { libc::epoll_create1(0) };
@@ -65,30 +64,21 @@ fn main() {
6564
let expected_value = fds[0] as u64;
6665
check_epoll_wait::<1>(epfd, &[(expected_event, expected_value)], 0);
6766

68-
let thread1 = spawn(move || {
69-
thread::park();
70-
let data = "abcde".as_bytes().as_ptr();
71-
let res = unsafe { libc::write(fds[1], data as *const libc::c_void, 5) };
72-
assert_eq!(res, 5);
73-
});
74-
7567
let expected_event = u32::try_from(libc::EPOLLIN | libc::EPOLLOUT).unwrap();
7668
let expected_value = fds[0] as u64;
77-
let thread2 = spawn(move || {
78-
thread::park();
69+
let thread1 = spawn(move || {
7970
check_epoll_wait::<1>(epfd, &[(expected_event, expected_value)], -1);
8071
//~^ERROR: deadlocked
8172
});
82-
let thread3 = spawn(move || {
83-
thread::park();
73+
let thread2 = spawn(move || {
8474
check_epoll_wait::<1>(epfd, &[(expected_event, expected_value)], -1);
8575
});
8676

87-
thread2.thread().unpark();
88-
thread::yield_now();
89-
thread3.thread().unpark();
90-
thread::yield_now();
91-
thread1.thread().unpark();
77+
let thread3 = spawn(move || {
78+
let data = "abcde".as_bytes().as_ptr();
79+
let res = unsafe { libc::write(fds[1], data as *const libc::c_void, 5) };
80+
assert_eq!(res, 5);
81+
});
9282

9383
thread1.join().unwrap();
9484
thread2.join().unwrap();

src/tools/miri/tests/fail-dep/libc/libc_epoll_block_two_thread.stderr

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
error: deadlock: the evaluated program deadlocked
2+
|
3+
= note: the evaluated program deadlocked
4+
= note: (no span available)
5+
= note: BACKTRACE on thread `unnamed-ID`:
6+
17
error: deadlock: the evaluated program deadlocked
28
--> RUSTLIB/std/src/sys/pal/PLATFORM/thread.rs:LL:CC
39
|
@@ -11,15 +17,9 @@ LL | let ret = unsafe { libc::pthread_join(id, ptr::null_mut()) };
1117
note: inside `main`
1218
--> tests/fail-dep/libc/libc_epoll_block_two_thread.rs:LL:CC
1319
|
14-
LL | thread2.join().unwrap();
20+
LL | thread1.join().unwrap();
1521
| ^^^^^^^^^^^^^^
1622

17-
error: deadlock: the evaluated program deadlocked
18-
|
19-
= note: the evaluated program deadlocked
20-
= note: (no span available)
21-
= note: BACKTRACE on thread `unnamed-ID`:
22-
2323
error: deadlock: the evaluated program deadlocked
2424
--> tests/fail-dep/libc/libc_epoll_block_two_thread.rs:LL:CC
2525
|

src/tools/miri/tests/pass-dep/libc/libc-eventfd.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,6 @@ fn test_two_threads_blocked_on_eventfd() {
197197
assert_eq!(res, 8);
198198

199199
let thread1 = thread::spawn(move || {
200-
thread::park();
201200
let sized_8_data = 1_u64.to_ne_bytes();
202201
let res: i64 = unsafe {
203202
libc::write(fd, sized_8_data.as_ptr() as *const libc::c_void, 8).try_into().unwrap()
@@ -207,7 +206,6 @@ fn test_two_threads_blocked_on_eventfd() {
207206
});
208207

209208
let thread2 = thread::spawn(move || {
210-
thread::park();
211209
let sized_8_data = 1_u64.to_ne_bytes();
212210
let res: i64 = unsafe {
213211
libc::write(fd, sized_8_data.as_ptr() as *const libc::c_void, 8).try_into().unwrap()
@@ -217,7 +215,6 @@ fn test_two_threads_blocked_on_eventfd() {
217215
});
218216

219217
let thread3 = thread::spawn(move || {
220-
thread::park();
221218
let mut buf: [u8; 8] = [0; 8];
222219
// This will unblock previously blocked eventfd read.
223220
let res = read_bytes(fd, &mut buf);
@@ -227,10 +224,6 @@ fn test_two_threads_blocked_on_eventfd() {
227224
assert_eq!(counter, (u64::MAX - 1));
228225
});
229226

230-
thread1.thread().unpark();
231-
thread2.thread().unpark();
232-
thread3.thread().unpark();
233-
234227
thread1.join().unwrap();
235228
thread2.join().unwrap();
236229
thread3.join().unwrap();

0 commit comments

Comments
 (0)