-
Notifications
You must be signed in to change notification settings - Fork 405
Implement blocking eventfd #3939
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This used to be done in both match arms. The comment even says that. Now it was moved to only run in one match arm. The comment is outdated now, and also -- why was it moved?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess it is because in the other arm, nothing actually changes, we just block?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes exactly. There are four scenarios that could happen here:
check_and_update_readiness.check_and_update_readiness.ErrorKind::WouldBlock.