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