virtio_driver/util/
eventfd.rs1use rustix::event::eventfd;
4pub use rustix::event::EventfdFlags;
5use rustix::fd::{AsFd, BorrowedFd, OwnedFd};
6use rustix::io::{read, write};
7use std::io::Error;
8use std::os::unix::io::{AsRawFd, RawFd};
9
10#[derive(Debug)]
11pub struct EventFd {
12 fd: OwnedFd,
13}
14
15impl EventFd {
16 #[allow(dead_code)] pub(crate) fn new(flags: EventfdFlags) -> Result<Self, Error> {
18 let fd = eventfd(0, flags)?;
19 Ok(EventFd { fd })
20 }
21
22 pub fn read(&self) -> Result<u64, Error> {
23 let mut buf = [0u8; 8];
24
25 read(&self.fd, &mut buf)?;
26
27 Ok(u64::from_ne_bytes(buf))
28 }
29
30 pub fn write(&self, val: u64) -> Result<(), Error> {
31 let buf = val.to_ne_bytes();
32
33 write(&self.fd, &buf)?;
34
35 Ok(())
36 }
37}
38
39impl AsFd for EventFd {
40 fn as_fd(&self) -> BorrowedFd {
41 self.fd.as_fd()
42 }
43}
44
45impl AsRawFd for EventFd {
46 fn as_raw_fd(&self) -> RawFd {
47 self.fd.as_raw_fd()
48 }
49}