| 1 |
use std::os::unix::io::{AsRawFd, RawFd}; |
| •2 |
use std::os::fd::AsFd; |
| 3 |
|
| 4 |
use super::{Interest, Mode, PollEvent, Readiness, Token}; |
| 5 |
|
| 6 |
use nix::sys::{ |
| 7 |
epoll::{ |
| 8 |
epoll_create1, epoll_ctl, epoll_wait, EpollCreateFlags, EpollEvent, EpollFlags, EpollOp, |
| 9 |
}, |
| 10 |
time::TimeSpec, |
| 11 |
timerfd::{ClockId, Expiration, TimerFd, TimerFlags, TimerSetTimeFlags}, |
| 12 |
}; |
| 13 |
|
| 14 |
pub struct Epoll { |
| 15 |
epoll_fd: RawFd, |
| 16 |
timer_fd: Option<TimerFd>, |
| 17 |
} |
| 18 |
|
| 19 |
const TIMER_DATA: u64 = u64::MAX; |
| 20 |
|
| 21 |
fn make_flags(interest: Interest, mode: Mode) -> EpollFlags { |
| 22 |
let mut flags = EpollFlags::empty(); |
| 23 |
if interest.readable { |
| 24 |
flags |= EpollFlags::EPOLLIN; |
| 25 |
} |
| 26 |
if interest.writable { |
| 27 |
flags |= EpollFlags::EPOLLOUT; |
| 28 |
} |
| 29 |
match mode { |
| 30 |
Mode::Level => { /* This is the default */ } |
| 31 |
Mode::Edge => flags |= EpollFlags::EPOLLET, |
| 32 |
Mode::OneShot => flags |= EpollFlags::EPOLLONESHOT, |
| 33 |
} |
| 34 |
flags |
| 35 |
} |
| 36 |
|
| 37 |
fn flags_to_readiness(flags: EpollFlags) -> Readiness { |
| 38 |
Readiness { |
| 39 |
readable: flags.contains(EpollFlags::EPOLLIN), |
| 40 |
writable: flags.contains(EpollFlags::EPOLLOUT), |
| 41 |
error: flags.contains(EpollFlags::EPOLLERR), |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
impl Epoll { |
| 46 |
pub(crate) fn new(high_precision: bool) -> crate::Result<Epoll> { |
| 47 |
let epoll_fd = epoll_create1(EpollCreateFlags::EPOLL_CLOEXEC)?; |
| 48 |
let mut timer_fd = None; |
| 49 |
if high_precision { |
| 50 |
// Prepare a timerfd for precise time tracking and register it to the event queue |
| 51 |
// This timerfd allows for nanosecond precision in setting the timout up (though in practice |
| 52 |
// we rather get ~10 microsecond precision), while epoll_wait() API only allows millisecond |
| 53 |
// granularity |
| 54 |
let timer = TimerFd::new( |
| 55 |
ClockId::CLOCK_MONOTONIC, |
| 56 |
TimerFlags::TFD_CLOEXEC | TimerFlags::TFD_NONBLOCK, |
| 57 |
)?; |
| 58 |
let mut timer_event = EpollEvent::new(EpollFlags::EPOLLIN, TIMER_DATA); |
| 59 |
epoll_ctl( |
| 60 |
epoll_fd, |
| 61 |
EpollOp::EpollCtlAdd, |
| •62 |
timer.as_fd().as_raw_fd(), |
| 63 |
&mut timer_event, |
| 64 |
)?; |
| 65 |
timer_fd = Some(timer); |
| 66 |
} |
| 67 |
Ok(Epoll { epoll_fd, timer_fd }) |
| 68 |
} |
| 69 |
|
| 70 |
pub(crate) fn poll( |
| 71 |
&mut self, |
| 72 |
timeout: Option<std::time::Duration>, |
| 73 |
) -> crate::Result<Vec<PollEvent>> { |
| 74 |
let mut buffer = [EpollEvent::empty(); 32]; |
| 75 |
if let Some(ref timer) = self.timer_fd { |
| 76 |
if let Some(timeout) = timeout { |
| 77 |
// Set up the precise timer |
| 78 |
timer.set( |
| 79 |
Expiration::OneShot(TimeSpec::from_duration(timeout)), |
| 80 |
TimerSetTimeFlags::empty(), |
| 81 |
)?; |
| 82 |
} |
| 83 |
} |
| 84 |
// add 1 to the millisecond wait, to round up for timer tracking. If the high precision timer is set up |
| 85 |
// it'll fire before that timeout |
| 86 |
let timeout = timeout.map(|d| (d.as_millis() + 1) as isize).unwrap_or(-1); |
| 87 |
let n_ready = epoll_wait(self.epoll_fd, &mut buffer, timeout)?; |
| 88 |
let events = buffer |
| 89 |
.iter() |
| 90 |
.take(n_ready) |
| 91 |
.flat_map(|event| { |
| 92 |
if event.data() == TIMER_DATA { |
| 93 |
// We woke up because the high-precision timer fired, we need to disarm it by reading its |
| 94 |
// contents to ensure it will be ready for next time |
| 95 |
// Timer is created in non-blocking mode, and should have already fired anyway, this |
| 96 |
// cannot possibly block |
| 97 |
let _ = self |
| 98 |
.timer_fd |
| 99 |
.as_ref() |
| 100 |
.expect("Got an event from high-precision timer while it is not set up?!") |
| 101 |
.wait(); |
| 102 |
// don't forward this event to downstream |
| 103 |
None |
| 104 |
} else { |
| 105 |
// In C, the underlying data type is a union including a void |
| 106 |
// pointer; in Rust's FFI bindings, it only exposes the u64. The |
| 107 |
// round-trip conversion is valid however. |
| 108 |
let token_ptr = event.data() as usize as *const Token; |
| 109 |
Some(PollEvent { |
| 110 |
readiness: flags_to_readiness(event.events()), |
| 111 |
// Why this is safe: it points to memory boxed and owned by |
| 112 |
// the parent Poller type. |
| 113 |
token: unsafe { *token_ptr }, |
| 114 |
}) |
| 115 |
} |
| 116 |
}) |
| 117 |
.collect(); |
| 118 |
if let Some(ref timer) = self.timer_fd { |
| 119 |
// in all cases, disarm the timer |
| 120 |
timer.unset()?; |
| 121 |
// clear the timer in case it fired between epoll_wait and now, as timer is in |
| 122 |
// non-blocking mode, this will return Err(WouldBlock) if it had not fired, so |
| 123 |
// we ignore the error |
| 124 |
let _ = timer.wait(); |
| 125 |
} |
| 126 |
Ok(events) |
| 127 |
} |
| 128 |
|
| 129 |
pub fn register( |
| 130 |
&mut self, |
| 131 |
fd: RawFd, |
| 132 |
interest: Interest, |
| 133 |
mode: Mode, |
| 134 |
token: *const Token, |
| 135 |
) -> crate::Result<()> { |
| 136 |
let mut event = EpollEvent::new(make_flags(interest, mode), token as usize as u64); |
| 137 |
epoll_ctl(self.epoll_fd, EpollOp::EpollCtlAdd, fd, &mut event).map_err(Into::into) |
| 138 |
} |
| 139 |
|
| 140 |
pub fn reregister( |
| 141 |
&mut self, |
| 142 |
fd: RawFd, |
| 143 |
interest: Interest, |
| 144 |
mode: Mode, |
| 145 |
token: *const Token, |
| 146 |
) -> crate::Result<()> { |
| 147 |
let mut event = EpollEvent::new(make_flags(interest, mode), token as usize as u64); |
| 148 |
epoll_ctl(self.epoll_fd, EpollOp::EpollCtlMod, fd, &mut event).map_err(Into::into) |
| 149 |
} |
| 150 |
|
| 151 |
pub fn unregister(&mut self, fd: RawFd) -> crate::Result<()> { |
| 152 |
epoll_ctl(self.epoll_fd, EpollOp::EpollCtlDel, fd, None).map_err(Into::into) |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
impl Drop for Epoll { |
| 157 |
fn drop(&mut self) { |
| 158 |
let _ = nix::unistd::close(self.epoll_fd); |
| 159 |
} |
| 160 |
} |