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

Comparing libev/ev.c (file contents):
Revision 1.37 by root, Thu Nov 1 13:33:12 2007 UTC vs.
Revision 1.43 by root, Fri Nov 2 20:21:33 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)
104
105#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
106#define ABSPRI(w) ((w)->priority - EV_MINPRI)
81 107
82typedef struct ev_watcher *W; 108typedef struct ev_watcher *W;
83typedef struct ev_watcher_list *WL; 109typedef struct ev_watcher_list *WL;
84typedef struct ev_watcher_time *WT; 110typedef struct ev_watcher_time *WT;
85 111
86static ev_tstamp now, diff; /* monotonic clock */ 112static ev_tstamp now_floor, now, diff; /* monotonic clock */
87ev_tstamp ev_now; 113ev_tstamp ev_now;
88int ev_method; 114int ev_method;
89 115
90static int have_monotonic; /* runtime */ 116static int have_monotonic; /* runtime */
91 117
111 137
112static ev_tstamp 138static ev_tstamp
113get_clock (void) 139get_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
175static ANPENDING *pendings; 201static ANPENDING *pendings [NUMPRI];
176static int pendingmax, pendingcnt; 202static int pendingmax [NUMPRI], pendingcnt [NUMPRI];
177 203
178static void 204static void
179event (W w, int events) 205event (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
193static void 219static void
194queue_events (W *events, int eventcnt, int type) 220queue_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
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
263/* called on EBADF to verify fds */ 302/* called on EBADF to verify fds */
264static void 303static void
265fd_recheck (void) 304fd_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 */
315static void
316fd_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
281static struct ev_timer **timers; 331static struct ev_timer **timers;
366 416
367static void 417static void
368sigcb (struct ev_io *iow, int revents) 418sigcb (struct ev_io *iow, int revents)
369{ 419{
370 struct ev_signal *w; 420 struct ev_signal *w;
371 int sig; 421 int signum;
372 422
373 read (sigpipe [0], &revents, 1); 423 read (sigpipe [0], &revents, 1);
374 gotsig = 0; 424 gotsig = 0;
375 425
376 for (sig = signalmax; sig--; ) 426 for (signum = signalmax; signum--; )
377 if (signals [sig].gotsig) 427 if (signals [signum].gotsig)
378 { 428 {
379 signals [sig].gotsig = 0; 429 signals [signum].gotsig = 0;
380 430
381 for (w = signals [sig].head; w; w = w->next) 431 for (w = signals [signum].head; w; w = w->next)
382 event ((W)w, EV_SIGNAL); 432 event ((W)w, EV_SIGNAL);
383 } 433 }
384} 434}
385 435
386static void 436static void
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
444int 497int
451ev_version_minor (void) 504ev_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 */
510static int
511enable_secure ()
512{
513 return getuid () != geteuid ()
514 || getgid () != getegid ();
515}
516
456int ev_init (int flags) 517int 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
527static void 598static void
528call_pending (void) 599call_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
542static void 616static void
543timers_reify (void) 617timers_reify (void)
544{ 618{
604 } 678 }
605 } 679 }
606 } 680 }
607} 681}
608 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
609static void 701static void
610time_update (void) 702time_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 {
744static void 850static void
745ev_clear_pending (W w) 851ev_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
754static void 860static void
755ev_start (W w, int active) 861ev_start (W w, int active)
756{ 862{
863 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI;
864 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
865
757 w->active = active; 866 w->active = active;
758} 867}
759 868
760static void 869static void
761ev_stop (W w) 870ev_stop (W w)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines