-
Notifications
You must be signed in to change notification settings - Fork 11
/
ev.h
1096 lines (988 loc) · 35.8 KB
/
ev.h
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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* BSD 2-Clause License
*
* Copyright (c) 2023, Andrea Giacomo Baldan All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* EV_H is a single header library that offers a set of APIs to create and
* handle a very lightweight event-loop based on the most common IO
* multiplexing implementations available on Unix-based systems:
*
* - Linux-based: epoll
* - BSD-based (osx): kqueue
* - All around: poll, select
*
* By setting a pre-processor macro definition it's possible to force the use
* of a wanted implementation.
*
* #define EPOLL 1 // set to use epoll
* #define KQUEUE 1 // set to use kqueue
* #define POLL 1 // set to use poll
* #define SELECT 1 // set to use select
*
* There's another 2 possible tweakable values, the number of events to monitor
* at once, which is set to 1024 by default
*
* #define EVENTLOOP_MAX_EVENTS 1024
*
* The timeout in ms to wait before returning on the blocking call that every
* IO mux implementation accepts, -1 means block forever until new events
* arrive.
* #define EVENTLOOP_TIMEOUT -1
*
* Exposed APIs si divided in generic loop management and TCP server helpers as
* the most common (but not the only) use cases for this event-loop libraries
* in general.
* Please refers to `examples` directory to see some common uses, in particular
* `echo_server.c` and `ping_pong.c` highlight two simple cases where the
* genric APIs are employed, `ev_tcp_server.c` instead show how to implement a
* trivial TCP server using helpers.
*/
#ifndef EV_H
#define EV_H
#ifdef __linux__
#include <linux/version.h>
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 44)
#define EPOLL 1
#define EVENTLOOP_BACKEND "epoll"
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 1, 23)
#define POLL 1
#define EVENTLOOP_BACKEND "poll"
#else
#define SELECT 1
#define EVENTLOOP_BACKEND "select"
#endif
#elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || \
defined(__NetBSD__)
#define KQUEUE 1
#define EVENTLOOP_BACKEND "kqueue"
#else
#define SELECT 1
#define EVENTLOOP_BACKEND "select"
#endif // __linux__
#include <assert.h>
#include <errno.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#ifdef __linux__
#include <sys/eventfd.h>
#include <sys/timerfd.h>
#else
#include <sys/socket.h>
#endif
/*
* Maximum number of events to monitor at a time, useful for epoll and poll
* calls, the value represents the length of the events array. Tweakable value.
*/
#define EVENTLOOP_MAX_EVENTS 1024
/*
* The timeout to wait before returning on the blocking call that every IO mux
* implementation accepts, -1 means block forever until new events arrive.
* Tweakable value.
*/
#define EVENTLOOP_TIMEOUT -1
/*
* Return codes */
#define EV_OK 0
#define EV_ERR -1
#define EV_OOM -2
/*
* Event types, meant to be OR-ed on a bitmask to define the type of an event
* which can have multiple traits
*/
enum ev_type {
EV_NONE = 0x00,
EV_READ = 0x01,
EV_WRITE = 0x02,
EV_DISCONNECT = 0x04,
EV_EVENTFD = 0x08,
EV_TIMERFD = 0x10,
EV_CLOSEFD = 0x20
};
/*
* Event loop context, carry the expected number of events to be monitored at
* every cycle and an opaque pointer to the backend used as engine
* (select | poll | epoll | kqueue).
* At start, the library tries to infer the multiplexing IO implementation
* available on the host machine, `void *api` will carry the API-dedicated
* struct as a rudimentary form of polymorphism.
* The context is the main handler of the event loop and is meant to be passed
* around on each callback fired during the execution of the host application.
*
* Ideally it should be at most one context per thread, it's the user's duty to
* take care of locking of possible shared parts and datastrutures but it's
* definetly a possibility to run multiple theads each one with his own loop,
* depending on the scenario, use-case by use-case it can be a feasible
* solutiion.
*/
typedef struct ev_ctx {
int events_nr;
int maxfd; // the maximum FD monitored by the event context,
// events_monitored must be at least maxfd long
int stop;
int is_running;
int maxevents;
unsigned long long fired_events;
struct ev *events_monitored;
void *api; // opaque pointer to platform defined backends
} ev_context;
/*
* Event structure used as the main carrier of clients information, it will be
* tracked by an array in every context created
*/
struct ev {
int fd;
int mask;
void *rdata; // opaque pointer for read callback args
void *wdata; // opaque pointer for write callback args
void (*rcallback)(ev_context *, void *); // read callback
void (*wcallback)(ev_context *, void *); // write callback
};
/*
* Initialize the ev_context, accepting the number of events to monitor; that
* value is indicative as if a FD exceeds the cap set the events array will be
* resized accordingly.
* The first thing done is the initialization of the api pointer according to
* the Mux IO backend found on the host machine
*/
int ev_init(ev_context *, int);
/*
* Just check if the ev_context is running, return 0 if it's not running, 1
* otherwise
*/
int ev_is_running(const ev_context *);
/*
* By design ev library can instantiate a default `ev_context`, calling
* `ev_get_context` the first time will create the loop as a singleton,
* subsequent calls will retrieve the same first context allocated
*/
ev_context *ev_get_context(void);
/*
* Call custom destroy function based on the api type set up calling `ev_init`
* and de-allocate all events monitored memory
*/
void ev_destroy(ev_context *);
/*
* Poll an event context for events, accepts a timeout or block forever,
* returning only when a list of FDs are ready to either READ, WRITE or TIMER
* to be executed.
*/
int ev_poll(ev_context *, time_t);
/*
* Blocks forever in a loop polling for events with ev_poll calls. At every
* cycle executes callbacks registered with each event
*/
int ev_run(ev_context *);
/*
* Trigger a stop on a running event, it's meant to be run as an event in a
* running ev_ctx
*/
void ev_stop(ev_context *);
/*
* Add a single FD to the underlying backend of the event loop. Equal to
* ev_fire_event just without an event to be carried. Useful to add simple
* descritors like a listening socket o message queue FD.
*/
int ev_watch_fd(ev_context *, int, int);
/*
* Remove a FD from the loop, even tho a close syscall is sufficient to remove
* the FD from the underlying backend such as EPOLL/SELECT, this call ensure
* that any associated events is cleaned out an set to EV_NONE
*/
int ev_del_fd(ev_context *, int);
/*
* Register a new event, semantically it's equal to ev_register_event but
* it's meant to be used when an FD is not already watched by the event loop.
* It could be easily integrated in ev_fire_event call but I prefer maintain
* the samantic separation of responsibilities.
*/
int ev_register_event(ev_context *, int, int,
void (*callback)(ev_context *, void *), void *);
int ev_register_cron(ev_context *, void (*callback)(ev_context *, void *),
void *, long long, long long);
/*
* Register a new event for the next loop cycle to a FD. Equal to ev_watch_fd
* but allow to carry an event object for the next cycle.
*/
int ev_fire_event(ev_context *, int, int,
void (*callback)(ev_context *, void *), void *);
#ifdef EV_SOURCE
#ifndef EV_SOURCE_ONCE
#define EV_SOURCE_ONCE
#if defined(EPOLL)
/*
* =========================
* Epoll backend functions
* =========================
*
* The epoll_api structure contains the epoll fd and the events array needed to
* wait on events with epoll_wait(2) blocking call. It's the best multiplexing
* IO api available on Linux systems and thus the optimal choice.
*/
#include <sys/epoll.h>
struct epoll_api {
int fd;
struct epoll_event *events;
};
/*
* Epoll management function, register a file descriptor to an EPOLL
* descriptor, to be monitored for read/write events
*/
static int epoll_add(int efd, int fd, int evs, void *data) {
struct epoll_event ev;
ev.data.fd = fd;
// Being ev.data a union, in case of data != NULL, fd will be set to random
if (data) ev.data.ptr = data;
ev.events = evs | EPOLLHUP | EPOLLERR;
return epoll_ctl(efd, EPOLL_CTL_ADD, fd, &ev);
}
/*
* Modify an epoll-monitored descriptor, can be set to EPOLLIN for read and
* EPOLLOUT for write
*/
static int epoll_mod(int efd, int fd, int evs, void *data) {
struct epoll_event ev;
ev.data.fd = fd;
// Being ev.data a union, in case of data != NULL, fd will be set to random
if (data) ev.data.ptr = data;
ev.events = evs | EPOLLHUP | EPOLLERR;
return epoll_ctl(efd, EPOLL_CTL_MOD, fd, &ev);
}
/*
* Remove a descriptor from an epoll descriptor, making it no-longer monitored
* for events
*/
static int epoll_del(int efd, int fd) {
return epoll_ctl(efd, EPOLL_CTL_DEL, fd, NULL);
}
static int ev_api_init(ev_context *ctx, int events_nr) {
struct epoll_api *e_api = malloc(sizeof(*e_api));
if (!e_api) return EV_OOM;
e_api->fd = epoll_create1(0);
e_api->events = calloc(events_nr, sizeof(struct epoll_event));
ctx->api = e_api;
ctx->maxfd = events_nr;
return EV_OK;
}
static void ev_api_destroy(ev_context *ctx) {
close(((struct epoll_api *)ctx->api)->fd);
free(((struct epoll_api *)ctx->api)->events);
free(ctx->api);
}
static int ev_api_get_event_type(ev_context *ctx, int idx) {
struct epoll_api *e_api = ctx->api;
int events = e_api->events[idx].events;
int ev_mask = ctx->events_monitored[e_api->events[idx].data.fd].mask;
// We want to remember the previous events only if they're not of type
// CLOSE or TIMER
int mask = ev_mask & (EV_CLOSEFD | EV_TIMERFD) ? ev_mask : EV_NONE;
if (events & (EPOLLERR | EPOLLHUP | EPOLLRDHUP)) mask |= EV_DISCONNECT;
if (events & EPOLLIN) mask |= EV_READ;
if (events & EPOLLOUT) mask |= EV_WRITE;
return mask;
}
static int ev_api_poll(ev_context *ctx, time_t timeout) {
struct epoll_api *e_api = ctx->api;
return epoll_wait(e_api->fd, e_api->events, ctx->events_nr, timeout);
}
static int ev_api_watch_fd(ev_context *ctx, int fd) {
struct epoll_api *e_api = ctx->api;
return epoll_add(e_api->fd, fd, EPOLLIN, NULL);
}
static int ev_api_del_fd(ev_context *ctx, int fd) {
struct epoll_api *e_api = ctx->api;
return epoll_del(e_api->fd, fd);
}
static int ev_api_register_event(ev_context *ctx, int fd, int mask) {
struct epoll_api *e_api = ctx->api;
int op = 0;
if (mask & EV_READ) op |= EPOLLIN;
if (mask & EV_WRITE) op |= EPOLLOUT;
return epoll_add(e_api->fd, fd, op, NULL);
}
static int ev_api_fire_event(ev_context *ctx, int fd, int mask) {
struct epoll_api *e_api = ctx->api;
int op = 0;
if (mask & EV_READ) op |= EPOLLIN;
if (mask & EV_WRITE) op |= EPOLLOUT;
if (mask & EV_EVENTFD) return epoll_add(e_api->fd, fd, op, NULL);
return epoll_mod(e_api->fd, fd, op, NULL);
}
/*
* Get the event on the idx position inside the events map. The event can also
* be an unset one (EV_NONE)
*/
static inline struct ev *ev_api_fetch_event(const ev_context *ctx, int idx,
int mask) {
(void)mask; // silence the compiler warning
int fd = ((struct epoll_api *)ctx->api)->events[idx].data.fd;
return ctx->events_monitored + fd;
}
#elif defined(POLL)
/*
* =========================
* Poll backend functions
* =========================
*
* The poll_api structure contains the number of fds to monitor and the array
* of pollfd structures associated. This number must be adjusted everytime a
* client disconnect or a new connection have an fd > nfds to avoid iterating
* over closed fds every time a new event is triggered.
* As select, poll iterate linearly over all the triggered events, without the
* hard limit of 1024 connections. It's the second best option available if no
* epoll or kqueue for Mac OSX are not present.
*/
#include <poll.h>
struct poll_api {
int nfds;
int events_monitored;
struct pollfd *fds;
};
static int ev_api_init(ev_context *ctx, int events_nr) {
struct poll_api *p_api = malloc(sizeof(*p_api));
if (!p_api) return EV_OOM;
p_api->nfds = 0;
p_api->fds = calloc(events_nr, sizeof(struct pollfd));
p_api->events_monitored = events_nr;
ctx->api = p_api;
ctx->maxfd = events_nr;
return EV_OK;
}
static void ev_api_destroy(ev_context *ctx) {
free(((struct poll_api *)ctx->api)->fds);
free(ctx->api);
}
static int ev_api_get_event_type(ev_context *ctx, int idx) {
struct poll_api *p_api = ctx->api;
int ev_mask = ctx->events_monitored[p_api->fds[idx].fd].mask;
// We want to remember the previous events only if they're not of type
// CLOSE or TIMER
int mask = ev_mask & (EV_CLOSEFD | EV_TIMERFD) ? ev_mask : 0;
if (p_api->fds[idx].revents & (POLLHUP | POLLERR)) mask |= EV_DISCONNECT;
if (p_api->fds[idx].revents & POLLIN) mask |= EV_READ;
if (p_api->fds[idx].revents & POLLOUT) mask |= EV_WRITE;
return mask;
}
static int ev_api_poll(ev_context *ctx, time_t timeout) {
struct poll_api *p_api = ctx->api;
int err = poll(p_api->fds, p_api->nfds, timeout);
if (err < 0) return EV_ERR;
return p_api->nfds;
}
/*
* Poll maintains in his state the number of file descriptors it monitors in a
* fixed size array just like the events we monitor over the primitive. If a
* resize is needed due to the number of fds have reached the length of the fds
* array, we must increase its size.
*/
static int ev_api_watch_fd(ev_context *ctx, int fd) {
struct poll_api *p_api = ctx->api;
p_api->fds[p_api->nfds].fd = fd;
p_api->fds[p_api->nfds].events = POLLIN;
p_api->nfds++;
if (p_api->nfds >= p_api->events_monitored) {
p_api->events_monitored *= 2;
p_api->fds = realloc(p_api->fds,
p_api->events_monitored * sizeof(struct pollfd));
}
return EV_OK;
}
static int ev_api_del_fd(ev_context *ctx, int fd) {
struct poll_api *p_api = ctx->api;
for (int i = 0; i < p_api->nfds; ++i) {
if (p_api->fds[i].fd == fd) {
p_api->fds[i].fd = -1;
p_api->fds[i].events = 0;
// Resize fds array
for (int j = i; j < p_api->nfds - 1; ++j)
p_api->fds[j].fd = p_api->fds[j + 1].fd;
p_api->nfds--;
break;
}
}
return EV_OK;
}
/*
* We have to check for resize even here just like ev_api_watch_fd.
*/
static int ev_api_register_event(ev_context *ctx, int fd, int mask) {
struct poll_api *p_api = ctx->api;
p_api->fds[p_api->nfds].fd = fd;
if (mask & EV_READ) p_api->fds[p_api->nfds].events |= POLLIN;
if (mask & EV_WRITE) p_api->fds[p_api->nfds].events |= POLLOUT;
p_api->nfds++;
if (p_api->nfds >= p_api->events_monitored) {
p_api->events_monitored *= 2;
p_api->fds = realloc(p_api->fds,
p_api->events_monitored * sizeof(struct pollfd));
}
return EV_OK;
}
static int ev_api_fire_event(ev_context *ctx, int fd, int mask) {
struct poll_api *p_api = ctx->api;
for (int i = 0; i < p_api->nfds; ++i) {
if (p_api->fds[i].fd == fd) {
p_api->fds[i].events = mask & EV_READ ? POLLIN : POLLOUT;
break;
}
}
return EV_OK;
}
/*
* Get the event on the idx position inside the events map. The event can also
* be an unset one (EV_NONE)
*/
static inline struct ev *ev_api_fetch_event(const ev_context *ctx, int idx,
int mask) {
return ctx->events_monitored + ((struct poll_api *)ctx->api)->fds[idx].fd;
}
#elif defined(SELECT)
/*
* ==========================
* Select backend functions
* ==========================
*
* The select_api structure contains two copies of the read/write fdset, it's
* a measure to reset the original monitored fd_set after each select(2) call
* as it's not safe to iterate over the already selected sets, select(2) make
* side-effects on the passed in fd_sets.
* At each new event all the monitored fd_set are iterated and check for read
* or write readiness; the number of monitored sockets is hard-capped at 1024.
* It's the oldest multiplexing IO and practically obiquitous, making it the
* perfect fallback for every system.
*/
// Maximum number of monitorable descriptors on select
#define SELECT_FDS_HARDCAP 1024
struct select_api {
fd_set rfds, wfds;
// Copy of the original fdset arrays to re-initialize them after each cycle
// we'll call them "service" fdset
fd_set _rfds, _wfds;
};
static int ev_api_init(ev_context *ctx, int events_nr) {
/*
* fd_set is an array of 32 i32 and each FD is represented by a bit so
* 32 x 32 = 1024 as hard limit
*/
assert(events_nr <= SELECT_FDS_HARDCAP);
struct select_api *s_api = malloc(sizeof(*s_api));
if (!s_api) return EV_OOM;
FD_ZERO(&s_api->rfds);
FD_ZERO(&s_api->wfds);
ctx->api = s_api;
ctx->maxfd = 0;
return EV_OK;
}
static void ev_api_destroy(ev_context *ctx) { free(ctx->api); }
static int ev_api_get_event_type(ev_context *ctx, int idx) {
struct select_api *s_api = ctx->api;
int ev_mask = ctx->events_monitored[idx].mask;
// We want to remember the previous events only if they're not of type
// CLOSE or TIMER
int mask = ev_mask & (EV_CLOSEFD | EV_TIMERFD) ? ev_mask : 0;
/*
* Select checks all FDs by looping to the highest registered FD it
* currently monitor. Even non set or non monitored FDs are inspected, we
* have to ensure that the FD is currently ready for IO, otherwise we'll
* end up looping all FDs and calling callbacks everytime, even when
* there's no need to.
*
* Also we have to check for ready FDs on "service" _fdsets, cause they're
* the ones employed on the select call to avoid side-effects on the
* originals.
*/
if (!FD_ISSET(idx, &s_api->_rfds) && !FD_ISSET(idx, &s_api->_wfds))
return EV_NONE;
if (FD_ISSET(idx, &s_api->_rfds)) mask |= EV_READ;
if (FD_ISSET(idx, &s_api->_wfds)) mask |= EV_WRITE;
return mask;
}
static int ev_api_poll(ev_context *ctx, time_t timeout) {
struct timeval *tv =
timeout > 0 ? &(struct timeval){0, timeout * 1000} : NULL;
struct select_api *s_api = ctx->api;
// Re-initialize fdset arrays cause select call side-effect the originals
memcpy(&s_api->_rfds, &s_api->rfds, sizeof(fd_set));
memcpy(&s_api->_wfds, &s_api->wfds, sizeof(fd_set));
int err = select(ctx->maxfd + 1, &s_api->_rfds, &s_api->_wfds, NULL, tv);
if (err < 0) return EV_ERR;
return ctx->maxfd + 1;
}
static int ev_api_watch_fd(ev_context *ctx, int fd) {
struct select_api *s_api = ctx->api;
FD_SET(fd, &s_api->rfds);
// Check for a possible new max fd, we don't want to miss events on FDs
if (fd > ctx->maxfd) ctx->maxfd = fd;
return EV_OK;
}
static int ev_api_del_fd(ev_context *ctx, int fd) {
struct select_api *s_api = ctx->api;
if (FD_ISSET(fd, &s_api->rfds)) FD_CLR(fd, &s_api->rfds);
if (FD_ISSET(fd, &s_api->wfds)) FD_CLR(fd, &s_api->wfds);
/*
* To remove FD from select we must determine the new maximum descriptor
* value based on the bits that are still turned on in the rfds set.
*/
if (fd == ctx->maxfd) {
while (!FD_ISSET(ctx->maxfd, &s_api->rfds) &&
!FD_ISSET(ctx->maxfd, &s_api->wfds))
ctx->maxfd -= 1;
}
return EV_OK;
}
static int ev_api_register_event(ev_context *ctx, int fd, int mask) {
struct select_api *s_api = ctx->api;
if (mask & EV_READ) FD_SET(fd, &s_api->rfds);
if (mask & EV_WRITE) FD_SET(fd, &s_api->wfds);
// Check for a possible new max fd, we don't want to miss events on FDs
if (fd > ctx->maxfd) ctx->maxfd = fd;
return EV_OK;
}
static int ev_api_fire_event(ev_context *ctx, int fd, int mask) {
struct select_api *s_api = ctx->api;
if (mask & EV_READ) FD_SET(fd, &s_api->rfds);
if (mask & EV_WRITE) FD_SET(fd, &s_api->wfds);
return EV_OK;
}
/*
* Get the event on the idx position inside the events map. The event can also
* be an unset one (EV_NONE)
*/
static inline struct ev *ev_api_fetch_event(const ev_context *ctx, int idx,
int mask) {
return ctx->events_monitored + idx;
}
#elif defined(KQUEUE)
/*
* ==========================
* Kqueue backend functions
* ==========================
*
* The Epoll counterpart on BSD systems, including Mac OSX, it's the older of
* the two Mux IO implementations and on par in terms of performances, a bit
* more versatile as it's possible to schedule timers directly as events
* instead of relying on support mechanisms like `timerfd` on linux.
*/
#include <sys/event.h>
#include <sys/time.h>
#include <sys/types.h>
struct kqueue_api {
int fd;
struct kevent *events;
};
static int ev_api_init(ev_context *ctx, int events_nr) {
struct kqueue_api *k_api = malloc(sizeof(*k_api));
if (!k_api) return EV_OOM;
k_api->fd = kqueue();
k_api->events = calloc(events_nr, sizeof(struct kevent));
ctx->api = k_api;
ctx->maxfd = events_nr;
return EV_OK;
}
static void ev_api_destroy(ev_context *ctx) {
close(((struct kqueue_api *)ctx->api)->fd);
free(((struct kqueue_api *)ctx->api)->events);
free(ctx->api);
}
static int ev_api_get_event_type(ev_context *ctx, int idx) {
struct kqueue_api *k_api = ctx->api;
int events_flags = k_api->events[idx].flags;
int events = k_api->events[idx].filter;
int ev_mask = ctx->events_monitored[k_api->events[idx].ident].mask;
// We want to remember the previous events only if they're not of type
// CLOSE or TIMER
int mask = ev_mask & (EV_CLOSEFD | EV_TIMERFD) ? ev_mask : EV_NONE;
if (events_flags & (EV_EOF | EV_ERROR)) mask |= EV_DISCONNECT;
if (events == EVFILT_READ) mask |= EV_READ;
if (events == EVFILT_WRITE) mask |= EV_WRITE;
return mask;
}
static int ev_api_poll(ev_context *ctx, time_t timeout) {
struct kqueue_api *k_api = ctx->api;
struct timespec ts_timeout;
ts_timeout.tv_sec = timeout;
ts_timeout.tv_nsec = 0;
int err = kevent(k_api->fd, NULL, 0, k_api->events, ctx->maxevents,
timeout > 0 ? &ts_timeout : NULL);
if (err < 0) return EV_ERR;
return err;
}
static int ev_api_del_fd(ev_context *ctx, int fd) {
struct kqueue_api *k_api = ctx->api;
struct kevent ke;
int ev_mask = ctx->events_monitored[fd].mask;
int mask = 0;
if (ev_mask & EV_READ) mask |= EVFILT_READ;
if (ev_mask & EV_WRITE) mask |= EVFILT_WRITE;
if (ev_mask & EV_TIMERFD) mask |= EVFILT_TIMER;
EV_SET(&ke, fd, mask, EV_DELETE, 0, 0, NULL);
if (kevent(k_api->fd, &ke, 1, NULL, 0, NULL) == -1) return EV_ERR;
return EV_OK;
}
static int ev_api_register_event(ev_context *ctx, int fd, int mask) {
struct kqueue_api *k_api = ctx->api;
struct kevent ke;
int op = 0;
if (mask & EV_READ) op |= EVFILT_READ;
if (mask & EV_WRITE) op |= EVFILT_WRITE;
EV_SET(&ke, fd, op, EV_ADD | EV_ONESHOT, 0, 0, NULL);
if (kevent(k_api->fd, &ke, 1, NULL, 0, NULL) == -1) return EV_ERR;
return EV_OK;
}
static int ev_api_watch_fd(ev_context *ctx, int fd) {
return ev_api_register_event(ctx, fd, EV_READ);
}
static int ev_api_fire_event(ev_context *ctx, int fd, int mask) {
struct kqueue_api *k_api = ctx->api;
struct kevent ke;
int op = 0;
if (mask & (EV_READ | EV_EVENTFD)) op |= EVFILT_READ;
if (mask & EV_WRITE) op |= EVFILT_WRITE;
EV_SET(&ke, fd, op, EV_ADD | EV_ONESHOT, 0, 0, NULL);
if (kevent(k_api->fd, &ke, 1, NULL, 0, NULL) == -1) return EV_ERR;
return EV_OK;
}
/*
* Get the event on the idx position inside the events map. The event can also
* be an unset one (EV_NONE)
*/
static inline struct ev *ev_api_fetch_event(const ev_context *ctx, int idx,
int mask) {
(void)mask; // silence compiler warning
int fd = ((struct kqueue_api *)ctx->api)->events[idx].ident;
return ctx->events_monitored + fd;
}
#endif // KQUEUE
static ev_context ev_default_ctx;
static int ev_default_ctx_inited = 0;
#ifdef __linux__
static int quit_sig;
#else
static int quit_sig[2];
#endif
// Stops epoll_wait loops by sending an event
static void ev_sigint_handler(int signum) {
(void)signum;
#ifdef __linux__
eventfd_write(quit_sig, 1);
#else
(void)write(quit_sig[0], &(unsigned long){1}, sizeof(unsigned long));
#endif
}
/*
* Eventloop stop callback, will be triggered by an EV_CLOSEFD event and stop
* the running loop, unblocking the call.
*/
static void ev_stop_callback(ev_context *ctx, void *arg) {
(void)arg;
ev_stop(ctx);
}
/*
* Process the event at the position idx in the events_monitored array. Read or
* write events can be executed on the same iteration, differentiating just
* on EV_CLOSEFD or EV_EVENTFD.
* Returns the number of fired callbacks.
*/
static int ev_process_event(ev_context *ctx, int idx, int mask) {
if (mask == EV_NONE) return EV_OK;
struct ev *e = ev_api_fetch_event(ctx, idx, mask);
int err = 0, fired = 0, fd = e->fd;
if (mask & EV_CLOSEFD) {
#ifdef __linux__
err = eventfd_read(fd, &(eventfd_t){0});
#else
err = read(fd, &(unsigned long){0}, sizeof(unsigned long));
#endif // __linux__
if (err < 0) return EV_OK;
e->rcallback(ctx, e->rdata);
++fired;
} else {
if (mask & EV_EVENTFD) {
#ifdef __linux__
err = eventfd_read(fd, &(eventfd_t){0L});
#else
err = read(fd, &(unsigned long){0}, sizeof(unsigned long));
#endif // __linux__
close(fd);
} else if (mask & EV_TIMERFD) {
err = read(fd, &(unsigned long int){0L}, sizeof(unsigned long int));
}
if (err < 0) return EV_OK;
if (mask & EV_READ) {
e->rcallback(ctx, e->rdata);
++fired;
}
if (mask & EV_WRITE) {
if (!fired || e->wcallback != e->rcallback) {
e->wcallback(ctx, e->wdata);
++fired;
}
}
}
return fired;
}
/*
* Auxiliary function, update FD, mask and data in monitored events array.
* Monitored events are the same number as the maximum FD registered in the
* context.
*/
static void ev_add_monitored(ev_context *ctx, int fd, int mask,
void (*callback)(ev_context *, void *),
void *ptr) {
/*
* TODO check for fd <= 1024 if using SELECT
* That is because FD_SETSIZE is fixed to 1024, fd_set is an array of 32
* i32 and each FD is represented by a bit so 32 x 32 = 1024 as hard limit
*/
if (fd >= ctx->maxevents) {
int i = ctx->maxevents;
ctx->maxevents = fd;
if (fd >= ctx->events_nr) {
ctx->events_monitored =
realloc(ctx->events_monitored, (fd + 1) * sizeof(struct ev));
for (; i < ctx->maxevents; ++i)
ctx->events_monitored[i].mask = EV_NONE;
}
}
ctx->events_monitored[fd].fd = fd;
ctx->events_monitored[fd].mask |= mask;
if (mask & EV_READ) {
ctx->events_monitored[fd].rdata = ptr;
ctx->events_monitored[fd].rcallback = callback;
}
if (mask & EV_WRITE) {
ctx->events_monitored[fd].wdata = ptr;
ctx->events_monitored[fd].wcallback = callback;
}
}
static inline int ev_get_event_type(ev_context *ctx, int idx) {
return ev_api_get_event_type(ctx, idx);
}
ev_context *ev_get_context(void) {
if (ev_default_ctx_inited == 0) {
#ifdef __linux__
quit_sig = eventfd(0, EFD_NONBLOCK);
#else
pipe(quit_sig);
#endif
signal(SIGINT, ev_sigint_handler);
signal(SIGTERM, ev_sigint_handler);
int err = ev_init(&ev_default_ctx, EVENTLOOP_MAX_EVENTS);
if (err < EV_OK) return NULL;
#ifdef __linux__
ev_register_event(&ev_default_ctx, quit_sig, EV_CLOSEFD | EV_READ,
ev_stop_callback, NULL);
#else
ev_register_event(&ev_default_ctx, quit_sig[1], EV_CLOSEFD | EV_READ,
ev_stop_callback, NULL);
#endif
ev_default_ctx_inited = 1;
}
return &ev_default_ctx;
}
int ev_init(ev_context *ctx, int events_nr) {
int err = ev_api_init(ctx, events_nr);
if (err < EV_OK) return err;
ctx->stop = 0;
ctx->fired_events = 0;
ctx->is_running = 0;
ctx->maxevents = events_nr;
ctx->events_nr = events_nr;
ctx->events_monitored = calloc(events_nr, sizeof(struct ev));
return EV_OK;
}
int ev_is_running(const ev_context *ctx) { return ctx->is_running; }
void ev_destroy(ev_context *ctx) {
for (int i = 0; i < ctx->maxevents; ++i) {
if (!(ctx->events_monitored[i].mask & EV_CLOSEFD) &&
ctx->events_monitored[i].mask != EV_NONE)
ev_del_fd(ctx, ctx->events_monitored[i].fd);
}
ctx->is_running = 0;
free(ctx->events_monitored);
ev_api_destroy(ctx);
}
/*
* Poll an event context for events, accepts a timeout or block forever,
* returning only when a list of FDs are ready to either READ, WRITE or TIMER
* to be executed.
*/
int ev_poll(ev_context *ctx, time_t timeout) {
return ev_api_poll(ctx, timeout);
}
/*
* Blocks forever in a loop polling for events with ev_poll calls. At every
* cycle executes callbacks registered with each event
*/
int ev_run(ev_context *ctx) {
int n = 0, events = 0;
/*
* Start an infinite loop, can be stopped only by scheduling an ev_stop
* callback or if an error on the underlying backend occur
*/
ctx->is_running = 1;
while (!ctx->stop) {
/*
* blocks polling for events, -1 means forever. Returns only in
* case of valid events ready to be processed or errors
*/
n = ev_poll(ctx, EVENTLOOP_TIMEOUT);
if (n < 0) {
/* Signals to all threads. Ignore it for now */
if (errno == EINTR) continue;
/* Error occured, break the loop */
break;
}
for (int i = 0; i < n; ++i) {
events = ev_get_event_type(ctx, i);
ctx->fired_events += ev_process_event(ctx, i, events);
}
}
return n;
}
/*
* Trigger a stop on a running event, it's meant to be run as an event in a
* running ev_ctx
*/
void ev_stop(ev_context *ctx) {
ctx->is_running = 0;
ctx->stop = 1;
}
/*
* Add a single FD to the underlying backend of the event loop. Equal to
* ev_fire_event just without an event to be carried. Useful to add simple
* descritors like a listening socket o message queue FD.
*/
int ev_watch_fd(ev_context *ctx, int fd, int mask) {
ev_add_monitored(ctx, fd, mask, NULL, NULL);
return ev_api_watch_fd(ctx, fd);
}
/*
* Remove a FD from the loop, even tho a close syscall is sufficient to remove
* the FD from the underlying backend such as EPOLL/SELECT, this call ensure
* that any associated events is cleaned out an set to EV_NONE
*/
int ev_del_fd(ev_context *ctx, int fd) {
memset(ctx->events_monitored + fd, 0x00, sizeof(struct ev));
return ev_api_del_fd(ctx, fd);
}
/*
* Register a new event, semantically it's equal to ev_fire_event but
* it's meant to be used when an FD is not already watched by the event loop.
* It could be easily integrated in ev_fire_event call but I prefer maintain
* the samantic separation of responsibilities.
*
* Set a callback and an argument to be passed to for the next loop cycle,