-
Notifications
You must be signed in to change notification settings - Fork 4
/
event.c
602 lines (500 loc) · 15.3 KB
/
event.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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
/*
* Based on QEMU's util/async.c
*
* Copyright (c) 2003-2008 Fabrice Bellard
* Copyright (c) 2009-2017 QEMU contributors
*
* 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 <string.h>
#include <unistd.h>
#include <sys/epoll.h>
#include <sys/eventfd.h>
#include <semaphore.h>
#include "catomic.h"
#include "queue.h"
#include "platform.h"
#include "event.h"
#include "logging.h"
enum {
/* Already enqueued and waiting for bh_poll() */
BH_PENDING = (1 << 0),
/* Invoke the callback */
BH_SCHEDULED = (1 << 1),
/* Delete without invoking callback */
BH_DELETED = (1 << 2),
/* Delete after invoking callback */
BH_ONESHOT = (1 << 3),
};
struct vhd_bh {
struct vhd_event_loop *ctx;
vhd_bh_cb *cb;
void *opaque;
SLIST_ENTRY(vhd_bh) next;
unsigned flags;
};
typedef SLIST_HEAD(, vhd_bh) vhd_bh_list;
struct vhd_event_loop {
int epollfd;
/* eventfd we use to cancel epoll_wait if needed */
int notifyfd;
/* number of currently attached events (for consistency checks) */
uint32_t num_events_attached;
bool notified;
/* vhd_terminate_event_loop has been completed */
bool is_terminated;
bool has_home_thread;
/* preallocated events buffer */
struct epoll_event *events;
uint64_t max_events;
vhd_bh_list bh_list;
SLIST_HEAD(, vhd_io_handler) deleted_handlers;
};
static void evloop_notify(struct vhd_event_loop *evloop)
{
if (!catomic_xchg(&evloop->notified, true)) {
vhd_set_eventfd(evloop->notifyfd);
}
}
static void notify_accept(struct vhd_event_loop *evloop)
{
if (catomic_read(&evloop->notified)) {
vhd_clear_eventfd(evloop->notifyfd);
catomic_xchg(&evloop->notified, false);
}
}
/* called concurrently from any thread */
static void bh_enqueue(struct vhd_bh *bh, unsigned new_flags)
{
struct vhd_event_loop *ctx = bh->ctx;
unsigned old_flags;
/*
* The memory barrier implicit in catomic_fetch_or makes sure that:
* 1. any writes needed by the callback are done before the locations are
* read in the bh_poll.
* 2. ctx is loaded before the callback has a chance to execute and bh
* could be freed.
* Paired with bh_dequeue().
*/
old_flags = catomic_fetch_or(&bh->flags, BH_PENDING | new_flags);
if (!(old_flags & BH_PENDING)) {
SLIST_INSERT_HEAD_ATOMIC(&ctx->bh_list, bh, next);
}
evloop_notify(ctx);
}
/* only called from bh_poll() and bh_cleanup() */
static struct vhd_bh *bh_dequeue(vhd_bh_list *head, unsigned *flags)
{
struct vhd_bh *bh = SLIST_FIRST_RCU(head);
if (!bh) {
return NULL;
}
SLIST_REMOVE_HEAD(head, next);
/*
* The catomic_and is paired with bh_enqueue(). The implicit memory barrier
* ensures that the callback sees all writes done by the scheduling thread.
* It also ensures that the scheduling thread sees the cleared flag before
* bh->cb has run, and thus will call evloop_notify again if necessary.
*/
*flags = catomic_fetch_and(&bh->flags, ~(BH_PENDING | BH_SCHEDULED));
return bh;
}
struct vhd_bh *vhd_bh_new(struct vhd_event_loop *ctx,
vhd_bh_cb *cb, void *opaque)
{
struct vhd_bh *bh = vhd_alloc(sizeof(*bh));
*bh = (struct vhd_bh){
.ctx = ctx,
.cb = cb,
.opaque = opaque,
};
return bh;
}
void vhd_bh_schedule_oneshot(struct vhd_event_loop *ctx,
vhd_bh_cb *cb, void *opaque)
{
struct vhd_bh *bh = vhd_bh_new(ctx, cb, opaque);
bh_enqueue(bh, BH_SCHEDULED | BH_ONESHOT);
}
void vhd_bh_schedule(struct vhd_bh *bh)
{
bh_enqueue(bh, BH_SCHEDULED);
}
/* this is async and doesn't interfere with already running bh */
void vhd_bh_cancel(struct vhd_bh *bh)
{
catomic_and(&bh->flags, ~BH_SCHEDULED);
}
/* this is async; deletion only happens in bh_poll, so need to enqueue first */
void vhd_bh_delete(struct vhd_bh *bh)
{
bh_enqueue(bh, BH_DELETED);
}
static void bh_call(struct vhd_bh *bh)
{
bh->cb(bh->opaque);
}
/*
* Execute bottom halves scheduled so far. Return true if any progress has
* been made (i.e. any bh was executed).
* Multiple occurrences of bh_poll cannot be called concurrently.
*/
static bool bh_poll(struct vhd_event_loop *ctx)
{
vhd_bh_list bh_list;
struct vhd_bh *bh;
unsigned flags;
bool ret = false;
SLIST_INIT(&bh_list);
/* swap bh list from ctx for a fresh one */
SLIST_MOVE_ATOMIC(&bh_list, &ctx->bh_list);
for (;;) {
bh = bh_dequeue(&bh_list, &flags);
if (!bh) {
break;
}
if ((flags & (BH_SCHEDULED | BH_DELETED)) == BH_SCHEDULED) {
ret = true;
bh_call(bh);
}
if (flags & (BH_DELETED | BH_ONESHOT)) {
vhd_free(bh);
}
}
return ret;
}
static void bh_cleanup(struct vhd_event_loop *ctx)
{
struct vhd_bh *bh;
unsigned flags;
for (;;) {
bh = bh_dequeue(&ctx->bh_list, &flags);
if (!bh) {
break;
}
/* only deleted bhs may remain */
assert(flags & BH_DELETED);
vhd_free(bh);
}
}
struct vhd_io_handler {
struct vhd_event_loop *evloop;
int (*read)(void *opaque);
/* FIXME: must really include write handler as well */
void *opaque;
int fd;
bool attached;
SLIST_ENTRY(vhd_io_handler) deleted_entry;
};
static int handle_one_event(struct vhd_io_handler *handler, int event_code)
{
if ((event_code & (EPOLLIN | EPOLLERR | EPOLLRDHUP)) && handler->read) {
return handler->read(handler->opaque);
}
return 0;
}
static int handle_events(struct vhd_event_loop *evloop, int nevents)
{
int nerr = 0;
struct epoll_event *events = evloop->events;
for (int i = 0; i < nevents; i++) {
struct vhd_io_handler *handler = events[i].data.ptr;
/* event loop notifer doesn't use a handler */
if (!handler) {
continue;
}
/* don't call into detached handler even if it's on the ready list */
if (!handler->attached) {
continue;
}
if (handle_one_event(handler, events[i].events)) {
nerr++;
}
}
/*
* The deleted handlers are detached and won't appear on the ready list any
* more, so it's now safe to actually delete them.
*/
while (!SLIST_EMPTY(&evloop->deleted_handlers)) {
struct vhd_io_handler *handler =
SLIST_FIRST(&evloop->deleted_handlers);
SLIST_REMOVE_HEAD(&evloop->deleted_handlers, deleted_entry);
vhd_free(handler);
}
return nerr;
}
struct vhd_event_loop *vhd_create_event_loop(size_t max_events)
{
int notifyfd;
int epollfd;
epollfd = epoll_create1(EPOLL_CLOEXEC);
if (epollfd < 0) {
VHD_LOG_ERROR("epoll_create1: %s", strerror(errno));
return NULL;
}
notifyfd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
if (notifyfd < 0) {
VHD_LOG_ERROR("eventfd() failed: %s", strerror(errno));
goto close_epoll;
}
/* Register notify eventfd, make sure it is level-triggered */
struct epoll_event ev = {
.events = EPOLLIN,
};
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, notifyfd, &ev) == -1) {
VHD_LOG_ERROR("epoll_ctl(EPOLL_CTL_ADD, notifyfd): %s",
strerror(errno));
goto error_out;
}
struct vhd_event_loop *evloop = vhd_alloc(sizeof(*evloop));
max_events++; /* +1 for notify eventfd */
*evloop = (struct vhd_event_loop) {
.epollfd = epollfd,
.notifyfd = notifyfd,
.max_events = max_events,
.events = vhd_calloc(sizeof(evloop->events[0]), max_events),
};
SLIST_INIT(&evloop->bh_list);
SLIST_INIT(&evloop->deleted_handlers);
return evloop;
error_out:
close(notifyfd);
close_epoll:
close(epollfd);
return NULL;
}
static __thread struct vhd_event_loop *home_evloop;
int vhd_run_event_loop(struct vhd_event_loop *evloop, int timeout_ms)
{
if (!home_evloop) {
bool had_home_thread = catomic_xchg(&evloop->has_home_thread, true);
VHD_VERIFY(!had_home_thread);
home_evloop = evloop;
}
VHD_ASSERT(evloop == home_evloop);
if (evloop->is_terminated) {
return 0;
}
int nev = epoll_wait(evloop->epollfd, evloop->events, evloop->max_events,
timeout_ms);
if (!nev) {
return -EAGAIN;
} else if (nev < 0) {
int ret = -errno;
if (ret == -EINTR) {
return -EAGAIN;
}
VHD_LOG_ERROR("epoll_wait internal error: %s", strerror(-ret));
return ret;
}
notify_accept(evloop);
bh_poll(evloop);
int nerr = handle_events(evloop, nev);
if (nerr) {
VHD_LOG_WARN("Got %d events, can't handle %d events", nev, nerr);
return -EIO;
}
return -EAGAIN;
}
static void evloop_stop_bh(void *opaque)
{
struct vhd_event_loop *evloop = opaque;
evloop->is_terminated = true;
}
void vhd_terminate_event_loop(struct vhd_event_loop *evloop)
{
vhd_bh_schedule_oneshot(evloop, evloop_stop_bh, evloop);
}
/*
* Only free the event loop when there's no concurrent access to it. One way
* to do it is to do free at the end of the thread running the event loop.
* Another is to wait for the thread running the event loop to terminate (to
* join it) and only do free afterwards.
*/
void vhd_free_event_loop(struct vhd_event_loop *evloop)
{
VHD_ASSERT(evloop->is_terminated);
VHD_ASSERT(evloop->num_events_attached == 0);
bh_cleanup(evloop);
close(evloop->epollfd);
close(evloop->notifyfd);
vhd_free(evloop->events);
vhd_free(evloop);
}
static void event_loop_inc_events(struct vhd_event_loop *evloop)
{
evloop->num_events_attached++;
}
static void event_loop_dec_events(struct vhd_event_loop *evloop)
{
VHD_ASSERT(evloop->num_events_attached > 0);
evloop->num_events_attached--;
}
int vhd_attach_io_handler(struct vhd_io_handler *handler)
{
struct vhd_event_loop *evloop = handler->evloop;
int fd = handler->fd;
struct epoll_event ev = {
.events = EPOLLIN | EPOLLHUP | EPOLLRDHUP,
.data.ptr = handler
};
/* to maintain fields consistency only do this in the home event loop */
VHD_ASSERT(evloop == home_evloop);
/* unlike detach, multiple attachment is a logic error */
VHD_ASSERT(!handler->attached);
if (epoll_ctl(evloop->epollfd, EPOLL_CTL_ADD, fd, &ev) < 0) {
int ret = -errno;
VHD_LOG_ERROR("Can't add event: %s", strerror(-ret));
return ret;
}
handler->attached = true;
return 0;
}
struct vhd_io_handler *vhd_add_io_handler(struct vhd_event_loop *evloop,
int fd, int (*read)(void *opaque),
void *opaque)
{
struct vhd_io_handler *handler;
handler = vhd_alloc(sizeof(*handler));
*handler = (struct vhd_io_handler) {
.evloop = evloop,
.fd = fd,
.read = read,
.opaque = opaque
};
if (vhd_attach_io_handler(handler) < 0) {
goto fail;
}
event_loop_inc_events(evloop);
return handler;
fail:
vhd_free(handler);
return NULL;
}
int vhd_detach_io_handler(struct vhd_io_handler *handler)
{
struct vhd_event_loop *evloop = handler->evloop;
/* to maintain fields consistency only do this in the home event loop */
VHD_ASSERT(evloop == home_evloop);
if (!handler->attached) {
return 0;
}
if (epoll_ctl(evloop->epollfd, EPOLL_CTL_DEL, handler->fd, NULL) < 0) {
int ret = -errno;
VHD_LOG_ERROR("Can't delete event: %s", strerror(-ret));
return ret;
}
/*
* The file descriptor being detached may still be sitting on the ready
* list returned by epoll_wait.
* Make sure the handler for it isn't called.
*/
handler->attached = false;
return 0;
}
int vhd_del_io_handler(struct vhd_io_handler *handler)
{
int ret;
struct vhd_event_loop *evloop = handler->evloop;
ret = vhd_detach_io_handler(handler);
if (ret < 0) {
return ret;
}
/*
* The file descriptor being deleted may still be sitting on the ready list
* returned by epoll_wait.
* Schedule it for deallocation at the end of the iteration after the ready
* event list processing is through.
*/
SLIST_INSERT_HEAD(&evloop->deleted_handlers, handler, deleted_entry);
event_loop_dec_events(evloop);
return 0;
}
void vhd_clear_eventfd(int fd)
{
eventfd_t unused;
while (eventfd_read(fd, &unused) && errno == EINTR) {
;
}
}
void vhd_set_eventfd(int fd)
{
while (eventfd_write(fd, 1) && errno == EINTR) {
;
}
}
struct vhd_work {
void (*func)(struct vhd_work *, void *);
void *opaque;
int ret;
sem_t wait;
};
void vhd_complete_work(struct vhd_work *work, int ret)
{
work->ret = ret;
/*
* sem_post is a full memory barrier so the vhd_submit_work_and_wait will
* see ->ret set above
*/
if (sem_post(&work->wait) < 0) {
/* log an error and continue as there's no better strategy */
VHD_LOG_ERROR("sem_post: %s", strerror(errno));
}
}
static void work_bh(void *opaque)
{
struct vhd_work *work = opaque;
work->func(work, work->opaque);
}
int vhd_submit_work_and_wait(struct vhd_event_loop *evloop,
void (*func)(struct vhd_work *, void *),
void *opaque)
{
int ret;
struct vhd_work work = {
.func = func,
.opaque = opaque,
};
/* waiting for completion in the same event loop would deadlock */
VHD_ASSERT(evloop != home_evloop);
/* sem_init can't fail when both arguments are zero */
ret = sem_init(&work.wait, 0, 0);
VHD_ASSERT(ret == 0);
vhd_bh_schedule_oneshot(evloop, work_bh, &work);
/*
* sem_wait may fail with either EINTR (we handle it) or EINVAL when
* called on invalid pointer, which is impossible here.
*/
do {
ret = sem_wait(&work.wait);
} while (ret < 0 && errno == EINTR);
VHD_ASSERT(ret == 0);
/*
* sem_destroy may fail only with EINVAL when called on invalid pointer,
* which is impossible here.
*/
ret = sem_destroy(&work.wait);
VHD_ASSERT(ret == 0);
/*
* sem_wait is a full memory barrier so this is the ->ret set in
* vhd_complete_work
*/
return work.ret;
}