1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//! # Event Descriptor
//! Describes events for creating `kevent` calls
use crate::constants::EV_UDATA_SPECIFIC;
/// A `kevent` description
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) struct EventDesc {
pub(crate) filter: i16,
pub(crate) flags: u16,
pub(crate) fflags: u32,
}
impl EventDesc {
/// Returns the `kevent` flags
/// required to make a new timer
pub(crate) fn new_timer() -> Self {
Self {
filter: libc::EVFILT_TIMER,
flags: libc::EV_ADD | libc::EV_ONESHOT,
fflags: libc::NOTE_NSECONDS | libc::NOTE_CRITICAL,
}
}
/// Returns the `kevent` flags required to take a
/// timer back off a queue
pub(crate) fn new_timer_delete() -> Self {
Self {
filter: libc::EVFILT_TIMER,
flags: libc::EV_DELETE,
fflags: 0,
}
}
/// Returns the `kevent` flags required to make
/// a timer that keeps firing
pub(crate) fn new_interval() -> Self {
Self {
filter: libc::EVFILT_TIMER,
flags: libc::EV_ADD | libc::EV_ENABLE,
fflags: libc::NOTE_NSECONDS,
}
}
/// Returns the `kevent` flags required to
/// raise a user triggered event
///
/// Registers the event and fires it in one syscall
pub(crate) fn new_user_trigger() -> Self {
Self {
filter: libc::EVFILT_USER,
flags: libc::EV_ADD | libc::EV_ONESHOT,
fflags: libc::NOTE_TRIGGER,
}
}
/// Returns the `kevent` flags required to wait
/// for a child process to exit, registered at its pid
pub(crate) fn new_proc_exit() -> Self {
Self {
filter: libc::EVFILT_PROC,
flags: libc::EV_ADD | libc::EV_ONESHOT,
fflags: libc::NOTE_EXIT,
}
}
/// Returns the `kevent` flags required to take a
/// process watch back off a queue
///
/// Also removes an exit already queued, which a later task on
/// the same thread could otherwise take for its own child
pub(crate) fn new_proc_delete() -> Self {
Self {
filter: libc::EVFILT_PROC,
flags: libc::EV_DELETE,
fflags: 0,
}
}
/// Returns the `kevent` flags required to watch a
/// descriptor for something to read
///
/// Level triggered, so the descriptor stays ready while it has
/// anything on it and one blocking `read` per wake is safe
pub(crate) fn new_read() -> Self {
Self {
filter: libc::EVFILT_READ,
flags: libc::EV_ADD,
fflags: 0,
}
}
/// Returns the `kevent` flags required to take a
/// read watch back off a queue
///
/// A descriptor at its end stays readable forever, so a watch
/// left on one turns a wait into a spin
pub(crate) fn new_read_delete() -> Self {
Self {
filter: libc::EVFILT_READ,
flags: libc::EV_DELETE,
fflags: 0,
}
}
/// Returns the `kevent` flags required to watch a
/// descriptor for room to write
///
/// Level triggered, and the room may be a single byte, so the
/// descriptor must be `O_NONBLOCK`
pub(crate) fn new_write() -> Self {
Self {
filter: libc::EVFILT_WRITE,
flags: libc::EV_ADD,
fflags: 0,
}
}
/// Returns the `kevent` flags required to take a write
/// watch back off a queue
///
/// Comes off before the descriptor is closed. Closing frees the
/// number for reuse while the kernel is still taking the watch
/// down
pub(crate) fn new_write_delete() -> Self {
Self {
filter: libc::EVFILT_WRITE,
flags: libc::EV_DELETE,
fflags: 0,
}
}
/// Returns the `kevent` flags required to wake a parked task
/// once, when its ident is ready for `filter`
///
/// Unique to the task, so another task parked on the same
/// socket keeps its own watch. `notes` is what a filter that
/// fires on nothing by itself is told to look for, and zero
/// for the rest
pub(crate) fn new_park(filter: i16, notes: u32) -> Self {
Self {
filter,
flags: libc::EV_ADD | libc::EV_ONESHOT | EV_UDATA_SPECIFIC,
fflags: notes,
}
}
/// Returns the `kevent` flags required to take one task's park
/// back off, leaving any other task's alone
pub(crate) fn new_park_delete(filter: i16) -> Self {
Self {
filter,
flags: libc::EV_DELETE | EV_UDATA_SPECIFIC,
fflags: 0,
}
}
/// Returns the `kevent` flags required to wait once for an
/// ident to be ready for `filter`, on a thread's own queue
///
/// `notes` is what a filter that fires on nothing by itself is
/// told to look for, and zero for the rest
pub(crate) fn new_ready(filter: i16, notes: u32) -> Self {
Self {
filter,
flags: libc::EV_ADD | libc::EV_ONESHOT,
fflags: notes,
}
}
/// Returns the `kevent` flags required to take a readiness
/// wait back off a thread's own queue
pub(crate) fn new_ready_delete(filter: i16) -> Self {
Self {
filter,
flags: libc::EV_DELETE,
fflags: 0,
}
}
}