-
Notifications
You must be signed in to change notification settings - Fork 0
/
ev.c
3009 lines (2410 loc) · 77.8 KB
/
ev.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
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
/*
* libev event processing core, watcher management
*
* Copyright (c) 2007,2008 Marc Alexander Lehmann <libev@schmorp.de>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modifica-
* tion, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. 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 AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
* CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
* CIAL, 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 OTH-
* ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Alternatively, the contents of this file may be used under the terms of
* the GNU General Public License ("GPL") version 2 or any later version,
* in which case the provisions of the GPL are applicable instead of
* the above. If you wish to allow the use of your version of this file
* only under the terms of the GPL and not to allow others to use your
* version of this file under the BSD license, indicate your decision
* by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL. If you do not delete the
* provisions above, a recipient may use your version of this file under
* either the BSD or the GPL.
*/
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
#include <errno.h>
#define EV_CONFIG_H "ev_config.h"
#define EV_H "ev.h"
/* this big block deduces configuration from config.h */
#ifndef EV_STANDALONE
# ifdef EV_CONFIG_H
# include EV_CONFIG_H
# else
# include "config.h"
# endif
# if HAVE_CLOCK_SYSCALL
# ifndef EV_USE_CLOCK_SYSCALL
# define EV_USE_CLOCK_SYSCALL 1
# ifndef EV_USE_REALTIME
# define EV_USE_REALTIME 0
# endif
# ifndef EV_USE_MONOTONIC
# define EV_USE_MONOTONIC 1
# endif
# endif
# endif
# if HAVE_CLOCK_GETTIME
# ifndef EV_USE_MONOTONIC
# define EV_USE_MONOTONIC 1
# endif
# ifndef EV_USE_REALTIME
# define EV_USE_REALTIME 1
# endif
# else
# ifndef EV_USE_MONOTONIC
# define EV_USE_MONOTONIC 0
# endif
# ifndef EV_USE_REALTIME
# define EV_USE_REALTIME 0
# endif
# endif
# ifndef EV_USE_NANOSLEEP
# if HAVE_NANOSLEEP
# define EV_USE_NANOSLEEP 1
# else
# define EV_USE_NANOSLEEP 0
# endif
# endif
# ifndef EV_USE_SELECT
# if HAVE_SELECT && HAVE_SYS_SELECT_H
# define EV_USE_SELECT 1
# else
# define EV_USE_SELECT 0
# endif
# endif
# ifndef EV_USE_POLL
# if HAVE_POLL && HAVE_POLL_H
# define EV_USE_POLL 1
# else
# define EV_USE_POLL 0
# endif
# endif
# ifndef EV_USE_EPOLL
# if HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H
# define EV_USE_EPOLL 1
# else
# define EV_USE_EPOLL 0
# endif
# endif
# ifndef EV_USE_KQUEUE
# if HAVE_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H
# define EV_USE_KQUEUE 1
# else
# define EV_USE_KQUEUE 0
# endif
# endif
# ifndef EV_USE_PORT
# if HAVE_PORT_H && HAVE_PORT_CREATE
# define EV_USE_PORT 1
# else
# define EV_USE_PORT 0
# endif
# endif
# ifndef EV_USE_INOTIFY
# if HAVE_INOTIFY_INIT && HAVE_SYS_INOTIFY_H
# define EV_USE_INOTIFY 1
# else
# define EV_USE_INOTIFY 0
# endif
# endif
# ifndef EV_USE_EVENTFD
# if HAVE_EVENTFD
# define EV_USE_EVENTFD 1
# else
# define EV_USE_EVENTFD 0
# endif
# endif
#endif
#include <math.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stddef.h>
#include <assert.h>
#include <sys/types.h>
#include <time.h>
#include <signal.h>
#ifdef EV_H
# include EV_H
#else
# include "ev.h"
#endif
#ifndef _WIN32
# include <sys/time.h>
# include <sys/wait.h>
# include <unistd.h>
#else
# include <io.h>
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
# ifndef EV_SELECT_IS_WINSOCKET
# define EV_SELECT_IS_WINSOCKET 1
# endif
#endif
/* this block tries to deduce configuration from header-defined symbols and defaults */
#ifndef EV_USE_CLOCK_SYSCALL
# if __linux && __GLIBC__ >= 2
# define EV_USE_CLOCK_SYSCALL 1
# else
# define EV_USE_CLOCK_SYSCALL 0
# endif
#endif
#ifndef EV_USE_MONOTONIC
# if defined (_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0
# define EV_USE_MONOTONIC 1
# else
# define EV_USE_MONOTONIC 0
# endif
#endif
#ifndef EV_USE_REALTIME
# define EV_USE_REALTIME 0
#endif
#ifndef EV_USE_NANOSLEEP
# if _POSIX_C_SOURCE >= 199309L
# define EV_USE_NANOSLEEP 1
# else
# define EV_USE_NANOSLEEP 0
# endif
#endif
#ifndef EV_USE_SELECT
# define EV_USE_SELECT 1
#endif
#ifndef EV_USE_POLL
# ifdef _WIN32
# define EV_USE_POLL 0
# else
# define EV_USE_POLL 1
# endif
#endif
#ifndef EV_USE_EPOLL
# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 4))
# define EV_USE_EPOLL 1
# else
# define EV_USE_EPOLL 0
# endif
#endif
#ifndef EV_USE_KQUEUE
# define EV_USE_KQUEUE 0
#endif
#ifndef EV_USE_PORT
# define EV_USE_PORT 0
#endif
#ifndef EV_USE_INOTIFY
# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 4))
# define EV_USE_INOTIFY 1
# else
# define EV_USE_INOTIFY 0
# endif
#endif
#ifndef EV_PID_HASHSIZE
# if EV_MINIMAL
# define EV_PID_HASHSIZE 1
# else
# define EV_PID_HASHSIZE 16
# endif
#endif
#ifndef EV_INOTIFY_HASHSIZE
# if EV_MINIMAL
# define EV_INOTIFY_HASHSIZE 1
# else
# define EV_INOTIFY_HASHSIZE 16
# endif
#endif
#ifndef EV_USE_EVENTFD
# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7))
# define EV_USE_EVENTFD 1
# else
# define EV_USE_EVENTFD 0
# endif
#endif
#if 0 /* debugging */
# define EV_VERIFY 3
# define EV_USE_4HEAP 1
# define EV_HEAP_CACHE_AT 1
#endif
#ifndef EV_VERIFY
# define EV_VERIFY !EV_MINIMAL
#endif
#ifndef EV_USE_4HEAP
# define EV_USE_4HEAP !EV_MINIMAL
#endif
#ifndef EV_HEAP_CACHE_AT
# define EV_HEAP_CACHE_AT !EV_MINIMAL
#endif
/* this block fixes any misconfiguration where we know we run into trouble otherwise */
#ifndef CLOCK_MONOTONIC
# undef EV_USE_MONOTONIC
# define EV_USE_MONOTONIC 0
#endif
#ifndef CLOCK_REALTIME
# undef EV_USE_REALTIME
# define EV_USE_REALTIME 0
#endif
#if !EV_STAT_ENABLE
# undef EV_USE_INOTIFY
# define EV_USE_INOTIFY 0
#endif
#if !EV_USE_NANOSLEEP
# ifndef _WIN32
# include <sys/select.h>
# endif
#endif
#if EV_USE_INOTIFY
# include <sys/utsname.h>
# include <sys/statfs.h>
# include <sys/inotify.h>
/* some very old inotify.h headers don't have IN_DONT_FOLLOW */
# ifndef IN_DONT_FOLLOW
# undef EV_USE_INOTIFY
# define EV_USE_INOTIFY 0
# endif
#endif
#if EV_SELECT_IS_WINSOCKET
# include <winsock.h>
#endif
/* on linux, we can use a (slow) syscall to avoid a dependency on pthread, */
/* which makes programs even slower. might work on other unices, too. */
#if EV_USE_CLOCK_SYSCALL
# include <syscall.h>
# define clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts))
# undef EV_USE_MONOTONIC
# define EV_USE_MONOTONIC 1
#endif
#if EV_USE_EVENTFD
/* our minimum requirement is glibc 2.7 which has the stub, but not the header */
# include <stdint.h>
# ifdef __cplusplus
extern "C" {
# endif
int eventfd(unsigned int initval, int flags);
# ifdef __cplusplus
}
# endif
#endif
/**/
#if EV_VERIFY >= 3
# define EV_FREQUENT_CHECK ev_loop_verify (EV_A)
#else
# define EV_FREQUENT_CHECK do { } while (0)
#endif
/*
* This is used to avoid floating point rounding problems.
* It is added to ev_rt_now when scheduling periodics
* to ensure progress, time-wise, even when rounding
* errors are against us.
* This value is good at least till the year 4000.
* Better solutions welcome.
*/
#define TIME_EPSILON 0.0001220703125 /* 1/8192 */
#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
#define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */
/*#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) // how often to try to free memory and re-check fds, TODO */
#if __GNUC__ >= 4
# define expect(expr,value) __builtin_expect ((expr),(value))
# define noinline __attribute__ ((noinline))
#else
# define expect(expr,value) (expr)
# define noinline
# if __STDC_VERSION__ < 199901L && __GNUC__ < 2
# define inline
# endif
#endif
#define expect_false(expr) expect ((expr) != 0, 0)
#define expect_true(expr) expect ((expr) != 0, 1)
#define inline_size static __inline
#if EV_MINIMAL
# define inline_speed static __noinline
#else
# define inline_speed static __inline
#endif
#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
#define ABSPRI(w) (((W)w)->priority - EV_MINPRI)
#define EMPTY /* required for microsofts broken pseudo-c compiler */
#define EMPTY2(a,b) /* used to suppress some warnings */
typedef ev_watcher *W;
typedef ev_watcher_list *WL;
typedef ev_watcher_time *WT;
#define ev_active(w) ((W)(w))->active
#define ev_at(w) ((WT)(w))->at
#if EV_USE_MONOTONIC
/* sig_atomic_t is used to avoid per-thread variables or locking but still */
/* giving it a reasonably high chance of working on typical architetcures */
static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
#endif
#ifdef _WIN32
# include "ev_win32.c"
#endif
/*****************************************************************************/
static void (*syserr_cb) (const char *msg);
void
ev_set_syserr_cb(void (*cb) (const char *msg)) {
syserr_cb = cb;
} static void noinline ev_syserr(const char *msg) {
if (!msg)
msg = "(libev) system error";
if (syserr_cb)
syserr_cb(msg);
else {
perror(msg);
abort();
}
}
static void *ev_realloc_emul(void *ptr, long size) {
/*
* some systems, notably openbsd and darwin, fail to properly
* * implement realloc (x, 0) (as required by both ansi c-98 and
* * the single unix specification, so work around them here.
*/
if (size)
return realloc(ptr, size);
free(ptr);
return 0;
}
static void *(*alloc) (void *ptr, long size) = ev_realloc_emul;
void
ev_set_allocator(void *(*cb) (void *ptr, long size)) {
alloc = cb;
}
inline_speed void *ev_realloc(void *ptr, long size) {
ptr = alloc(ptr, size);
if (!ptr && size) {
fprintf(stderr, "libev: cannot allocate %ld bytes, aborting.", size);
abort();
}
return ptr;
}
#define ev_malloc(size) ev_realloc (0, (size))
#define ev_free(ptr) ev_realloc ((ptr), 0)
/*****************************************************************************/
typedef struct {
WL head;
unsigned char events;
unsigned char reify;
unsigned char emask; /* the epoll backend stores the actual kernel mask in here */
unsigned char unused;
#if EV_USE_EPOLL
unsigned int egen; /* generation counter to counter epoll bugs */
#endif
#if EV_SELECT_IS_WINSOCKET
SOCKET handle;
#endif
} ANFD;
typedef struct {
W w;
int events;
} ANPENDING;
#if EV_USE_INOTIFY
/* hash table entry per inotify-id */
typedef struct {
WL head;
} ANFS;
#endif
/* Heap Entry */
#if EV_HEAP_CACHE_AT
typedef struct {
ev_tstamp at;
WT w;
} ANHE;
#define ANHE_w(he) (he).w /* access watcher, read-write */
#define ANHE_at(he) (he).at /* access cached at, read-only */
#define ANHE_at_cache(he) (he).at = (he).w->at /* update at from watcher */
#else
typedef WT ANHE;
#define ANHE_w(he) (he)
#define ANHE_at(he) (he)->at
#define ANHE_at_cache(he)
#endif
#if EV_MULTIPLICITY
struct ev_loop {
ev_tstamp ev_rt_now;
#define ev_rt_now ((loop)->ev_rt_now)
#define VAR(name,decl) decl;
#include "ev_vars.h"
#undef VAR
};
#include "ev_wrap.h"
static struct ev_loop default_loop_struct;
struct ev_loop *ev_default_loop_ptr;
#else
ev_tstamp ev_rt_now;
#define VAR(name,decl) static decl;
#include "ev_vars.h"
#undef VAR
static int ev_default_loop_ptr;
#endif
/*****************************************************************************/
ev_tstamp ev_time(void) {
#if EV_USE_REALTIME
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
return ts.tv_sec + ts.tv_nsec * 1e-9;
#else
struct timeval tv;
gettimeofday(&tv, 0);
return tv.tv_sec + tv.tv_usec * 1e-6;
#endif
}
ev_tstamp inline_size get_clock(void) {
#if EV_USE_MONOTONIC
if (expect_true(have_monotonic)) {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ts.tv_sec + ts.tv_nsec * 1e-9;
}
#endif
return ev_time();
}
#if EV_MULTIPLICITY
ev_tstamp ev_now(EV_P) {
return ev_rt_now;
}
#endif
void
ev_sleep(ev_tstamp delay) {
if (delay > 0.) {
#if EV_USE_NANOSLEEP
struct timespec ts;
ts.tv_sec = (time_t) delay;
ts.tv_nsec = (long)((delay - (ev_tstamp) (ts.tv_sec)) * 1e9);
nanosleep(&ts, 0);
#elif defined(_WIN32)
Sleep((unsigned long)(delay * 1e3));
#else
struct timeval tv;
tv.tv_sec = (time_t) delay;
tv.tv_usec = (long)((delay - (ev_tstamp) (tv.tv_sec)) * 1e6);
/*
* here we rely on sys/time.h + sys/types.h + unistd.h providing select
*/
/*
* somehting nto guaranteed by newer posix versions, but guaranteed
*/
/*
* by older ones
*/
select(0, 0, 0, 0, &tv);
#endif
}
}
/*****************************************************************************/
#define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */
int inline_size array_nextsize(int elem, int cur, int cnt) {
int ncur = cur + 1;
do
ncur <<= 1;
while (cnt > ncur);
/*
* if size is large, round to MALLOC_ROUND - 4 * longs to accomodate malloc overhead
*/
if (elem * ncur > MALLOC_ROUND - sizeof(void *) * 4) {
ncur *= elem;
ncur = (ncur + elem + (MALLOC_ROUND - 1) + sizeof(void *) * 4) & ~(MALLOC_ROUND - 1);
ncur = ncur - sizeof(void *) * 4;
ncur /= elem;
}
return ncur;
}
static noinline void *array_realloc(int elem, void *base, int *cur, int cnt) {
*cur = array_nextsize(elem, *cur, cnt);
return ev_realloc(base, elem * *cur);
}
#define array_init_zero(base,count) \
memset ((void *)(base), 0, sizeof (*(base)) * (count))
#define array_needsize(type,base,cur,cnt,init) \
if (expect_false ((cnt) > (cur))) \
{ \
int ocur_ = (cur); \
(base) = (type *)array_realloc \
(sizeof (type), (base), &(cur), (cnt)); \
init ((base) + (ocur_), (cur) - ocur_); \
}
#if 0
#define array_slim(type,stem) \
if (stem ## max < array_roundsize (stem ## cnt >> 2)) \
{ \
stem ## max = array_roundsize (stem ## cnt >> 1); \
base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\
fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
}
#endif
#define array_free(stem, idx) \
ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
/*****************************************************************************/
void noinline ev_feed_event(EV_P_ void *w, int revents) {
W w_ = (W) w;
int pri = ABSPRI(w_);
if (expect_false(w_->pending))
pendings[pri][w_->pending - 1].events |= revents;
else {
w_->pending = ++pendingcnt[pri];
array_needsize(ANPENDING, pendings[pri], pendingmax[pri], w_->pending, EMPTY2);
pendings[pri][w_->pending - 1].w = w_;
pendings[pri][w_->pending - 1].events = revents;
}
}
void inline_speed queue_events(EV_P_ W * events, int eventcnt, int type) {
int i;
for (i = 0; i < eventcnt; ++i)
ev_feed_event(EV_A_ events[i], type);
}
/*****************************************************************************/
void inline_speed fd_event(EV_P_ int fd, int revents) {
ANFD *anfd = anfds + fd;
ev_io *w;
for (w = (ev_io *) anfd->head; w; w = (ev_io *) ((WL) w)->next) {
int ev = w->events & revents;
if (ev)
ev_feed_event(EV_A_(W) w, ev);
}
}
void
ev_feed_fd_event(EV_P_ int fd, int revents) {
if (fd >= 0 && fd < anfdmax)
fd_event(EV_A_ fd, revents);
}
void inline_size fd_reify(EV_P) {
int i;
for (i = 0; i < fdchangecnt; ++i) {
int fd = fdchanges[i];
ANFD *anfd = anfds + fd;
ev_io *w;
unsigned char events = 0;
for (w = (ev_io *) anfd->head; w; w = (ev_io *) ((WL) w)->next)
events |= (unsigned char)w->events;
#if EV_SELECT_IS_WINSOCKET
if (events) {
unsigned long arg;
#ifdef EV_FD_TO_WIN32_HANDLE
anfd->handle = EV_FD_TO_WIN32_HANDLE(fd);
#else
anfd->handle = _get_osfhandle(fd);
#endif
assert(("libev only supports socket fds in this configuration",
ioctlsocket(anfd->handle, FIONREAD, &arg) == 0));
}
#endif
{
unsigned char o_events = anfd->events;
unsigned char o_reify = anfd->reify;
anfd->reify = 0;
anfd->events = events;
if (o_events != events || o_reify & EV_IOFDSET)
backend_modify(EV_A_ fd, o_events, events);
}
}
fdchangecnt = 0;
}
void inline_size fd_change(EV_P_ int fd, int flags) {
unsigned char reify = anfds[fd].reify;
anfds[fd].reify |= flags;
if (expect_true(!reify)) {
++fdchangecnt;
array_needsize(int, fdchanges, fdchangemax, fdchangecnt, EMPTY2);
fdchanges[fdchangecnt - 1] = fd;
}
}
void inline_speed fd_kill(EV_P_ int fd) {
ev_io *w;
while ((w = (ev_io *) anfds[fd].head)) {
ev_io_stop(EV_A_ w);
ev_feed_event(EV_A_(W) w, EV_ERROR | EV_READ | EV_WRITE);
}
}
int inline_size fd_valid(int fd) {
#ifdef _WIN32
return _get_osfhandle(fd) != -1;
#else
return fcntl(fd, F_GETFD) != -1;
#endif
}
/* called on EBADF to verify fds */
static void noinline fd_ebadf(EV_P) {
int fd;
for (fd = 0; fd < anfdmax; ++fd)
if (anfds[fd].events)
if (!fd_valid(fd) && errno == EBADF)
fd_kill(EV_A_ fd);
}
/* called on ENOMEM in select/poll to kill some fds and retry */
static void noinline fd_enomem(EV_P) {
int fd;
for (fd = anfdmax; fd--;)
if (anfds[fd].events) {
fd_kill(EV_A_ fd);
return;
}
}
/* usually called after fork if backend needs to re-arm all fds from scratch */
static void noinline fd_rearm_all(EV_P) {
int fd;
for (fd = 0; fd < anfdmax; ++fd)
if (anfds[fd].events) {
anfds[fd].events = 0;
anfds[fd].emask = 0;
fd_change(EV_A_ fd, EV_IOFDSET | 1);
}
}
/*****************************************************************************/
/*
* the heap functions want a real array index. array index 0 uis guaranteed to not
* be in-use at any time. the first heap entry is at array [HEAP0]. DHEAP gives
* the branching factor of the d-tree.
*/
/*
* at the moment we allow libev the luxury of two heaps,
* a small-code-size 2-heap one and a ~1.5kb larger 4-heap
* which is more cache-efficient.
* the difference is about 5% with 50000+ watchers.
*/
#if EV_USE_4HEAP
#define DHEAP 4
#define HEAP0 (DHEAP - 1) /* index of first element in heap */
#define HPARENT(k) ((((k) - HEAP0 - 1) / DHEAP) + HEAP0)
#define UPHEAP_DONE(p,k) ((p) == (k))
/* away from the root */
void inline_speed downheap(ANHE * heap, int N, int k) {
ANHE he = heap[k];
ANHE *E = heap + N + HEAP0;
for (;;) {
ev_tstamp minat;
ANHE *minpos;
ANHE *pos = heap + DHEAP * (k - HEAP0) + HEAP0 + 1;
/*
* find minimum child
*/
if (expect_true(pos + DHEAP - 1 < E)) {
/*
* fast path
*/ (minpos = pos + 0),
(minat = ANHE_at(*minpos));
if (ANHE_at(pos[1]) < minat)
(minpos = pos + 1), (minat = ANHE_at(*minpos));
if (ANHE_at(pos[2]) < minat)
(minpos = pos + 2), (minat = ANHE_at(*minpos));
if (ANHE_at(pos[3]) < minat)
(minpos = pos + 3), (minat = ANHE_at(*minpos));
} else if (pos < E) {
/*
* slow path
*/ (minpos = pos + 0),
(minat = ANHE_at(*minpos));
if (pos + 1 < E && ANHE_at(pos[1]) < minat)
(minpos = pos + 1), (minat = ANHE_at(*minpos));
if (pos + 2 < E && ANHE_at(pos[2]) < minat)
(minpos = pos + 2), (minat = ANHE_at(*minpos));
if (pos + 3 < E && ANHE_at(pos[3]) < minat)
(minpos = pos + 3), (minat = ANHE_at(*minpos));
} else
break;
if (ANHE_at(he) <= minat)
break;
heap[k] = *minpos;
ev_active(ANHE_w(*minpos)) = k;
k = minpos - heap;
}
heap[k] = he;
ev_active(ANHE_w(he)) = k;
}
#else /* 4HEAP */
#define HEAP0 1
#define HPARENT(k) ((k) >> 1)
#define UPHEAP_DONE(p,k) (!(p))
/* away from the root */
void inline_speed downheap(ANHE * heap, int N, int k) {
ANHE he = heap[k];
for (;;) {
int c = k << 1;
if (c > N + HEAP0 - 1)
break;
c += c + 1 < N + HEAP0 && ANHE_at(heap[c]) > ANHE_at(heap[c + 1])
? 1 : 0;
if (ANHE_at(he) <= ANHE_at(heap[c]))
break;
heap[k] = heap[c];
ev_active(ANHE_w(heap[k])) = k;
k = c;
}
heap[k] = he;
ev_active(ANHE_w(he)) = k;
}
#endif
/* towards the root */
void inline_speed upheap(ANHE * heap, int k) {
ANHE he = heap[k];
for (;;) {
int p = HPARENT(k);
if (UPHEAP_DONE(p, k)
|| ANHE_at(heap[p]) <= ANHE_at(he))
break;
heap[k] = heap[p];
ev_active(ANHE_w(heap[k])) = k;
k = p;
}
heap[k] = he;
ev_active(ANHE_w(he)) = k;
}
void inline_size adjustheap(ANHE * heap, int N, int k) {
if (k > HEAP0 && ANHE_at(heap[HPARENT(k)]) >= ANHE_at(heap[k]))
upheap(heap, k);
else
downheap(heap, N, k);
}
/* rebuild the heap: this function is used only once and executed rarely */
void inline_size reheap(ANHE * heap, int N) {
int i;
/*
* we don't use floyds algorithm, upheap is simpler and is more cache-efficient
*/
/*
* also, this is easy to implement and correct for both 2-heaps and 4-heaps
*/
for (i = 0; i < N; ++i)
upheap(heap, i + HEAP0);
}
/*****************************************************************************/
typedef struct {
WL head;
EV_ATOMIC_T gotsig;
} ANSIG;
static ANSIG *signals;
static int signalmax;
static EV_ATOMIC_T gotsig;
/*****************************************************************************/
void inline_speed fd_intern(int fd) {
#ifdef _WIN32
unsigned long arg = 1;
ioctlsocket(_get_osfhandle(fd), FIONBIO, &arg);
#else
fcntl(fd, F_SETFD, FD_CLOEXEC);
fcntl(fd, F_SETFL, O_NONBLOCK);
#endif
}
static void noinline evpipe_init(EV_P) {
if (!ev_is_active(&pipeev)) {
#if EV_USE_EVENTFD
if ((evfd = eventfd(0, 0)) >= 0) {
evpipe[0] = -1;
fd_intern(evfd);
ev_io_set(&pipeev, evfd, EV_READ);
} else
#endif
{
while (pipe(evpipe))
ev_syserr("(libev) error creating signal/async pipe");
fd_intern(evpipe[0]);
fd_intern(evpipe[1]);
ev_io_set(&pipeev, evpipe[0], EV_READ);
}
ev_io_start(EV_A_ & pipeev);
ev_unref(EV_A); /* watcher should not keep loop alive */
}
}
void inline_size evpipe_write(EV_P_ EV_ATOMIC_T * flag) {
if (!*flag) {
int old_errno = errno; /* save errno because write might clobber it */
*flag = 1;
#if EV_USE_EVENTFD
if (evfd >= 0) {
uint64_t counter = 1;
write(evfd, &counter, sizeof(uint64_t));
} else
#endif
write(evpipe[1], &old_errno, 1);
errno = old_errno;
}
}