| 1 |
|
| 2 |
|
| 3 |
use std::io; |
| 4 |
use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd, RawFd}; |
| 5 |
use std::time::Instant; |
| 6 |
|
| 7 |
use rustix::buffer::spare_capacity; |
| 8 |
use rustix::event::{port, PollFlags, Timespec}; |
| 9 |
use rustix::fd::OwnedFd; |
| 10 |
use rustix::io::{fcntl_getfd, fcntl_setfd, FdFlags}; |
| 11 |
|
| 12 |
use crate::{Event, PollMode}; |
| 13 |
|
| 14 |
|
| 15 |
#[derive(Debug)] |
| 16 |
pub struct Poller { |
| 17 |
|
| 18 |
port_fd: OwnedFd, |
| 19 |
} |
| 20 |
|
| 21 |
impl Poller { |
| 22 |
|
| 23 |
pub fn new() -> io::Result<Poller> { |
| 24 |
let port_fd = port::create()?; |
| 25 |
let flags = fcntl_getfd(&port_fd)?; |
| 26 |
fcntl_setfd(&port_fd, flags | FdFlags::CLOEXEC)?; |
| 27 |
|
| 28 |
#[cfg(feature = "tracing")] |
| 29 |
tracing::trace!( |
| 30 |
port_fd = ?port_fd.as_raw_fd(), |
| 31 |
"new", |
| 32 |
); |
| 33 |
|
| 34 |
Ok(Poller { port_fd }) |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
pub fn supports_level(&self) -> bool { |
| 39 |
false |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
pub fn supports_edge(&self) -> bool { |
| 44 |
false |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
pub unsafe fn add(&self, fd: RawFd, ev: Event, mode: PollMode) -> io::Result<()> { |
| 53 |
|
| 54 |
self.modify(BorrowedFd::borrow_raw(fd), ev, mode) |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
pub fn modify(&self, fd: BorrowedFd<'_>, ev: Event, mode: PollMode) -> io::Result<()> { |
| 59 |
#[cfg(feature = "tracing")] |
| 60 |
let span = tracing::trace_span!( |
| 61 |
"modify", |
| 62 |
port_fd = ?self.port_fd.as_raw_fd(), |
| 63 |
?fd, |
| 64 |
?ev, |
| 65 |
); |
| 66 |
#[cfg(feature = "tracing")] |
| 67 |
let _enter = span.enter(); |
| 68 |
|
| 69 |
let mut flags = PollFlags::empty(); |
| 70 |
if ev.readable { |
| 71 |
flags |= read_flags(); |
| 72 |
} |
| 73 |
if ev.writable { |
| 74 |
flags |= write_flags(); |
| 75 |
} |
| 76 |
|
| 77 |
if mode != PollMode::Oneshot { |
| 78 |
return Err(crate::unsupported_error( |
| 79 |
"this kind of event is not supported with event ports", |
| 80 |
)); |
| 81 |
} |
| 82 |
|
| 83 |
unsafe { |
| 84 |
port::associate_fd(&self.port_fd, fd, flags, ev.key as _)?; |
| 85 |
} |
| 86 |
|
| 87 |
Ok(()) |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
pub fn delete(&self, fd: BorrowedFd<'_>) -> io::Result<()> { |
| 92 |
#[cfg(feature = "tracing")] |
| 93 |
let span = tracing::trace_span!( |
| 94 |
"delete", |
| 95 |
port_fd = ?self.port_fd.as_raw_fd(), |
| 96 |
?fd, |
| 97 |
); |
| 98 |
#[cfg(feature = "tracing")] |
| 99 |
let _enter = span.enter(); |
| 100 |
|
| 101 |
let result = unsafe { port::dissociate_fd(&self.port_fd, fd) }; |
| 102 |
if let Err(e) = result { |
| 103 |
match e { |
| 104 |
rustix::io::Errno::NOENT => return Ok(()), |
| 105 |
_ => return Err(e.into()), |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
Ok(()) |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
pub fn wait_deadline(&self, events: &mut Events, deadline: Option<Instant>) -> io::Result<()> { |
| 114 |
#[cfg(feature = "tracing")] |
| 115 |
let span = tracing::trace_span!( |
| 116 |
"wait", |
| 117 |
port_fd = ?self.port_fd.as_raw_fd(), |
| 118 |
?deadline, |
| 119 |
); |
| 120 |
#[cfg(feature = "tracing")] |
| 121 |
let _enter = span.enter(); |
| 122 |
|
| 123 |
let timeout = deadline.map(|deadline| deadline.saturating_duration_since(Instant::now())); |
| 124 |
|
| 125 |
|
| 126 |
let timeout = match timeout { |
| 127 |
Some(t) => Timespec::try_from(t).ok(), |
| 128 |
None => None, |
| 129 |
}; |
| 130 |
|
| 131 |
|
| 132 |
let res = port::getn( |
| 133 |
&self.port_fd, |
| 134 |
spare_capacity(&mut events.list), |
| 135 |
1, |
| 136 |
timeout.as_ref(), |
| 137 |
); |
| 138 |
#[cfg(feature = "tracing")] |
| 139 |
tracing::trace!( |
| 140 |
port_fd = ?self.port_fd, |
| 141 |
res = ?events.list.len(), |
| 142 |
"new events" |
| 143 |
); |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
if let Err(e) = res { |
| 149 |
match e { |
| 150 |
rustix::io::Errno::TIME => {} |
| 151 |
_ => return Err(e.into()), |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
Ok(()) |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
pub fn notify(&self) -> io::Result<()> { |
| 160 |
const PORT_SOURCE_USER: i32 = 3; |
| 161 |
|
| 162 |
#[cfg(feature = "tracing")] |
| 163 |
let span = tracing::trace_span!( |
| 164 |
"notify", |
| 165 |
port_fd = ?self.port_fd.as_raw_fd(), |
| 166 |
); |
| 167 |
#[cfg(feature = "tracing")] |
| 168 |
let _enter = span.enter(); |
| 169 |
|
| 170 |
|
| 171 |
port::send(&self.port_fd, PORT_SOURCE_USER, crate::NOTIFY_KEY as _)?; |
| 172 |
|
| 173 |
Ok(()) |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
impl AsRawFd for Poller { |
| 178 |
fn as_raw_fd(&self) -> RawFd { |
| 179 |
self.port_fd.as_raw_fd() |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
impl AsFd for Poller { |
| 184 |
fn as_fd(&self) -> BorrowedFd<'_> { |
| 185 |
self.port_fd.as_fd() |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
fn read_flags() -> PollFlags { |
| 191 |
PollFlags::IN | PollFlags::HUP | PollFlags::ERR | PollFlags::PRI |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
fn write_flags() -> PollFlags { |
| 196 |
PollFlags::OUT | PollFlags::HUP | PollFlags::ERR |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
pub struct Events { |
| 201 |
list: Vec<port::Event>, |
| 202 |
} |
| 203 |
|
| 204 |
unsafe impl Send for Events {} |
| 205 |
|
| 206 |
impl Events { |
| 207 |
|
| 208 |
pub fn with_capacity(cap: usize) -> Events { |
| 209 |
Events { |
| 210 |
list: Vec::with_capacity(cap), |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
pub fn iter(&self) -> impl Iterator<Item = Event> + '_ { |
| 216 |
self.list.iter().map(|ev| { |
| 217 |
let flags = PollFlags::from_bits_truncate(ev.events() as _); |
| 218 |
Event { |
| 219 |
key: ev.userdata() as usize, |
| 220 |
readable: flags.intersects(read_flags()), |
| 221 |
writable: flags.intersects(write_flags()), |
| 222 |
extra: EventExtra { flags }, |
| 223 |
} |
| 224 |
}) |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
pub fn clear(&mut self) { |
| 229 |
self.list.clear(); |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
pub fn capacity(&self) -> usize { |
| 234 |
self.list.capacity() |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
#[derive(Debug, Copy, Clone, PartialEq, Eq)] |
| 240 |
pub struct EventExtra { |
| 241 |
|
| 242 |
flags: PollFlags, |
| 243 |
} |
| 244 |
|
| 245 |
impl EventExtra { |
| 246 |
|
| 247 |
#[inline] |
| 248 |
pub const fn empty() -> EventExtra { |
| 249 |
EventExtra { |
| 250 |
flags: PollFlags::empty(), |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
#[inline] |
| 256 |
pub fn set_hup(&mut self, value: bool) { |
| 257 |
self.flags.set(PollFlags::HUP, value); |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
#[inline] |
| 262 |
pub fn set_pri(&mut self, value: bool) { |
| 263 |
self.flags.set(PollFlags::PRI, value); |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
#[inline] |
| 268 |
pub fn is_hup(&self) -> bool { |
| 269 |
self.flags.contains(PollFlags::HUP) |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
#[inline] |
| 274 |
pub fn is_pri(&self) -> bool { |
| 275 |
self.flags.contains(PollFlags::PRI) |
| 276 |
} |
| 277 |
|
| 278 |
#[inline] |
| 279 |
pub fn is_connect_failed(&self) -> Option<bool> { |
| 280 |
Some(self.flags.contains(PollFlags::ERR) && self.flags.contains(PollFlags::HUP)) |
| 281 |
} |
| 282 |
|
| 283 |
#[inline] |
| 284 |
pub fn is_err(&self) -> Option<bool> { |
| 285 |
Some(self.flags.contains(PollFlags::ERR)) |
| 286 |
} |
| 287 |
} |