| 1 |
use std::{io, os::unix::io::RawFd}; |
| 2 |
|
| 3 |
use nix::{ |
| 4 |
libc::{c_long, time_t, timespec}, |
| 5 |
sys::event::{kevent_ts, kqueue, EventFilter, EventFlag, FilterFlag, KEvent}, |
| 6 |
}; |
| 7 |
|
| 8 |
use super::{Interest, Mode, PollEvent, Readiness, Token}; |
| 9 |
|
| 10 |
pub struct Kqueue { |
| 11 |
kq: RawFd, |
| 12 |
} |
| 13 |
|
| 14 |
fn mode_to_flag(mode: Mode) -> EventFlag { |
| 15 |
match mode { |
| 16 |
Mode::Level => EventFlag::empty(), |
| 17 |
Mode::OneShot => EventFlag::EV_DISPATCH, |
| 18 |
Mode::Edge => EventFlag::EV_CLEAR, |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
impl Kqueue { |
| 23 |
|
| 24 |
pub(crate) fn new(_high_precision: bool) -> crate::Result<Kqueue> { |
| 25 |
let kq = kqueue()?; |
| 26 |
Ok(Kqueue { kq }) |
| 27 |
} |
| 28 |
|
| 29 |
pub(crate) fn poll( |
| 30 |
&mut self, |
| 31 |
timeout: Option<std::time::Duration>, |
| 32 |
) -> crate::Result<Vec<PollEvent>> { |
| 33 |
let mut buffer = [KEvent::new( |
| 34 |
0, |
| 35 |
EventFilter::EVFILT_READ, |
| 36 |
EventFlag::empty(), |
| 37 |
FilterFlag::empty(), |
| 38 |
0, |
| 39 |
0, |
| 40 |
); 32]; |
| 41 |
|
| 42 |
let nevents = kevent_ts( |
| 43 |
self.kq, |
| 44 |
&[], |
| 45 |
&mut buffer, |
| 46 |
timeout.map(|d| timespec { |
| 47 |
tv_sec: d.as_secs() as time_t, |
| 48 |
tv_nsec: d.subsec_nanos() as c_long, |
| 49 |
}), |
| 50 |
)?; |
| 51 |
|
| 52 |
let ret = buffer |
| 53 |
.iter() |
| 54 |
.take(nevents) |
| 55 |
.map(|event| { |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
let token_ptr = event.udata() as usize as *const Token; |
| 60 |
PollEvent { |
| 61 |
readiness: Readiness { |
| 62 |
readable: event.filter() == Ok(EventFilter::EVFILT_READ), |
| 63 |
writable: event.filter() == Ok(EventFilter::EVFILT_WRITE), |
| 64 |
error: event.flags().contains(EventFlag::EV_ERROR) && event.data() != 0, |
| 65 |
}, |
| 66 |
|
| 67 |
|
| 68 |
token: unsafe { *token_ptr }, |
| 69 |
} |
| 70 |
}) |
| 71 |
.collect(); |
| 72 |
Ok(ret) |
| 73 |
} |
| 74 |
|
| 75 |
pub fn register( |
| 76 |
&mut self, |
| 77 |
fd: RawFd, |
| 78 |
interest: Interest, |
| 79 |
mode: Mode, |
| 80 |
token: *const Token, |
| 81 |
) -> crate::Result<()> { |
| 82 |
self.reregister(fd, interest, mode, token) |
| 83 |
} |
| 84 |
|
| 85 |
pub fn reregister( |
| 86 |
&mut self, |
| 87 |
fd: RawFd, |
| 88 |
interest: Interest, |
| 89 |
mode: Mode, |
| 90 |
token: *const Token, |
| 91 |
) -> crate::Result<()> { |
| 92 |
let write_flags = if interest.writable { |
| 93 |
EventFlag::EV_ADD | EventFlag::EV_RECEIPT | mode_to_flag(mode) |
| 94 |
} else { |
| 95 |
EventFlag::EV_DELETE |
| 96 |
}; |
| 97 |
let read_flags = if interest.readable { |
| 98 |
EventFlag::EV_ADD | EventFlag::EV_RECEIPT | mode_to_flag(mode) |
| 99 |
} else { |
| 100 |
EventFlag::EV_DELETE |
| 101 |
}; |
| 102 |
|
| 103 |
let changes = [ |
| 104 |
KEvent::new( |
| 105 |
fd as usize, |
| 106 |
EventFilter::EVFILT_WRITE, |
| 107 |
write_flags, |
| 108 |
FilterFlag::empty(), |
| 109 |
0, |
| 110 |
token as usize as isize, |
| 111 |
), |
| 112 |
KEvent::new( |
| 113 |
fd as usize, |
| 114 |
EventFilter::EVFILT_READ, |
| 115 |
read_flags, |
| 116 |
FilterFlag::empty(), |
| 117 |
0, |
| 118 |
token as usize as isize, |
| 119 |
), |
| 120 |
]; |
| 121 |
|
| 122 |
let mut out = [ |
| 123 |
KEvent::new( |
| 124 |
0, |
| 125 |
EventFilter::EVFILT_WRITE, |
| 126 |
EventFlag::empty(), |
| 127 |
FilterFlag::empty(), |
| 128 |
0, |
| 129 |
0, |
| 130 |
), |
| 131 |
KEvent::new( |
| 132 |
0, |
| 133 |
EventFilter::EVFILT_READ, |
| 134 |
EventFlag::empty(), |
| 135 |
FilterFlag::empty(), |
| 136 |
0, |
| 137 |
0, |
| 138 |
), |
| 139 |
]; |
| 140 |
|
| 141 |
kevent_ts(self.kq, &changes, &mut out, None)?; |
| 142 |
|
| 143 |
for o in &out { |
| 144 |
if o.flags().contains(EventFlag::EV_ERROR) && o.data() != 0 { |
| 145 |
let e = io::Error::from_raw_os_error(o.data() as i32); |
| 146 |
|
| 147 |
if e.kind() != io::ErrorKind::NotFound { |
| 148 |
return Err(e.into()); |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
Ok(()) |
| 153 |
} |
| 154 |
|
| 155 |
pub fn unregister(&mut self, fd: RawFd) -> crate::Result<()> { |
| 156 |
let changes = [ |
| 157 |
KEvent::new( |
| 158 |
fd as usize, |
| 159 |
EventFilter::EVFILT_WRITE, |
| 160 |
EventFlag::EV_DELETE, |
| 161 |
FilterFlag::empty(), |
| 162 |
0, |
| 163 |
0, |
| 164 |
), |
| 165 |
KEvent::new( |
| 166 |
fd as usize, |
| 167 |
EventFilter::EVFILT_READ, |
| 168 |
EventFlag::EV_DELETE, |
| 169 |
FilterFlag::empty(), |
| 170 |
0, |
| 171 |
0, |
| 172 |
), |
| 173 |
]; |
| 174 |
|
| 175 |
let mut out = [ |
| 176 |
KEvent::new( |
| 177 |
0, |
| 178 |
EventFilter::EVFILT_WRITE, |
| 179 |
EventFlag::empty(), |
| 180 |
FilterFlag::empty(), |
| 181 |
0, |
| 182 |
0, |
| 183 |
), |
| 184 |
KEvent::new( |
| 185 |
0, |
| 186 |
EventFilter::EVFILT_READ, |
| 187 |
EventFlag::empty(), |
| 188 |
FilterFlag::empty(), |
| 189 |
0, |
| 190 |
0, |
| 191 |
), |
| 192 |
]; |
| 193 |
|
| 194 |
kevent_ts(self.kq, &changes, &mut out, None)?; |
| 195 |
|
| 196 |
|
| 197 |
let mut notfound = 0; |
| 198 |
|
| 199 |
for o in &out { |
| 200 |
if o.flags().contains(EventFlag::EV_ERROR) && o.data() != 0 { |
| 201 |
let e = io::Error::from_raw_os_error(o.data() as i32); |
| 202 |
|
| 203 |
if e.kind() != io::ErrorKind::NotFound { |
| 204 |
return Err(e.into()); |
| 205 |
} else { |
| 206 |
notfound += 1; |
| 207 |
} |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
if notfound == 2 { |
| 212 |
return Err(std::io::Error::from(io::ErrorKind::NotFound).into()); |
| 213 |
} |
| 214 |
|
| 215 |
Ok(()) |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
impl Drop for Kqueue { |
| 220 |
fn drop(&mut self) { |
| 221 |
let _ = nix::unistd::close(self.kq); |
| 222 |
} |
| 223 |
} |