| 1 |
|
| 2 |
|
| 3 |
use crate::os::kqueue::Signal; |
| 4 |
|
| 5 |
use polling::os::kqueue::{PollerKqueueExt, Process, ProcessOps, Signal as PollSignal}; |
| 6 |
use polling::{Event, PollMode, Poller}; |
| 7 |
|
| 8 |
use std::fmt; |
| 9 |
use std::io::Result; |
| 10 |
use std::num::NonZeroI32; |
| 11 |
use std::os::unix::io::{AsRawFd, BorrowedFd, RawFd}; |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#[doc(hidden)] |
| 17 |
pub enum Registration { |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
Fd(RawFd), |
| 26 |
|
| 27 |
|
| 28 |
Signal(Signal), |
| 29 |
|
| 30 |
|
| 31 |
Process(NonZeroI32), |
| 32 |
} |
| 33 |
|
| 34 |
impl fmt::Debug for Registration { |
| 35 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 36 |
match self { |
| 37 |
Self::Fd(raw) => fmt::Debug::fmt(raw, f), |
| 38 |
Self::Signal(signal) => fmt::Debug::fmt(signal, f), |
| 39 |
Self::Process(process) => fmt::Debug::fmt(process, f), |
| 40 |
} |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
impl Registration { |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
pub(crate) unsafe fn new(f: BorrowedFd<'_>) -> Self { |
| 51 |
Self::Fd(f.as_raw_fd()) |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
#[inline] |
| 56 |
pub(crate) fn add(&self, poller: &Poller, token: usize) -> Result<()> { |
| 57 |
match self { |
| 58 |
Self::Fd(raw) => { |
| 59 |
|
| 60 |
unsafe { poller.add(*raw, Event::none(token)) } |
| 61 |
} |
| 62 |
Self::Signal(signal) => { |
| 63 |
poller.add_filter(PollSignal(signal.0), token, PollMode::Oneshot) |
| 64 |
} |
| 65 |
Self::Process(pid) => poller.add_filter( |
| 66 |
unsafe { Process::from_pid(*pid, ProcessOps::Exit) }, |
| 67 |
token, |
| 68 |
PollMode::Oneshot, |
| 69 |
), |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
#[inline] |
| 75 |
pub(crate) fn modify(&self, poller: &Poller, interest: Event) -> Result<()> { |
| 76 |
match self { |
| 77 |
Self::Fd(raw) => { |
| 78 |
|
| 79 |
let fd = unsafe { BorrowedFd::borrow_raw(*raw) }; |
| 80 |
poller.modify(fd, interest) |
| 81 |
} |
| 82 |
Self::Signal(signal) => { |
| 83 |
poller.modify_filter(PollSignal(signal.0), interest.key, PollMode::Oneshot) |
| 84 |
} |
| 85 |
Self::Process(pid) => poller.modify_filter( |
| 86 |
unsafe { Process::from_pid(*pid, ProcessOps::Exit) }, |
| 87 |
interest.key, |
| 88 |
PollMode::Oneshot, |
| 89 |
), |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
#[inline] |
| 95 |
pub(crate) fn delete(&self, poller: &Poller) -> Result<()> { |
| 96 |
match self { |
| 97 |
Self::Fd(raw) => { |
| 98 |
|
| 99 |
let fd = unsafe { BorrowedFd::borrow_raw(*raw) }; |
| 100 |
poller.delete(fd) |
| 101 |
} |
| 102 |
Self::Signal(signal) => poller.delete_filter(PollSignal(signal.0)), |
| 103 |
Self::Process(pid) => { |
| 104 |
poller.delete_filter(unsafe { Process::from_pid(*pid, ProcessOps::Exit) }) |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
} |