Skip to content

Commit

Permalink
EventFd type
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanWoollett-Light committed Dec 18, 2022
1 parent 99ea907 commit 8ce915d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
(#[1662](https://github.com/nix-rust/nix/pull/1662))
- Added `CanRaw` to `SockProtocol` and `CanBcm` as a separate `SocProtocol` constant.
([#1912](https://github.com/nix-rust/nix/pull/1912))
- Added `EventFd` type.
([#1912](https://github.com/nix-rust/nix/pull/1912))

### Changed

Expand Down
6 changes: 3 additions & 3 deletions src/sys/epoll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl EpollEvent {

/// A safe wrapper around [`epoll`](https://man7.org/linux/man-pages/man7/epoll.7.html).
/// ```
/// # use nix::sys::{epoll::{Epoll, EpollEvent, EpollFlags, EpollCreateFlags}, eventfd::{eventfd, EfdFlags}};
/// # use nix::sys::{epoll::{Epoll, EpollEvent, EpollFlags, EpollCreateFlags}, eventfd::{EventFd, EfdFlags}};
/// # use nix::unistd::write;
/// # use std::os::unix::io::{OwnedFd, FromRawFd, AsRawFd, AsFd};
/// # use std::time::{Instant, Duration};
Expand All @@ -83,11 +83,11 @@ impl EpollEvent {
/// let epoll = Epoll::new(EpollCreateFlags::empty())?;
///
/// // Create eventfd & Add event
/// let eventfd = eventfd(0, EfdFlags::empty())?;
/// let eventfd = EventFd::new()?;
/// epoll.add(&eventfd, EpollEvent::new(EpollFlags::EPOLLIN,DATA))?;
///
/// // Arm eventfd & Time wait
/// write(eventfd.as_raw_fd(), &1u64.to_ne_bytes())?;
/// eventfd.arm()?;
/// let now = Instant::now();
///
/// // Wait on event
Expand Down
54 changes: 52 additions & 2 deletions src/sys/eventfd.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::errno::Errno;
use crate::Result;
use std::os::unix::io::{FromRawFd, OwnedFd};
use crate::{Result,unistd};
use std::os::unix::io::{FromRawFd, OwnedFd, AsRawFd, AsFd, RawFd, BorrowedFd};

libc_bitflags! {
pub struct EfdFlags: libc::c_int {
Expand All @@ -10,8 +10,58 @@ libc_bitflags! {
}
}

#[deprecated(since = "0.27.0", note = "Use EventFd::value_and_flags() instead")]
pub fn eventfd(initval: libc::c_uint, flags: EfdFlags) -> Result<OwnedFd> {
let res = unsafe { libc::eventfd(initval, flags.bits()) };

Errno::result(res).map(|r| unsafe { OwnedFd::from_raw_fd(r) })
}

#[derive(Debug)]
pub struct EventFd(pub OwnedFd);
impl EventFd {
/// [`EventFd::value_and_flags`] with `init_val = 0` and `flags = EfdFlags::empty()`.
pub fn new() -> Result<Self> {
let res = unsafe { libc::eventfd(0, EfdFlags::empty().bits()) };
Errno::result(res).map(|r| Self(unsafe { OwnedFd::from_raw_fd(r) }))
}
/// Constructs [`EventFd`] with the given `init_val` and `flags`.
///
/// Wrapper around [`libc::eventfd`].
pub fn value_and_flags(init_val: u32, flags: EfdFlags) -> Result<Self> {
let res = unsafe { libc::eventfd(init_val, flags.bits()) };
Errno::result(res).map(|r| Self(unsafe { OwnedFd::from_raw_fd(r) }))
}
/// [`EventFd::value_and_flags`] with `init_val = 0` and given `flags`.
pub fn flags(flags: EfdFlags) -> Result<Self> {
let res = unsafe { libc::eventfd(0, flags.bits()) };
Errno::result(res).map(|r| Self(unsafe { OwnedFd::from_raw_fd(r) }))
}
/// [`EventFd::value_and_flags`] with given `init_val` and `flags = EfdFlags::empty()`.
pub fn value(init_val: u32) -> Result<Self> {
let res = unsafe { libc::eventfd(init_val, EfdFlags::empty().bits()) };
Errno::result(res).map(|r| Self(unsafe { OwnedFd::from_raw_fd(r) }))
}
/// [`EventFd::write`] with `1`.
pub fn arm(&self) -> Result<usize> {
unistd::write(self.0.as_raw_fd(),&1u64.to_ne_bytes())
}
/// [`EventFd::write`] with `0`.
pub fn defuse(&self) -> Result<usize> {
unistd::write(self.0.as_raw_fd(),&0u64.to_ne_bytes())
}
/// Writes a given `value` to the file descriptor.
pub fn write(&self, value: u64) -> Result<usize> {
unistd::write(self.0.as_raw_fd(),&value.to_ne_bytes())
}
}
impl AsFd for EventFd {
fn as_fd(&self) -> BorrowedFd {
self.0.as_fd()
}
}
impl AsRawFd for EventFd {
fn as_raw_fd(&self) -> RawFd {
self.0.as_raw_fd()
}
}

0 comments on commit 8ce915d

Please sign in to comment.