|
1 | 1 | use crate::errno::Errno;
|
2 |
| -use crate::{Result,unistd}; |
3 |
| -use std::os::unix::io::{FromRawFd, OwnedFd, AsRawFd, AsFd, RawFd, BorrowedFd}; |
| 2 | +use crate::Result; |
| 3 | +use libc::eventfd_t; |
| 4 | +use std::mem::MaybeUninit; |
| 5 | +use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, OwnedFd, RawFd}; |
4 | 6 |
|
5 | 7 | libc_bitflags! {
|
6 | 8 | pub struct EfdFlags: libc::c_int {
|
@@ -57,14 +59,20 @@ impl EventFd {
|
57 | 59 | /// The next `value` calls to `poll`, `select` or `epoll` will return immediately.
|
58 | 60 | ///
|
59 | 61 | /// [`EventFd::write`] with `value`.
|
60 |
| - pub fn write(&self, value: u64) -> Result<usize> { |
61 |
| - unistd::write(&self.0,&value.to_ne_bytes()) |
| 62 | + pub fn write(&self, value: eventfd_t) -> Result<usize> { |
| 63 | + let res = unsafe { libc::eventfd_write(self.0.as_raw_fd(), value) }; |
| 64 | + Errno::result(res).map(|res| res as usize) |
62 | 65 | }
|
63 | 66 | // Reads the value from the file descriptor.
|
64 |
| - pub fn read(&self) -> Result<u64> { |
65 |
| - let mut arr = [0; std::mem::size_of::<u64>()]; |
66 |
| - unistd::read(self.0.as_raw_fd(),&mut arr)?; |
67 |
| - Ok(u64::from_ne_bytes(arr)) |
| 67 | + pub fn read(&self) -> Result<eventfd_t> { |
| 68 | + let mut buf = MaybeUninit::uninit(); |
| 69 | + let res = |
| 70 | + unsafe { libc::eventfd_read(self.0.as_raw_fd(), buf.as_mut_ptr()) }; |
| 71 | + Errno::result(res)?; |
| 72 | + |
| 73 | + // SAFETY: |
| 74 | + // It is guaranteed to be initialized by the `eventfd_read()` function. |
| 75 | + Ok(unsafe { buf.assume_init() }) |
68 | 76 | }
|
69 | 77 | }
|
70 | 78 | impl AsFd for EventFd {
|
|
0 commit comments