use crate::os::kqueue::Signal;
use polling::os::kqueue::{PollerKqueueExt, Process, ProcessOps, Signal as PollSignal};
use polling::{Event, PollMode, Poller};
use std::fmt;
use std::io::Result;
use std::num::NonZeroI32;
use std::os::unix::io::{AsRawFd, BorrowedFd, RawFd};
#[doc(hidden)]
pub enum Registration {
Fd(RawFd),
Signal(Signal),
Process(NonZeroI32),
}
impl fmt::Debug for Registration {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Fd(raw) => fmt::Debug::fmt(raw, f),
Self::Signal(signal) => fmt::Debug::fmt(signal, f),
Self::Process(process) => fmt::Debug::fmt(process, f),
}
}
}
impl Registration {
pub(crate) unsafe fn new(f: BorrowedFd<'_>) -> Self {
Self::Fd(f.as_raw_fd())
}
#[inline]
pub(crate) fn add(&self, poller: &Poller, token: usize) -> Result<()> {
match self {
Self::Fd(raw) => {
unsafe { poller.add(*raw, Event::none(token)) }
}
Self::Signal(signal) => {
poller.add_filter(PollSignal(signal.0), token, PollMode::Oneshot)
}
Self::Process(pid) => poller.add_filter(
unsafe { Process::from_pid(*pid, ProcessOps::Exit) },
token,
PollMode::Oneshot,
),
}
}
#[inline]
pub(crate) fn modify(&self, poller: &Poller, interest: Event) -> Result<()> {
match self {
Self::Fd(raw) => {
let fd = unsafe { BorrowedFd::borrow_raw(*raw) };
poller.modify(fd, interest)
}
Self::Signal(signal) => {
poller.modify_filter(PollSignal(signal.0), interest.key, PollMode::Oneshot)
}
Self::Process(pid) => poller.modify_filter(
unsafe { Process::from_pid(*pid, ProcessOps::Exit) },
interest.key,
PollMode::Oneshot,
),
}
}
#[inline]
pub(crate) fn delete(&self, poller: &Poller) -> Result<()> {
match self {
Self::Fd(raw) => {
let fd = unsafe { BorrowedFd::borrow_raw(*raw) };
poller.delete(fd)
}
Self::Signal(signal) => poller.delete_filter(PollSignal(signal.0)),
Self::Process(pid) => {
poller.delete_filter(unsafe { Process::from_pid(*pid, ProcessOps::Exit) })
}
}
}
}