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

Comparing libev/ev.c (file contents):
Revision 1.36 by root, Thu Nov 1 13:11:11 2007 UTC vs.
Revision 1.42 by root, Fri Nov 2 20:05:05 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# ifdef CLOCK_MONOTONIC
53# define EV_USE_MONOTONIC 1 54# define EV_USE_MONOTONIC 1
54# endif
55#endif 55#endif
56 56
57#ifndef EV_USE_SELECT 57#ifndef EV_USE_SELECT
58# define EV_USE_SELECT 1 58# define EV_USE_SELECT 1
59#endif 59#endif
60 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
61#ifndef EV_USE_EPOLL 65#ifndef EV_USE_EPOLL
62# define EV_USE_EPOLL 0 66# define EV_USE_EPOLL 0
63#endif 67#endif
64 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
65#ifndef CLOCK_REALTIME 80#ifndef CLOCK_REALTIME
81# undef EV_USE_REALTIME
66# define EV_USE_REALTIME 0 82# define EV_USE_REALTIME 0
67#endif 83#endif
68#ifndef EV_USE_REALTIME 84
69# define EV_USE_REALTIME 1 /* posix requirement, but might be slower */ 85/**/
70#endif
71 86
72#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) */
73#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) */
74#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 */
75#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 */
76 91
77#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)
78 107
79typedef struct ev_watcher *W; 108typedef struct ev_watcher *W;
80typedef struct ev_watcher_list *WL; 109typedef struct ev_watcher_list *WL;
81typedef struct ev_watcher_time *WT; 110typedef struct ev_watcher_time *WT;
82 111
83static ev_tstamp now, diff; /* monotonic clock */ 112static ev_tstamp now_floor, now, diff; /* monotonic clock */
84ev_tstamp ev_now; 113ev_tstamp ev_now;
85int ev_method; 114int ev_method;
86 115
87static int have_monotonic; /* runtime */ 116static int have_monotonic; /* runtime */
88 117
108 137
109static ev_tstamp 138static ev_tstamp
110get_clock (void) 139get_clock (void)
111{ 140{
112#if EV_USE_MONOTONIC 141#if EV_USE_MONOTONIC
113 if (have_monotonic) 142 if (expect_true (have_monotonic))
114 { 143 {
115 struct timespec ts; 144 struct timespec ts;
116 clock_gettime (CLOCK_MONOTONIC, &ts); 145 clock_gettime (CLOCK_MONOTONIC, &ts);
117 return ts.tv_sec + ts.tv_nsec * 1e-9; 146 return ts.tv_sec + ts.tv_nsec * 1e-9;
118 } 147 }
122} 151}
123 152
124#define array_roundsize(base,n) ((n) | 4 & ~3) 153#define array_roundsize(base,n) ((n) | 4 & ~3)
125 154
126#define array_needsize(base,cur,cnt,init) \ 155#define array_needsize(base,cur,cnt,init) \
127 if ((cnt) > cur) \ 156 if (expect_false ((cnt) > cur)) \
128 { \ 157 { \
129 int newcnt = cur; \ 158 int newcnt = cur; \
130 do \ 159 do \
131 { \ 160 { \
132 newcnt = array_roundsize (base, newcnt << 1); \ 161 newcnt = array_roundsize (base, newcnt << 1); \
167{ 196{
168 W w; 197 W w;
169 int events; 198 int events;
170} ANPENDING; 199} ANPENDING;
171 200
172static ANPENDING *pendings; 201static ANPENDING *pendings [NUMPRI];
173static int pendingmax, pendingcnt; 202static int pendingmax [NUMPRI], pendingcnt [NUMPRI];
174 203
175static void 204static void
176event (W w, int events) 205event (W w, int events)
177{ 206{
178 if (w->pending) 207 if (w->pending)
179 { 208 {
180 pendings [w->pending - 1].events |= events; 209 pendings [ABSPRI (w)][w->pending - 1].events |= events;
181 return; 210 return;
182 } 211 }
183 212
184 w->pending = ++pendingcnt; 213 w->pending = ++pendingcnt [ABSPRI (w)];
185 array_needsize (pendings, pendingmax, pendingcnt, ); 214 array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], );
186 pendings [pendingcnt - 1].w = w; 215 pendings [ABSPRI (w)][w->pending - 1].w = w;
187 pendings [pendingcnt - 1].events = events; 216 pendings [ABSPRI (w)][w->pending - 1].events = events;
188} 217}
189 218
190static void 219static void
191queue_events (W *events, int eventcnt, int type) 220queue_events (W *events, int eventcnt, int type)
192{ 221{
255 ++fdchangecnt; 284 ++fdchangecnt;
256 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 285 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
257 fdchanges [fdchangecnt - 1] = fd; 286 fdchanges [fdchangecnt - 1] = fd;
258} 287}
259 288
289static void
290fd_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
260/* called on EBADF to verify fds */ 302/* called on EBADF to verify fds */
261static void 303static void
262fd_recheck (void) 304fd_ebadf (void)
263{ 305{
264 int fd; 306 int fd;
265 307
266 for (fd = 0; fd < anfdmax; ++fd) 308 for (fd = 0; fd < anfdmax; ++fd)
267 if (anfds [fd].events) 309 if (anfds [fd].events)
268 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 310 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
269 while (anfds [fd].head) 311 fd_kill (fd);
312}
313
314/* called on ENOMEM in select/poll to kill some fds and retry */
315static void
316fd_enomem (void)
317{
318 int fd = anfdmax;
319
320 while (fd--)
321 if (anfds [fd].events)
270 { 322 {
271 ev_io_stop (anfds [fd].head); 323 close (fd);
272 event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE); 324 fd_kill (fd);
325 return;
273 } 326 }
274} 327}
275 328
276/*****************************************************************************/ 329/*****************************************************************************/
277 330
278static struct ev_timer **timers; 331static struct ev_timer **timers;
363 416
364static void 417static void
365sigcb (struct ev_io *iow, int revents) 418sigcb (struct ev_io *iow, int revents)
366{ 419{
367 struct ev_signal *w; 420 struct ev_signal *w;
368 int sig; 421 int signum;
369 422
370 read (sigpipe [0], &revents, 1); 423 read (sigpipe [0], &revents, 1);
371 gotsig = 0; 424 gotsig = 0;
372 425
373 for (sig = signalmax; sig--; ) 426 for (signum = signalmax; signum--; )
374 if (signals [sig].gotsig) 427 if (signals [signum].gotsig)
375 { 428 {
376 signals [sig].gotsig = 0; 429 signals [signum].gotsig = 0;
377 430
378 for (w = signals [sig].head; w; w = w->next) 431 for (w = signals [signum].head; w; w = w->next)
379 event ((W)w, EV_SIGNAL); 432 event ((W)w, EV_SIGNAL);
380 } 433 }
381} 434}
382 435
383static void 436static void
420 struct ev_child *w; 473 struct ev_child *w;
421 int pid, status; 474 int pid, status;
422 475
423 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) 476 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1)
424 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) 477 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next)
425 if (w->pid == pid || w->pid == -1) 478 if (w->pid == pid || !w->pid)
426 { 479 {
427 w->status = status; 480 w->status = status;
428 event ((W)w, EV_CHILD); 481 event ((W)w, EV_CHILD);
429 } 482 }
430} 483}
432/*****************************************************************************/ 485/*****************************************************************************/
433 486
434#if EV_USE_EPOLL 487#if EV_USE_EPOLL
435# include "ev_epoll.c" 488# include "ev_epoll.c"
436#endif 489#endif
490#if EV_USE_POLL
491# include "ev_poll.c"
492#endif
437#if EV_USE_SELECT 493#if EV_USE_SELECT
438# include "ev_select.c" 494# include "ev_select.c"
439#endif 495#endif
440 496
441int 497int
448ev_version_minor (void) 504ev_version_minor (void)
449{ 505{
450 return EV_VERSION_MINOR; 506 return EV_VERSION_MINOR;
451} 507}
452 508
509/* return true if we are running with elevated privileges and ignore env variables */
510static int
511enable_secure ()
512{
513 return getuid () != geteuid ()
514 || getgid () != getegid ();
515}
516
453int ev_init (int flags) 517int ev_init (int methods)
454{ 518{
455 if (!ev_method) 519 if (!ev_method)
456 { 520 {
457#if EV_USE_MONOTONIC 521#if EV_USE_MONOTONIC
458 { 522 {
460 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 524 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
461 have_monotonic = 1; 525 have_monotonic = 1;
462 } 526 }
463#endif 527#endif
464 528
465 ev_now = ev_time (); 529 ev_now = ev_time ();
466 now = get_clock (); 530 now = get_clock ();
531 now_floor = now;
467 diff = ev_now - now; 532 diff = ev_now - now;
468 533
469 if (pipe (sigpipe)) 534 if (pipe (sigpipe))
470 return 0; 535 return 0;
471 536
537 if (methods == EVMETHOD_AUTO)
538 if (!enable_secure () && getenv ("LIBEV_METHODS"))
539 methods = atoi (getenv ("LIBEV_METHODS"));
540 else
472 ev_method = EVMETHOD_NONE; 541 methods = EVMETHOD_ANY;
542
543 ev_method = 0;
473#if EV_USE_EPOLL 544#if EV_USE_EPOLL
474 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);
475#endif 549#endif
476#if EV_USE_SELECT 550#if EV_USE_SELECT
477 if (ev_method == EVMETHOD_NONE) select_init (flags); 551 if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods);
478#endif 552#endif
479 553
480 if (ev_method) 554 if (ev_method)
481 { 555 {
482 ev_watcher_init (&sigev, sigcb); 556 ev_watcher_init (&sigev, sigcb);
522/*****************************************************************************/ 596/*****************************************************************************/
523 597
524static void 598static void
525call_pending (void) 599call_pending (void)
526{ 600{
601 int pri;
602
603 for (pri = NUMPRI; pri--; )
527 while (pendingcnt) 604 while (pendingcnt [pri])
528 { 605 {
529 ANPENDING *p = pendings + --pendingcnt; 606 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
530 607
531 if (p->w) 608 if (p->w)
532 { 609 {
533 p->w->pending = 0; 610 p->w->pending = 0;
534 p->w->cb (p->w, p->events); 611 p->w->cb (p->w, p->events);
535 } 612 }
536 } 613 }
537} 614}
538 615
539static void 616static void
540timers_reify (void) 617timers_reify (void)
541{ 618{
601 } 678 }
602 } 679 }
603 } 680 }
604} 681}
605 682
683static int
684time_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
606static void 701static void
607time_update (void) 702time_update (void)
608{ 703{
609 int i; 704 int i;
610 705
611 ev_now = ev_time (); 706#if EV_USE_MONOTONIC
612
613 if (have_monotonic) 707 if (expect_true (have_monotonic))
614 { 708 {
709 if (time_update_monotonic ())
710 {
615 ev_tstamp odiff = diff; 711 ev_tstamp odiff = diff;
616 712
617 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 */
618 { 714 {
619 now = get_clock ();
620 diff = ev_now - now; 715 diff = ev_now - now;
621 716
622 if (fabs (odiff - diff) < MIN_TIMEJUMP) 717 if (fabs (odiff - diff) < MIN_TIMEJUMP)
623 return; /* all is well */ 718 return; /* all is well */
624 719
625 ev_now = ev_time (); 720 ev_now = ev_time ();
721 now = get_clock ();
722 now_floor = now;
626 } 723 }
627 724
628 periodics_reschedule (diff - odiff); 725 periodics_reschedule (diff - odiff);
629 /* no timer adjustment, as the monotonic clock doesn't jump */ 726 /* no timer adjustment, as the monotonic clock doesn't jump */
727 }
630 } 728 }
631 else 729 else
730#endif
632 { 731 {
732 ev_now = ev_time ();
733
633 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))
634 { 735 {
635 periodics_reschedule (ev_now - now); 736 periodics_reschedule (ev_now - now);
636 737
637 /* 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 */
638 for (i = 0; i < timercnt; ++i) 739 for (i = 0; i < timercnt; ++i)
651 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; 752 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
652 753
653 do 754 do
654 { 755 {
655 /* queue check watchers (and execute them) */ 756 /* queue check watchers (and execute them) */
656 if (preparecnt) 757 if (expect_false (preparecnt))
657 { 758 {
658 queue_events ((W *)prepares, preparecnt, EV_PREPARE); 759 queue_events ((W *)prepares, preparecnt, EV_PREPARE);
659 call_pending (); 760 call_pending ();
660 } 761 }
661 762
664 765
665 /* calculate blocking time */ 766 /* calculate blocking time */
666 767
667 /* 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
668 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 {
669 ev_now = ev_time (); 776 ev_now = ev_time ();
777 now = ev_now;
778 }
670 779
671 if (flags & EVLOOP_NONBLOCK || idlecnt) 780 if (flags & EVLOOP_NONBLOCK || idlecnt)
672 block = 0.; 781 block = 0.;
673 else 782 else
674 { 783 {
675 block = MAX_BLOCKTIME; 784 block = MAX_BLOCKTIME;
676 785
677 if (timercnt) 786 if (timercnt)
678 { 787 {
679 ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; 788 ev_tstamp to = timers [0]->at - now + method_fudge;
680 if (block > to) block = to; 789 if (block > to) block = to;
681 } 790 }
682 791
683 if (periodiccnt) 792 if (periodiccnt)
684 { 793 {
741static void 850static void
742ev_clear_pending (W w) 851ev_clear_pending (W w)
743{ 852{
744 if (w->pending) 853 if (w->pending)
745 { 854 {
746 pendings [w->pending - 1].w = 0; 855 pendings [ABSPRI (w)][w->pending - 1].w = 0;
747 w->pending = 0; 856 w->pending = 0;
748 } 857 }
749} 858}
750 859
751static void 860static void
763/*****************************************************************************/ 872/*****************************************************************************/
764 873
765void 874void
766ev_io_start (struct ev_io *w) 875ev_io_start (struct ev_io *w)
767{ 876{
877 int fd = w->fd;
878
768 if (ev_is_active (w)) 879 if (ev_is_active (w))
769 return; 880 return;
770
771 int fd = w->fd;
772 881
773 assert (("ev_io_start called with negative fd", fd >= 0)); 882 assert (("ev_io_start called with negative fd", fd >= 0));
774 883
775 ev_start ((W)w, 1); 884 ev_start ((W)w, 1);
776 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 885 array_needsize (anfds, anfdmax, fd + 1, anfds_init);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines