-
-
Notifications
You must be signed in to change notification settings - Fork 411
/
epoll.c
535 lines (444 loc) · 13.5 KB
/
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
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
#define PONY_WANT_ATOMIC_DEFS
#include "asio.h"
#include "event.h"
#ifdef ASIO_USE_EPOLL
#include "../actor/messageq.h"
#include "../mem/pool.h"
#include "../sched/cpu.h"
#include "../sched/scheduler.h"
#include "../sched/systematic_testing.h"
#include "ponyassert.h"
#include <sys/epoll.h>
#include <sys/eventfd.h>
#include <sys/timerfd.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <stdbool.h>
#ifdef USE_VALGRIND
#include <valgrind/helgrind.h>
#endif
#define MAX_SIGNAL 128
struct asio_backend_t
{
int epfd;
int wakeup; /* eventfd to break epoll loop */
struct epoll_event events[MAX_EVENTS];
PONY_ATOMIC(asio_event_t*) sighandlers[MAX_SIGNAL];
PONY_ATOMIC(bool) terminate;
messageq_t q;
};
static void send_request(asio_event_t* ev, int req)
{
asio_backend_t* b = ponyint_asio_get_backend();
pony_assert(b != NULL);
asio_msg_t* msg = (asio_msg_t*)pony_alloc_msg(
POOL_INDEX(sizeof(asio_msg_t)), 0);
msg->event = ev;
msg->flags = req;
ponyint_thread_messageq_push(&b->q, (pony_msg_t*)msg, (pony_msg_t*)msg
#ifdef USE_DYNAMIC_TRACE
, SPECIAL_THREADID_EPOLL, SPECIAL_THREADID_EPOLL
#endif
);
eventfd_write(b->wakeup, 1);
}
static void signal_handler(int sig)
{
if(sig >= MAX_SIGNAL)
return;
asio_backend_t* b = ponyint_asio_get_backend();
pony_assert(b != NULL);
asio_event_t* ev = atomic_load_explicit(&b->sighandlers[sig],
memory_order_acquire);
#ifdef USE_VALGRIND
ANNOTATE_HAPPENS_AFTER(&b->sighandlers[sig]);
#endif
if(ev == NULL)
return;
eventfd_write(ev->fd, 1);
}
#if !defined(USE_SCHEDULER_SCALING_PTHREADS)
static void empty_signal_handler(int sig)
{
(void) sig;
}
#endif
static void handle_queue(asio_backend_t* b)
{
asio_msg_t* msg;
while((msg = (asio_msg_t*)ponyint_thread_messageq_pop(&b->q
#ifdef USE_DYNAMIC_TRACE
, SPECIAL_THREADID_EPOLL
#endif
)) != NULL)
{
asio_event_t* ev = msg->event;
switch(msg->flags)
{
case ASIO_DISPOSABLE:
pony_asio_event_send(ev, ASIO_DISPOSABLE, 0);
break;
default: {}
}
}
}
asio_backend_t* ponyint_asio_backend_init()
{
asio_backend_t* b = POOL_ALLOC(asio_backend_t);
memset(b, 0, sizeof(asio_backend_t));
ponyint_messageq_init(&b->q);
b->epfd = epoll_create1(EPOLL_CLOEXEC);
b->wakeup = eventfd(0, EFD_NONBLOCK);
if(b->epfd == 0 || b->wakeup == 0)
{
POOL_FREE(asio_backend_t, b);
return NULL;
}
struct epoll_event ep;
ep.data.ptr = b;
ep.events = EPOLLIN | EPOLLRDHUP | EPOLLET;
epoll_ctl(b->epfd, EPOLL_CTL_ADD, b->wakeup, &ep);
#if !defined(USE_SCHEDULER_SCALING_PTHREADS)
// Make sure we ignore signals related to scheduler sleeping/waking
// as the default for those signals is termination
struct sigaction new_action;
new_action.sa_handler = empty_signal_handler;
sigemptyset (&new_action.sa_mask);
// ask to restart interrupted syscalls to match `signal` behavior
new_action.sa_flags = SA_RESTART;
sigaction(PONY_SCHED_SLEEP_WAKE_SIGNAL, &new_action, NULL);
#endif
return b;
}
void ponyint_asio_backend_final(asio_backend_t* b)
{
atomic_store_explicit(&b->terminate, true, memory_order_relaxed);
eventfd_write(b->wakeup, 1);
}
// Single function for resubscribing to both reads and writes for an event
PONY_API void pony_asio_event_resubscribe(asio_event_t* ev)
{
// needs to be a valid event that is one shot enabled
if((ev == NULL) ||
(ev->flags == ASIO_DISPOSABLE) ||
(ev->flags == ASIO_DESTROYED) ||
!(ev->flags & ASIO_ONESHOT))
{
pony_assert(0);
return;
}
asio_backend_t* b = ponyint_asio_get_backend();
pony_assert(b != NULL);
struct epoll_event ep;
ep.data.ptr = ev;
ep.events = EPOLLONESHOT;
bool something_to_resub = false;
// if the event is supposed to be listening for write notifications
// and it is currently not writeable
if((ev->flags & ASIO_WRITE) && !ev->writeable)
{
something_to_resub = true;
ep.events |= EPOLLOUT;
}
// if the event is supposed to be listening for read notifications
// and it is currently not readable
if((ev->flags & ASIO_READ) && !ev->readable)
{
something_to_resub = true;
ep.events |= EPOLLRDHUP;
ep.events |= EPOLLIN;
}
// only resubscribe if there is something to resubscribe to
if (something_to_resub)
epoll_ctl(b->epfd, EPOLL_CTL_MOD, ev->fd, &ep);
}
// Kept to maintain backwards compatibility so folks don't
// have to change their code to use `pony_asio_event_resubscribe`
// immediately
PONY_API void pony_asio_event_resubscribe_write(asio_event_t* ev)
{
pony_asio_event_resubscribe(ev);
}
// Kept to maintain backwards compatibility so folks don't
// have to change their code to use `pony_asio_event_resubscribe`
// immediately
PONY_API void pony_asio_event_resubscribe_read(asio_event_t* ev)
{
pony_asio_event_resubscribe(ev);
}
DECLARE_THREAD_FN(ponyint_asio_backend_dispatch)
{
ponyint_cpu_affinity(ponyint_asio_get_cpu());
pony_register_thread();
asio_backend_t* b = arg;
pony_assert(b != NULL);
#if !defined(USE_SCHEDULER_SCALING_PTHREADS)
// Make sure we block signals related to scheduler sleeping/waking
// so they queue up to avoid race conditions
sigset_t set;
sigemptyset(&set);
sigaddset(&set, PONY_SCHED_SLEEP_WAKE_SIGNAL);
pthread_sigmask(SIG_BLOCK, &set, NULL);
#endif
#if defined(USE_SYSTEMATIC_TESTING)
// sleep thread until we're ready to start processing
SYSTEMATIC_TESTING_WAIT_START(ponyint_asio_get_backend_tid(), ponyint_asio_get_backend_sleep_object());
#endif
while(!atomic_load_explicit(&b->terminate, memory_order_acquire))
{
int wait_time = -1;
#if defined(USE_SYSTEMATIC_TESTING)
wait_time = 10;
#endif
int event_cnt = epoll_wait(b->epfd, b->events, MAX_EVENTS, wait_time);
SYSTEMATIC_TESTING_YIELD();
for(int i = 0; i < event_cnt; i++)
{
struct epoll_event* ep = &(b->events[i]);
if(ep->data.ptr == b)
continue;
asio_event_t* ev = ep->data.ptr;
uint32_t flags = 0;
uint32_t count = 0;
if(ev->flags & ASIO_READ)
{
if(ep->events & (EPOLLIN | EPOLLRDHUP | EPOLLHUP | EPOLLERR))
{
// Send read notification to an actor if either
// * the event is not a one shot event
// * the event is a one shot event and we haven't already sent a notification
// if the event is a one shot event and we have already sent a notification
// don't send another one until we are asked for it again (i.e. the actor
// gets a 0 byte read and sets `readable` to false and resubscribes to reads
if(((ev->flags & ASIO_ONESHOT) && !ev->readable) || !(ev->flags & ASIO_ONESHOT))
{
ev->readable = true;
flags |= ASIO_READ;
}
}
}
if(ev->flags & ASIO_WRITE)
{
if(ep->events & EPOLLOUT)
{
// Send write notification to an actor if either
// * the event is not a one shot event
// * the event is a one shot event and we haven't already sent a notification
// if the event is a one shot event and we have already sent a notification
// don't send another one until we are asked for it again (i.e. the actor
// gets partial write and sets `writeable` to false and resubscribes to writes
if(((ev->flags & ASIO_ONESHOT) && !ev->writeable) || !(ev->flags & ASIO_ONESHOT))
{
flags |= ASIO_WRITE;
ev->writeable = true;
}
}
}
if(ev->flags & ASIO_TIMER)
{
if(ep->events & (EPOLLIN | EPOLLRDHUP | EPOLLHUP | EPOLLERR))
{
uint64_t missed;
ssize_t rc = read(ev->fd, &missed, sizeof(uint64_t));
(void)rc;
flags |= ASIO_TIMER;
}
}
if(ev->flags & ASIO_SIGNAL)
{
if(ep->events & (EPOLLIN | EPOLLRDHUP | EPOLLHUP | EPOLLERR))
{
uint64_t missed;
ssize_t rc = read(ev->fd, &missed, sizeof(uint64_t));
(void)rc;
flags |= ASIO_SIGNAL;
count = (uint32_t)missed;
}
}
// if we had a valid event of some type that needs to be sent
// to an actor
if(flags != 0)
{
// if this event hasn't been destroyed or disposed.
// to avoid a race condition if destroyed or dispoed events
// are resubscribed
if((ev->flags != ASIO_DISPOSABLE) && (ev->flags != ASIO_DESTROYED))
// if this event is using one shot and should auto resubscribe and
// then resubscribe
if(ev->flags & ASIO_ONESHOT)
pony_asio_event_resubscribe(ev);
// send the event to the actor
pony_asio_event_send(ev, flags, count);
}
}
handle_queue(b);
}
close(b->epfd);
close(b->wakeup);
ponyint_messageq_destroy(&b->q, true);
POOL_FREE(asio_backend_t, b);
SYSTEMATIC_TESTING_STOP_THREAD();
pony_unregister_thread();
return NULL;
}
static void timer_set_nsec(int fd, uint64_t nsec)
{
struct itimerspec ts;
ts.it_interval.tv_sec = 0;
ts.it_interval.tv_nsec = 0;
ts.it_value.tv_sec = (time_t)(nsec / 1000000000);
ts.it_value.tv_nsec = (long)(nsec - (ts.it_value.tv_sec * 1000000000));
timerfd_settime(fd, 0, &ts, NULL);
}
PONY_API void pony_asio_event_subscribe(asio_event_t* ev)
{
if((ev == NULL) ||
(ev->flags == ASIO_DISPOSABLE) ||
(ev->flags == ASIO_DESTROYED))
{
pony_assert(0);
return;
}
asio_backend_t* b = ponyint_asio_get_backend();
pony_assert(b != NULL);
if(ev->noisy)
{
uint64_t old_count = ponyint_asio_noisy_add();
// tell scheduler threads that asio has at least one noisy actor
// if the old_count was 0
if (old_count == 0)
ponyint_sched_noisy_asio(SPECIAL_THREADID_EPOLL);
}
struct epoll_event ep;
ep.data.ptr = ev;
ep.events = EPOLLRDHUP;
if(ev->flags & ASIO_READ)
ep.events |= EPOLLIN;
if(ev->flags & ASIO_WRITE)
ep.events |= EPOLLOUT;
if(ev->flags & ASIO_TIMER)
{
ev->fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK);
timer_set_nsec(ev->fd, ev->nsec);
ep.events |= EPOLLIN;
}
if(ev->flags & ASIO_SIGNAL)
{
int sig = (int)ev->nsec;
asio_event_t* prev = NULL;
#ifdef USE_VALGRIND
ANNOTATE_HAPPENS_BEFORE(&b->sighandlers[sig]);
#endif
if((sig < MAX_SIGNAL) &&
atomic_compare_exchange_strong_explicit(&b->sighandlers[sig], &prev, ev,
memory_order_acq_rel, memory_order_acquire))
{
struct sigaction new_action;
new_action.sa_handler = signal_handler;
sigemptyset (&new_action.sa_mask);
// ask to restart interrupted syscalls to match `signal` behavior
new_action.sa_flags = SA_RESTART;
sigaction(sig, &new_action, NULL);
ev->fd = eventfd(0, EFD_NONBLOCK);
ep.events |= EPOLLIN;
} else {
return;
}
}
if(ev->flags & ASIO_ONESHOT)
{
ep.events |= EPOLLONESHOT;
} else {
// Only use edge triggering if one shot isn't enabled.
// This is because of how the runtime gets notifications
// from epoll in this ASIO thread and then notifies the
// appropriate actor to read/write as necessary.
// specifically, it seems there's an edge case/race condition
// with edge triggering where if there is already data waiting
// on the socket, then epoll might not be triggering immediately
// when an edge triggered epoll request is made.
ep.events |= EPOLLET;
}
epoll_ctl(b->epfd, EPOLL_CTL_ADD, ev->fd, &ep);
}
PONY_API void pony_asio_event_setnsec(asio_event_t* ev, uint64_t nsec)
{
if((ev == NULL) ||
(ev->flags == ASIO_DISPOSABLE) ||
(ev->flags == ASIO_DESTROYED))
{
pony_assert(0);
return;
}
if(ev->flags & ASIO_TIMER)
{
ev->nsec = nsec;
timer_set_nsec(ev->fd, nsec);
}
}
PONY_API void pony_asio_event_unsubscribe(asio_event_t* ev)
{
if((ev == NULL) ||
(ev->flags == ASIO_DISPOSABLE) ||
(ev->flags == ASIO_DESTROYED))
{
pony_assert(0);
return;
}
asio_backend_t* b = ponyint_asio_get_backend();
pony_assert(b != NULL);
if(ev->noisy)
{
uint64_t old_count = ponyint_asio_noisy_remove();
// tell scheduler threads that asio has no noisy actors
// if the old_count was 1
if (old_count == 1)
{
ponyint_sched_unnoisy_asio(SPECIAL_THREADID_EPOLL);
// maybe wake up a scheduler thread if they've all fallen asleep
ponyint_sched_maybe_wakeup_if_all_asleep(-1);
}
ev->noisy = false;
}
epoll_ctl(b->epfd, EPOLL_CTL_DEL, ev->fd, NULL);
if(ev->flags & ASIO_TIMER)
{
if(ev->fd != -1)
{
close(ev->fd);
ev->fd = -1;
}
}
if(ev->flags & ASIO_SIGNAL)
{
int sig = (int)ev->nsec;
asio_event_t* prev = ev;
#ifdef USE_VALGRIND
ANNOTATE_HAPPENS_BEFORE(&b->sighandlers[sig]);
#endif
if((sig < MAX_SIGNAL) &&
atomic_compare_exchange_strong_explicit(&b->sighandlers[sig], &prev, NULL,
memory_order_acq_rel, memory_order_acquire))
{
struct sigaction new_action;
#if !defined(USE_SCHEDULER_SCALING_PTHREADS)
// Make sure we ignore signals related to scheduler sleeping/waking
// as the default for those signals is termination
if(sig == PONY_SCHED_SLEEP_WAKE_SIGNAL)
new_action.sa_handler = empty_signal_handler;
else
#endif
new_action.sa_handler = SIG_DFL;
sigemptyset (&new_action.sa_mask);
// ask to restart interrupted syscalls to match `signal` behavior
new_action.sa_flags = SA_RESTART;
sigaction(sig, &new_action, NULL);
close(ev->fd);
ev->fd = -1;
}
}
ev->flags = ASIO_DISPOSABLE;
send_request(ev, ASIO_DISPOSABLE);
}
#endif