diff options
Diffstat (limited to 'src/sys/unix/selector/kqueue.rs')
-rw-r--r-- | src/sys/unix/selector/kqueue.rs | 83 |
1 files changed, 36 insertions, 47 deletions
diff --git a/src/sys/unix/selector/kqueue.rs b/src/sys/unix/selector/kqueue.rs index b36a537..1eedec0 100644 --- a/src/sys/unix/selector/kqueue.rs +++ b/src/sys/unix/selector/kqueue.rs @@ -1,6 +1,6 @@ use crate::{Interest, Token}; use log::error; -use std::mem::MaybeUninit; +use std::mem::{self, MaybeUninit}; use std::ops::{Deref, DerefMut}; use std::os::unix::io::{AsRawFd, RawFd}; #[cfg(debug_assertions)] @@ -21,7 +21,7 @@ type Count = libc::size_t; // Type of the `filter` field in the `kevent` structure. #[cfg(any(target_os = "dragonfly", target_os = "freebsd", target_os = "openbsd"))] type Filter = libc::c_short; -#[cfg(any(target_os = "macos", target_os = "ios"))] +#[cfg(any(target_os = "ios", target_os = "macos"))] type Filter = i16; #[cfg(target_os = "netbsd")] type Filter = u32; @@ -29,22 +29,11 @@ type Filter = u32; // Type of the `flags` field in the `kevent` structure. #[cfg(any(target_os = "dragonfly", target_os = "freebsd", target_os = "openbsd"))] type Flags = libc::c_ushort; -#[cfg(any(target_os = "macos", target_os = "ios"))] +#[cfg(any(target_os = "ios", target_os = "macos"))] type Flags = u16; #[cfg(target_os = "netbsd")] type Flags = u32; -// Type of the `data` field in the `kevent` structure. -#[cfg(any( - target_os = "dragonfly", - target_os = "freebsd", - target_os = "ios", - target_os = "macos" -))] -type Data = libc::intptr_t; -#[cfg(any(target_os = "netbsd", target_os = "openbsd"))] -type Data = i64; - // Type of the `udata` field in the `kevent` structure. #[cfg(not(target_os = "netbsd"))] type UData = *mut libc::c_void; @@ -57,9 +46,8 @@ macro_rules! kevent { ident: $id as libc::uintptr_t, filter: $filter as Filter, flags: $flags, - fflags: 0, - data: 0, udata: $data as UData, + ..unsafe { mem::zeroed() } } }; } @@ -75,15 +63,17 @@ pub struct Selector { impl Selector { pub fn new() -> io::Result<Selector> { - syscall!(kqueue()) - .and_then(|kq| syscall!(fcntl(kq, libc::F_SETFD, libc::FD_CLOEXEC)).map(|_| kq)) - .map(|kq| Selector { - #[cfg(debug_assertions)] - id: NEXT_ID.fetch_add(1, Ordering::Relaxed), - kq, - #[cfg(debug_assertions)] - has_waker: AtomicBool::new(false), - }) + let kq = syscall!(kqueue())?; + let selector = Selector { + #[cfg(debug_assertions)] + id: NEXT_ID.fetch_add(1, Ordering::Relaxed), + kq, + #[cfg(debug_assertions)] + has_waker: AtomicBool::new(false), + }; + + syscall!(fcntl(kq, libc::F_SETFD, libc::FD_CLOEXEC))?; + Ok(selector) } pub fn try_clone(&self) -> io::Result<Selector> { @@ -163,7 +153,7 @@ impl Selector { // the array. slice::from_raw_parts_mut(changes[0].as_mut_ptr(), n_changes) }; - kevent_register(self.kq, changes, &[libc::EPIPE as Data]) + kevent_register(self.kq, changes, &[libc::EPIPE as i64]) } pub fn reregister(&self, fd: RawFd, token: Token, interests: Interest) -> io::Result<()> { @@ -195,7 +185,7 @@ impl Selector { kevent_register( self.kq, &mut changes, - &[libc::ENOENT as Data, libc::EPIPE as Data], + &[libc::ENOENT as i64, libc::EPIPE as i64], ) } @@ -211,7 +201,7 @@ impl Selector { // the ENOENT error when it comes up. The ENOENT error informs us that // the filter wasn't there in first place, but we don't really care // about that since our goal is to remove it. - kevent_register(self.kq, &mut changes, &[libc::ENOENT as Data]) + kevent_register(self.kq, &mut changes, &[libc::ENOENT as i64]) } #[cfg(debug_assertions)] @@ -264,7 +254,7 @@ impl Selector { fn kevent_register( kq: RawFd, changes: &mut [libc::kevent], - ignored_errors: &[Data], + ignored_errors: &[i64], ) -> io::Result<()> { syscall!(kevent( kq, @@ -285,15 +275,15 @@ fn kevent_register( Err(err) } }) - .and_then(|()| check_errors(&changes, ignored_errors)) + .and_then(|()| check_errors(changes, ignored_errors)) } /// Check all events for possible errors, it returns the first error found. -fn check_errors(events: &[libc::kevent], ignored_errors: &[Data]) -> io::Result<()> { +fn check_errors(events: &[libc::kevent], ignored_errors: &[i64]) -> io::Result<()> { for event in events { // We can't use references to packed structures (in checking the ignored // errors), so we need copy the data out before use. - let data = event.data; + let data = event.data as _; // Check for the error flag, the actual error will be in the `data` // field. if (event.flags & libc::EV_ERROR != 0) && data != 0 && !ignored_errors.contains(&data) { @@ -415,7 +405,7 @@ pub mod event { target_os = "dragonfly", target_os = "freebsd", target_os = "ios", - target_os = "macos" + target_os = "macos", ))] { event.filter == libc::EVFILT_AIO @@ -424,7 +414,7 @@ pub mod event { target_os = "dragonfly", target_os = "freebsd", target_os = "ios", - target_os = "macos" + target_os = "macos", )))] { false @@ -460,7 +450,7 @@ pub mod event { target_os = "freebsd", target_os = "dragonfly", target_os = "ios", - target_os = "macos" + target_os = "macos", ))] libc::EVFILT_FS, #[cfg(target_os = "freebsd")] @@ -469,7 +459,7 @@ pub mod event { target_os = "freebsd", target_os = "dragonfly", target_os = "ios", - target_os = "macos" + target_os = "macos", ))] libc::EVFILT_USER, #[cfg(target_os = "freebsd")] @@ -526,49 +516,49 @@ pub mod event { target_os = "dragonfly", target_os = "freebsd", target_os = "ios", - target_os = "macos" + target_os = "macos", ))] libc::NOTE_TRIGGER, #[cfg(any( target_os = "dragonfly", target_os = "freebsd", target_os = "ios", - target_os = "macos" + target_os = "macos", ))] libc::NOTE_FFNOP, #[cfg(any( target_os = "dragonfly", target_os = "freebsd", target_os = "ios", - target_os = "macos" + target_os = "macos", ))] libc::NOTE_FFAND, #[cfg(any( target_os = "dragonfly", target_os = "freebsd", target_os = "ios", - target_os = "macos" + target_os = "macos", ))] libc::NOTE_FFOR, #[cfg(any( target_os = "dragonfly", target_os = "freebsd", target_os = "ios", - target_os = "macos" + target_os = "macos", ))] libc::NOTE_FFCOPY, #[cfg(any( target_os = "dragonfly", target_os = "freebsd", target_os = "ios", - target_os = "macos" + target_os = "macos", ))] libc::NOTE_FFCTRLMASK, #[cfg(any( target_os = "dragonfly", target_os = "freebsd", target_os = "ios", - target_os = "macos" + target_os = "macos", ))] libc::NOTE_FFLAGSMASK, libc::NOTE_LOWAT, @@ -603,21 +593,21 @@ pub mod event { target_os = "dragonfly", target_os = "freebsd", target_os = "netbsd", - target_os = "openbsd" + target_os = "openbsd", ))] libc::NOTE_TRACK, #[cfg(any( target_os = "dragonfly", target_os = "freebsd", target_os = "netbsd", - target_os = "openbsd" + target_os = "openbsd", ))] libc::NOTE_TRACKERR, #[cfg(any( target_os = "dragonfly", target_os = "freebsd", target_os = "netbsd", - target_os = "openbsd" + target_os = "openbsd", ))] libc::NOTE_CHILD, #[cfg(any(target_os = "ios", target_os = "macos"))] @@ -645,7 +635,6 @@ pub mod event { #[cfg(any(target_os = "freebsd", target_os = "ios", target_os = "macos"))] libc::NOTE_NSECONDS, #[cfg(any(target_os = "ios", target_os = "macos"))] - #[cfg(any(target_os = "freebsd", target_os = "ios", target_os = "macos"))] libc::NOTE_ABSOLUTE, #[cfg(any(target_os = "ios", target_os = "macos"))] libc::NOTE_LEEWAY, |