… | |
… | |
46 | #include <sys/types.h> |
46 | #include <sys/types.h> |
47 | #include <sys/wait.h> |
47 | #include <sys/wait.h> |
48 | #include <sys/time.h> |
48 | #include <sys/time.h> |
49 | #include <time.h> |
49 | #include <time.h> |
50 | |
50 | |
|
|
51 | /**/ |
|
|
52 | |
51 | #ifndef EV_USE_MONOTONIC |
53 | #ifndef EV_USE_MONOTONIC |
52 | # define EV_USE_MONOTONIC 1 |
54 | # define EV_USE_MONOTONIC 1 |
53 | #endif |
55 | #endif |
|
|
56 | |
|
|
57 | #ifndef EV_USE_SELECT |
|
|
58 | # define EV_USE_SELECT 1 |
|
|
59 | #endif |
|
|
60 | |
|
|
61 | #ifndef EV_USE_POLL |
|
|
62 | # define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */ |
|
|
63 | #endif |
|
|
64 | |
|
|
65 | #ifndef EV_USE_EPOLL |
|
|
66 | # define EV_USE_EPOLL 0 |
|
|
67 | #endif |
|
|
68 | |
|
|
69 | #ifndef EV_USE_KQUEUE |
|
|
70 | # define EV_USE_KQUEUE 0 |
|
|
71 | #endif |
|
|
72 | |
|
|
73 | #ifndef EV_USE_REALTIME |
|
|
74 | # define EV_USE_REALTIME 1 |
|
|
75 | #endif |
|
|
76 | |
|
|
77 | /**/ |
54 | |
78 | |
55 | #ifndef CLOCK_MONOTONIC |
79 | #ifndef CLOCK_MONOTONIC |
56 | # undef EV_USE_MONOTONIC |
80 | # undef EV_USE_MONOTONIC |
57 | # define EV_USE_MONOTONIC 0 |
81 | # define EV_USE_MONOTONIC 0 |
58 | #endif |
82 | #endif |
59 | |
83 | |
60 | #ifndef EV_USE_SELECT |
|
|
61 | # define EV_USE_SELECT 1 |
|
|
62 | #endif |
|
|
63 | |
|
|
64 | #ifndef EV_USE_EPOLL |
|
|
65 | # define EV_USE_EPOLL 0 |
|
|
66 | #endif |
|
|
67 | |
|
|
68 | #ifndef CLOCK_REALTIME |
84 | #ifndef CLOCK_REALTIME |
|
|
85 | # undef EV_USE_REALTIME |
69 | # define EV_USE_REALTIME 0 |
86 | # define EV_USE_REALTIME 0 |
70 | #endif |
87 | #endif |
71 | #ifndef EV_USE_REALTIME |
88 | |
72 | # define EV_USE_REALTIME 1 /* posix requirement, but might be slower */ |
89 | /**/ |
73 | #endif |
|
|
74 | |
90 | |
75 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
91 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
76 | #define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detetc time jumps) */ |
92 | #define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */ |
77 | #define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ |
93 | #define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ |
78 | #define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */ |
94 | /*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ |
79 | |
95 | |
80 | #include "ev.h" |
96 | #include "ev.h" |
|
|
97 | |
|
|
98 | #if __GNUC__ >= 3 |
|
|
99 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
100 | # define inline inline |
|
|
101 | #else |
|
|
102 | # define expect(expr,value) (expr) |
|
|
103 | # define inline static |
|
|
104 | #endif |
|
|
105 | |
|
|
106 | #define expect_false(expr) expect ((expr) != 0, 0) |
|
|
107 | #define expect_true(expr) expect ((expr) != 0, 1) |
|
|
108 | |
|
|
109 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
|
|
110 | #define ABSPRI(w) ((w)->priority - EV_MINPRI) |
81 | |
111 | |
82 | typedef struct ev_watcher *W; |
112 | typedef struct ev_watcher *W; |
83 | typedef struct ev_watcher_list *WL; |
113 | typedef struct ev_watcher_list *WL; |
84 | typedef struct ev_watcher_time *WT; |
114 | typedef struct ev_watcher_time *WT; |
85 | |
115 | |
86 | static ev_tstamp now, diff; /* monotonic clock */ |
116 | static ev_tstamp now_floor, now, diff; /* monotonic clock */ |
87 | ev_tstamp ev_now; |
117 | ev_tstamp ev_now; |
88 | int ev_method; |
118 | int ev_method; |
89 | |
119 | |
90 | static int have_monotonic; /* runtime */ |
120 | static int have_monotonic; /* runtime */ |
91 | |
121 | |
… | |
… | |
111 | |
141 | |
112 | static ev_tstamp |
142 | static ev_tstamp |
113 | get_clock (void) |
143 | get_clock (void) |
114 | { |
144 | { |
115 | #if EV_USE_MONOTONIC |
145 | #if EV_USE_MONOTONIC |
116 | if (have_monotonic) |
146 | if (expect_true (have_monotonic)) |
117 | { |
147 | { |
118 | struct timespec ts; |
148 | struct timespec ts; |
119 | clock_gettime (CLOCK_MONOTONIC, &ts); |
149 | clock_gettime (CLOCK_MONOTONIC, &ts); |
120 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
150 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
121 | } |
151 | } |
… | |
… | |
125 | } |
155 | } |
126 | |
156 | |
127 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
157 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
128 | |
158 | |
129 | #define array_needsize(base,cur,cnt,init) \ |
159 | #define array_needsize(base,cur,cnt,init) \ |
130 | if ((cnt) > cur) \ |
160 | if (expect_false ((cnt) > cur)) \ |
131 | { \ |
161 | { \ |
132 | int newcnt = cur; \ |
162 | int newcnt = cur; \ |
133 | do \ |
163 | do \ |
134 | { \ |
164 | { \ |
135 | newcnt = array_roundsize (base, newcnt << 1); \ |
165 | newcnt = array_roundsize (base, newcnt << 1); \ |
… | |
… | |
170 | { |
200 | { |
171 | W w; |
201 | W w; |
172 | int events; |
202 | int events; |
173 | } ANPENDING; |
203 | } ANPENDING; |
174 | |
204 | |
175 | static ANPENDING *pendings; |
205 | static ANPENDING *pendings [NUMPRI]; |
176 | static int pendingmax, pendingcnt; |
206 | static int pendingmax [NUMPRI], pendingcnt [NUMPRI]; |
177 | |
207 | |
178 | static void |
208 | static void |
179 | event (W w, int events) |
209 | event (W w, int events) |
180 | { |
210 | { |
181 | if (w->pending) |
211 | if (w->pending) |
182 | { |
212 | { |
183 | pendings [w->pending - 1].events |= events; |
213 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
184 | return; |
214 | return; |
185 | } |
215 | } |
186 | |
216 | |
187 | w->pending = ++pendingcnt; |
217 | w->pending = ++pendingcnt [ABSPRI (w)]; |
188 | array_needsize (pendings, pendingmax, pendingcnt, ); |
218 | array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], ); |
189 | pendings [pendingcnt - 1].w = w; |
219 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
190 | pendings [pendingcnt - 1].events = events; |
220 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
191 | } |
221 | } |
192 | |
222 | |
193 | static void |
223 | static void |
194 | queue_events (W *events, int eventcnt, int type) |
224 | queue_events (W *events, int eventcnt, int type) |
195 | { |
225 | { |
… | |
… | |
258 | ++fdchangecnt; |
288 | ++fdchangecnt; |
259 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
289 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
260 | fdchanges [fdchangecnt - 1] = fd; |
290 | fdchanges [fdchangecnt - 1] = fd; |
261 | } |
291 | } |
262 | |
292 | |
|
|
293 | static void |
|
|
294 | fd_kill (int fd) |
|
|
295 | { |
|
|
296 | struct ev_io *w; |
|
|
297 | |
|
|
298 | printf ("killing fd %d\n", fd);//D |
|
|
299 | while ((w = anfds [fd].head)) |
|
|
300 | { |
|
|
301 | ev_io_stop (w); |
|
|
302 | event ((W)w, EV_ERROR | EV_READ | EV_WRITE); |
|
|
303 | } |
|
|
304 | } |
|
|
305 | |
263 | /* called on EBADF to verify fds */ |
306 | /* called on EBADF to verify fds */ |
264 | static void |
307 | static void |
265 | fd_recheck (void) |
308 | fd_ebadf (void) |
266 | { |
309 | { |
267 | int fd; |
310 | int fd; |
268 | |
311 | |
269 | for (fd = 0; fd < anfdmax; ++fd) |
312 | for (fd = 0; fd < anfdmax; ++fd) |
270 | if (anfds [fd].events) |
313 | if (anfds [fd].events) |
271 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
314 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
272 | while (anfds [fd].head) |
315 | fd_kill (fd); |
|
|
316 | } |
|
|
317 | |
|
|
318 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
|
|
319 | static void |
|
|
320 | fd_enomem (void) |
|
|
321 | { |
|
|
322 | int fd = anfdmax; |
|
|
323 | |
|
|
324 | while (fd--) |
|
|
325 | if (anfds [fd].events) |
273 | { |
326 | { |
274 | ev_io_stop (anfds [fd].head); |
327 | close (fd); |
275 | event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE); |
328 | fd_kill (fd); |
|
|
329 | return; |
276 | } |
330 | } |
277 | } |
331 | } |
278 | |
332 | |
279 | /*****************************************************************************/ |
333 | /*****************************************************************************/ |
280 | |
334 | |
281 | static struct ev_timer **timers; |
335 | static struct ev_timer **timers; |
… | |
… | |
432 | } |
486 | } |
433 | } |
487 | } |
434 | |
488 | |
435 | /*****************************************************************************/ |
489 | /*****************************************************************************/ |
436 | |
490 | |
|
|
491 | #if EV_USE_KQUEUE |
|
|
492 | # include "ev_kqueue.c" |
|
|
493 | #endif |
437 | #if EV_USE_EPOLL |
494 | #if EV_USE_EPOLL |
438 | # include "ev_epoll.c" |
495 | # include "ev_epoll.c" |
439 | #endif |
496 | #endif |
|
|
497 | #if EV_USE_POLL |
|
|
498 | # include "ev_poll.c" |
|
|
499 | #endif |
440 | #if EV_USE_SELECT |
500 | #if EV_USE_SELECT |
441 | # include "ev_select.c" |
501 | # include "ev_select.c" |
442 | #endif |
502 | #endif |
443 | |
503 | |
444 | int |
504 | int |
… | |
… | |
451 | ev_version_minor (void) |
511 | ev_version_minor (void) |
452 | { |
512 | { |
453 | return EV_VERSION_MINOR; |
513 | return EV_VERSION_MINOR; |
454 | } |
514 | } |
455 | |
515 | |
|
|
516 | /* return true if we are running with elevated privileges and ignore env variables */ |
|
|
517 | static int |
|
|
518 | enable_secure () |
|
|
519 | { |
|
|
520 | return getuid () != geteuid () |
|
|
521 | || getgid () != getegid (); |
|
|
522 | } |
|
|
523 | |
456 | int ev_init (int flags) |
524 | int ev_init (int methods) |
457 | { |
525 | { |
458 | if (!ev_method) |
526 | if (!ev_method) |
459 | { |
527 | { |
460 | #if EV_USE_MONOTONIC |
528 | #if EV_USE_MONOTONIC |
461 | { |
529 | { |
… | |
… | |
463 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
531 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
464 | have_monotonic = 1; |
532 | have_monotonic = 1; |
465 | } |
533 | } |
466 | #endif |
534 | #endif |
467 | |
535 | |
468 | ev_now = ev_time (); |
536 | ev_now = ev_time (); |
469 | now = get_clock (); |
537 | now = get_clock (); |
|
|
538 | now_floor = now; |
470 | diff = ev_now - now; |
539 | diff = ev_now - now; |
471 | |
540 | |
472 | if (pipe (sigpipe)) |
541 | if (pipe (sigpipe)) |
473 | return 0; |
542 | return 0; |
474 | |
543 | |
|
|
544 | if (methods == EVMETHOD_AUTO) |
|
|
545 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
|
|
546 | methods = atoi (getenv ("LIBEV_METHODS")); |
|
|
547 | else |
475 | ev_method = EVMETHOD_NONE; |
548 | methods = EVMETHOD_ANY; |
|
|
549 | |
|
|
550 | ev_method = 0; |
|
|
551 | #if EV_USE_KQUEUE |
|
|
552 | if (!ev_method && (methods & EVMETHOD_KQUEUE)) kqueue_init (methods); |
|
|
553 | #endif |
476 | #if EV_USE_EPOLL |
554 | #if EV_USE_EPOLL |
477 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
555 | if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods); |
|
|
556 | #endif |
|
|
557 | #if EV_USE_POLL |
|
|
558 | if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods); |
478 | #endif |
559 | #endif |
479 | #if EV_USE_SELECT |
560 | #if EV_USE_SELECT |
480 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
561 | if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods); |
481 | #endif |
562 | #endif |
482 | |
563 | |
483 | if (ev_method) |
564 | if (ev_method) |
484 | { |
565 | { |
485 | ev_watcher_init (&sigev, sigcb); |
566 | ev_watcher_init (&sigev, sigcb); |
… | |
… | |
525 | /*****************************************************************************/ |
606 | /*****************************************************************************/ |
526 | |
607 | |
527 | static void |
608 | static void |
528 | call_pending (void) |
609 | call_pending (void) |
529 | { |
610 | { |
|
|
611 | int pri; |
|
|
612 | |
|
|
613 | for (pri = NUMPRI; pri--; ) |
530 | while (pendingcnt) |
614 | while (pendingcnt [pri]) |
531 | { |
615 | { |
532 | ANPENDING *p = pendings + --pendingcnt; |
616 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
533 | |
617 | |
534 | if (p->w) |
618 | if (p->w) |
535 | { |
619 | { |
536 | p->w->pending = 0; |
620 | p->w->pending = 0; |
537 | p->w->cb (p->w, p->events); |
621 | p->w->cb (p->w, p->events); |
538 | } |
622 | } |
539 | } |
623 | } |
540 | } |
624 | } |
541 | |
625 | |
542 | static void |
626 | static void |
543 | timers_reify (void) |
627 | timers_reify (void) |
544 | { |
628 | { |
… | |
… | |
604 | } |
688 | } |
605 | } |
689 | } |
606 | } |
690 | } |
607 | } |
691 | } |
608 | |
692 | |
|
|
693 | static int |
|
|
694 | time_update_monotonic (void) |
|
|
695 | { |
|
|
696 | now = get_clock (); |
|
|
697 | |
|
|
698 | if (expect_true (now - now_floor < MIN_TIMEJUMP * .5)) |
|
|
699 | { |
|
|
700 | ev_now = now + diff; |
|
|
701 | return 0; |
|
|
702 | } |
|
|
703 | else |
|
|
704 | { |
|
|
705 | now_floor = now; |
|
|
706 | ev_now = ev_time (); |
|
|
707 | return 1; |
|
|
708 | } |
|
|
709 | } |
|
|
710 | |
609 | static void |
711 | static void |
610 | time_update (void) |
712 | time_update (void) |
611 | { |
713 | { |
612 | int i; |
714 | int i; |
613 | |
715 | |
614 | ev_now = ev_time (); |
716 | #if EV_USE_MONOTONIC |
615 | |
|
|
616 | if (have_monotonic) |
717 | if (expect_true (have_monotonic)) |
617 | { |
718 | { |
|
|
719 | if (time_update_monotonic ()) |
|
|
720 | { |
618 | ev_tstamp odiff = diff; |
721 | ev_tstamp odiff = diff; |
619 | |
722 | |
620 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
723 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
621 | { |
724 | { |
622 | now = get_clock (); |
|
|
623 | diff = ev_now - now; |
725 | diff = ev_now - now; |
624 | |
726 | |
625 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
727 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
626 | return; /* all is well */ |
728 | return; /* all is well */ |
627 | |
729 | |
628 | ev_now = ev_time (); |
730 | ev_now = ev_time (); |
|
|
731 | now = get_clock (); |
|
|
732 | now_floor = now; |
629 | } |
733 | } |
630 | |
734 | |
631 | periodics_reschedule (diff - odiff); |
735 | periodics_reschedule (diff - odiff); |
632 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
736 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
|
|
737 | } |
633 | } |
738 | } |
634 | else |
739 | else |
|
|
740 | #endif |
635 | { |
741 | { |
|
|
742 | ev_now = ev_time (); |
|
|
743 | |
636 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
744 | if (expect_false (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
637 | { |
745 | { |
638 | periodics_reschedule (ev_now - now); |
746 | periodics_reschedule (ev_now - now); |
639 | |
747 | |
640 | /* adjust timers. this is easy, as the offset is the same for all */ |
748 | /* adjust timers. this is easy, as the offset is the same for all */ |
641 | for (i = 0; i < timercnt; ++i) |
749 | for (i = 0; i < timercnt; ++i) |
… | |
… | |
654 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
762 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
655 | |
763 | |
656 | do |
764 | do |
657 | { |
765 | { |
658 | /* queue check watchers (and execute them) */ |
766 | /* queue check watchers (and execute them) */ |
659 | if (preparecnt) |
767 | if (expect_false (preparecnt)) |
660 | { |
768 | { |
661 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
769 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
662 | call_pending (); |
770 | call_pending (); |
663 | } |
771 | } |
664 | |
772 | |
… | |
… | |
667 | |
775 | |
668 | /* calculate blocking time */ |
776 | /* calculate blocking time */ |
669 | |
777 | |
670 | /* we only need this for !monotonic clockor timers, but as we basically |
778 | /* we only need this for !monotonic clockor timers, but as we basically |
671 | always have timers, we just calculate it always */ |
779 | always have timers, we just calculate it always */ |
|
|
780 | #if EV_USE_MONOTONIC |
|
|
781 | if (expect_true (have_monotonic)) |
|
|
782 | time_update_monotonic (); |
|
|
783 | else |
|
|
784 | #endif |
|
|
785 | { |
672 | ev_now = ev_time (); |
786 | ev_now = ev_time (); |
|
|
787 | now = ev_now; |
|
|
788 | } |
673 | |
789 | |
674 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
790 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
675 | block = 0.; |
791 | block = 0.; |
676 | else |
792 | else |
677 | { |
793 | { |
678 | block = MAX_BLOCKTIME; |
794 | block = MAX_BLOCKTIME; |
679 | |
795 | |
680 | if (timercnt) |
796 | if (timercnt) |
681 | { |
797 | { |
682 | ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; |
798 | ev_tstamp to = timers [0]->at - now + method_fudge; |
683 | if (block > to) block = to; |
799 | if (block > to) block = to; |
684 | } |
800 | } |
685 | |
801 | |
686 | if (periodiccnt) |
802 | if (periodiccnt) |
687 | { |
803 | { |
… | |
… | |
744 | static void |
860 | static void |
745 | ev_clear_pending (W w) |
861 | ev_clear_pending (W w) |
746 | { |
862 | { |
747 | if (w->pending) |
863 | if (w->pending) |
748 | { |
864 | { |
749 | pendings [w->pending - 1].w = 0; |
865 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
750 | w->pending = 0; |
866 | w->pending = 0; |
751 | } |
867 | } |
752 | } |
868 | } |
753 | |
869 | |
754 | static void |
870 | static void |
755 | ev_start (W w, int active) |
871 | ev_start (W w, int active) |
756 | { |
872 | { |
|
|
873 | if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; |
|
|
874 | if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; |
|
|
875 | |
757 | w->active = active; |
876 | w->active = active; |
758 | } |
877 | } |
759 | |
878 | |
760 | static void |
879 | static void |
761 | ev_stop (W w) |
880 | ev_stop (W w) |