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

Comparing libev/ev.c (file contents):
Revision 1.33 by root, Thu Nov 1 11:11:22 2007 UTC vs.
Revision 1.43 by root, Fri Nov 2 20:21:33 2007 UTC

1/* 1/*
2 * libev event processing core, watcher management
3 *
2 * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> 4 * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de>
3 * All rights reserved. 5 * All rights reserved.
4 * 6 *
5 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are 8 * modification, are permitted provided that the following conditions are
44#include <sys/types.h> 46#include <sys/types.h>
45#include <sys/wait.h> 47#include <sys/wait.h>
46#include <sys/time.h> 48#include <sys/time.h>
47#include <time.h> 49#include <time.h>
48 50
51/**/
52
49#ifndef EV_USE_MONOTONIC 53#ifndef EV_USE_MONOTONIC
50# ifdef CLOCK_MONOTONIC
51# define EV_USE_MONOTONIC 1 54# define EV_USE_MONOTONIC 1
52# endif
53#endif 55#endif
54 56
55#ifndef EV_USE_SELECT 57#ifndef EV_USE_SELECT
56# define EV_USE_SELECT 1 58# define EV_USE_SELECT 1
57#endif 59#endif
58 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
59#ifndef EV_USE_EPOLL 65#ifndef EV_USE_EPOLL
60# define EV_USE_EPOLL 0 66# define EV_USE_EPOLL 0
61#endif 67#endif
62 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
63#ifndef CLOCK_REALTIME 80#ifndef CLOCK_REALTIME
81# undef EV_USE_REALTIME
64# define EV_USE_REALTIME 0 82# define EV_USE_REALTIME 0
65#endif 83#endif
66#ifndef EV_USE_REALTIME 84
67# define EV_USE_REALTIME 1 /* posix requirement, but might be slower */ 85/**/
68#endif
69 86
70#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) */
71#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) */
72#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 */
73#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 */
74 91
75#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)
76 107
77typedef struct ev_watcher *W; 108typedef struct ev_watcher *W;
78typedef struct ev_watcher_list *WL; 109typedef struct ev_watcher_list *WL;
79typedef struct ev_watcher_time *WT; 110typedef struct ev_watcher_time *WT;
80 111
81static ev_tstamp now, diff; /* monotonic clock */ 112static ev_tstamp now_floor, now, diff; /* monotonic clock */
82ev_tstamp ev_now; 113ev_tstamp ev_now;
83int ev_method; 114int ev_method;
84 115
85static int have_monotonic; /* runtime */ 116static int have_monotonic; /* runtime */
86 117
106 137
107static ev_tstamp 138static ev_tstamp
108get_clock (void) 139get_clock (void)
109{ 140{
110#if EV_USE_MONOTONIC 141#if EV_USE_MONOTONIC
111 if (have_monotonic) 142 if (expect_true (have_monotonic))
112 { 143 {
113 struct timespec ts; 144 struct timespec ts;
114 clock_gettime (CLOCK_MONOTONIC, &ts); 145 clock_gettime (CLOCK_MONOTONIC, &ts);
115 return ts.tv_sec + ts.tv_nsec * 1e-9; 146 return ts.tv_sec + ts.tv_nsec * 1e-9;
116 } 147 }
120} 151}
121 152
122#define array_roundsize(base,n) ((n) | 4 & ~3) 153#define array_roundsize(base,n) ((n) | 4 & ~3)
123 154
124#define array_needsize(base,cur,cnt,init) \ 155#define array_needsize(base,cur,cnt,init) \
125 if ((cnt) > cur) \ 156 if (expect_false ((cnt) > cur)) \
126 { \ 157 { \
127 int newcnt = cur; \ 158 int newcnt = cur; \
128 do \ 159 do \
129 { \ 160 { \
130 newcnt = array_roundsize (base, newcnt << 1); \ 161 newcnt = array_roundsize (base, newcnt << 1); \
165{ 196{
166 W w; 197 W w;
167 int events; 198 int events;
168} ANPENDING; 199} ANPENDING;
169 200
170static ANPENDING *pendings; 201static ANPENDING *pendings [NUMPRI];
171static int pendingmax, pendingcnt; 202static int pendingmax [NUMPRI], pendingcnt [NUMPRI];
172 203
173static void 204static void
174event (W w, int events) 205event (W w, int events)
175{ 206{
176 if (w->pending) 207 if (w->pending)
177 { 208 {
178 pendings [w->pending - 1].events |= events; 209 pendings [ABSPRI (w)][w->pending - 1].events |= events;
179 return; 210 return;
180 } 211 }
181 212
182 w->pending = ++pendingcnt; 213 w->pending = ++pendingcnt [ABSPRI (w)];
183 array_needsize (pendings, pendingmax, pendingcnt, ); 214 array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], );
184 pendings [pendingcnt - 1].w = w; 215 pendings [ABSPRI (w)][w->pending - 1].w = w;
185 pendings [pendingcnt - 1].events = events; 216 pendings [ABSPRI (w)][w->pending - 1].events = events;
186} 217}
187 218
188static void 219static void
189queue_events (W *events, int eventcnt, int type) 220queue_events (W *events, int eventcnt, int type)
190{ 221{
253 ++fdchangecnt; 284 ++fdchangecnt;
254 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 285 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
255 fdchanges [fdchangecnt - 1] = fd; 286 fdchanges [fdchangecnt - 1] = fd;
256} 287}
257 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
258/* called on EBADF to verify fds */ 302/* called on EBADF to verify fds */
259static void 303static void
260fd_recheck (void) 304fd_ebadf (void)
261{ 305{
262 int fd; 306 int fd;
263 307
264 for (fd = 0; fd < anfdmax; ++fd) 308 for (fd = 0; fd < anfdmax; ++fd)
265 if (anfds [fd].events) 309 if (anfds [fd].events)
266 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 310 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
267 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)
268 { 322 {
269 ev_io_stop (anfds [fd].head); 323 close (fd);
270 event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE); 324 fd_kill (fd);
325 return;
271 } 326 }
272} 327}
273 328
274/*****************************************************************************/ 329/*****************************************************************************/
275 330
276static struct ev_timer **timers; 331static struct ev_timer **timers;
323/*****************************************************************************/ 378/*****************************************************************************/
324 379
325typedef struct 380typedef struct
326{ 381{
327 struct ev_signal *head; 382 struct ev_signal *head;
328 sig_atomic_t gotsig; 383 sig_atomic_t volatile gotsig;
329} ANSIG; 384} ANSIG;
330 385
331static ANSIG *signals; 386static ANSIG *signals;
332static int signalmax; 387static int signalmax;
333 388
334static int sigpipe [2]; 389static int sigpipe [2];
335static sig_atomic_t gotsig; 390static sig_atomic_t volatile gotsig;
336static struct ev_io sigev; 391static struct ev_io sigev;
337 392
338static void 393static void
339signals_init (ANSIG *base, int count) 394signals_init (ANSIG *base, int count)
340{ 395{
353 signals [signum - 1].gotsig = 1; 408 signals [signum - 1].gotsig = 1;
354 409
355 if (!gotsig) 410 if (!gotsig)
356 { 411 {
357 gotsig = 1; 412 gotsig = 1;
358 write (sigpipe [1], &gotsig, 1); 413 write (sigpipe [1], &signum, 1);
359 } 414 }
360} 415}
361 416
362static void 417static void
363sigcb (struct ev_io *iow, int revents) 418sigcb (struct ev_io *iow, int revents)
364{ 419{
365 struct ev_signal *w; 420 struct ev_signal *w;
366 int sig; 421 int signum;
367 422
423 read (sigpipe [0], &revents, 1);
368 gotsig = 0; 424 gotsig = 0;
369 read (sigpipe [0], &revents, 1);
370 425
371 for (sig = signalmax; sig--; ) 426 for (signum = signalmax; signum--; )
372 if (signals [sig].gotsig) 427 if (signals [signum].gotsig)
373 { 428 {
374 signals [sig].gotsig = 0; 429 signals [signum].gotsig = 0;
375 430
376 for (w = signals [sig].head; w; w = w->next) 431 for (w = signals [signum].head; w; w = w->next)
377 event ((W)w, EV_SIGNAL); 432 event ((W)w, EV_SIGNAL);
378 } 433 }
379} 434}
380 435
381static void 436static void
418 struct ev_child *w; 473 struct ev_child *w;
419 int pid, status; 474 int pid, status;
420 475
421 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) 476 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1)
422 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) 477 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next)
423 if (w->pid == pid || w->pid == -1) 478 if (w->pid == pid || !w->pid)
424 { 479 {
425 w->status = status; 480 w->status = status;
426 event ((W)w, EV_CHILD); 481 event ((W)w, EV_CHILD);
427 } 482 }
428} 483}
430/*****************************************************************************/ 485/*****************************************************************************/
431 486
432#if EV_USE_EPOLL 487#if EV_USE_EPOLL
433# include "ev_epoll.c" 488# include "ev_epoll.c"
434#endif 489#endif
490#if EV_USE_POLL
491# include "ev_poll.c"
492#endif
435#if EV_USE_SELECT 493#if EV_USE_SELECT
436# include "ev_select.c" 494# include "ev_select.c"
437#endif 495#endif
438 496
439int 497int
446ev_version_minor (void) 504ev_version_minor (void)
447{ 505{
448 return EV_VERSION_MINOR; 506 return EV_VERSION_MINOR;
449} 507}
450 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
451int ev_init (int flags) 517int ev_init (int methods)
452{ 518{
453 if (!ev_method) 519 if (!ev_method)
454 { 520 {
455#if EV_USE_MONOTONIC 521#if EV_USE_MONOTONIC
456 { 522 {
458 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 524 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
459 have_monotonic = 1; 525 have_monotonic = 1;
460 } 526 }
461#endif 527#endif
462 528
463 ev_now = ev_time (); 529 ev_now = ev_time ();
464 now = get_clock (); 530 now = get_clock ();
531 now_floor = now;
465 diff = ev_now - now; 532 diff = ev_now - now;
466 533
467 if (pipe (sigpipe)) 534 if (pipe (sigpipe))
468 return 0; 535 return 0;
469 536
537 if (methods == EVMETHOD_AUTO)
538 if (!enable_secure () && getenv ("LIBEV_METHODS"))
539 methods = atoi (getenv ("LIBEV_METHODS"));
540 else
470 ev_method = EVMETHOD_NONE; 541 methods = EVMETHOD_ANY;
542
543 ev_method = 0;
471#if EV_USE_EPOLL 544#if EV_USE_EPOLL
472 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);
473#endif 549#endif
474#if EV_USE_SELECT 550#if EV_USE_SELECT
475 if (ev_method == EVMETHOD_NONE) select_init (flags); 551 if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods);
476#endif 552#endif
477 553
478 if (ev_method) 554 if (ev_method)
479 { 555 {
480 ev_watcher_init (&sigev, sigcb); 556 ev_watcher_init (&sigev, sigcb);
489} 565}
490 566
491/*****************************************************************************/ 567/*****************************************************************************/
492 568
493void 569void
494ev_prefork (void) 570ev_fork_prepare (void)
495{ 571{
496 /* nop */ 572 /* nop */
497} 573}
498 574
499void 575void
500ev_postfork_parent (void) 576ev_fork_parent (void)
501{ 577{
502 /* nop */ 578 /* nop */
503} 579}
504 580
505void 581void
506ev_postfork_child (void) 582ev_fork_child (void)
507{ 583{
508#if EV_USE_EPOLL 584#if EV_USE_EPOLL
509 if (ev_method == EVMETHOD_EPOLL) 585 if (ev_method == EVMETHOD_EPOLL)
510 epoll_postfork_child (); 586 epoll_postfork_child ();
511#endif 587#endif
520/*****************************************************************************/ 596/*****************************************************************************/
521 597
522static void 598static void
523call_pending (void) 599call_pending (void)
524{ 600{
601 int pri;
602
603 for (pri = NUMPRI; pri--; )
525 while (pendingcnt) 604 while (pendingcnt [pri])
526 { 605 {
527 ANPENDING *p = pendings + --pendingcnt; 606 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
528 607
529 if (p->w) 608 if (p->w)
530 { 609 {
531 p->w->pending = 0; 610 p->w->pending = 0;
532 p->w->cb (p->w, p->events); 611 p->w->cb (p->w, p->events);
533 } 612 }
534 } 613 }
535} 614}
536 615
537static void 616static void
538timers_reify (void) 617timers_reify (void)
539{ 618{
599 } 678 }
600 } 679 }
601 } 680 }
602} 681}
603 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
604static void 701static void
605time_update (void) 702time_update (void)
606{ 703{
607 int i; 704 int i;
608 705
609 ev_now = ev_time (); 706#if EV_USE_MONOTONIC
610
611 if (have_monotonic) 707 if (expect_true (have_monotonic))
612 { 708 {
709 if (time_update_monotonic ())
710 {
613 ev_tstamp odiff = diff; 711 ev_tstamp odiff = diff;
614 712
615 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 */
616 { 714 {
617 now = get_clock ();
618 diff = ev_now - now; 715 diff = ev_now - now;
619 716
620 if (fabs (odiff - diff) < MIN_TIMEJUMP) 717 if (fabs (odiff - diff) < MIN_TIMEJUMP)
621 return; /* all is well */ 718 return; /* all is well */
622 719
623 ev_now = ev_time (); 720 ev_now = ev_time ();
721 now = get_clock ();
722 now_floor = now;
624 } 723 }
625 724
626 periodics_reschedule (diff - odiff); 725 periodics_reschedule (diff - odiff);
627 /* no timer adjustment, as the monotonic clock doesn't jump */ 726 /* no timer adjustment, as the monotonic clock doesn't jump */
727 }
628 } 728 }
629 else 729 else
730#endif
630 { 731 {
732 ev_now = ev_time ();
733
631 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))
632 { 735 {
633 periodics_reschedule (ev_now - now); 736 periodics_reschedule (ev_now - now);
634 737
635 /* 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 */
636 for (i = 0; i < timercnt; ++i) 739 for (i = 0; i < timercnt; ++i)
649 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; 752 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
650 753
651 do 754 do
652 { 755 {
653 /* queue check watchers (and execute them) */ 756 /* queue check watchers (and execute them) */
654 if (preparecnt) 757 if (expect_false (preparecnt))
655 { 758 {
656 queue_events ((W *)prepares, preparecnt, EV_PREPARE); 759 queue_events ((W *)prepares, preparecnt, EV_PREPARE);
657 call_pending (); 760 call_pending ();
658 } 761 }
659 762
662 765
663 /* calculate blocking time */ 766 /* calculate blocking time */
664 767
665 /* 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
666 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 {
667 ev_now = ev_time (); 776 ev_now = ev_time ();
777 now = ev_now;
778 }
668 779
669 if (flags & EVLOOP_NONBLOCK || idlecnt) 780 if (flags & EVLOOP_NONBLOCK || idlecnt)
670 block = 0.; 781 block = 0.;
671 else 782 else
672 { 783 {
673 block = MAX_BLOCKTIME; 784 block = MAX_BLOCKTIME;
674 785
675 if (timercnt) 786 if (timercnt)
676 { 787 {
677 ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; 788 ev_tstamp to = timers [0]->at - now + method_fudge;
678 if (block > to) block = to; 789 if (block > to) block = to;
679 } 790 }
680 791
681 if (periodiccnt) 792 if (periodiccnt)
682 { 793 {
739static void 850static void
740ev_clear_pending (W w) 851ev_clear_pending (W w)
741{ 852{
742 if (w->pending) 853 if (w->pending)
743 { 854 {
744 pendings [w->pending - 1].w = 0; 855 pendings [ABSPRI (w)][w->pending - 1].w = 0;
745 w->pending = 0; 856 w->pending = 0;
746 } 857 }
747} 858}
748 859
749static void 860static void
750ev_start (W w, int active) 861ev_start (W w, int active)
751{ 862{
863 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI;
864 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
865
752 w->active = active; 866 w->active = active;
753} 867}
754 868
755static void 869static void
756ev_stop (W w) 870ev_stop (W w)
761/*****************************************************************************/ 875/*****************************************************************************/
762 876
763void 877void
764ev_io_start (struct ev_io *w) 878ev_io_start (struct ev_io *w)
765{ 879{
880 int fd = w->fd;
881
766 if (ev_is_active (w)) 882 if (ev_is_active (w))
767 return; 883 return;
768
769 int fd = w->fd;
770 884
771 assert (("ev_io_start called with negative fd", fd >= 0)); 885 assert (("ev_io_start called with negative fd", fd >= 0));
772 886
773 ev_start ((W)w, 1); 887 ev_start ((W)w, 1);
774 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 888 array_needsize (anfds, anfdmax, fd + 1, anfds_init);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines