1 use std
::convert
::TryFrom
;
3 use std
::os
::raw
::c_int
;
4 use std
::os
::unix
::io
::{AsRawFd, FromRawFd, RawFd}
;
5 use std
::time
::Duration
;
7 use crate::error
::io_err_other
;
10 pub type EpollEvent
= libc
::epoll_event
;
12 pub const EPOLLIN
: u32 = libc
::EPOLLIN
as u32;
13 pub const EPOLLET
: u32 = libc
::EPOLLET
as u32;
14 pub const EPOLLOUT
: u32 = libc
::EPOLLOUT
as u32;
15 pub const EPOLLERR
: u32 = libc
::EPOLLERR
as u32;
16 pub const EPOLLHUP
: u32 = libc
::EPOLLHUP
as u32;
23 pub fn new() -> io
::Result
<Self> {
24 let fd
= unsafe { Fd::from_raw_fd(c_try!(libc::epoll_create1(libc::EPOLL_CLOEXEC))) }
;
28 pub fn add_file
<T
: AsRawFd
>(&self, fd
: &T
, events
: u32, data
: u64) -> io
::Result
<()> {
29 self.add_fd(fd
.as_raw_fd(), events
, data
)
32 pub fn modify_file
<T
: AsRawFd
>(&self, fd
: &T
, events
: u32, data
: u64) -> io
::Result
<()> {
33 self.modify_fd(fd
.as_raw_fd(), events
, data
)
36 pub fn remove_file
<T
: AsRawFd
>(&self, fd
: &T
) -> io
::Result
<()> {
37 self.remove_fd(fd
.as_raw_fd())
40 fn addmod_fd(&self, op
: c_int
, fd
: RawFd
, events
: u32, data
: u64) -> io
::Result
<()> {
41 let mut events
= libc
::epoll_event
{
45 c_try
!(unsafe { libc::epoll_ctl(self.fd.as_raw_fd(), op, fd, &mut events) }
);
49 pub fn add_fd(&self, fd
: RawFd
, events
: u32, data
: u64) -> io
::Result
<()> {
50 self.addmod_fd(libc
::EPOLL_CTL_ADD
, fd
, events
, data
)
53 pub fn modify_fd(&self, fd
: RawFd
, events
: u32, data
: u64) -> io
::Result
<()> {
54 self.addmod_fd(libc
::EPOLL_CTL_MOD
, fd
, events
, data
)
57 pub fn remove_fd(&self, fd
: RawFd
) -> io
::Result
<()> {
71 event_buf
: &mut [EpollEvent
],
72 timeout
: Option
<Duration
>,
73 ) -> io
::Result
<usize> {
75 .map(|t
| c_int
::try_from(t
.as_millis()))
77 .map_err(io_err_other
)?
79 let epfd
= self.fd
.as_raw_fd();
80 let buf_len
= c_int
::try_from(event_buf
.len()).map_err(io_err_other
)?
;
81 let rc
= c_try
!(unsafe { libc::epoll_wait(epfd, event_buf.as_mut_ptr(), buf_len, millis) }
);