Repository navigation
Expand file tree
/
Copy pathevl-kqueue.c
More file actions
347 lines (285 loc) · 7.42 KB
/
Copy pathevl-kqueue.c
File metadata and controls
347 lines (285 loc) · 7.42 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/* $OpenBSD$ */
/*
* Copyright (c) 2017 David Gwynne <dlg@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/event.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include "evl-internal.h"
static void *evl_kq_init(void);
static void evl_kq_destroy(void *);
static int evl_kq_dispatch(struct evl_base *,
const struct timespec *);
static int evl_kq_io_create(struct evl_io *);
static void evl_kq_io_add(struct evl_io *);
static void evl_kq_io_del(struct evl_io *);
static void evl_kq_io_destroy(struct evl_io *);
const struct evl_ops evl_ops_kq = {
evl_kq_init,
evl_kq_destroy,
evl_kq_dispatch,
evl_kq_io_create,
evl_kq_io_add,
evl_kq_io_del,
evl_kq_io_destroy,
};
struct evl_kq {
int evlkq_fd;
struct kevent *evlkq_kevents;
unsigned int evlkq_keventslen;
unsigned int evlkq_nevents;
unsigned int evlkq_nchanges;
};
static void *
evl_kq_init(void)
{
struct evl_kq *evlkq;
int fd;
evlkq = malloc(sizeof(*evlkq));
if (evlkq == NULL)
return (NULL);
fd = kqueue();
if (fd == -1) {
free(evlkq);
return (NULL);
}
evlkq->evlkq_fd = fd;
evlkq->evlkq_kevents = NULL;
evlkq->evlkq_keventslen = 0;
evlkq->evlkq_nevents = 0;
evlkq->evlkq_nchanges = 0;
return (evlkq);
}
static void
evl_kq_destroy(void *backend)
{
struct evl_kq *evlkq = backend;
free(evlkq->evlkq_kevents);
close(evlkq->evlkq_fd);
free(evlkq);
}
static void
evl_kq_io_fire(const struct kevent *kev, int events)
{
struct evl_io *evlio;
struct evl_work *evl;
if (ISSET(kev->flags, EV_ERROR)) {
switch (kev->data) {
case 0:
/* handle EV_RECEIPT */
evlio = kev->udata;
evlio->evl_io_idx = ~0; /* invalidate changes */
return;
case EBADF:
/* this can happen after a close */
return;
default:
/* oh well */
return;
}
}
evlio = kev->udata;
evlio->evl_io_idx = ~0; /* invalidate changes */
evl = &evlio->evl_io_work;
if (ISSET(evl->evl_event, EVL_RW) != EVL_RW)
SET(events, ISSET(evl->evl_event, EVL_PERSIST));
else
SET(events, EVL_PERSIST);
evl_io_fire(evlio, events);
}
static int
evl_kq_dispatch(struct evl_base *evlb, const struct timespec *ts)
{
struct evl_kq *evlkq = evl_backend(evlb);
struct kevent *kevs = evlkq->evlkq_kevents, *kev;
int nevents;
int i;
nevents = kevent(evlkq->evlkq_fd, kevs, evlkq->evlkq_nchanges,
kevs, evlkq->evlkq_keventslen, ts);
if (nevents == -1) {
if (errno == EINTR)
return (0);
return (-1);
}
evlkq->evlkq_nchanges = 0;
for (i = 0; i < nevents; i++) {
kev = &kevs[i];
switch (kev->filter) {
case EVFILT_READ:
evl_kq_io_fire(kev, EVL_READ);
break;
case EVFILT_WRITE:
evl_kq_io_fire(kev, EVL_WRITE);
break;
}
}
return (0);
}
static struct kevent *
evl_kq_next_change(struct evl_kq *evlkq, unsigned int n)
{
struct kevent *kev;
unsigned int len = evlkq->evlkq_nevents + n + 1;
if (len >= evlkq->evlkq_keventslen) {
kev = reallocarray(evlkq->evlkq_kevents, len, sizeof(*kev));
if (kev == NULL)
return (NULL);
/* commit */
evlkq->evlkq_kevents = kev;
evlkq->evlkq_keventslen = len;
} else
kev = evlkq->evlkq_kevents;
return (kev + evlkq->evlkq_nchanges + n);
}
static int
evl_kq_io_create(struct evl_io *evlio)
{
struct evl_base *evlb = evl_io_base(evlio);
struct evl_kq *evlkq = evl_backend(evlb);
struct evl_work *evl = &evlio->evl_io_work;
struct kevent *kev;
unsigned int n;
int flags = EV_ADD | EV_DISABLE | EV_RECEIPT;
int fd;
fd = evl->evl_ident;
n = 0;
if (ISSET(evl->evl_event, EVL_READ)) {
kev = evl_kq_next_change(evlkq, n++);
if (kev == NULL)
return (-1);
EV_SET(kev, fd, EVFILT_READ, flags, NOTE_EOF, 0, evlio);
}
if (ISSET(evl->evl_event, EVL_WRITE)) {
kev = evl_kq_next_change(evlkq, n++);
if (kev == NULL)
return (-1);
EV_SET(kev, fd, EVFILT_WRITE, flags, 0, 0, evlio);
}
/* commit */
evlio->evl_io_idx = evlkq->evlkq_nchanges;
evlkq->evlkq_nchanges += n;
evlkq->evlkq_nevents += n;
return (0);
}
static void
evl_kq_io(struct evl_io *evlio, int flags)
{
struct evl_base *evlb = evl_io_base(evlio);
struct evl_kq *evlkq = evl_backend(evlb);
struct evl_work *evl = &evlio->evl_io_work;
struct kevent *kev;
unsigned int idx;
int fd;
idx = evlio->evl_io_idx;
if (idx == ~0U) {
/* new changes */
idx = evlkq->evlkq_nchanges;
fd = evl->evl_ident;
if (ISSET(evl->evl_event, EVL_READ)) {
kev = evlkq->evlkq_kevents + idx++;
EV_SET(kev, fd, EVFILT_READ, flags, NOTE_EOF, 0, evlio);
}
if (ISSET(evl->evl_event, EVL_WRITE)) {
kev = evlkq->evlkq_kevents + idx++;
EV_SET(kev, fd, EVFILT_WRITE, flags, 0, 0, evlio);
}
evlkq->evlkq_nchanges = idx;
} else {
/* tweak existing changes */
if (ISSET(evl->evl_event, EVL_READ)) {
kev = evlkq->evlkq_kevents + idx++;
CLR(kev->flags, ~EV_ADD);
SET(kev->flags, flags);
}
if (ISSET(evl->evl_event, EVL_WRITE)) {
kev = evlkq->evlkq_kevents + idx++;
CLR(kev->flags, ~EV_ADD);
SET(kev->flags, flags);
}
}
}
static void
evl_kq_io_add(struct evl_io *evlio)
{
struct evl_work *evl = &evlio->evl_io_work;
int flags = EV_ENABLE | EV_RECEIPT;
if (ISSET(evl->evl_event, EVL_RW) != EVL_RW &&
!ISSET(evl->evl_event, EVL_PERSIST))
SET(flags, EV_DISPATCH);
evl_kq_io(evlio, flags);
}
static void
evl_kq_io_del(struct evl_io *evlio)
{
int flags = EV_DISABLE | EV_RECEIPT;
evl_kq_io(evlio, flags);
}
static inline unsigned int
evl_kq_nevents(const struct evl_io *evlio)
{
return (ISSET(evlio->evl_io_work.evl_event, EVL_RW) == EVL_RW ? 2 : 1);
}
static void
evl_kq_io_destroy(struct evl_io *evlio)
{
struct evl_base *evlb = evl_io_base(evlio);
struct evl_kq *evlkq = evl_backend(evlb);
struct evl_work *evl = &evlio->evl_io_work;
struct kevent *kev, *nkev;
unsigned int nevents, n, idx;
int fd;
idx = evlio->evl_io_idx;
if (idx == ~0U) { /* new changes */
idx = evlkq->evlkq_nchanges;
fd = evl->evl_ident;
nevents = 0;
if (ISSET(evl->evl_event, EVL_READ)) {
kev = evlkq->evlkq_kevents + idx++;
EV_SET(kev, fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);
nevents++;
}
if (ISSET(evl->evl_event, EVL_WRITE)) {
kev = evlkq->evlkq_kevents + idx++;
EV_SET(kev, fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
nevents++;
}
evlkq->evlkq_nchanges = idx;
} else {
/* event is already queued for a change */
kev = evlkq->evlkq_kevents + idx;
nevents = evl_kq_nevents(evlio);
if (ISSET(kev->flags, EV_ADD)) {
/* event was just added, so remove it from changes */
evlkq->evlkq_nchanges -= nevents;
nkev = kev + nevents;
while (idx < evlkq->evlkq_nchanges) {
evlio = nkev->udata;
evlio->evl_io_idx = idx;
n = evl_kq_nevents(evlio);
idx += n;
while (n--)
*kev++ = *nkev++;
}
} else {
/* update in place */
for (n = 0; n < nevents; n++)
kev[n].flags = EV_DELETE;
}
}
evlkq->evlkq_nevents -= nevents;
}