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.44 by root, Fri Nov 2 20:59:14 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_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/**/
78
79#ifndef CLOCK_MONOTONIC
80# undef EV_USE_MONOTONIC
81# define EV_USE_MONOTONIC 0
82#endif
83
65#ifndef CLOCK_REALTIME 84#ifndef CLOCK_REALTIME
85# undef EV_USE_REALTIME
66# define EV_USE_REALTIME 0 86# define EV_USE_REALTIME 0
67#endif 87#endif
68#ifndef EV_USE_REALTIME 88
69# define EV_USE_REALTIME 1 /* posix requirement, but might be slower */ 89/**/
70#endif
71 90
72#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) */
73#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) */
74#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 */
75#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 */
76 95
77#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)
78 111
79typedef struct ev_watcher *W; 112typedef struct ev_watcher *W;
80typedef struct ev_watcher_list *WL; 113typedef struct ev_watcher_list *WL;
81typedef struct ev_watcher_time *WT; 114typedef struct ev_watcher_time *WT;
82 115
83static ev_tstamp now, diff; /* monotonic clock */ 116static ev_tstamp now_floor, now, diff; /* monotonic clock */
84ev_tstamp ev_now; 117ev_tstamp ev_now;
85int ev_method; 118int ev_method;
86 119
87static int have_monotonic; /* runtime */ 120static int have_monotonic; /* runtime */
88 121
108 141
109static ev_tstamp 142static ev_tstamp
110get_clock (void) 143get_clock (void)
111{ 144{
112#if EV_USE_MONOTONIC 145#if EV_USE_MONOTONIC
113 if (have_monotonic) 146 if (expect_true (have_monotonic))
114 { 147 {
115 struct timespec ts; 148 struct timespec ts;
116 clock_gettime (CLOCK_MONOTONIC, &ts); 149 clock_gettime (CLOCK_MONOTONIC, &ts);
117 return ts.tv_sec + ts.tv_nsec * 1e-9; 150 return ts.tv_sec + ts.tv_nsec * 1e-9;
118 } 151 }
122} 155}
123 156
124#define array_roundsize(base,n) ((n) | 4 & ~3) 157#define array_roundsize(base,n) ((n) | 4 & ~3)
125 158
126#define array_needsize(base,cur,cnt,init) \ 159#define array_needsize(base,cur,cnt,init) \
127 if ((cnt) > cur) \ 160 if (expect_false ((cnt) > cur)) \
128 { \ 161 { \
129 int newcnt = cur; \ 162 int newcnt = cur; \
130 do \ 163 do \
131 { \ 164 { \
132 newcnt = array_roundsize (base, newcnt << 1); \ 165 newcnt = array_roundsize (base, newcnt << 1); \
167{ 200{
168 W w; 201 W w;
169 int events; 202 int events;
170} ANPENDING; 203} ANPENDING;
171 204
172static ANPENDING *pendings; 205static ANPENDING *pendings [NUMPRI];
173static int pendingmax, pendingcnt; 206static int pendingmax [NUMPRI], pendingcnt [NUMPRI];
174 207
175static void 208static void
176event (W w, int events) 209event (W w, int events)
177{ 210{
178 if (w->pending) 211 if (w->pending)
179 { 212 {
180 pendings [w->pending - 1].events |= events; 213 pendings [ABSPRI (w)][w->pending - 1].events |= events;
181 return; 214 return;
182 } 215 }
183 216
184 w->pending = ++pendingcnt; 217 w->pending = ++pendingcnt [ABSPRI (w)];
185 array_needsize (pendings, pendingmax, pendingcnt, ); 218 array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], );
186 pendings [pendingcnt - 1].w = w; 219 pendings [ABSPRI (w)][w->pending - 1].w = w;
187 pendings [pendingcnt - 1].events = events; 220 pendings [ABSPRI (w)][w->pending - 1].events = events;
188} 221}
189 222
190static void 223static void
191queue_events (W *events, int eventcnt, int type) 224queue_events (W *events, int eventcnt, int type)
192{ 225{
255 ++fdchangecnt; 288 ++fdchangecnt;
256 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 289 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
257 fdchanges [fdchangecnt - 1] = fd; 290 fdchanges [fdchangecnt - 1] = fd;
258} 291}
259 292
293static void
294fd_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
260/* called on EBADF to verify fds */ 306/* called on EBADF to verify fds */
261static void 307static void
262fd_recheck (void) 308fd_ebadf (void)
263{ 309{
264 int fd; 310 int fd;
265 311
266 for (fd = 0; fd < anfdmax; ++fd) 312 for (fd = 0; fd < anfdmax; ++fd)
267 if (anfds [fd].events) 313 if (anfds [fd].events)
268 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 314 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
269 while (anfds [fd].head) 315 fd_kill (fd);
316}
317
318/* called on ENOMEM in select/poll to kill some fds and retry */
319static void
320fd_enomem (void)
321{
322 int fd = anfdmax;
323
324 while (fd--)
325 if (anfds [fd].events)
270 { 326 {
271 ev_io_stop (anfds [fd].head); 327 close (fd);
272 event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE); 328 fd_kill (fd);
329 return;
273 } 330 }
274} 331}
275 332
276/*****************************************************************************/ 333/*****************************************************************************/
277 334
278static struct ev_timer **timers; 335static struct ev_timer **timers;
363 420
364static void 421static void
365sigcb (struct ev_io *iow, int revents) 422sigcb (struct ev_io *iow, int revents)
366{ 423{
367 struct ev_signal *w; 424 struct ev_signal *w;
368 int sig; 425 int signum;
369 426
370 read (sigpipe [0], &revents, 1); 427 read (sigpipe [0], &revents, 1);
371 gotsig = 0; 428 gotsig = 0;
372 429
373 for (sig = signalmax; sig--; ) 430 for (signum = signalmax; signum--; )
374 if (signals [sig].gotsig) 431 if (signals [signum].gotsig)
375 { 432 {
376 signals [sig].gotsig = 0; 433 signals [signum].gotsig = 0;
377 434
378 for (w = signals [sig].head; w; w = w->next) 435 for (w = signals [signum].head; w; w = w->next)
379 event ((W)w, EV_SIGNAL); 436 event ((W)w, EV_SIGNAL);
380 } 437 }
381} 438}
382 439
383static void 440static void
420 struct ev_child *w; 477 struct ev_child *w;
421 int pid, status; 478 int pid, status;
422 479
423 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) 480 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1)
424 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) 481 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next)
425 if (w->pid == pid || w->pid == -1) 482 if (w->pid == pid || !w->pid)
426 { 483 {
427 w->status = status; 484 w->status = status;
428 event ((W)w, EV_CHILD); 485 event ((W)w, EV_CHILD);
429 } 486 }
430} 487}
431 488
432/*****************************************************************************/ 489/*****************************************************************************/
433 490
491#if EV_USE_KQUEUE
492# include "ev_kqueue.c"
493#endif
434#if EV_USE_EPOLL 494#if EV_USE_EPOLL
435# include "ev_epoll.c" 495# include "ev_epoll.c"
436#endif 496#endif
497#if EV_USE_POLL
498# include "ev_poll.c"
499#endif
437#if EV_USE_SELECT 500#if EV_USE_SELECT
438# include "ev_select.c" 501# include "ev_select.c"
439#endif 502#endif
440 503
441int 504int
448ev_version_minor (void) 511ev_version_minor (void)
449{ 512{
450 return EV_VERSION_MINOR; 513 return EV_VERSION_MINOR;
451} 514}
452 515
516/* return true if we are running with elevated privileges and ignore env variables */
517static int
518enable_secure ()
519{
520 return getuid () != geteuid ()
521 || getgid () != getegid ();
522}
523
453int ev_init (int flags) 524int ev_init (int methods)
454{ 525{
455 if (!ev_method) 526 if (!ev_method)
456 { 527 {
457#if EV_USE_MONOTONIC 528#if EV_USE_MONOTONIC
458 { 529 {
460 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 531 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
461 have_monotonic = 1; 532 have_monotonic = 1;
462 } 533 }
463#endif 534#endif
464 535
465 ev_now = ev_time (); 536 ev_now = ev_time ();
466 now = get_clock (); 537 now = get_clock ();
538 now_floor = now;
467 diff = ev_now - now; 539 diff = ev_now - now;
468 540
469 if (pipe (sigpipe)) 541 if (pipe (sigpipe))
470 return 0; 542 return 0;
471 543
544 if (methods == EVMETHOD_AUTO)
545 if (!enable_secure () && getenv ("LIBEV_METHODS"))
546 methods = atoi (getenv ("LIBEV_METHODS"));
547 else
472 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
473#if EV_USE_EPOLL 554#if EV_USE_EPOLL
474 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);
475#endif 559#endif
476#if EV_USE_SELECT 560#if EV_USE_SELECT
477 if (ev_method == EVMETHOD_NONE) select_init (flags); 561 if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods);
478#endif 562#endif
479 563
480 if (ev_method) 564 if (ev_method)
481 { 565 {
482 ev_watcher_init (&sigev, sigcb); 566 ev_watcher_init (&sigev, sigcb);
522/*****************************************************************************/ 606/*****************************************************************************/
523 607
524static void 608static void
525call_pending (void) 609call_pending (void)
526{ 610{
611 int pri;
612
613 for (pri = NUMPRI; pri--; )
527 while (pendingcnt) 614 while (pendingcnt [pri])
528 { 615 {
529 ANPENDING *p = pendings + --pendingcnt; 616 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
530 617
531 if (p->w) 618 if (p->w)
532 { 619 {
533 p->w->pending = 0; 620 p->w->pending = 0;
534 p->w->cb (p->w, p->events); 621 p->w->cb (p->w, p->events);
535 } 622 }
536 } 623 }
537} 624}
538 625
539static void 626static void
540timers_reify (void) 627timers_reify (void)
541{ 628{
601 } 688 }
602 } 689 }
603 } 690 }
604} 691}
605 692
693static int
694time_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
606static void 711static void
607time_update (void) 712time_update (void)
608{ 713{
609 int i; 714 int i;
610 715
611 ev_now = ev_time (); 716#if EV_USE_MONOTONIC
612
613 if (have_monotonic) 717 if (expect_true (have_monotonic))
614 { 718 {
719 if (time_update_monotonic ())
720 {
615 ev_tstamp odiff = diff; 721 ev_tstamp odiff = diff;
616 722
617 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 */
618 { 724 {
619 now = get_clock ();
620 diff = ev_now - now; 725 diff = ev_now - now;
621 726
622 if (fabs (odiff - diff) < MIN_TIMEJUMP) 727 if (fabs (odiff - diff) < MIN_TIMEJUMP)
623 return; /* all is well */ 728 return; /* all is well */
624 729
625 ev_now = ev_time (); 730 ev_now = ev_time ();
731 now = get_clock ();
732 now_floor = now;
626 } 733 }
627 734
628 periodics_reschedule (diff - odiff); 735 periodics_reschedule (diff - odiff);
629 /* no timer adjustment, as the monotonic clock doesn't jump */ 736 /* no timer adjustment, as the monotonic clock doesn't jump */
737 }
630 } 738 }
631 else 739 else
740#endif
632 { 741 {
742 ev_now = ev_time ();
743
633 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))
634 { 745 {
635 periodics_reschedule (ev_now - now); 746 periodics_reschedule (ev_now - now);
636 747
637 /* 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 */
638 for (i = 0; i < timercnt; ++i) 749 for (i = 0; i < timercnt; ++i)
651 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; 762 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
652 763
653 do 764 do
654 { 765 {
655 /* queue check watchers (and execute them) */ 766 /* queue check watchers (and execute them) */
656 if (preparecnt) 767 if (expect_false (preparecnt))
657 { 768 {
658 queue_events ((W *)prepares, preparecnt, EV_PREPARE); 769 queue_events ((W *)prepares, preparecnt, EV_PREPARE);
659 call_pending (); 770 call_pending ();
660 } 771 }
661 772
664 775
665 /* calculate blocking time */ 776 /* calculate blocking time */
666 777
667 /* 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
668 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 {
669 ev_now = ev_time (); 786 ev_now = ev_time ();
787 now = ev_now;
788 }
670 789
671 if (flags & EVLOOP_NONBLOCK || idlecnt) 790 if (flags & EVLOOP_NONBLOCK || idlecnt)
672 block = 0.; 791 block = 0.;
673 else 792 else
674 { 793 {
675 block = MAX_BLOCKTIME; 794 block = MAX_BLOCKTIME;
676 795
677 if (timercnt) 796 if (timercnt)
678 { 797 {
679 ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; 798 ev_tstamp to = timers [0]->at - now + method_fudge;
680 if (block > to) block = to; 799 if (block > to) block = to;
681 } 800 }
682 801
683 if (periodiccnt) 802 if (periodiccnt)
684 { 803 {
741static void 860static void
742ev_clear_pending (W w) 861ev_clear_pending (W w)
743{ 862{
744 if (w->pending) 863 if (w->pending)
745 { 864 {
746 pendings [w->pending - 1].w = 0; 865 pendings [ABSPRI (w)][w->pending - 1].w = 0;
747 w->pending = 0; 866 w->pending = 0;
748 } 867 }
749} 868}
750 869
751static void 870static void
752ev_start (W w, int active) 871ev_start (W w, int active)
753{ 872{
873 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI;
874 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
875
754 w->active = active; 876 w->active = active;
755} 877}
756 878
757static void 879static void
758ev_stop (W w) 880ev_stop (W w)
763/*****************************************************************************/ 885/*****************************************************************************/
764 886
765void 887void
766ev_io_start (struct ev_io *w) 888ev_io_start (struct ev_io *w)
767{ 889{
890 int fd = w->fd;
891
768 if (ev_is_active (w)) 892 if (ev_is_active (w))
769 return; 893 return;
770
771 int fd = w->fd;
772 894
773 assert (("ev_io_start called with negative fd", fd >= 0)); 895 assert (("ev_io_start called with negative fd", fd >= 0));
774 896
775 ev_start ((W)w, 1); 897 ev_start ((W)w, 1);
776 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 898 array_needsize (anfds, anfdmax, fd + 1, anfds_init);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines