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

Comparing libev/ev.c (file contents):
Revision 1.35 by root, Thu Nov 1 11:55:54 2007 UTC vs.
Revision 1.40 by root, Fri Nov 2 11:02:23 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
59#ifndef EV_USE_EPOLL 61#ifndef EV_USE_EPOLL
60# define EV_USE_EPOLL 0 62# define EV_USE_EPOLL 0
61#endif 63#endif
62 64
65#ifndef EV_USE_REALTIME
66# define EV_USE_REALTIME 1
67#endif
68
69/**/
70
71#ifndef CLOCK_MONOTONIC
72# undef EV_USE_MONOTONIC
73# define EV_USE_MONOTONIC 0
74#endif
75
63#ifndef CLOCK_REALTIME 76#ifndef CLOCK_REALTIME
77# undef EV_USE_REALTIME
64# define EV_USE_REALTIME 0 78# define EV_USE_REALTIME 0
65#endif 79#endif
66#ifndef EV_USE_REALTIME 80
67# define EV_USE_REALTIME 1 /* posix requirement, but might be slower */ 81/**/
68#endif
69 82
70#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 83#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) */ 84#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 */ 85#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 */ 86/*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */
74 87
75#include "ev.h" 88#include "ev.h"
89
90#if __GNUC__ >= 3
91# define expect(expr,value) __builtin_expect ((expr),(value))
92# define inline inline
93#else
94# define expect(expr,value) (expr)
95# define inline static
96#endif
97
98#define expect_false(expr) expect ((expr) != 0, 0)
99#define expect_true(expr) expect ((expr) != 0, 1)
76 100
77typedef struct ev_watcher *W; 101typedef struct ev_watcher *W;
78typedef struct ev_watcher_list *WL; 102typedef struct ev_watcher_list *WL;
79typedef struct ev_watcher_time *WT; 103typedef struct ev_watcher_time *WT;
80 104
81static ev_tstamp now, diff; /* monotonic clock */ 105static ev_tstamp now_floor, now, diff; /* monotonic clock */
82ev_tstamp ev_now; 106ev_tstamp ev_now;
83int ev_method; 107int ev_method;
84 108
85static int have_monotonic; /* runtime */ 109static int have_monotonic; /* runtime */
86 110
106 130
107static ev_tstamp 131static ev_tstamp
108get_clock (void) 132get_clock (void)
109{ 133{
110#if EV_USE_MONOTONIC 134#if EV_USE_MONOTONIC
111 if (have_monotonic) 135 if (expect_true (have_monotonic))
112 { 136 {
113 struct timespec ts; 137 struct timespec ts;
114 clock_gettime (CLOCK_MONOTONIC, &ts); 138 clock_gettime (CLOCK_MONOTONIC, &ts);
115 return ts.tv_sec + ts.tv_nsec * 1e-9; 139 return ts.tv_sec + ts.tv_nsec * 1e-9;
116 } 140 }
120} 144}
121 145
122#define array_roundsize(base,n) ((n) | 4 & ~3) 146#define array_roundsize(base,n) ((n) | 4 & ~3)
123 147
124#define array_needsize(base,cur,cnt,init) \ 148#define array_needsize(base,cur,cnt,init) \
125 if ((cnt) > cur) \ 149 if (expect_false ((cnt) > cur)) \
126 { \ 150 { \
127 int newcnt = cur; \ 151 int newcnt = cur; \
128 do \ 152 do \
129 { \ 153 { \
130 newcnt = array_roundsize (base, newcnt << 1); \ 154 newcnt = array_roundsize (base, newcnt << 1); \
361 385
362static void 386static void
363sigcb (struct ev_io *iow, int revents) 387sigcb (struct ev_io *iow, int revents)
364{ 388{
365 struct ev_signal *w; 389 struct ev_signal *w;
366 int sig; 390 int signum;
367 391
368 read (sigpipe [0], &revents, 1); 392 read (sigpipe [0], &revents, 1);
369 gotsig = 0; 393 gotsig = 0;
370 394
371 for (sig = signalmax; sig--; ) 395 for (signum = signalmax; signum--; )
372 if (signals [sig].gotsig) 396 if (signals [signum].gotsig)
373 { 397 {
374 signals [sig].gotsig = 0; 398 signals [signum].gotsig = 0;
375 399
376 for (w = signals [sig].head; w; w = w->next) 400 for (w = signals [signum].head; w; w = w->next)
377 event ((W)w, EV_SIGNAL); 401 event ((W)w, EV_SIGNAL);
378 } 402 }
379} 403}
380 404
381static void 405static void
418 struct ev_child *w; 442 struct ev_child *w;
419 int pid, status; 443 int pid, status;
420 444
421 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) 445 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1)
422 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) 446 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next)
423 if (w->pid == pid || w->pid == -1) 447 if (w->pid == pid || !w->pid)
424 { 448 {
425 w->status = status; 449 w->status = status;
426 event ((W)w, EV_CHILD); 450 event ((W)w, EV_CHILD);
427 } 451 }
428} 452}
458 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 482 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
459 have_monotonic = 1; 483 have_monotonic = 1;
460 } 484 }
461#endif 485#endif
462 486
463 ev_now = ev_time (); 487 ev_now = ev_time ();
464 now = get_clock (); 488 now = get_clock ();
489 now_floor = now;
465 diff = ev_now - now; 490 diff = ev_now - now;
466 491
467 if (pipe (sigpipe)) 492 if (pipe (sigpipe))
468 return 0; 493 return 0;
469 494
470 ev_method = EVMETHOD_NONE; 495 ev_method = EVMETHOD_NONE;
599 } 624 }
600 } 625 }
601 } 626 }
602} 627}
603 628
629static int
630time_update_monotonic (void)
631{
632 now = get_clock ();
633
634 if (expect_true (now - now_floor < MIN_TIMEJUMP * .5))
635 {
636 ev_now = now + diff;
637 return 0;
638 }
639 else
640 {
641 now_floor = now;
642 ev_now = ev_time ();
643 return 1;
644 }
645}
646
604static void 647static void
605time_update (void) 648time_update (void)
606{ 649{
607 int i; 650 int i;
608 651
609 ev_now = ev_time (); 652#if EV_USE_MONOTONIC
610
611 if (have_monotonic) 653 if (expect_true (have_monotonic))
612 { 654 {
655 if (time_update_monotonic ())
656 {
613 ev_tstamp odiff = diff; 657 ev_tstamp odiff = diff;
614 658
615 for (i = 4; --i; ) /* loop a few times, before making important decisions */ 659 for (i = 4; --i; ) /* loop a few times, before making important decisions */
616 { 660 {
617 now = get_clock ();
618 diff = ev_now - now; 661 diff = ev_now - now;
619 662
620 if (fabs (odiff - diff) < MIN_TIMEJUMP) 663 if (fabs (odiff - diff) < MIN_TIMEJUMP)
621 return; /* all is well */ 664 return; /* all is well */
622 665
623 ev_now = ev_time (); 666 ev_now = ev_time ();
667 now = get_clock ();
668 now_floor = now;
624 } 669 }
625 670
626 periodics_reschedule (diff - odiff); 671 periodics_reschedule (diff - odiff);
627 /* no timer adjustment, as the monotonic clock doesn't jump */ 672 /* no timer adjustment, as the monotonic clock doesn't jump */
673 }
628 } 674 }
629 else 675 else
676#endif
630 { 677 {
678 ev_now = ev_time ();
679
631 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) 680 if (expect_false (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
632 { 681 {
633 periodics_reschedule (ev_now - now); 682 periodics_reschedule (ev_now - now);
634 683
635 /* adjust timers. this is easy, as the offset is the same for all */ 684 /* adjust timers. this is easy, as the offset is the same for all */
636 for (i = 0; i < timercnt; ++i) 685 for (i = 0; i < timercnt; ++i)
649 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; 698 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
650 699
651 do 700 do
652 { 701 {
653 /* queue check watchers (and execute them) */ 702 /* queue check watchers (and execute them) */
654 if (preparecnt) 703 if (expect_false (preparecnt))
655 { 704 {
656 queue_events ((W *)prepares, preparecnt, EV_PREPARE); 705 queue_events ((W *)prepares, preparecnt, EV_PREPARE);
657 call_pending (); 706 call_pending ();
658 } 707 }
659 708
662 711
663 /* calculate blocking time */ 712 /* calculate blocking time */
664 713
665 /* we only need this for !monotonic clockor timers, but as we basically 714 /* we only need this for !monotonic clockor timers, but as we basically
666 always have timers, we just calculate it always */ 715 always have timers, we just calculate it always */
716#if EV_USE_MONOTONIC
717 if (expect_true (have_monotonic))
718 time_update_monotonic ();
719 else
720#endif
721 {
667 ev_now = ev_time (); 722 ev_now = ev_time ();
723 now = ev_now;
724 }
668 725
669 if (flags & EVLOOP_NONBLOCK || idlecnt) 726 if (flags & EVLOOP_NONBLOCK || idlecnt)
670 block = 0.; 727 block = 0.;
671 else 728 else
672 { 729 {
673 block = MAX_BLOCKTIME; 730 block = MAX_BLOCKTIME;
674 731
675 if (timercnt) 732 if (timercnt)
676 { 733 {
677 ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; 734 ev_tstamp to = timers [0]->at - now + method_fudge;
678 if (block > to) block = to; 735 if (block > to) block = to;
679 } 736 }
680 737
681 if (periodiccnt) 738 if (periodiccnt)
682 { 739 {
761/*****************************************************************************/ 818/*****************************************************************************/
762 819
763void 820void
764ev_io_start (struct ev_io *w) 821ev_io_start (struct ev_io *w)
765{ 822{
823 int fd = w->fd;
824
766 if (ev_is_active (w)) 825 if (ev_is_active (w))
767 return; 826 return;
768
769 int fd = w->fd;
770 827
771 assert (("ev_io_start called with negative fd", fd >= 0)); 828 assert (("ev_io_start called with negative fd", fd >= 0));
772 829
773 ev_start ((W)w, 1); 830 ev_start ((W)w, 1);
774 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 831 array_needsize (anfds, anfdmax, fd + 1, anfds_init);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines