1 | /* |
1 | /* |
|
|
2 | * libev event processing core, watcher management |
|
|
3 | * |
2 | * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> |
4 | * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> |
3 | * All rights reserved. |
5 | * All rights reserved. |
4 | * |
6 | * |
5 | * Redistribution and use in source and binary forms, with or without |
7 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions are |
8 | * modification, are permitted provided that the following conditions are |
… | |
… | |
44 | #include <sys/types.h> |
46 | #include <sys/types.h> |
45 | #include <sys/wait.h> |
47 | #include <sys/wait.h> |
46 | #include <sys/time.h> |
48 | #include <sys/time.h> |
47 | #include <time.h> |
49 | #include <time.h> |
48 | |
50 | |
|
|
51 | /**/ |
|
|
52 | |
49 | #ifndef EV_USE_MONOTONIC |
53 | #ifndef EV_USE_MONOTONIC |
50 | # ifdef CLOCK_MONOTONIC |
|
|
51 | # define EV_USE_MONOTONIC 1 |
54 | # define EV_USE_MONOTONIC 1 |
52 | # endif |
|
|
53 | #endif |
55 | #endif |
54 | |
56 | |
55 | #ifndef EV_USE_SELECT |
57 | #ifndef EV_USE_SELECT |
56 | # define EV_USE_SELECT 1 |
58 | # define EV_USE_SELECT 1 |
57 | #endif |
59 | #endif |
58 | |
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 | |
59 | #ifndef EV_USE_EPOLL |
65 | #ifndef EV_USE_EPOLL |
60 | # define EV_USE_EPOLL 0 |
66 | # define EV_USE_EPOLL 0 |
61 | #endif |
67 | #endif |
62 | |
68 | |
|
|
69 | #ifndef EV_USE_REALTIME |
|
|
70 | # define EV_USE_REALTIME 1 |
|
|
71 | #endif |
|
|
72 | |
|
|
73 | /**/ |
|
|
74 | |
|
|
75 | #ifndef CLOCK_MONOTONIC |
|
|
76 | # undef EV_USE_MONOTONIC |
|
|
77 | # define EV_USE_MONOTONIC 0 |
|
|
78 | #endif |
|
|
79 | |
63 | #ifndef CLOCK_REALTIME |
80 | #ifndef CLOCK_REALTIME |
|
|
81 | # undef EV_USE_REALTIME |
64 | # define EV_USE_REALTIME 0 |
82 | # define EV_USE_REALTIME 0 |
65 | #endif |
83 | #endif |
66 | #ifndef EV_USE_REALTIME |
84 | |
67 | # define EV_USE_REALTIME 1 /* posix requirement, but might be slower */ |
85 | /**/ |
68 | #endif |
|
|
69 | |
86 | |
70 | #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) */ |
71 | #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) */ |
72 | #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 */ |
73 | #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 */ |
74 | |
91 | |
75 | #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) |
76 | |
104 | |
77 | typedef struct ev_watcher *W; |
105 | typedef struct ev_watcher *W; |
78 | typedef struct ev_watcher_list *WL; |
106 | typedef struct ev_watcher_list *WL; |
79 | typedef struct ev_watcher_time *WT; |
107 | typedef struct ev_watcher_time *WT; |
80 | |
108 | |
81 | static ev_tstamp now, diff; /* monotonic clock */ |
109 | static ev_tstamp now_floor, now, diff; /* monotonic clock */ |
82 | ev_tstamp ev_now; |
110 | ev_tstamp ev_now; |
83 | int ev_method; |
111 | int ev_method; |
84 | |
112 | |
85 | static int have_monotonic; /* runtime */ |
113 | static int have_monotonic; /* runtime */ |
86 | |
114 | |
… | |
… | |
106 | |
134 | |
107 | static ev_tstamp |
135 | static ev_tstamp |
108 | get_clock (void) |
136 | get_clock (void) |
109 | { |
137 | { |
110 | #if EV_USE_MONOTONIC |
138 | #if EV_USE_MONOTONIC |
111 | if (have_monotonic) |
139 | if (expect_true (have_monotonic)) |
112 | { |
140 | { |
113 | struct timespec ts; |
141 | struct timespec ts; |
114 | clock_gettime (CLOCK_MONOTONIC, &ts); |
142 | clock_gettime (CLOCK_MONOTONIC, &ts); |
115 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
143 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
116 | } |
144 | } |
… | |
… | |
120 | } |
148 | } |
121 | |
149 | |
122 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
150 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
123 | |
151 | |
124 | #define array_needsize(base,cur,cnt,init) \ |
152 | #define array_needsize(base,cur,cnt,init) \ |
125 | if ((cnt) > cur) \ |
153 | if (expect_false ((cnt) > cur)) \ |
126 | { \ |
154 | { \ |
127 | int newcnt = cur; \ |
155 | int newcnt = cur; \ |
128 | do \ |
156 | do \ |
129 | { \ |
157 | { \ |
130 | newcnt = array_roundsize (base, newcnt << 1); \ |
158 | newcnt = array_roundsize (base, newcnt << 1); \ |
… | |
… | |
253 | ++fdchangecnt; |
281 | ++fdchangecnt; |
254 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
282 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
255 | fdchanges [fdchangecnt - 1] = fd; |
283 | fdchanges [fdchangecnt - 1] = fd; |
256 | } |
284 | } |
257 | |
285 | |
|
|
286 | static void |
|
|
287 | fd_kill (int fd) |
|
|
288 | { |
|
|
289 | struct ev_io *w; |
|
|
290 | |
|
|
291 | printf ("killing fd %d\n", fd);//D |
|
|
292 | while ((w = anfds [fd].head)) |
|
|
293 | { |
|
|
294 | ev_io_stop (w); |
|
|
295 | event ((W)w, EV_ERROR | EV_READ | EV_WRITE); |
|
|
296 | } |
|
|
297 | } |
|
|
298 | |
258 | /* called on EBADF to verify fds */ |
299 | /* called on EBADF to verify fds */ |
259 | static void |
300 | static void |
260 | fd_recheck (void) |
301 | fd_ebadf (void) |
261 | { |
302 | { |
262 | int fd; |
303 | int fd; |
263 | |
304 | |
264 | for (fd = 0; fd < anfdmax; ++fd) |
305 | for (fd = 0; fd < anfdmax; ++fd) |
265 | if (anfds [fd].events) |
306 | if (anfds [fd].events) |
266 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
307 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
267 | while (anfds [fd].head) |
308 | fd_kill (fd); |
|
|
309 | } |
|
|
310 | |
|
|
311 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
|
|
312 | static void |
|
|
313 | fd_enomem (void) |
|
|
314 | { |
|
|
315 | int fd = anfdmax; |
|
|
316 | |
|
|
317 | while (fd--) |
|
|
318 | if (anfds [fd].events) |
268 | { |
319 | { |
269 | ev_io_stop (anfds [fd].head); |
320 | close (fd); |
270 | event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE); |
321 | fd_kill (fd); |
|
|
322 | return; |
271 | } |
323 | } |
272 | } |
324 | } |
273 | |
325 | |
274 | /*****************************************************************************/ |
326 | /*****************************************************************************/ |
275 | |
327 | |
276 | static struct ev_timer **timers; |
328 | static struct ev_timer **timers; |
… | |
… | |
361 | |
413 | |
362 | static void |
414 | static void |
363 | sigcb (struct ev_io *iow, int revents) |
415 | sigcb (struct ev_io *iow, int revents) |
364 | { |
416 | { |
365 | struct ev_signal *w; |
417 | struct ev_signal *w; |
366 | int sig; |
418 | int signum; |
367 | |
419 | |
368 | read (sigpipe [0], &revents, 1); |
420 | read (sigpipe [0], &revents, 1); |
369 | gotsig = 0; |
421 | gotsig = 0; |
370 | |
422 | |
371 | for (sig = signalmax; sig--; ) |
423 | for (signum = signalmax; signum--; ) |
372 | if (signals [sig].gotsig) |
424 | if (signals [signum].gotsig) |
373 | { |
425 | { |
374 | signals [sig].gotsig = 0; |
426 | signals [signum].gotsig = 0; |
375 | |
427 | |
376 | for (w = signals [sig].head; w; w = w->next) |
428 | for (w = signals [signum].head; w; w = w->next) |
377 | event ((W)w, EV_SIGNAL); |
429 | event ((W)w, EV_SIGNAL); |
378 | } |
430 | } |
379 | } |
431 | } |
380 | |
432 | |
381 | static void |
433 | static void |
… | |
… | |
418 | struct ev_child *w; |
470 | struct ev_child *w; |
419 | int pid, status; |
471 | int pid, status; |
420 | |
472 | |
421 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
473 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
422 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
474 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
423 | if (w->pid == pid || w->pid == -1) |
475 | if (w->pid == pid || !w->pid) |
424 | { |
476 | { |
425 | w->status = status; |
477 | w->status = status; |
426 | event ((W)w, EV_CHILD); |
478 | event ((W)w, EV_CHILD); |
427 | } |
479 | } |
428 | } |
480 | } |
… | |
… | |
430 | /*****************************************************************************/ |
482 | /*****************************************************************************/ |
431 | |
483 | |
432 | #if EV_USE_EPOLL |
484 | #if EV_USE_EPOLL |
433 | # include "ev_epoll.c" |
485 | # include "ev_epoll.c" |
434 | #endif |
486 | #endif |
|
|
487 | #if EV_USE_POLL |
|
|
488 | # include "ev_poll.c" |
|
|
489 | #endif |
435 | #if EV_USE_SELECT |
490 | #if EV_USE_SELECT |
436 | # include "ev_select.c" |
491 | # include "ev_select.c" |
437 | #endif |
492 | #endif |
438 | |
493 | |
439 | int |
494 | int |
… | |
… | |
446 | ev_version_minor (void) |
501 | ev_version_minor (void) |
447 | { |
502 | { |
448 | return EV_VERSION_MINOR; |
503 | return EV_VERSION_MINOR; |
449 | } |
504 | } |
450 | |
505 | |
|
|
506 | /* return true if we are running with elevated privileges and ignore env variables */ |
|
|
507 | static int |
|
|
508 | enable_secure () |
|
|
509 | { |
|
|
510 | return getuid () != geteuid () |
|
|
511 | || getgid () != getegid (); |
|
|
512 | } |
|
|
513 | |
451 | int ev_init (int flags) |
514 | int ev_init (int methods) |
452 | { |
515 | { |
453 | if (!ev_method) |
516 | if (!ev_method) |
454 | { |
517 | { |
455 | #if EV_USE_MONOTONIC |
518 | #if EV_USE_MONOTONIC |
456 | { |
519 | { |
… | |
… | |
458 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
521 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
459 | have_monotonic = 1; |
522 | have_monotonic = 1; |
460 | } |
523 | } |
461 | #endif |
524 | #endif |
462 | |
525 | |
463 | ev_now = ev_time (); |
526 | ev_now = ev_time (); |
464 | now = get_clock (); |
527 | now = get_clock (); |
|
|
528 | now_floor = now; |
465 | diff = ev_now - now; |
529 | diff = ev_now - now; |
466 | |
530 | |
467 | if (pipe (sigpipe)) |
531 | if (pipe (sigpipe)) |
468 | return 0; |
532 | return 0; |
469 | |
533 | |
|
|
534 | if (methods == EVMETHOD_AUTO) |
|
|
535 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
|
|
536 | methods = atoi (getenv ("LIBEV_METHODS")); |
|
|
537 | else |
470 | ev_method = EVMETHOD_NONE; |
538 | methods = EVMETHOD_ANY; |
|
|
539 | |
|
|
540 | ev_method = 0; |
471 | #if EV_USE_EPOLL |
541 | #if EV_USE_EPOLL |
472 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
542 | if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods); |
|
|
543 | #endif |
|
|
544 | #if EV_USE_POLL |
|
|
545 | if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods); |
473 | #endif |
546 | #endif |
474 | #if EV_USE_SELECT |
547 | #if EV_USE_SELECT |
475 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
548 | if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods); |
476 | #endif |
549 | #endif |
477 | |
550 | |
478 | if (ev_method) |
551 | if (ev_method) |
479 | { |
552 | { |
480 | ev_watcher_init (&sigev, sigcb); |
553 | ev_watcher_init (&sigev, sigcb); |
… | |
… | |
599 | } |
672 | } |
600 | } |
673 | } |
601 | } |
674 | } |
602 | } |
675 | } |
603 | |
676 | |
|
|
677 | static int |
|
|
678 | time_update_monotonic (void) |
|
|
679 | { |
|
|
680 | now = get_clock (); |
|
|
681 | |
|
|
682 | if (expect_true (now - now_floor < MIN_TIMEJUMP * .5)) |
|
|
683 | { |
|
|
684 | ev_now = now + diff; |
|
|
685 | return 0; |
|
|
686 | } |
|
|
687 | else |
|
|
688 | { |
|
|
689 | now_floor = now; |
|
|
690 | ev_now = ev_time (); |
|
|
691 | return 1; |
|
|
692 | } |
|
|
693 | } |
|
|
694 | |
604 | static void |
695 | static void |
605 | time_update (void) |
696 | time_update (void) |
606 | { |
697 | { |
607 | int i; |
698 | int i; |
608 | |
699 | |
609 | ev_now = ev_time (); |
700 | #if EV_USE_MONOTONIC |
610 | |
|
|
611 | if (have_monotonic) |
701 | if (expect_true (have_monotonic)) |
612 | { |
702 | { |
|
|
703 | if (time_update_monotonic ()) |
|
|
704 | { |
613 | ev_tstamp odiff = diff; |
705 | ev_tstamp odiff = diff; |
614 | |
706 | |
615 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
707 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
616 | { |
708 | { |
617 | now = get_clock (); |
|
|
618 | diff = ev_now - now; |
709 | diff = ev_now - now; |
619 | |
710 | |
620 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
711 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
621 | return; /* all is well */ |
712 | return; /* all is well */ |
622 | |
713 | |
623 | ev_now = ev_time (); |
714 | ev_now = ev_time (); |
|
|
715 | now = get_clock (); |
|
|
716 | now_floor = now; |
624 | } |
717 | } |
625 | |
718 | |
626 | periodics_reschedule (diff - odiff); |
719 | periodics_reschedule (diff - odiff); |
627 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
720 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
|
|
721 | } |
628 | } |
722 | } |
629 | else |
723 | else |
|
|
724 | #endif |
630 | { |
725 | { |
|
|
726 | ev_now = ev_time (); |
|
|
727 | |
631 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
728 | if (expect_false (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
632 | { |
729 | { |
633 | periodics_reschedule (ev_now - now); |
730 | periodics_reschedule (ev_now - now); |
634 | |
731 | |
635 | /* adjust timers. this is easy, as the offset is the same for all */ |
732 | /* adjust timers. this is easy, as the offset is the same for all */ |
636 | for (i = 0; i < timercnt; ++i) |
733 | for (i = 0; i < timercnt; ++i) |
… | |
… | |
649 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
746 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
650 | |
747 | |
651 | do |
748 | do |
652 | { |
749 | { |
653 | /* queue check watchers (and execute them) */ |
750 | /* queue check watchers (and execute them) */ |
654 | if (preparecnt) |
751 | if (expect_false (preparecnt)) |
655 | { |
752 | { |
656 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
753 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
657 | call_pending (); |
754 | call_pending (); |
658 | } |
755 | } |
659 | |
756 | |
… | |
… | |
662 | |
759 | |
663 | /* calculate blocking time */ |
760 | /* calculate blocking time */ |
664 | |
761 | |
665 | /* we only need this for !monotonic clockor timers, but as we basically |
762 | /* we only need this for !monotonic clockor timers, but as we basically |
666 | always have timers, we just calculate it always */ |
763 | always have timers, we just calculate it always */ |
|
|
764 | #if EV_USE_MONOTONIC |
|
|
765 | if (expect_true (have_monotonic)) |
|
|
766 | time_update_monotonic (); |
|
|
767 | else |
|
|
768 | #endif |
|
|
769 | { |
667 | ev_now = ev_time (); |
770 | ev_now = ev_time (); |
|
|
771 | now = ev_now; |
|
|
772 | } |
668 | |
773 | |
669 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
774 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
670 | block = 0.; |
775 | block = 0.; |
671 | else |
776 | else |
672 | { |
777 | { |
673 | block = MAX_BLOCKTIME; |
778 | block = MAX_BLOCKTIME; |
674 | |
779 | |
675 | if (timercnt) |
780 | if (timercnt) |
676 | { |
781 | { |
677 | ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; |
782 | ev_tstamp to = timers [0]->at - now + method_fudge; |
678 | if (block > to) block = to; |
783 | if (block > to) block = to; |
679 | } |
784 | } |
680 | |
785 | |
681 | if (periodiccnt) |
786 | if (periodiccnt) |
682 | { |
787 | { |
… | |
… | |
761 | /*****************************************************************************/ |
866 | /*****************************************************************************/ |
762 | |
867 | |
763 | void |
868 | void |
764 | ev_io_start (struct ev_io *w) |
869 | ev_io_start (struct ev_io *w) |
765 | { |
870 | { |
|
|
871 | int fd = w->fd; |
|
|
872 | |
766 | if (ev_is_active (w)) |
873 | if (ev_is_active (w)) |
767 | return; |
874 | return; |
768 | |
|
|
769 | int fd = w->fd; |
|
|
770 | |
875 | |
771 | assert (("ev_io_start called with negative fd", fd >= 0)); |
876 | assert (("ev_io_start called with negative fd", fd >= 0)); |
772 | |
877 | |
773 | ev_start ((W)w, 1); |
878 | ev_start ((W)w, 1); |
774 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
879 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |