-
Notifications
You must be signed in to change notification settings - Fork 157
/
kqueue.c.inc
307 lines (287 loc) · 10.1 KB
/
kqueue.c.inc
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
/*
Copyright (c) 2016 Martin Sustrik
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom
the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
*/
#include <errno.h>
#include <limits.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/event.h>
#include <sys/time.h>
#include <unistd.h>
#include "cr.h"
#include "list.h"
#include "pollset.h"
#include "utils.h"
#include "ctx.h"
#define DILL_ENDLIST 0xffffffff
#define DILL_CHNGSSIZE 128
#define DILL_EVSSIZE 128
#define FDW_IN 1
#define FDW_OUT 2
struct dill_fdinfo {
struct dill_fdclause *in;
struct dill_fdclause *out;
uint16_t currevs;
uint16_t firing;
/* 1-based index, 0 stands for "not part of the list", DILL_ENDLIST
stands for "no more elements in the list. */
uint32_t next;
/* 1 if the file descriptor is cached. 0 otherwise. */
unsigned int cached : 1;
};
int dill_ctx_pollset_init(struct dill_ctx_pollset *ctx) {
int err;
/* Allocate one info per fd. */
ctx->nfdinfos = dill_maxfds();
ctx->fdinfos = calloc(ctx->nfdinfos, sizeof(struct dill_fdinfo));
if(dill_slow(!ctx->fdinfos)) {err = ENOMEM; goto error1;}
/* Changelist is empty. */
ctx->changelist = DILL_ENDLIST;
/* Create kernel-side pollset. */
ctx->kfd = kqueue();
if(dill_slow(ctx->kfd < 0)) {err = errno; goto error2;}
return 0;
error2:
free(ctx->fdinfos);
ctx->fdinfos = NULL;
error1:
errno = err;
return -1;
}
void dill_ctx_pollset_term(struct dill_ctx_pollset *ctx) {
/* Kqueue documentation says that a kqueue descriptor won't
survive a fork. However, implementations seem to disagree.
On FreeBSD the following function succeeds. On OSX it returns
EACCESS. Therefore we ignore the return value. */
close(ctx->kfd);
free(ctx->fdinfos);
}
static void dill_fdcancelin(struct dill_clause *cl) {
struct dill_fdinfo *fdinfo =
dill_cont(cl, struct dill_fdclause, cl)->fdinfo;
fdinfo->in = NULL;
if(!fdinfo->next) {
struct dill_ctx_pollset *ctx = &dill_getctx->pollset;
fdinfo->next = ctx->changelist;
ctx->changelist = fdinfo - ctx->fdinfos + 1;
}
}
static void dill_fdcancelout(struct dill_clause *cl) {
struct dill_fdinfo *fdinfo =
dill_cont(cl, struct dill_fdclause, cl)->fdinfo;
fdinfo->out = NULL;
if(!fdinfo->next) {
struct dill_ctx_pollset *ctx = &dill_getctx->pollset;
fdinfo->next = ctx->changelist;
ctx->changelist = fdinfo - ctx->fdinfos + 1;
}
}
int dill_pollset_in(struct dill_fdclause *fdcl, int id, int fd) {
struct dill_ctx_pollset *ctx = &dill_getctx->pollset;
if(dill_slow(fd < 0 || fd >= ctx->nfdinfos)) {errno = EBADF; return -1;}
struct dill_fdinfo *fdi = &ctx->fdinfos[fd];
/* If not yet cached, check whether fd exists and if so add it
to pollset. */
if(dill_slow(!fdi->cached)) {
struct kevent ev;
EV_SET(&ev, fd, EVFILT_READ, EV_ADD, 0, 0, 0);
int rc = kevent(ctx->kfd, &ev, 1, NULL, 0, NULL);
if(dill_slow(rc < 0 && errno == EBADF)) return -1;
dill_assert(rc >= 0);
fdi->in = NULL;
fdi->out = NULL;
fdi->currevs = FDW_IN;
fdi->firing = 0;
fdi->next = 0;
fdi->cached = 1;
}
if(dill_slow(fdi->in)) {errno = EBUSY; return -1;}
/* If fd is not yet in the pollset, add it there. */
else if(!fdi->next) {
fdi->next = ctx->changelist;
ctx->changelist = fd + 1;
}
fdcl->fdinfo = fdi;
fdi->in = fdcl;
dill_waitfor(&fdcl->cl, id, dill_fdcancelin);
return 0;
}
int dill_pollset_out(struct dill_fdclause *fdcl, int id, int fd) {
struct dill_ctx_pollset *ctx = &dill_getctx->pollset;
if(dill_slow(fd < 0 || fd >= ctx->nfdinfos)) {errno = EBADF; return -1;}
struct dill_fdinfo *fdi = &ctx->fdinfos[fd];
/* If not yet cached, check whether the fd exists and if it does,
add it to the pollset. */
if(dill_slow(!fdi->cached)) {
struct kevent ev;
EV_SET(&ev, fd, EVFILT_WRITE, EV_ADD, 0, 0, 0);
int rc = kevent(ctx->kfd, &ev, 1, NULL, 0, NULL);
if(dill_slow(rc < 0 && errno == EBADF)) return -1;
dill_assert(rc >= 0);
fdi->in = NULL;
fdi->out = NULL;
fdi->currevs = FDW_OUT;
fdi->firing = 0;
fdi->next = 0;
fdi->cached = 1;
}
if(dill_slow(fdi->out)) {errno = EBUSY; return -1;}
/* If the fd is not yet in the pollset, add it there. */
else if(!fdi->next) {
fdi->next = ctx->changelist;
ctx->changelist = fd + 1;
}
fdcl->fdinfo = fdi;
fdi->out = fdcl;
dill_waitfor(&fdcl->cl, id, dill_fdcancelout);
return 0;
}
int dill_pollset_clean(int fd) {
struct dill_ctx_pollset *ctx = &dill_getctx->pollset;
struct dill_fdinfo *fdi = &ctx->fdinfos[fd];
if(!fdi->cached) return 0;
/* We cannot clean an fd that someone is waiting for. */
if(dill_slow(fdi->in || fdi->out)) {errno = EBUSY; return -1;}
/* Remove the file descriptor from the pollset if it is still there. */
int nevs = 0;
struct kevent evs[2];
if(fdi->currevs & FDW_IN) {
EV_SET(&evs[nevs], fd, EVFILT_READ, EV_DELETE, 0, 0, 0);
++nevs;
}
if(fdi->currevs & FDW_OUT) {
EV_SET(&evs[nevs], fd, EVFILT_WRITE, EV_DELETE, 0, 0, 0);
++nevs;
}
if(nevs) {
int rc = kevent(ctx->kfd, evs, nevs, NULL, 0, NULL);
dill_assert(rc != -1);
}
fdi->currevs = 0;
/* If needed, remove the fd from the changelist. */
if(fdi->next) {
uint32_t *pidx = &ctx->changelist;
while(1) {
dill_assert(*pidx != 0 && *pidx != DILL_ENDLIST);
if(*pidx - 1 == fd) break;
pidx = &ctx->fdinfos[*pidx - 1].next;
}
*pidx = fdi->next;
fdi->next = 0;
}
/* Mark the fd as not used. */
fdi->cached = 0;
return 0;
}
int dill_pollset_poll(int timeout) {
struct dill_ctx_pollset *ctx = &dill_getctx->pollset;
/* Apply any changes to the pollset. */
struct kevent chngs[DILL_CHNGSSIZE];
int nchngs = 0;
while(ctx->changelist != DILL_ENDLIST) {
/* Flush the changes to the pollset even if there is one empty entry
left in the changeset. That way, we make sure that both in & out
associated with the next file descriptor can be filled in if we
choose not to flush the changes yet. */
if(nchngs >= DILL_CHNGSSIZE - 1) {
int rc = kevent(ctx->kfd, chngs, nchngs, NULL, 0, NULL);
dill_assert(rc != -1);
nchngs = 0;
}
int fd = ctx->changelist - 1;
struct dill_fdinfo *fdi = &ctx->fdinfos[fd];
if(fdi->in) {
if(!(fdi->currevs & FDW_IN)) {
EV_SET(&chngs[nchngs], fd, EVFILT_READ, EV_ADD, 0, 0, 0);
fdi->currevs |= FDW_IN;
++nchngs;
}
}
else {
if(fdi->currevs & FDW_IN) {
EV_SET(&chngs[nchngs], fd, EVFILT_READ, EV_DELETE, 0, 0, 0);
fdi->currevs &= ~FDW_IN;
++nchngs;
}
}
if(fdi->out) {
if(!(fdi->currevs & FDW_OUT)) {
EV_SET(&chngs[nchngs], fd, EVFILT_WRITE, EV_ADD, 0, 0, 0);
fdi->currevs |= FDW_OUT;
++nchngs;
}
}
else {
if(fdi->currevs & FDW_OUT) {
EV_SET(&chngs[nchngs], fd, EVFILT_WRITE, EV_DELETE, 0, 0, 0);
fdi->currevs &= ~FDW_OUT;
++nchngs;
}
}
fdi->firing = 0;
ctx->changelist = fdi->next;
fdi->next = 0;
}
/* Wait for events. */
struct kevent evs[DILL_EVSSIZE];
struct timespec ts;
if(timeout >= 0) {
ts.tv_sec = timeout / 1000;
ts.tv_nsec = (((long)timeout) % 1000) * 1000000;
}
int nevs = kevent(ctx->kfd, chngs, nchngs, evs, DILL_EVSSIZE,
timeout < 0 ? NULL : &ts);
if(nevs < 0 && errno == EINTR) return -1;
dill_assert(nevs >= 0);
/* Join events on file descriptor basis.
Put all the firing fds into the changelist. */
int i;
for(i = 0; i != nevs; ++i) {
dill_assert(evs[i].flags != EV_ERROR);
int fd = (int)evs[i].ident;
struct dill_fdinfo *fdi = &ctx->fdinfos[fd];
/* Add firing event to the result list. */
if(evs[i].flags == EV_EOF)
fdi->firing |= (FDW_IN | FDW_OUT);
else {
if(evs[i].filter == EVFILT_READ)
fdi->firing |= FDW_IN;
if(evs[i].filter == EVFILT_WRITE)
fdi->firing |= FDW_OUT;
}
if(!fdi->next) {
fdi->next = ctx->changelist;
ctx->changelist = fd + 1;
}
}
/* Resume blocked coroutines. */
uint32_t chl = ctx->changelist;
while(chl != DILL_ENDLIST) {
int fd = chl - 1;
struct dill_fdinfo *fdi = &ctx->fdinfos[fd];
if(fdi->in && (fdi->firing & FDW_IN))
dill_trigger(&fdi->in->cl, 0);
if(fdi->out && (fdi->firing & FDW_OUT))
dill_trigger(&fdi->out->cl, 0);
fdi->firing = 0;
chl = fdi->next;
}
/* Return 0 on timeout or 1 if at least one coroutine was resumed. */
return nevs > 0 ? 1 : 0;
}