1 use std
::convert
::TryFrom
;
3 use std
::time
::Duration
;
4 use std
::os
::raw
::c_int
;
5 use std
::os
::unix
::io
::{AsRawFd, FromRawFd, RawFd}
;
8 use crate::error
::io_err_other
;
10 pub type EpollEvent
= libc
::epoll_event
;
17 pub fn new() -> io
::Result
<Self> {
18 let fd
= unsafe { Fd::from_raw_fd(c_try!(libc::epoll_create1(libc::EPOLL_CLOEXEC))) }
;
22 pub fn add_file
<T
: AsRawFd
>(&self, fd
: &T
, events
: u32, data
: u64) -> io
::Result
<()> {
23 self.add_fd(fd
.as_raw_fd(), events
, data
)
26 pub fn modify_file
<T
: AsRawFd
>(&self, fd
: &T
, events
: u32, data
: u64) -> io
::Result
<()> {
27 self.modify_fd(fd
.as_raw_fd(), events
, data
)
30 pub fn remove_file
<T
: AsRawFd
>(&self, fd
: &T
) -> io
::Result
<()> {
31 self.remove_fd(fd
.as_raw_fd())
34 fn addmod_fd(&self, op
: c_int
, fd
: RawFd
, events
: u32, data
: u64) -> io
::Result
<()> {
35 let mut events
= libc
::epoll_event
{
39 c_try
!(unsafe { libc::epoll_ctl(self.fd.as_raw_fd(), op, fd, &mut events) }
);
43 fn add_fd(&self, fd
: RawFd
, events
: u32, data
: u64) -> io
::Result
<()> {
44 self.addmod_fd(libc
::EPOLL_CTL_ADD
, fd
, events
, data
)
47 fn modify_fd(&self, fd
: RawFd
, events
: u32, data
: u64) -> io
::Result
<()> {
48 self.addmod_fd(libc
::EPOLL_CTL_MOD
, fd
, events
, data
)
51 fn remove_fd(&self, fd
: RawFd
) -> io
::Result
<()> {
63 pub fn wait(&self, event_buf
: &mut [EpollEvent
], timeout
: Option
<Duration
>) -> io
::Result
<usize> {
65 .map(|t
| c_int
::try_from(t
.as_millis()))
67 .map_err(io_err_other
)?
69 let epfd
= self.fd
.as_raw_fd();
70 let buf_len
= c_int
::try_from(event_buf
.len()).map_err(io_err_other
)?
;
71 let rc
= c_try
!(unsafe { libc::epoll_wait(epfd, event_buf.as_mut_ptr(), buf_len, millis) }
);