-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
mk_event_epoll.c
421 lines (352 loc) · 9.8 KB
/
mk_event_epoll.c
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* Monkey HTTP Server
* ==================
* Copyright 2001-2017 Eduardo Silva <eduardo@monkey.io>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <unistd.h>
#include <fcntl.h>
#include <sys/epoll.h>
#ifdef MK_HAVE_EVENTFD
#include <sys/eventfd.h>
#endif
#ifdef MK_HAVE_TIMERFD_CREATE
#include <sys/timerfd.h>
#endif
#include <time.h>
#include <mk_core/mk_event.h>
#include <mk_core/mk_memory.h>
#include <mk_core/mk_utils.h>
/* For old systems */
#ifndef EPOLLRDHUP
#define EPOLLRDHUP 0x2000
#endif
static inline int _mk_event_init()
{
return 0;
}
static inline void *_mk_event_loop_create(int size)
{
int efd;
struct mk_event_ctx *ctx;
/* Main event context */
ctx = mk_mem_alloc_z(sizeof(struct mk_event_ctx));
if (!ctx) {
return NULL;
}
/* Create the epoll instance */
#ifdef EPOLL_CLOEXEC
efd = epoll_create1(EPOLL_CLOEXEC);
#else
efd = epoll_create(1);
if (efd > 0) {
if (fcntl(efd, F_SETFD, FD_CLOEXEC) == -1) {
perror("fcntl");
}
}
#endif
if (efd == -1) {
mk_libc_error("epoll_create");
mk_mem_free(ctx);
return NULL;
}
ctx->efd = efd;
/* Allocate space for events queue */
ctx->events = mk_mem_alloc_z(sizeof(struct epoll_event) * size);
if (!ctx->events) {
close(ctx->efd);
mk_mem_free(ctx);
return NULL;
}
ctx->queue_size = size;
return ctx;
}
/* Close handlers and memory */
static inline void _mk_event_loop_destroy(struct mk_event_ctx *ctx)
{
close(ctx->efd);
mk_mem_free(ctx->events);
mk_mem_free(ctx);
}
/*
* It register certain events for the file descriptor in question, if
* the file descriptor have not been registered, create a new entry.
*/
static inline int _mk_event_add(struct mk_event_ctx *ctx, int fd,
int type, uint32_t events, void *data)
{
int op;
int ret;
struct mk_event *event;
struct epoll_event ep_event;
/* Verify the FD status and desired operation */
event = (struct mk_event *) data;
if (event->mask == MK_EVENT_EMPTY) {
op = EPOLL_CTL_ADD;
event->fd = fd;
event->status = MK_EVENT_REGISTERED;
event->type = type;
}
else {
op = EPOLL_CTL_MOD;
if (type != MK_EVENT_UNMODIFIED) {
event->type = type;
}
}
ep_event.events = EPOLLERR | EPOLLHUP | EPOLLRDHUP;
ep_event.data.ptr = data;
if (events & MK_EVENT_READ) {
ep_event.events |= EPOLLIN;
}
if (events & MK_EVENT_WRITE) {
ep_event.events |= EPOLLOUT;
}
ret = epoll_ctl(ctx->efd, op, fd, &ep_event);
if (ret < 0) {
mk_libc_error("epoll_ctl");
return -1;
}
event->mask = events;
event->priority = MK_EVENT_PRIORITY_DEFAULT;
event->_priority_head.next = NULL;
event->_priority_head.prev = NULL;
return ret;
}
/* Delete an event */
static inline int _mk_event_del(struct mk_event_ctx *ctx, struct mk_event *event)
{
int ret;
if ((event->status & MK_EVENT_REGISTERED) == 0) {
return 0;
}
ret = epoll_ctl(ctx->efd, EPOLL_CTL_DEL, event->fd, NULL);
MK_TRACE("[FD %i] Epoll, remove from QUEUE_FD=%i, ret=%i",
event->fd, ctx->efd, ret);
if (ret < 0) {
#ifdef MK_HAVE_TRACE
mk_libc_warn("epoll_ctl");
#endif
}
/* Remove from priority queue */
if (event->_priority_head.next != NULL &&
event->_priority_head.prev != NULL) {
mk_list_del(&event->_priority_head);
}
MK_EVENT_NEW(event);
return ret;
}
#ifdef MK_HAVE_TIMERFD_CREATE
/* Register a timeout file descriptor */
static inline int _mk_event_timeout_create(struct mk_event_ctx *ctx,
time_t sec, long nsec, void *data)
{
int ret;
int timer_fd;
struct itimerspec its;
struct timespec now;
struct mk_event *event;
mk_bug(!data);
memset(&its, '\0', sizeof(struct itimerspec));
if (clock_gettime(CLOCK_MONOTONIC, &now) != 0) {
mk_libc_error("clock_gettime");
return -1;
}
/* expiration interval */
its.it_interval.tv_sec = sec;
its.it_interval.tv_nsec = nsec;
/*
* initial expiration: note that we don't use nanoseconds in the timer,
* feel free to send a Pull Request if you need it.
*/
its.it_value.tv_sec = now.tv_sec + sec;
its.it_value.tv_nsec = 0;
timer_fd = timerfd_create(CLOCK_MONOTONIC, 0);
if (timer_fd == -1) {
mk_libc_error("timerfd");
return -1;
}
ret = timerfd_settime(timer_fd, TFD_TIMER_ABSTIME, &its, NULL);
if (ret < 0) {
mk_libc_error("timerfd_settime");
close(timer_fd);
return -1;
}
event = data;
event->fd = timer_fd;
event->type = MK_EVENT_NOTIFICATION;
event->mask = MK_EVENT_EMPTY;
/* register the timer into the epoll queue */
ret = _mk_event_add(ctx, timer_fd,
MK_EVENT_NOTIFICATION, MK_EVENT_READ, data);
if (ret != 0) {
close(timer_fd);
return ret;
}
return timer_fd;
}
#else /* MK_HAVE_TIMERFD_CREATE */
struct fd_timer {
int fd;
time_t sec;
long nsec;
};
/*
* Timeout worker, it writes a byte every certain amount of seconds, it finish
* once the other end of the pipe closes the fd[0].
*/
void _timeout_worker(void *arg)
{
int ret;
uint64_t val = 1;
struct fd_timer *timer;
struct timespec t_spec;
timer = (struct fd_timer *) arg;
t_spec.tv_sec = timer->sec;
t_spec.tv_nsec = timer->nsec;
while (1) {
/* sleep for a while */
nanosleep(&t_spec, NULL);
/* send notification */
ret = write(timer->fd, &val, sizeof(uint64_t));
if (ret == -1) {
perror("write");
break;
}
}
close(timer->fd);
free(timer);
}
/*
* This routine creates a timer, since timerfd_create(2) is not available (as
* Monkey could be be compiled in a very old Linux system), we implement a similar
* function through a thread and a pipe(2).
*/
static inline int _mk_event_timeout_create(struct mk_event_ctx *ctx,
time_t sec, long nsec, void *data)
{
int ret;
int fd[2];
struct mk_event *event;
struct fd_timer *timer;
pthread_t tid;
timer = mk_mem_alloc(sizeof(struct fd_timer));
if (!timer) {
return -1;
}
ret = pipe(fd);
if (ret < 0) {
mk_mem_free(timer);
mk_libc_error("pipe");
return ret;
}
event = (struct mk_event *) data;
event->fd = fd[0];
event->type = MK_EVENT_NOTIFICATION;
event->mask = MK_EVENT_EMPTY;
_mk_event_add(ctx, fd[0], MK_EVENT_NOTIFICATION, MK_EVENT_READ, data);
event->mask = MK_EVENT_READ;
/* Compose the timer context, this is released inside the worker thread */
timer->fd = fd[1];
timer->sec = sec;
timer->nsec = nsec;
/* Now the dirty workaround, create a thread */
ret = mk_utils_worker_spawn(_timeout_worker, timer, &tid);
if (ret < 0) {
close(fd[0]);
close(fd[1]);
mk_mem_free(timer);
return -1;
}
return fd[0];
}
#endif /* MK_HAVE_TIMERFD_CREATE */
static inline int _mk_event_timeout_destroy(struct mk_event_ctx *ctx, void *data)
{
struct mk_event *event;
if (!data) {
return 0;
}
event = (struct mk_event *) data;
_mk_event_del(ctx, event);
close(event->fd);
return 0;
}
static inline int _mk_event_channel_create(struct mk_event_ctx *ctx,
int *r_fd, int *w_fd, void *data)
{
int ret;
int fd[2];
struct mk_event *event;
ret = pipe(fd);
if (ret < 0) {
mk_libc_error("pipe");
return ret;
}
event = data;
event->fd = fd[0];
event->type = MK_EVENT_NOTIFICATION;
event->mask = MK_EVENT_EMPTY;
ret = _mk_event_add(ctx, fd[0],
MK_EVENT_NOTIFICATION, MK_EVENT_READ, event);
if (ret != 0) {
close(fd[0]);
close(fd[1]);
return ret;
}
*r_fd = fd[0];
*w_fd = fd[1];
return 0;
}
static inline int _mk_event_inject(struct mk_event_loop *loop,
struct mk_event *event,
int mask,
int prevent_duplication)
{
size_t index;
struct mk_event_ctx *ctx;
ctx = loop->data;
if (prevent_duplication) {
for (index = 0 ; index < loop->n_events ; index++) {
if (ctx->events[index].data.ptr == event) {
return 0;
}
}
}
event->mask = mask;
ctx->events[loop->n_events].data.ptr = event;
loop->n_events++;
return 0;
}
static inline int _mk_event_wait_2(struct mk_event_loop *loop, int timeout)
{
struct mk_event_ctx *ctx = loop->data;
int ret = 0;
while(1) {
ret = epoll_wait(ctx->efd, ctx->events, ctx->queue_size, timeout);
if (ret >= 0) {
break;
}
else if(ret < 0 && errno != EINTR) {
mk_libc_error("epoll_wait");
break;
}
/* retry when errno is EINTR */
}
loop->n_events = ret;
return ret;
}
static inline char *_mk_event_backend()
{
return "epoll";
}