ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev.c
(Generate patch)

Comparing libev/ev.c (file contents):
Revision 1.39 by root, Thu Nov 1 17:17:32 2007 UTC vs.
Revision 1.41 by root, Fri Nov 2 16:54:34 2007 UTC

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)
81 104
82typedef struct ev_watcher *W; 105typedef struct ev_watcher *W;
83typedef struct ev_watcher_list *WL; 106typedef struct ev_watcher_list *WL;
84typedef struct ev_watcher_time *WT; 107typedef struct ev_watcher_time *WT;
85 108
86static ev_tstamp now, diff; /* monotonic clock */ 109static ev_tstamp now_floor, now, diff; /* monotonic clock */
87ev_tstamp ev_now; 110ev_tstamp ev_now;
88int ev_method; 111int ev_method;
89 112
90static int have_monotonic; /* runtime */ 113static int have_monotonic; /* runtime */
91 114
111 134
112static ev_tstamp 135static ev_tstamp
113get_clock (void) 136get_clock (void)
114{ 137{
115#if EV_USE_MONOTONIC 138#if EV_USE_MONOTONIC
116 if (have_monotonic) 139 if (expect_true (have_monotonic))
117 { 140 {
118 struct timespec ts; 141 struct timespec ts;
119 clock_gettime (CLOCK_MONOTONIC, &ts); 142 clock_gettime (CLOCK_MONOTONIC, &ts);
120 return ts.tv_sec + ts.tv_nsec * 1e-9; 143 return ts.tv_sec + ts.tv_nsec * 1e-9;
121 } 144 }
125} 148}
126 149
127#define array_roundsize(base,n) ((n) | 4 & ~3) 150#define array_roundsize(base,n) ((n) | 4 & ~3)
128 151
129#define array_needsize(base,cur,cnt,init) \ 152#define array_needsize(base,cur,cnt,init) \
130 if ((cnt) > cur) \ 153 if (expect_false ((cnt) > cur)) \
131 { \ 154 { \
132 int newcnt = cur; \ 155 int newcnt = cur; \
133 do \ 156 do \
134 { \ 157 { \
135 newcnt = array_roundsize (base, newcnt << 1); \ 158 newcnt = array_roundsize (base, newcnt << 1); \
258 ++fdchangecnt; 281 ++fdchangecnt;
259 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 282 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
260 fdchanges [fdchangecnt - 1] = fd; 283 fdchanges [fdchangecnt - 1] = fd;
261} 284}
262 285
286static void
287fd_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
263/* called on EBADF to verify fds */ 299/* called on EBADF to verify fds */
264static void 300static void
265fd_recheck (void) 301fd_ebadf (void)
266{ 302{
267 int fd; 303 int fd;
268 304
269 for (fd = 0; fd < anfdmax; ++fd) 305 for (fd = 0; fd < anfdmax; ++fd)
270 if (anfds [fd].events) 306 if (anfds [fd].events)
271 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 307 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
272 while (anfds [fd].head) 308 fd_kill (fd);
309}
310
311/* called on ENOMEM in select/poll to kill some fds and retry */
312static void
313fd_enomem (void)
314{
315 int fd = anfdmax;
316
317 while (fd--)
318 if (anfds [fd].events)
273 { 319 {
274 ev_io_stop (anfds [fd].head); 320 close (fd);
275 event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE); 321 fd_kill (fd);
322 return;
276 } 323 }
277} 324}
278 325
279/*****************************************************************************/ 326/*****************************************************************************/
280 327
281static struct ev_timer **timers; 328static struct ev_timer **timers;
435/*****************************************************************************/ 482/*****************************************************************************/
436 483
437#if EV_USE_EPOLL 484#if EV_USE_EPOLL
438# include "ev_epoll.c" 485# include "ev_epoll.c"
439#endif 486#endif
487#if EV_USE_POLL
488# include "ev_poll.c"
489#endif
440#if EV_USE_SELECT 490#if EV_USE_SELECT
441# include "ev_select.c" 491# include "ev_select.c"
442#endif 492#endif
443 493
444int 494int
451ev_version_minor (void) 501ev_version_minor (void)
452{ 502{
453 return EV_VERSION_MINOR; 503 return EV_VERSION_MINOR;
454} 504}
455 505
506/* return true if we are running with elevated privileges and ignore env variables */
507static int
508enable_secure ()
509{
510 return getuid () != geteuid ()
511 || getgid () != getegid ();
512}
513
456int ev_init (int flags) 514int ev_init (int methods)
457{ 515{
458 if (!ev_method) 516 if (!ev_method)
459 { 517 {
460#if EV_USE_MONOTONIC 518#if EV_USE_MONOTONIC
461 { 519 {
463 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 521 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
464 have_monotonic = 1; 522 have_monotonic = 1;
465 } 523 }
466#endif 524#endif
467 525
468 ev_now = ev_time (); 526 ev_now = ev_time ();
469 now = get_clock (); 527 now = get_clock ();
528 now_floor = now;
470 diff = ev_now - now; 529 diff = ev_now - now;
471 530
472 if (pipe (sigpipe)) 531 if (pipe (sigpipe))
473 return 0; 532 return 0;
474 533
534 if (methods == EVMETHOD_AUTO)
535 if (!enable_secure () && getenv ("LIBEV_METHODS"))
536 methods = atoi (getenv ("LIBEV_METHODS"));
537 else
475 ev_method = EVMETHOD_NONE; 538 methods = EVMETHOD_ANY;
539
540 ev_method = 0;
476#if EV_USE_EPOLL 541#if EV_USE_EPOLL
477 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);
478#endif 546#endif
479#if EV_USE_SELECT 547#if EV_USE_SELECT
480 if (ev_method == EVMETHOD_NONE) select_init (flags); 548 if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods);
481#endif 549#endif
482 550
483 if (ev_method) 551 if (ev_method)
484 { 552 {
485 ev_watcher_init (&sigev, sigcb); 553 ev_watcher_init (&sigev, sigcb);
604 } 672 }
605 } 673 }
606 } 674 }
607} 675}
608 676
677static int
678time_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
609static void 695static void
610time_update (void) 696time_update (void)
611{ 697{
612 int i; 698 int i;
613 699
614 ev_now = ev_time (); 700#if EV_USE_MONOTONIC
615
616 if (have_monotonic) 701 if (expect_true (have_monotonic))
617 { 702 {
703 if (time_update_monotonic ())
704 {
618 ev_tstamp odiff = diff; 705 ev_tstamp odiff = diff;
619 706
620 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 */
621 { 708 {
622 now = get_clock ();
623 diff = ev_now - now; 709 diff = ev_now - now;
624 710
625 if (fabs (odiff - diff) < MIN_TIMEJUMP) 711 if (fabs (odiff - diff) < MIN_TIMEJUMP)
626 return; /* all is well */ 712 return; /* all is well */
627 713
628 ev_now = ev_time (); 714 ev_now = ev_time ();
715 now = get_clock ();
716 now_floor = now;
629 } 717 }
630 718
631 periodics_reschedule (diff - odiff); 719 periodics_reschedule (diff - odiff);
632 /* no timer adjustment, as the monotonic clock doesn't jump */ 720 /* no timer adjustment, as the monotonic clock doesn't jump */
721 }
633 } 722 }
634 else 723 else
724#endif
635 { 725 {
726 ev_now = ev_time ();
727
636 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))
637 { 729 {
638 periodics_reschedule (ev_now - now); 730 periodics_reschedule (ev_now - now);
639 731
640 /* 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 */
641 for (i = 0; i < timercnt; ++i) 733 for (i = 0; i < timercnt; ++i)
654 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; 746 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
655 747
656 do 748 do
657 { 749 {
658 /* queue check watchers (and execute them) */ 750 /* queue check watchers (and execute them) */
659 if (preparecnt) 751 if (expect_false (preparecnt))
660 { 752 {
661 queue_events ((W *)prepares, preparecnt, EV_PREPARE); 753 queue_events ((W *)prepares, preparecnt, EV_PREPARE);
662 call_pending (); 754 call_pending ();
663 } 755 }
664 756
667 759
668 /* calculate blocking time */ 760 /* calculate blocking time */
669 761
670 /* 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
671 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 {
672 ev_now = ev_time (); 770 ev_now = ev_time ();
771 now = ev_now;
772 }
673 773
674 if (flags & EVLOOP_NONBLOCK || idlecnt) 774 if (flags & EVLOOP_NONBLOCK || idlecnt)
675 block = 0.; 775 block = 0.;
676 else 776 else
677 { 777 {
678 block = MAX_BLOCKTIME; 778 block = MAX_BLOCKTIME;
679 779
680 if (timercnt) 780 if (timercnt)
681 { 781 {
682 ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; 782 ev_tstamp to = timers [0]->at - now + method_fudge;
683 if (block > to) block = to; 783 if (block > to) block = to;
684 } 784 }
685 785
686 if (periodiccnt) 786 if (periodiccnt)
687 { 787 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines