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

Comparing libev/ev.c (file contents):
Revision 1.38 by root, Thu Nov 1 15:21:13 2007 UTC vs.
Revision 1.46 by root, Sat Nov 3 09:20:12 2007 UTC

42#include <stdio.h> 42#include <stdio.h>
43 43
44#include <assert.h> 44#include <assert.h>
45#include <errno.h> 45#include <errno.h>
46#include <sys/types.h> 46#include <sys/types.h>
47#ifndef WIN32
47#include <sys/wait.h> 48# include <sys/wait.h>
49#endif
48#include <sys/time.h> 50#include <sys/time.h>
49#include <time.h> 51#include <time.h>
50 52
53/**/
54
51#ifndef EV_USE_MONOTONIC 55#ifndef EV_USE_MONOTONIC
52# define EV_USE_MONOTONIC 1 56# define EV_USE_MONOTONIC 1
53#endif 57#endif
58
59#ifndef EV_USE_SELECT
60# define EV_USE_SELECT 1
61#endif
62
63#ifndef EV_USE_POLL
64# define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */
65#endif
66
67#ifndef EV_USE_EPOLL
68# define EV_USE_EPOLL 0
69#endif
70
71#ifndef EV_USE_KQUEUE
72# define EV_USE_KQUEUE 0
73#endif
74
75#ifndef EV_USE_REALTIME
76# define EV_USE_REALTIME 1
77#endif
78
79/**/
54 80
55#ifndef CLOCK_MONOTONIC 81#ifndef CLOCK_MONOTONIC
56# undef EV_USE_MONOTONIC 82# undef EV_USE_MONOTONIC
57# define EV_USE_MONOTONIC 0 83# define EV_USE_MONOTONIC 0
58#endif 84#endif
59 85
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 86#ifndef CLOCK_REALTIME
87# undef EV_USE_REALTIME
69# define EV_USE_REALTIME 0 88# define EV_USE_REALTIME 0
70#endif 89#endif
71#ifndef EV_USE_REALTIME 90
72# define EV_USE_REALTIME 1 /* posix requirement, but might be slower */ 91/**/
73#endif
74 92
75#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 93#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) */ 94#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 */ 95#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 */ 96/*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */
79 97
80#include "ev.h" 98#include "ev.h"
99
100#if __GNUC__ >= 3
101# define expect(expr,value) __builtin_expect ((expr),(value))
102# define inline inline
103#else
104# define expect(expr,value) (expr)
105# define inline static
106#endif
107
108#define expect_false(expr) expect ((expr) != 0, 0)
109#define expect_true(expr) expect ((expr) != 0, 1)
110
111#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
112#define ABSPRI(w) ((w)->priority - EV_MINPRI)
81 113
82typedef struct ev_watcher *W; 114typedef struct ev_watcher *W;
83typedef struct ev_watcher_list *WL; 115typedef struct ev_watcher_list *WL;
84typedef struct ev_watcher_time *WT; 116typedef struct ev_watcher_time *WT;
85 117
86static ev_tstamp now, diff; /* monotonic clock */ 118static ev_tstamp now_floor, now, diff; /* monotonic clock */
87ev_tstamp ev_now; 119ev_tstamp ev_now;
88int ev_method; 120int ev_method;
89 121
90static int have_monotonic; /* runtime */ 122static int have_monotonic; /* runtime */
91 123
111 143
112static ev_tstamp 144static ev_tstamp
113get_clock (void) 145get_clock (void)
114{ 146{
115#if EV_USE_MONOTONIC 147#if EV_USE_MONOTONIC
116 if (have_monotonic) 148 if (expect_true (have_monotonic))
117 { 149 {
118 struct timespec ts; 150 struct timespec ts;
119 clock_gettime (CLOCK_MONOTONIC, &ts); 151 clock_gettime (CLOCK_MONOTONIC, &ts);
120 return ts.tv_sec + ts.tv_nsec * 1e-9; 152 return ts.tv_sec + ts.tv_nsec * 1e-9;
121 } 153 }
125} 157}
126 158
127#define array_roundsize(base,n) ((n) | 4 & ~3) 159#define array_roundsize(base,n) ((n) | 4 & ~3)
128 160
129#define array_needsize(base,cur,cnt,init) \ 161#define array_needsize(base,cur,cnt,init) \
130 if ((cnt) > cur) \ 162 if (expect_false ((cnt) > cur)) \
131 { \ 163 { \
132 int newcnt = cur; \ 164 int newcnt = cur; \
133 do \ 165 do \
134 { \ 166 { \
135 newcnt = array_roundsize (base, newcnt << 1); \ 167 newcnt = array_roundsize (base, newcnt << 1); \
170{ 202{
171 W w; 203 W w;
172 int events; 204 int events;
173} ANPENDING; 205} ANPENDING;
174 206
175static ANPENDING *pendings; 207static ANPENDING *pendings [NUMPRI];
176static int pendingmax, pendingcnt; 208static int pendingmax [NUMPRI], pendingcnt [NUMPRI];
177 209
178static void 210static void
179event (W w, int events) 211event (W w, int events)
180{ 212{
181 if (w->pending) 213 if (w->pending)
182 { 214 {
183 pendings [w->pending - 1].events |= events; 215 pendings [ABSPRI (w)][w->pending - 1].events |= events;
184 return; 216 return;
185 } 217 }
186 218
187 w->pending = ++pendingcnt; 219 w->pending = ++pendingcnt [ABSPRI (w)];
188 array_needsize (pendings, pendingmax, pendingcnt, ); 220 array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], );
189 pendings [pendingcnt - 1].w = w; 221 pendings [ABSPRI (w)][w->pending - 1].w = w;
190 pendings [pendingcnt - 1].events = events; 222 pendings [ABSPRI (w)][w->pending - 1].events = events;
191} 223}
192 224
193static void 225static void
194queue_events (W *events, int eventcnt, int type) 226queue_events (W *events, int eventcnt, int type)
195{ 227{
258 ++fdchangecnt; 290 ++fdchangecnt;
259 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 291 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
260 fdchanges [fdchangecnt - 1] = fd; 292 fdchanges [fdchangecnt - 1] = fd;
261} 293}
262 294
295static void
296fd_kill (int fd)
297{
298 struct ev_io *w;
299
300 printf ("killing fd %d\n", fd);//D
301 while ((w = anfds [fd].head))
302 {
303 ev_io_stop (w);
304 event ((W)w, EV_ERROR | EV_READ | EV_WRITE);
305 }
306}
307
263/* called on EBADF to verify fds */ 308/* called on EBADF to verify fds */
264static void 309static void
265fd_recheck (void) 310fd_ebadf (void)
266{ 311{
267 int fd; 312 int fd;
268 313
269 for (fd = 0; fd < anfdmax; ++fd) 314 for (fd = 0; fd < anfdmax; ++fd)
270 if (anfds [fd].events) 315 if (anfds [fd].events)
271 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 316 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
272 while (anfds [fd].head) 317 fd_kill (fd);
318}
319
320/* called on ENOMEM in select/poll to kill some fds and retry */
321static void
322fd_enomem (void)
323{
324 int fd = anfdmax;
325
326 while (fd--)
327 if (anfds [fd].events)
273 { 328 {
274 ev_io_stop (anfds [fd].head); 329 close (fd);
275 event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE); 330 fd_kill (fd);
331 return;
276 } 332 }
277} 333}
278 334
279/*****************************************************************************/ 335/*****************************************************************************/
280 336
281static struct ev_timer **timers; 337static struct ev_timer **timers;
384} 440}
385 441
386static void 442static void
387siginit (void) 443siginit (void)
388{ 444{
445#ifndef WIN32
389 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); 446 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC);
390 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); 447 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC);
391 448
392 /* rather than sort out wether we really need nb, set it */ 449 /* rather than sort out wether we really need nb, set it */
393 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); 450 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
394 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 451 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
452#endif
395 453
396 ev_io_set (&sigev, sigpipe [0], EV_READ); 454 ev_io_set (&sigev, sigpipe [0], EV_READ);
397 ev_io_start (&sigev); 455 ev_io_start (&sigev);
398} 456}
399 457
411/*****************************************************************************/ 469/*****************************************************************************/
412 470
413static struct ev_child *childs [PID_HASHSIZE]; 471static struct ev_child *childs [PID_HASHSIZE];
414static struct ev_signal childev; 472static struct ev_signal childev;
415 473
474#ifndef WIN32
475
416#ifndef WCONTINUED 476#ifndef WCONTINUED
417# define WCONTINUED 0 477# define WCONTINUED 0
418#endif 478#endif
419 479
420static void 480static void
423 struct ev_child *w; 483 struct ev_child *w;
424 int pid, status; 484 int pid, status;
425 485
426 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) 486 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1)
427 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) 487 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next)
428 if (w->pid == pid || w->pid == -1) 488 if (w->pid == pid || !w->pid)
429 { 489 {
490 w->rpid = pid;
430 w->status = status; 491 w->rstatus = status;
431 event ((W)w, EV_CHILD); 492 event ((W)w, EV_CHILD);
432 } 493 }
433} 494}
434 495
435/*****************************************************************************/ 496#endif
436 497
498/*****************************************************************************/
499
500#if EV_USE_KQUEUE
501# include "ev_kqueue.c"
502#endif
437#if EV_USE_EPOLL 503#if EV_USE_EPOLL
438# include "ev_epoll.c" 504# include "ev_epoll.c"
439#endif 505#endif
506#if EV_USE_POLL
507# include "ev_poll.c"
508#endif
440#if EV_USE_SELECT 509#if EV_USE_SELECT
441# include "ev_select.c" 510# include "ev_select.c"
442#endif 511#endif
443 512
444int 513int
451ev_version_minor (void) 520ev_version_minor (void)
452{ 521{
453 return EV_VERSION_MINOR; 522 return EV_VERSION_MINOR;
454} 523}
455 524
525/* return true if we are running with elevated privileges and ignore env variables */
526static int
527enable_secure ()
528{
529 return getuid () != geteuid ()
530 || getgid () != getegid ();
531}
532
456int ev_init (int flags) 533int ev_init (int methods)
457{ 534{
458 if (!ev_method) 535 if (!ev_method)
459 { 536 {
460#if EV_USE_MONOTONIC 537#if EV_USE_MONOTONIC
461 { 538 {
463 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 540 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
464 have_monotonic = 1; 541 have_monotonic = 1;
465 } 542 }
466#endif 543#endif
467 544
468 ev_now = ev_time (); 545 ev_now = ev_time ();
469 now = get_clock (); 546 now = get_clock ();
547 now_floor = now;
470 diff = ev_now - now; 548 diff = ev_now - now;
471 549
472 if (pipe (sigpipe)) 550 if (pipe (sigpipe))
473 return 0; 551 return 0;
474 552
553 if (methods == EVMETHOD_AUTO)
554 if (!enable_secure () && getenv ("LIBEV_METHODS"))
555 methods = atoi (getenv ("LIBEV_METHODS"));
556 else
475 ev_method = EVMETHOD_NONE; 557 methods = EVMETHOD_ANY;
558
559 ev_method = 0;
560#if EV_USE_KQUEUE
561 if (!ev_method && (methods & EVMETHOD_KQUEUE)) kqueue_init (methods);
562#endif
476#if EV_USE_EPOLL 563#if EV_USE_EPOLL
477 if (ev_method == EVMETHOD_NONE) epoll_init (flags); 564 if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods);
565#endif
566#if EV_USE_POLL
567 if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods);
478#endif 568#endif
479#if EV_USE_SELECT 569#if EV_USE_SELECT
480 if (ev_method == EVMETHOD_NONE) select_init (flags); 570 if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods);
481#endif 571#endif
482 572
483 if (ev_method) 573 if (ev_method)
484 { 574 {
485 ev_watcher_init (&sigev, sigcb); 575 ev_watcher_init (&sigev, sigcb);
486 siginit (); 576 siginit ();
487 577
578#ifndef WIN32
488 ev_signal_init (&childev, childcb, SIGCHLD); 579 ev_signal_init (&childev, childcb, SIGCHLD);
489 ev_signal_start (&childev); 580 ev_signal_start (&childev);
581#endif
490 } 582 }
491 } 583 }
492 584
493 return ev_method; 585 return ev_method;
494} 586}
525/*****************************************************************************/ 617/*****************************************************************************/
526 618
527static void 619static void
528call_pending (void) 620call_pending (void)
529{ 621{
622 int pri;
623
624 for (pri = NUMPRI; pri--; )
530 while (pendingcnt) 625 while (pendingcnt [pri])
531 { 626 {
532 ANPENDING *p = pendings + --pendingcnt; 627 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
533 628
534 if (p->w) 629 if (p->w)
535 { 630 {
536 p->w->pending = 0; 631 p->w->pending = 0;
537 p->w->cb (p->w, p->events); 632 p->w->cb (p->w, p->events);
538 } 633 }
539 } 634 }
540} 635}
541 636
542static void 637static void
543timers_reify (void) 638timers_reify (void)
544{ 639{
604 } 699 }
605 } 700 }
606 } 701 }
607} 702}
608 703
704static int
705time_update_monotonic (void)
706{
707 now = get_clock ();
708
709 if (expect_true (now - now_floor < MIN_TIMEJUMP * .5))
710 {
711 ev_now = now + diff;
712 return 0;
713 }
714 else
715 {
716 now_floor = now;
717 ev_now = ev_time ();
718 return 1;
719 }
720}
721
609static void 722static void
610time_update (void) 723time_update (void)
611{ 724{
612 int i; 725 int i;
613 726
614 ev_now = ev_time (); 727#if EV_USE_MONOTONIC
615
616 if (have_monotonic) 728 if (expect_true (have_monotonic))
617 { 729 {
730 if (time_update_monotonic ())
731 {
618 ev_tstamp odiff = diff; 732 ev_tstamp odiff = diff;
619 733
620 for (i = 4; --i; ) /* loop a few times, before making important decisions */ 734 for (i = 4; --i; ) /* loop a few times, before making important decisions */
621 { 735 {
622 now = get_clock ();
623 diff = ev_now - now; 736 diff = ev_now - now;
624 737
625 if (fabs (odiff - diff) < MIN_TIMEJUMP) 738 if (fabs (odiff - diff) < MIN_TIMEJUMP)
626 return; /* all is well */ 739 return; /* all is well */
627 740
628 ev_now = ev_time (); 741 ev_now = ev_time ();
742 now = get_clock ();
743 now_floor = now;
629 } 744 }
630 745
631 periodics_reschedule (diff - odiff); 746 periodics_reschedule (diff - odiff);
632 /* no timer adjustment, as the monotonic clock doesn't jump */ 747 /* no timer adjustment, as the monotonic clock doesn't jump */
748 }
633 } 749 }
634 else 750 else
751#endif
635 { 752 {
753 ev_now = ev_time ();
754
636 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) 755 if (expect_false (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
637 { 756 {
638 periodics_reschedule (ev_now - now); 757 periodics_reschedule (ev_now - now);
639 758
640 /* adjust timers. this is easy, as the offset is the same for all */ 759 /* adjust timers. this is easy, as the offset is the same for all */
641 for (i = 0; i < timercnt; ++i) 760 for (i = 0; i < timercnt; ++i)
654 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; 773 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
655 774
656 do 775 do
657 { 776 {
658 /* queue check watchers (and execute them) */ 777 /* queue check watchers (and execute them) */
659 if (preparecnt) 778 if (expect_false (preparecnt))
660 { 779 {
661 queue_events ((W *)prepares, preparecnt, EV_PREPARE); 780 queue_events ((W *)prepares, preparecnt, EV_PREPARE);
662 call_pending (); 781 call_pending ();
663 } 782 }
664 783
667 786
668 /* calculate blocking time */ 787 /* calculate blocking time */
669 788
670 /* we only need this for !monotonic clockor timers, but as we basically 789 /* we only need this for !monotonic clockor timers, but as we basically
671 always have timers, we just calculate it always */ 790 always have timers, we just calculate it always */
791#if EV_USE_MONOTONIC
792 if (expect_true (have_monotonic))
793 time_update_monotonic ();
794 else
795#endif
796 {
672 ev_now = ev_time (); 797 ev_now = ev_time ();
798 now = ev_now;
799 }
673 800
674 if (flags & EVLOOP_NONBLOCK || idlecnt) 801 if (flags & EVLOOP_NONBLOCK || idlecnt)
675 block = 0.; 802 block = 0.;
676 else 803 else
677 { 804 {
678 block = MAX_BLOCKTIME; 805 block = MAX_BLOCKTIME;
679 806
680 if (timercnt) 807 if (timercnt)
681 { 808 {
682 ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; 809 ev_tstamp to = timers [0]->at - now + method_fudge;
683 if (block > to) block = to; 810 if (block > to) block = to;
684 } 811 }
685 812
686 if (periodiccnt) 813 if (periodiccnt)
687 { 814 {
744static void 871static void
745ev_clear_pending (W w) 872ev_clear_pending (W w)
746{ 873{
747 if (w->pending) 874 if (w->pending)
748 { 875 {
749 pendings [w->pending - 1].w = 0; 876 pendings [ABSPRI (w)][w->pending - 1].w = 0;
750 w->pending = 0; 877 w->pending = 0;
751 } 878 }
752} 879}
753 880
754static void 881static void
755ev_start (W w, int active) 882ev_start (W w, int active)
756{ 883{
884 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI;
885 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
886
757 w->active = active; 887 w->active = active;
758} 888}
759 889
760static void 890static void
761ev_stop (W w) 891ev_stop (W w)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines