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.41 by root, Fri Nov 2 16:54:34 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)
78 104
79typedef struct ev_watcher *W; 105typedef struct ev_watcher *W;
80typedef struct ev_watcher_list *WL; 106typedef struct ev_watcher_list *WL;
81typedef struct ev_watcher_time *WT; 107typedef struct ev_watcher_time *WT;
82 108
83static ev_tstamp now, diff; /* monotonic clock */ 109static ev_tstamp now_floor, now, diff; /* monotonic clock */
84ev_tstamp ev_now; 110ev_tstamp ev_now;
85int ev_method; 111int ev_method;
86 112
87static int have_monotonic; /* runtime */ 113static int have_monotonic; /* runtime */
88 114
108 134
109static ev_tstamp 135static ev_tstamp
110get_clock (void) 136get_clock (void)
111{ 137{
112#if EV_USE_MONOTONIC 138#if EV_USE_MONOTONIC
113 if (have_monotonic) 139 if (expect_true (have_monotonic))
114 { 140 {
115 struct timespec ts; 141 struct timespec ts;
116 clock_gettime (CLOCK_MONOTONIC, &ts); 142 clock_gettime (CLOCK_MONOTONIC, &ts);
117 return ts.tv_sec + ts.tv_nsec * 1e-9; 143 return ts.tv_sec + ts.tv_nsec * 1e-9;
118 } 144 }
122} 148}
123 149
124#define array_roundsize(base,n) ((n) | 4 & ~3) 150#define array_roundsize(base,n) ((n) | 4 & ~3)
125 151
126#define array_needsize(base,cur,cnt,init) \ 152#define array_needsize(base,cur,cnt,init) \
127 if ((cnt) > cur) \ 153 if (expect_false ((cnt) > cur)) \
128 { \ 154 { \
129 int newcnt = cur; \ 155 int newcnt = cur; \
130 do \ 156 do \
131 { \ 157 { \
132 newcnt = array_roundsize (base, newcnt << 1); \ 158 newcnt = array_roundsize (base, newcnt << 1); \
255 ++fdchangecnt; 281 ++fdchangecnt;
256 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 282 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
257 fdchanges [fdchangecnt - 1] = fd; 283 fdchanges [fdchangecnt - 1] = fd;
258} 284}
259 285
286static void
287fd_kill (int fd)
288{
289 struct ev_io *w;
290
291 printf ("killing fd %d\n", fd);//D
292 while ((w = anfds [fd].head))
293 {
294 ev_io_stop (w);
295 event ((W)w, EV_ERROR | EV_READ | EV_WRITE);
296 }
297}
298
260/* called on EBADF to verify fds */ 299/* called on EBADF to verify fds */
261static void 300static void
262fd_recheck (void) 301fd_ebadf (void)
263{ 302{
264 int fd; 303 int fd;
265 304
266 for (fd = 0; fd < anfdmax; ++fd) 305 for (fd = 0; fd < anfdmax; ++fd)
267 if (anfds [fd].events) 306 if (anfds [fd].events)
268 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 307 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
269 while (anfds [fd].head) 308 fd_kill (fd);
309}
310
311/* called on ENOMEM in select/poll to kill some fds and retry */
312static void
313fd_enomem (void)
314{
315 int fd = anfdmax;
316
317 while (fd--)
318 if (anfds [fd].events)
270 { 319 {
271 ev_io_stop (anfds [fd].head); 320 close (fd);
272 event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE); 321 fd_kill (fd);
322 return;
273 } 323 }
274} 324}
275 325
276/*****************************************************************************/ 326/*****************************************************************************/
277 327
278static struct ev_timer **timers; 328static struct ev_timer **timers;
363 413
364static void 414static void
365sigcb (struct ev_io *iow, int revents) 415sigcb (struct ev_io *iow, int revents)
366{ 416{
367 struct ev_signal *w; 417 struct ev_signal *w;
368 int sig; 418 int signum;
369 419
370 read (sigpipe [0], &revents, 1); 420 read (sigpipe [0], &revents, 1);
371 gotsig = 0; 421 gotsig = 0;
372 422
373 for (sig = signalmax; sig--; ) 423 for (signum = signalmax; signum--; )
374 if (signals [sig].gotsig) 424 if (signals [signum].gotsig)
375 { 425 {
376 signals [sig].gotsig = 0; 426 signals [signum].gotsig = 0;
377 427
378 for (w = signals [sig].head; w; w = w->next) 428 for (w = signals [signum].head; w; w = w->next)
379 event ((W)w, EV_SIGNAL); 429 event ((W)w, EV_SIGNAL);
380 } 430 }
381} 431}
382 432
383static void 433static void
420 struct ev_child *w; 470 struct ev_child *w;
421 int pid, status; 471 int pid, status;
422 472
423 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) 473 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1)
424 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) 474 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next)
425 if (w->pid == pid || w->pid == -1) 475 if (w->pid == pid || !w->pid)
426 { 476 {
427 w->status = status; 477 w->status = status;
428 event ((W)w, EV_CHILD); 478 event ((W)w, EV_CHILD);
429 } 479 }
430} 480}
432/*****************************************************************************/ 482/*****************************************************************************/
433 483
434#if EV_USE_EPOLL 484#if EV_USE_EPOLL
435# include "ev_epoll.c" 485# include "ev_epoll.c"
436#endif 486#endif
487#if EV_USE_POLL
488# include "ev_poll.c"
489#endif
437#if EV_USE_SELECT 490#if EV_USE_SELECT
438# include "ev_select.c" 491# include "ev_select.c"
439#endif 492#endif
440 493
441int 494int
448ev_version_minor (void) 501ev_version_minor (void)
449{ 502{
450 return EV_VERSION_MINOR; 503 return EV_VERSION_MINOR;
451} 504}
452 505
506/* return true if we are running with elevated privileges and ignore env variables */
507static int
508enable_secure ()
509{
510 return getuid () != geteuid ()
511 || getgid () != getegid ();
512}
513
453int ev_init (int flags) 514int ev_init (int methods)
454{ 515{
455 if (!ev_method) 516 if (!ev_method)
456 { 517 {
457#if EV_USE_MONOTONIC 518#if EV_USE_MONOTONIC
458 { 519 {
460 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 521 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
461 have_monotonic = 1; 522 have_monotonic = 1;
462 } 523 }
463#endif 524#endif
464 525
465 ev_now = ev_time (); 526 ev_now = ev_time ();
466 now = get_clock (); 527 now = get_clock ();
528 now_floor = now;
467 diff = ev_now - now; 529 diff = ev_now - now;
468 530
469 if (pipe (sigpipe)) 531 if (pipe (sigpipe))
470 return 0; 532 return 0;
471 533
534 if (methods == EVMETHOD_AUTO)
535 if (!enable_secure () && getenv ("LIBEV_METHODS"))
536 methods = atoi (getenv ("LIBEV_METHODS"));
537 else
472 ev_method = EVMETHOD_NONE; 538 methods = EVMETHOD_ANY;
539
540 ev_method = 0;
473#if EV_USE_EPOLL 541#if EV_USE_EPOLL
474 if (ev_method == EVMETHOD_NONE) epoll_init (flags); 542 if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods);
543#endif
544#if EV_USE_POLL
545 if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods);
475#endif 546#endif
476#if EV_USE_SELECT 547#if EV_USE_SELECT
477 if (ev_method == EVMETHOD_NONE) select_init (flags); 548 if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods);
478#endif 549#endif
479 550
480 if (ev_method) 551 if (ev_method)
481 { 552 {
482 ev_watcher_init (&sigev, sigcb); 553 ev_watcher_init (&sigev, sigcb);
601 } 672 }
602 } 673 }
603 } 674 }
604} 675}
605 676
677static int
678time_update_monotonic (void)
679{
680 now = get_clock ();
681
682 if (expect_true (now - now_floor < MIN_TIMEJUMP * .5))
683 {
684 ev_now = now + diff;
685 return 0;
686 }
687 else
688 {
689 now_floor = now;
690 ev_now = ev_time ();
691 return 1;
692 }
693}
694
606static void 695static void
607time_update (void) 696time_update (void)
608{ 697{
609 int i; 698 int i;
610 699
611 ev_now = ev_time (); 700#if EV_USE_MONOTONIC
612
613 if (have_monotonic) 701 if (expect_true (have_monotonic))
614 { 702 {
703 if (time_update_monotonic ())
704 {
615 ev_tstamp odiff = diff; 705 ev_tstamp odiff = diff;
616 706
617 for (i = 4; --i; ) /* loop a few times, before making important decisions */ 707 for (i = 4; --i; ) /* loop a few times, before making important decisions */
618 { 708 {
619 now = get_clock ();
620 diff = ev_now - now; 709 diff = ev_now - now;
621 710
622 if (fabs (odiff - diff) < MIN_TIMEJUMP) 711 if (fabs (odiff - diff) < MIN_TIMEJUMP)
623 return; /* all is well */ 712 return; /* all is well */
624 713
625 ev_now = ev_time (); 714 ev_now = ev_time ();
715 now = get_clock ();
716 now_floor = now;
626 } 717 }
627 718
628 periodics_reschedule (diff - odiff); 719 periodics_reschedule (diff - odiff);
629 /* no timer adjustment, as the monotonic clock doesn't jump */ 720 /* no timer adjustment, as the monotonic clock doesn't jump */
721 }
630 } 722 }
631 else 723 else
724#endif
632 { 725 {
726 ev_now = ev_time ();
727
633 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) 728 if (expect_false (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
634 { 729 {
635 periodics_reschedule (ev_now - now); 730 periodics_reschedule (ev_now - now);
636 731
637 /* adjust timers. this is easy, as the offset is the same for all */ 732 /* adjust timers. this is easy, as the offset is the same for all */
638 for (i = 0; i < timercnt; ++i) 733 for (i = 0; i < timercnt; ++i)
651 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; 746 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
652 747
653 do 748 do
654 { 749 {
655 /* queue check watchers (and execute them) */ 750 /* queue check watchers (and execute them) */
656 if (preparecnt) 751 if (expect_false (preparecnt))
657 { 752 {
658 queue_events ((W *)prepares, preparecnt, EV_PREPARE); 753 queue_events ((W *)prepares, preparecnt, EV_PREPARE);
659 call_pending (); 754 call_pending ();
660 } 755 }
661 756
664 759
665 /* calculate blocking time */ 760 /* calculate blocking time */
666 761
667 /* we only need this for !monotonic clockor timers, but as we basically 762 /* we only need this for !monotonic clockor timers, but as we basically
668 always have timers, we just calculate it always */ 763 always have timers, we just calculate it always */
764#if EV_USE_MONOTONIC
765 if (expect_true (have_monotonic))
766 time_update_monotonic ();
767 else
768#endif
769 {
669 ev_now = ev_time (); 770 ev_now = ev_time ();
771 now = ev_now;
772 }
670 773
671 if (flags & EVLOOP_NONBLOCK || idlecnt) 774 if (flags & EVLOOP_NONBLOCK || idlecnt)
672 block = 0.; 775 block = 0.;
673 else 776 else
674 { 777 {
675 block = MAX_BLOCKTIME; 778 block = MAX_BLOCKTIME;
676 779
677 if (timercnt) 780 if (timercnt)
678 { 781 {
679 ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; 782 ev_tstamp to = timers [0]->at - now + method_fudge;
680 if (block > to) block = to; 783 if (block > to) block = to;
681 } 784 }
682 785
683 if (periodiccnt) 786 if (periodiccnt)
684 { 787 {
763/*****************************************************************************/ 866/*****************************************************************************/
764 867
765void 868void
766ev_io_start (struct ev_io *w) 869ev_io_start (struct ev_io *w)
767{ 870{
871 int fd = w->fd;
872
768 if (ev_is_active (w)) 873 if (ev_is_active (w))
769 return; 874 return;
770
771 int fd = w->fd;
772 875
773 assert (("ev_io_start called with negative fd", fd >= 0)); 876 assert (("ev_io_start called with negative fd", fd >= 0));
774 877
775 ev_start ((W)w, 1); 878 ev_start ((W)w, 1);
776 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 879 array_needsize (anfds, anfdmax, fd + 1, anfds_init);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines