Debian Codegraph @

src/reactor/kqueue.rs

raw symbols SCIP
1 // SPDX-License-Identifier: MIT OR Apache-2.0
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 /// The raw registration into the reactor.
14 ///
15 /// This needs to be public, since it is technically exposed through the `QueueableSealed` trait.
16 #[doc(hidden)]
17 pub enum Registration {
18 /// Raw file descriptor for readability/writability.
19 ///
20 ///
21 /// # Invariant
22 ///
23 /// This describes a valid file descriptor that has not been `close`d. It will not be
24 /// closed while this object is alive.
25 Fd(RawFd),
26
27 /// Raw signal number for signal delivery.
28 Signal(Signal),
29
30 /// Pid for process termination.
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 /// Add this file descriptor into the reactor.
46 ///
47 /// # Safety
48 ///
49 /// The provided file descriptor must be valid and not be closed while this object is alive.
50 pub(crate) unsafe fn new(f: BorrowedFd<'_>) -> Self {
51 Self::Fd(f.as_raw_fd())
52 }
53
54 /// Registers the object into the reactor.
55 #[inline]
56 pub(crate) fn add(&self, poller: &Poller, token: usize) -> Result<()> {
57 match self {
58 Self::Fd(raw) => {
59 // SAFETY: This object's existence validates the invariants of Poller::add
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 /// Re-registers the object into the reactor.
74 #[inline]
75 pub(crate) fn modify(&self, poller: &Poller, interest: Event) -> Result<()> {
76 match self {
77 Self::Fd(raw) => {
78 // SAFETY: self.raw is a valid file descriptor
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 /// Deregisters the object from the reactor.
94 #[inline]
95 pub(crate) fn delete(&self, poller: &Poller) -> Result<()> {
96 match self {
97 Self::Fd(raw) => {
98 // SAFETY: self.raw is a valid file descriptor
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 }