-
Notifications
You must be signed in to change notification settings - Fork 349
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3939 from tiif/blockeventfd
Implement blocking eventfd
- Loading branch information
Showing
11 changed files
with
491 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
//@only-target: linux | ||
//~^ERROR: deadlocked | ||
//~^^ERROR: deadlocked | ||
//@compile-flags: -Zmiri-preemption-rate=0 | ||
//@error-in-other-file: deadlock | ||
|
||
use std::thread; | ||
|
||
// Test the behaviour of a thread being blocked on an eventfd read, get unblocked, and then | ||
// get blocked again. | ||
|
||
// The expected execution is | ||
// 1. Thread 1 blocks. | ||
// 2. Thread 2 blocks. | ||
// 3. Thread 3 unblocks both thread 1 and thread 2. | ||
// 4. Thread 1 reads. | ||
// 5. Thread 2's `read` deadlocked. | ||
|
||
fn main() { | ||
// eventfd write will block when EFD_NONBLOCK flag is clear | ||
// and the addition caused counter to exceed u64::MAX - 1. | ||
let flags = libc::EFD_CLOEXEC; | ||
let fd = unsafe { libc::eventfd(0, flags) }; | ||
|
||
let thread1 = thread::spawn(move || { | ||
thread::park(); | ||
let mut buf: [u8; 8] = [0; 8]; | ||
// This read will block initially. | ||
let res: i64 = unsafe { libc::read(fd, buf.as_mut_ptr().cast(), 8).try_into().unwrap() }; | ||
assert_eq!(res, 8); | ||
let counter = u64::from_ne_bytes(buf); | ||
assert_eq!(counter, 1_u64); | ||
}); | ||
|
||
let thread2 = thread::spawn(move || { | ||
thread::park(); | ||
let mut buf: [u8; 8] = [0; 8]; | ||
// This read will block initially, then get unblocked by thread3, then get blocked again | ||
// because the `read` in thread1 executes first and set the counter to 0 again. | ||
let res: i64 = unsafe { libc::read(fd, buf.as_mut_ptr().cast(), 8).try_into().unwrap() }; | ||
//~^ERROR: deadlocked | ||
assert_eq!(res, 8); | ||
let counter = u64::from_ne_bytes(buf); | ||
assert_eq!(counter, 1_u64); | ||
}); | ||
|
||
let thread3 = thread::spawn(move || { | ||
thread::park(); | ||
let sized_8_data = 1_u64.to_ne_bytes(); | ||
// Write 1 to the counter, so both thread1 and thread2 will unblock. | ||
let res: i64 = unsafe { | ||
libc::write(fd, sized_8_data.as_ptr() as *const libc::c_void, 8).try_into().unwrap() | ||
}; | ||
// Make sure that write is successful. | ||
assert_eq!(res, 8); | ||
}); | ||
|
||
thread1.thread().unpark(); | ||
thread2.thread().unpark(); | ||
thread3.thread().unpark(); | ||
|
||
thread1.join().unwrap(); | ||
thread2.join().unwrap(); | ||
thread3.join().unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
error: deadlock: the evaluated program deadlocked | ||
--> RUSTLIB/std/src/sys/pal/PLATFORM/thread.rs:LL:CC | ||
| | ||
LL | let ret = unsafe { libc::pthread_join(id, ptr::null_mut()) }; | ||
| ^ the evaluated program deadlocked | ||
| | ||
= note: BACKTRACE: | ||
= note: inside `std::sys::pal::PLATFORM::thread::Thread::join` at RUSTLIB/std/src/sys/pal/PLATFORM/thread.rs:LL:CC | ||
= note: inside `std::thread::JoinInner::<'_, ()>::join` at RUSTLIB/std/src/thread/mod.rs:LL:CC | ||
= note: inside `std::thread::JoinHandle::<()>::join` at RUSTLIB/std/src/thread/mod.rs:LL:CC | ||
note: inside `main` | ||
--> tests/fail-dep/libc/eventfd_block_read_twice.rs:LL:CC | ||
| | ||
LL | thread2.join().unwrap(); | ||
| ^^^^^^^^^^^^^^ | ||
|
||
error: deadlock: the evaluated program deadlocked | ||
| | ||
= note: the evaluated program deadlocked | ||
= note: (no span available) | ||
= note: BACKTRACE on thread `unnamed-ID`: | ||
|
||
error: deadlock: the evaluated program deadlocked | ||
--> tests/fail-dep/libc/eventfd_block_read_twice.rs:LL:CC | ||
| | ||
LL | let res: i64 = unsafe { libc::read(fd, buf.as_mut_ptr().cast(), 8).try_into().unwrap() }; | ||
| ^ the evaluated program deadlocked | ||
| | ||
= note: BACKTRACE on thread `unnamed-ID`: | ||
= note: inside closure at tests/fail-dep/libc/eventfd_block_read_twice.rs:LL:CC | ||
|
||
error: deadlock: the evaluated program deadlocked | ||
| | ||
= note: the evaluated program deadlocked | ||
= note: (no span available) | ||
= note: BACKTRACE on thread `unnamed-ID`: | ||
|
||
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace | ||
|
||
error: aborting due to 4 previous errors | ||
|
Oops, something went wrong.