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

Comparing libev/ev.c (file contents):
Revision 1.79 by root, Fri Nov 9 15:15:20 2007 UTC vs.
Revision 1.109 by root, Mon Nov 12 05:53:55 2007 UTC

26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */ 30 */
31
32#ifdef __cplusplus
33extern "C" {
34#endif
35
31#ifndef EV_STANDALONE 36#ifndef EV_STANDALONE
32# include "config.h" 37# include "config.h"
33 38
34# if HAVE_CLOCK_GETTIME 39# if HAVE_CLOCK_GETTIME
40# ifndef EV_USE_MONOTONIC
35# define EV_USE_MONOTONIC 1 41# define EV_USE_MONOTONIC 1
42# endif
43# ifndef EV_USE_REALTIME
36# define EV_USE_REALTIME 1 44# define EV_USE_REALTIME 1
45# endif
37# endif 46# endif
38 47
39# if HAVE_SELECT && HAVE_SYS_SELECT_H 48# if HAVE_SELECT && HAVE_SYS_SELECT_H && !defined (EV_USE_SELECT)
40# define EV_USE_SELECT 1 49# define EV_USE_SELECT 1
41# endif 50# endif
42 51
43# if HAVE_POLL && HAVE_POLL_H 52# if HAVE_POLL && HAVE_POLL_H && !defined (EV_USE_POLL)
44# define EV_USE_POLL 1 53# define EV_USE_POLL 1
45# endif 54# endif
46 55
47# if HAVE_EPOLL && HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H 56# if HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H && !defined (EV_USE_EPOLL)
48# define EV_USE_EPOLL 1 57# define EV_USE_EPOLL 1
49# endif 58# endif
50 59
51# if HAVE_KQUEUE && HAVE_WORKING_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H 60# if HAVE_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H && !defined (EV_USE_KQUEUE)
52# define EV_USE_KQUEUE 1 61# define EV_USE_KQUEUE 1
53# endif 62# endif
54 63
55#endif 64#endif
56 65
66#include <sys/types.h> 75#include <sys/types.h>
67#include <time.h> 76#include <time.h>
68 77
69#include <signal.h> 78#include <signal.h>
70 79
71#ifndef WIN32 80#ifndef _WIN32
72# include <unistd.h> 81# include <unistd.h>
73# include <sys/time.h> 82# include <sys/time.h>
74# include <sys/wait.h> 83# include <sys/wait.h>
84#else
85# define WIN32_LEAN_AND_MEAN
86# include <windows.h>
87# ifndef EV_SELECT_IS_WINSOCKET
88# define EV_SELECT_IS_WINSOCKET 1
75#endif 89# endif
90#endif
91
76/**/ 92/**/
77 93
78#ifndef EV_USE_MONOTONIC 94#ifndef EV_USE_MONOTONIC
79# define EV_USE_MONOTONIC 1 95# define EV_USE_MONOTONIC 1
80#endif 96#endif
81 97
82#ifndef EV_USE_SELECT 98#ifndef EV_USE_SELECT
83# define EV_USE_SELECT 1 99# define EV_USE_SELECT 1
100# define EV_SELECT_USE_FD_SET 1
84#endif 101#endif
85 102
86#ifndef EV_USE_POLL 103#ifndef EV_USE_POLL
87# define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */ 104# ifdef _WIN32
105# define EV_USE_POLL 0
106# else
107# define EV_USE_POLL 1
108# endif
88#endif 109#endif
89 110
90#ifndef EV_USE_EPOLL 111#ifndef EV_USE_EPOLL
91# define EV_USE_EPOLL 0 112# define EV_USE_EPOLL 0
92#endif 113#endif
93 114
94#ifndef EV_USE_KQUEUE 115#ifndef EV_USE_KQUEUE
95# define EV_USE_KQUEUE 0 116# define EV_USE_KQUEUE 0
96#endif 117#endif
97 118
98#ifndef EV_USE_WIN32
99# ifdef WIN32
100# define EV_USE_WIN32 0 /* it does not exist, use select */
101# undef EV_USE_SELECT
102# define EV_USE_SELECT 1
103# else
104# define EV_USE_WIN32 0
105# endif
106#endif
107
108#ifndef EV_USE_REALTIME 119#ifndef EV_USE_REALTIME
109# define EV_USE_REALTIME 1 120# define EV_USE_REALTIME 1
110#endif 121#endif
111 122
112/**/ 123/**/
124
125/* darwin simply cannot be helped */
126#ifdef __APPLE__
127# undef EV_USE_POLL
128# undef EV_USE_KQUEUE
129#endif
113 130
114#ifndef CLOCK_MONOTONIC 131#ifndef CLOCK_MONOTONIC
115# undef EV_USE_MONOTONIC 132# undef EV_USE_MONOTONIC
116# define EV_USE_MONOTONIC 0 133# define EV_USE_MONOTONIC 0
117#endif 134#endif
118 135
119#ifndef CLOCK_REALTIME 136#ifndef CLOCK_REALTIME
120# undef EV_USE_REALTIME 137# undef EV_USE_REALTIME
121# define EV_USE_REALTIME 0 138# define EV_USE_REALTIME 0
139#endif
140
141#if EV_SELECT_IS_WINSOCKET
142# include <winsock.h>
122#endif 143#endif
123 144
124/**/ 145/**/
125 146
126#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 147#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
127#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */ 148#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */
128#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ 149#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */
129/*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ 150/*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */
130 151
152#ifdef EV_H
153# include EV_H
154#else
131#include "ev.h" 155# include "ev.h"
156#endif
132 157
133#if __GNUC__ >= 3 158#if __GNUC__ >= 3
134# define expect(expr,value) __builtin_expect ((expr),(value)) 159# define expect(expr,value) __builtin_expect ((expr),(value))
135# define inline inline 160# define inline inline
136#else 161#else
142#define expect_true(expr) expect ((expr) != 0, 1) 167#define expect_true(expr) expect ((expr) != 0, 1)
143 168
144#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) 169#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
145#define ABSPRI(w) ((w)->priority - EV_MINPRI) 170#define ABSPRI(w) ((w)->priority - EV_MINPRI)
146 171
172#define EMPTY /* required for microsofts broken pseudo-c compiler */
173
147typedef struct ev_watcher *W; 174typedef struct ev_watcher *W;
148typedef struct ev_watcher_list *WL; 175typedef struct ev_watcher_list *WL;
149typedef struct ev_watcher_time *WT; 176typedef struct ev_watcher_time *WT;
150 177
151static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 178static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
152 179
180#ifdef _WIN32
153#include "ev_win32.c" 181# include "ev_win32.c"
182#endif
154 183
155/*****************************************************************************/ 184/*****************************************************************************/
156 185
157static void (*syserr_cb)(const char *msg); 186static void (*syserr_cb)(const char *msg);
158 187
205typedef struct 234typedef struct
206{ 235{
207 WL head; 236 WL head;
208 unsigned char events; 237 unsigned char events;
209 unsigned char reify; 238 unsigned char reify;
239#if EV_SELECT_IS_WINSOCKET
240 SOCKET handle;
241#endif
210} ANFD; 242} ANFD;
211 243
212typedef struct 244typedef struct
213{ 245{
214 W w; 246 W w;
215 int events; 247 int events;
216} ANPENDING; 248} ANPENDING;
217 249
218#if EV_MULTIPLICITY 250#if EV_MULTIPLICITY
219 251
220struct ev_loop 252 struct ev_loop
221{ 253 {
254 ev_tstamp ev_rt_now;
255 #define ev_rt_now ((loop)->ev_rt_now)
222# define VAR(name,decl) decl; 256 #define VAR(name,decl) decl;
223# include "ev_vars.h" 257 #include "ev_vars.h"
224};
225# undef VAR 258 #undef VAR
259 };
226# include "ev_wrap.h" 260 #include "ev_wrap.h"
261
262 struct ev_loop default_loop_struct;
263 static struct ev_loop *default_loop;
227 264
228#else 265#else
229 266
267 ev_tstamp ev_rt_now;
230# define VAR(name,decl) static decl; 268 #define VAR(name,decl) static decl;
231# include "ev_vars.h" 269 #include "ev_vars.h"
232# undef VAR 270 #undef VAR
271
272 static int default_loop;
233 273
234#endif 274#endif
235 275
236/*****************************************************************************/ 276/*****************************************************************************/
237 277
238inline ev_tstamp 278ev_tstamp
239ev_time (void) 279ev_time (void)
240{ 280{
241#if EV_USE_REALTIME 281#if EV_USE_REALTIME
242 struct timespec ts; 282 struct timespec ts;
243 clock_gettime (CLOCK_REALTIME, &ts); 283 clock_gettime (CLOCK_REALTIME, &ts);
262#endif 302#endif
263 303
264 return ev_time (); 304 return ev_time ();
265} 305}
266 306
307#if EV_MULTIPLICITY
267ev_tstamp 308ev_tstamp
268ev_now (EV_P) 309ev_now (EV_P)
269{ 310{
270 return rt_now; 311 return ev_rt_now;
271} 312}
313#endif
272 314
273#define array_roundsize(type,n) ((n) | 4 & ~3) 315#define array_roundsize(type,n) (((n) | 4) & ~3)
274 316
275#define array_needsize(type,base,cur,cnt,init) \ 317#define array_needsize(type,base,cur,cnt,init) \
276 if (expect_false ((cnt) > cur)) \ 318 if (expect_false ((cnt) > cur)) \
277 { \ 319 { \
278 int newcnt = cur; \ 320 int newcnt = cur; \
293 stem ## max = array_roundsize (stem ## cnt >> 1); \ 335 stem ## max = array_roundsize (stem ## cnt >> 1); \
294 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ 336 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\
295 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ 337 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
296 } 338 }
297 339
298/* microsoft's pseudo-c is quite far from C as the rest of the world and the standard knows it */
299/* bringing us everlasting joy in form of stupid extra macros that are not required in C */
300#define array_free_microshit(stem) \
301 ev_free (stem ## s); stem ## cnt = stem ## max = 0;
302
303#define array_free(stem, idx) \ 340#define array_free(stem, idx) \
304 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; 341 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
305 342
306/*****************************************************************************/ 343/*****************************************************************************/
307 344
381 int events = 0; 418 int events = 0;
382 419
383 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) 420 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
384 events |= w->events; 421 events |= w->events;
385 422
423#if EV_SELECT_IS_WINSOCKET
424 if (events)
425 {
426 unsigned long argp;
427 anfd->handle = _get_osfhandle (fd);
428 assert (("libev only supports socket fds in this configuration", ioctlsocket (anfd->handle, FIONREAD, &argp) == 0));
429 }
430#endif
431
386 anfd->reify = 0; 432 anfd->reify = 0;
387 433
388 method_modify (EV_A_ fd, anfd->events, events); 434 method_modify (EV_A_ fd, anfd->events, events);
389 anfd->events = events; 435 anfd->events = events;
390 } 436 }
418} 464}
419 465
420static int 466static int
421fd_valid (int fd) 467fd_valid (int fd)
422{ 468{
423#ifdef WIN32 469#ifdef _WIN32
424 return !!win32_get_osfhandle (fd); 470 return _get_osfhandle (fd) != -1;
425#else 471#else
426 return fcntl (fd, F_GETFD) != -1; 472 return fcntl (fd, F_GETFD) != -1;
427#endif 473#endif
428} 474}
429 475
509 555
510 heap [k] = w; 556 heap [k] = w;
511 ((W)heap [k])->active = k + 1; 557 ((W)heap [k])->active = k + 1;
512} 558}
513 559
560inline void
561adjustheap (WT *heap, int N, int k)
562{
563 upheap (heap, k);
564 downheap (heap, N, k);
565}
566
514/*****************************************************************************/ 567/*****************************************************************************/
515 568
516typedef struct 569typedef struct
517{ 570{
518 WL head; 571 WL head;
539} 592}
540 593
541static void 594static void
542sighandler (int signum) 595sighandler (int signum)
543{ 596{
544#if WIN32 597#if _WIN32
545 signal (signum, sighandler); 598 signal (signum, sighandler);
546#endif 599#endif
547 600
548 signals [signum - 1].gotsig = 1; 601 signals [signum - 1].gotsig = 1;
549 602
550 if (!gotsig) 603 if (!gotsig)
551 { 604 {
552 int old_errno = errno; 605 int old_errno = errno;
553 gotsig = 1; 606 gotsig = 1;
554#ifdef WIN32
555 send (sigpipe [1], &signum, 1, MSG_DONTWAIT);
556#else
557 write (sigpipe [1], &signum, 1); 607 write (sigpipe [1], &signum, 1);
558#endif
559 errno = old_errno; 608 errno = old_errno;
560 } 609 }
561} 610}
562 611
563void 612void
564ev_feed_signal_event (EV_P_ int signum) 613ev_feed_signal_event (EV_P_ int signum)
565{ 614{
615 WL w;
616
566#if EV_MULTIPLICITY 617#if EV_MULTIPLICITY
567 assert (("feeding signal events is only supported in the default loop", loop == default_loop)); 618 assert (("feeding signal events is only supported in the default loop", loop == default_loop));
568#endif 619#endif
569 620
570 --signum; 621 --signum;
579} 630}
580 631
581static void 632static void
582sigcb (EV_P_ struct ev_io *iow, int revents) 633sigcb (EV_P_ struct ev_io *iow, int revents)
583{ 634{
584 WL w;
585 int signum; 635 int signum;
586 636
587#ifdef WIN32
588 recv (sigpipe [0], &revents, 1, MSG_DONTWAIT);
589#else
590 read (sigpipe [0], &revents, 1); 637 read (sigpipe [0], &revents, 1);
591#endif
592 gotsig = 0; 638 gotsig = 0;
593 639
594 for (signum = signalmax; signum--; ) 640 for (signum = signalmax; signum--; )
595 if (signals [signum].gotsig) 641 if (signals [signum].gotsig)
596 sigevent (EV_A_ signum + 1); 642 ev_feed_signal_event (EV_A_ signum + 1);
643}
644
645inline void
646fd_intern (int fd)
647{
648#ifdef _WIN32
649 int arg = 1;
650 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg);
651#else
652 fcntl (fd, F_SETFD, FD_CLOEXEC);
653 fcntl (fd, F_SETFL, O_NONBLOCK);
654#endif
597} 655}
598 656
599static void 657static void
600siginit (EV_P) 658siginit (EV_P)
601{ 659{
602#ifndef WIN32 660 fd_intern (sigpipe [0]);
603 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); 661 fd_intern (sigpipe [1]);
604 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC);
605
606 /* rather than sort out wether we really need nb, set it */
607 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
608 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
609#endif
610 662
611 ev_io_set (&sigev, sigpipe [0], EV_READ); 663 ev_io_set (&sigev, sigpipe [0], EV_READ);
612 ev_io_start (EV_A_ &sigev); 664 ev_io_start (EV_A_ &sigev);
613 ev_unref (EV_A); /* child watcher should not keep loop alive */ 665 ev_unref (EV_A); /* child watcher should not keep loop alive */
614} 666}
615 667
616/*****************************************************************************/ 668/*****************************************************************************/
617 669
618static struct ev_child *childs [PID_HASHSIZE]; 670static struct ev_child *childs [PID_HASHSIZE];
619 671
620#ifndef WIN32 672#ifndef _WIN32
621 673
622static struct ev_signal childev; 674static struct ev_signal childev;
623 675
624#ifndef WCONTINUED 676#ifndef WCONTINUED
625# define WCONTINUED 0 677# define WCONTINUED 0
686 738
687/* return true if we are running with elevated privileges and should ignore env variables */ 739/* return true if we are running with elevated privileges and should ignore env variables */
688static int 740static int
689enable_secure (void) 741enable_secure (void)
690{ 742{
691#ifdef WIN32 743#ifdef _WIN32
692 return 0; 744 return 0;
693#else 745#else
694 return getuid () != geteuid () 746 return getuid () != geteuid ()
695 || getgid () != getegid (); 747 || getgid () != getegid ();
696#endif 748#endif
701{ 753{
702 return method; 754 return method;
703} 755}
704 756
705static void 757static void
706loop_init (EV_P_ int methods) 758loop_init (EV_P_ unsigned int flags)
707{ 759{
708 if (!method) 760 if (!method)
709 { 761 {
710#if EV_USE_MONOTONIC 762#if EV_USE_MONOTONIC
711 { 763 {
713 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 765 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
714 have_monotonic = 1; 766 have_monotonic = 1;
715 } 767 }
716#endif 768#endif
717 769
718 rt_now = ev_time (); 770 ev_rt_now = ev_time ();
719 mn_now = get_clock (); 771 mn_now = get_clock ();
720 now_floor = mn_now; 772 now_floor = mn_now;
721 rtmn_diff = rt_now - mn_now; 773 rtmn_diff = ev_rt_now - mn_now;
722 774
723 if (methods == EVMETHOD_AUTO) 775 if (!(flags & EVMETHOD_NOENV) && !enable_secure () && getenv ("LIBEV_FLAGS"))
724 if (!enable_secure () && getenv ("LIBEV_METHODS"))
725 methods = atoi (getenv ("LIBEV_METHODS")); 776 flags = atoi (getenv ("LIBEV_FLAGS"));
726 else 777
727 methods = EVMETHOD_ANY; 778 if (!(flags & 0x0000ffff))
779 flags |= 0x0000ffff;
728 780
729 method = 0; 781 method = 0;
730#if EV_USE_WIN32
731 if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods);
732#endif
733#if EV_USE_KQUEUE 782#if EV_USE_KQUEUE
734 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); 783 if (!method && (flags & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ flags);
735#endif 784#endif
736#if EV_USE_EPOLL 785#if EV_USE_EPOLL
737 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); 786 if (!method && (flags & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ flags);
738#endif 787#endif
739#if EV_USE_POLL 788#if EV_USE_POLL
740 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); 789 if (!method && (flags & EVMETHOD_POLL )) method = poll_init (EV_A_ flags);
741#endif 790#endif
742#if EV_USE_SELECT 791#if EV_USE_SELECT
743 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 792 if (!method && (flags & EVMETHOD_SELECT)) method = select_init (EV_A_ flags);
744#endif 793#endif
745 794
746 ev_watcher_init (&sigev, sigcb); 795 ev_init (&sigev, sigcb);
747 ev_set_priority (&sigev, EV_MAXPRI); 796 ev_set_priority (&sigev, EV_MAXPRI);
748 } 797 }
749} 798}
750 799
751void 800void
752loop_destroy (EV_P) 801loop_destroy (EV_P)
753{ 802{
754 int i; 803 int i;
755 804
756#if EV_USE_WIN32
757 if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A);
758#endif
759#if EV_USE_KQUEUE 805#if EV_USE_KQUEUE
760 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); 806 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
761#endif 807#endif
762#if EV_USE_EPOLL 808#if EV_USE_EPOLL
763 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); 809 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A);
771 817
772 for (i = NUMPRI; i--; ) 818 for (i = NUMPRI; i--; )
773 array_free (pending, [i]); 819 array_free (pending, [i]);
774 820
775 /* have to use the microsoft-never-gets-it-right macro */ 821 /* have to use the microsoft-never-gets-it-right macro */
776 array_free_microshit (fdchange); 822 array_free (fdchange, EMPTY);
777 array_free_microshit (timer); 823 array_free (timer, EMPTY);
778 array_free_microshit (periodic); 824#if EV_PERIODICS
779 array_free_microshit (idle); 825 array_free (periodic, EMPTY);
780 array_free_microshit (prepare); 826#endif
781 array_free_microshit (check); 827 array_free (idle, EMPTY);
828 array_free (prepare, EMPTY);
829 array_free (check, EMPTY);
782 830
783 method = 0; 831 method = 0;
784} 832}
785 833
786static void 834static void
811 postfork = 0; 859 postfork = 0;
812} 860}
813 861
814#if EV_MULTIPLICITY 862#if EV_MULTIPLICITY
815struct ev_loop * 863struct ev_loop *
816ev_loop_new (int methods) 864ev_loop_new (unsigned int flags)
817{ 865{
818 struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); 866 struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop));
819 867
820 memset (loop, 0, sizeof (struct ev_loop)); 868 memset (loop, 0, sizeof (struct ev_loop));
821 869
822 loop_init (EV_A_ methods); 870 loop_init (EV_A_ flags);
823 871
824 if (ev_method (EV_A)) 872 if (ev_method (EV_A))
825 return loop; 873 return loop;
826 874
827 return 0; 875 return 0;
841} 889}
842 890
843#endif 891#endif
844 892
845#if EV_MULTIPLICITY 893#if EV_MULTIPLICITY
846struct ev_loop default_loop_struct;
847static struct ev_loop *default_loop;
848
849struct ev_loop * 894struct ev_loop *
850#else 895#else
851static int default_loop;
852
853int 896int
854#endif 897#endif
855ev_default_loop (int methods) 898ev_default_loop (unsigned int flags)
856{ 899{
857 if (sigpipe [0] == sigpipe [1]) 900 if (sigpipe [0] == sigpipe [1])
858 if (pipe (sigpipe)) 901 if (pipe (sigpipe))
859 return 0; 902 return 0;
860 903
870 913
871 if (ev_method (EV_A)) 914 if (ev_method (EV_A))
872 { 915 {
873 siginit (EV_A); 916 siginit (EV_A);
874 917
875#ifndef WIN32 918#ifndef _WIN32
876 ev_signal_init (&childev, childcb, SIGCHLD); 919 ev_signal_init (&childev, childcb, SIGCHLD);
877 ev_set_priority (&childev, EV_MAXPRI); 920 ev_set_priority (&childev, EV_MAXPRI);
878 ev_signal_start (EV_A_ &childev); 921 ev_signal_start (EV_A_ &childev);
879 ev_unref (EV_A); /* child watcher should not keep loop alive */ 922 ev_unref (EV_A); /* child watcher should not keep loop alive */
880#endif 923#endif
891{ 934{
892#if EV_MULTIPLICITY 935#if EV_MULTIPLICITY
893 struct ev_loop *loop = default_loop; 936 struct ev_loop *loop = default_loop;
894#endif 937#endif
895 938
896#ifndef WIN32 939#ifndef _WIN32
897 ev_ref (EV_A); /* child watcher */ 940 ev_ref (EV_A); /* child watcher */
898 ev_signal_stop (EV_A_ &childev); 941 ev_signal_stop (EV_A_ &childev);
899#endif 942#endif
900 943
901 ev_ref (EV_A); /* signal watcher */ 944 ev_ref (EV_A); /* signal watcher */
943 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 986 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
944 987
945 if (p->w) 988 if (p->w)
946 { 989 {
947 p->w->pending = 0; 990 p->w->pending = 0;
948 p->w->cb (EV_A_ p->w, p->events); 991 EV_CB_INVOKE (p->w, p->events);
949 } 992 }
950 } 993 }
951} 994}
952 995
953static void 996static void
961 1004
962 /* first reschedule or stop timer */ 1005 /* first reschedule or stop timer */
963 if (w->repeat) 1006 if (w->repeat)
964 { 1007 {
965 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); 1008 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
1009
966 ((WT)w)->at = mn_now + w->repeat; 1010 ((WT)w)->at += w->repeat;
1011 if (((WT)w)->at < mn_now)
1012 ((WT)w)->at = mn_now;
1013
967 downheap ((WT *)timers, timercnt, 0); 1014 downheap ((WT *)timers, timercnt, 0);
968 } 1015 }
969 else 1016 else
970 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 1017 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
971 1018
972 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); 1019 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
973 } 1020 }
974} 1021}
975 1022
1023#if EV_PERIODICS
976static void 1024static void
977periodics_reify (EV_P) 1025periodics_reify (EV_P)
978{ 1026{
979 while (periodiccnt && ((WT)periodics [0])->at <= rt_now) 1027 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
980 { 1028 {
981 struct ev_periodic *w = periodics [0]; 1029 struct ev_periodic *w = periodics [0];
982 1030
983 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 1031 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
984 1032
985 /* first reschedule or stop timer */ 1033 /* first reschedule or stop timer */
986 if (w->reschedule_cb) 1034 if (w->reschedule_cb)
987 { 1035 {
988 ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, rt_now + 0.0001); 1036 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001);
989
990 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > rt_now)); 1037 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now));
991 downheap ((WT *)periodics, periodiccnt, 0); 1038 downheap ((WT *)periodics, periodiccnt, 0);
992 } 1039 }
993 else if (w->interval) 1040 else if (w->interval)
994 { 1041 {
995 ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; 1042 ((WT)w)->at += floor ((ev_rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval;
996 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now)); 1043 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > ev_rt_now));
997 downheap ((WT *)periodics, periodiccnt, 0); 1044 downheap ((WT *)periodics, periodiccnt, 0);
998 } 1045 }
999 else 1046 else
1000 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 1047 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
1001 1048
1012 for (i = 0; i < periodiccnt; ++i) 1059 for (i = 0; i < periodiccnt; ++i)
1013 { 1060 {
1014 struct ev_periodic *w = periodics [i]; 1061 struct ev_periodic *w = periodics [i];
1015 1062
1016 if (w->reschedule_cb) 1063 if (w->reschedule_cb)
1017 ((WT)w)->at = w->reschedule_cb (w, rt_now); 1064 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1018 else if (w->interval) 1065 else if (w->interval)
1019 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; 1066 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1020 } 1067 }
1021 1068
1022 /* now rebuild the heap */ 1069 /* now rebuild the heap */
1023 for (i = periodiccnt >> 1; i--; ) 1070 for (i = periodiccnt >> 1; i--; )
1024 downheap ((WT *)periodics, periodiccnt, i); 1071 downheap ((WT *)periodics, periodiccnt, i);
1025} 1072}
1073#endif
1026 1074
1027inline int 1075inline int
1028time_update_monotonic (EV_P) 1076time_update_monotonic (EV_P)
1029{ 1077{
1030 mn_now = get_clock (); 1078 mn_now = get_clock ();
1031 1079
1032 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) 1080 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
1033 { 1081 {
1034 rt_now = rtmn_diff + mn_now; 1082 ev_rt_now = rtmn_diff + mn_now;
1035 return 0; 1083 return 0;
1036 } 1084 }
1037 else 1085 else
1038 { 1086 {
1039 now_floor = mn_now; 1087 now_floor = mn_now;
1040 rt_now = ev_time (); 1088 ev_rt_now = ev_time ();
1041 return 1; 1089 return 1;
1042 } 1090 }
1043} 1091}
1044 1092
1045static void 1093static void
1054 { 1102 {
1055 ev_tstamp odiff = rtmn_diff; 1103 ev_tstamp odiff = rtmn_diff;
1056 1104
1057 for (i = 4; --i; ) /* loop a few times, before making important decisions */ 1105 for (i = 4; --i; ) /* loop a few times, before making important decisions */
1058 { 1106 {
1059 rtmn_diff = rt_now - mn_now; 1107 rtmn_diff = ev_rt_now - mn_now;
1060 1108
1061 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) 1109 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)
1062 return; /* all is well */ 1110 return; /* all is well */
1063 1111
1064 rt_now = ev_time (); 1112 ev_rt_now = ev_time ();
1065 mn_now = get_clock (); 1113 mn_now = get_clock ();
1066 now_floor = mn_now; 1114 now_floor = mn_now;
1067 } 1115 }
1068 1116
1117# if EV_PERIODICS
1069 periodics_reschedule (EV_A); 1118 periodics_reschedule (EV_A);
1119# endif
1070 /* no timer adjustment, as the monotonic clock doesn't jump */ 1120 /* no timer adjustment, as the monotonic clock doesn't jump */
1071 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ 1121 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */
1072 } 1122 }
1073 } 1123 }
1074 else 1124 else
1075#endif 1125#endif
1076 { 1126 {
1077 rt_now = ev_time (); 1127 ev_rt_now = ev_time ();
1078 1128
1079 if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) 1129 if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
1080 { 1130 {
1131#if EV_PERIODICS
1081 periodics_reschedule (EV_A); 1132 periodics_reschedule (EV_A);
1133#endif
1082 1134
1083 /* adjust timers. this is easy, as the offset is the same for all */ 1135 /* adjust timers. this is easy, as the offset is the same for all */
1084 for (i = 0; i < timercnt; ++i) 1136 for (i = 0; i < timercnt; ++i)
1085 ((WT)timers [i])->at += rt_now - mn_now; 1137 ((WT)timers [i])->at += ev_rt_now - mn_now;
1086 } 1138 }
1087 1139
1088 mn_now = rt_now; 1140 mn_now = ev_rt_now;
1089 } 1141 }
1090} 1142}
1091 1143
1092void 1144void
1093ev_ref (EV_P) 1145ev_ref (EV_P)
1133 if (expect_true (have_monotonic)) 1185 if (expect_true (have_monotonic))
1134 time_update_monotonic (EV_A); 1186 time_update_monotonic (EV_A);
1135 else 1187 else
1136#endif 1188#endif
1137 { 1189 {
1138 rt_now = ev_time (); 1190 ev_rt_now = ev_time ();
1139 mn_now = rt_now; 1191 mn_now = ev_rt_now;
1140 } 1192 }
1141 1193
1142 if (flags & EVLOOP_NONBLOCK || idlecnt) 1194 if (flags & EVLOOP_NONBLOCK || idlecnt)
1143 block = 0.; 1195 block = 0.;
1144 else 1196 else
1149 { 1201 {
1150 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; 1202 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge;
1151 if (block > to) block = to; 1203 if (block > to) block = to;
1152 } 1204 }
1153 1205
1206#if EV_PERIODICS
1154 if (periodiccnt) 1207 if (periodiccnt)
1155 { 1208 {
1156 ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge; 1209 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge;
1157 if (block > to) block = to; 1210 if (block > to) block = to;
1158 } 1211 }
1212#endif
1159 1213
1160 if (block < 0.) block = 0.; 1214 if (block < 0.) block = 0.;
1161 } 1215 }
1162 1216
1163 method_poll (EV_A_ block); 1217 method_poll (EV_A_ block);
1164 1218
1165 /* update rt_now, do magic */ 1219 /* update ev_rt_now, do magic */
1166 time_update (EV_A); 1220 time_update (EV_A);
1167 1221
1168 /* queue pending timers and reschedule them */ 1222 /* queue pending timers and reschedule them */
1169 timers_reify (EV_A); /* relative timers called last */ 1223 timers_reify (EV_A); /* relative timers called last */
1224#if EV_PERIODICS
1170 periodics_reify (EV_A); /* absolute timers called first */ 1225 periodics_reify (EV_A); /* absolute timers called first */
1226#endif
1171 1227
1172 /* queue idle watchers unless io or timers are pending */ 1228 /* queue idle watchers unless io or timers are pending */
1173 if (idlecnt && !any_pending (EV_A)) 1229 if (idlecnt && !any_pending (EV_A))
1174 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1230 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
1175 1231
1266{ 1322{
1267 ev_clear_pending (EV_A_ (W)w); 1323 ev_clear_pending (EV_A_ (W)w);
1268 if (!ev_is_active (w)) 1324 if (!ev_is_active (w))
1269 return; 1325 return;
1270 1326
1327 assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax));
1328
1271 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 1329 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
1272 ev_stop (EV_A_ (W)w); 1330 ev_stop (EV_A_ (W)w);
1273 1331
1274 fd_change (EV_A_ w->fd); 1332 fd_change (EV_A_ w->fd);
1275} 1333}
1302 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1360 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1303 1361
1304 if (((W)w)->active < timercnt--) 1362 if (((W)w)->active < timercnt--)
1305 { 1363 {
1306 timers [((W)w)->active - 1] = timers [timercnt]; 1364 timers [((W)w)->active - 1] = timers [timercnt];
1307 downheap ((WT *)timers, timercnt, ((W)w)->active - 1); 1365 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1308 } 1366 }
1309 1367
1310 ((WT)w)->at = w->repeat; 1368 ((WT)w)->at -= mn_now;
1311 1369
1312 ev_stop (EV_A_ (W)w); 1370 ev_stop (EV_A_ (W)w);
1313} 1371}
1314 1372
1315void 1373void
1318 if (ev_is_active (w)) 1376 if (ev_is_active (w))
1319 { 1377 {
1320 if (w->repeat) 1378 if (w->repeat)
1321 { 1379 {
1322 ((WT)w)->at = mn_now + w->repeat; 1380 ((WT)w)->at = mn_now + w->repeat;
1323 downheap ((WT *)timers, timercnt, ((W)w)->active - 1); 1381 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1324 } 1382 }
1325 else 1383 else
1326 ev_timer_stop (EV_A_ w); 1384 ev_timer_stop (EV_A_ w);
1327 } 1385 }
1328 else if (w->repeat) 1386 else if (w->repeat)
1329 ev_timer_start (EV_A_ w); 1387 ev_timer_start (EV_A_ w);
1330} 1388}
1331 1389
1390#if EV_PERIODICS
1332void 1391void
1333ev_periodic_start (EV_P_ struct ev_periodic *w) 1392ev_periodic_start (EV_P_ struct ev_periodic *w)
1334{ 1393{
1335 if (ev_is_active (w)) 1394 if (ev_is_active (w))
1336 return; 1395 return;
1337 1396
1338 if (w->reschedule_cb) 1397 if (w->reschedule_cb)
1339 ((WT)w)->at = w->reschedule_cb (w, rt_now); 1398 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1340 else if (w->interval) 1399 else if (w->interval)
1341 { 1400 {
1342 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); 1401 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
1343 /* this formula differs from the one in periodic_reify because we do not always round up */ 1402 /* this formula differs from the one in periodic_reify because we do not always round up */
1344 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; 1403 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1345 } 1404 }
1346 1405
1347 ev_start (EV_A_ (W)w, ++periodiccnt); 1406 ev_start (EV_A_ (W)w, ++periodiccnt);
1348 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); 1407 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void));
1349 periodics [periodiccnt - 1] = w; 1408 periodics [periodiccnt - 1] = w;
1362 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1421 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1363 1422
1364 if (((W)w)->active < periodiccnt--) 1423 if (((W)w)->active < periodiccnt--)
1365 { 1424 {
1366 periodics [((W)w)->active - 1] = periodics [periodiccnt]; 1425 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1367 downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); 1426 adjustheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1368 } 1427 }
1369 1428
1370 ev_stop (EV_A_ (W)w); 1429 ev_stop (EV_A_ (W)w);
1371} 1430}
1372 1431
1373void 1432void
1374ev_periodic_again (EV_P_ struct ev_periodic *w) 1433ev_periodic_again (EV_P_ struct ev_periodic *w)
1375{ 1434{
1435 /* TODO: use adjustheap and recalculation */
1376 ev_periodic_stop (EV_A_ w); 1436 ev_periodic_stop (EV_A_ w);
1377 ev_periodic_start (EV_A_ w); 1437 ev_periodic_start (EV_A_ w);
1378} 1438}
1439#endif
1379 1440
1380void 1441void
1381ev_idle_start (EV_P_ struct ev_idle *w) 1442ev_idle_start (EV_P_ struct ev_idle *w)
1382{ 1443{
1383 if (ev_is_active (w)) 1444 if (ev_is_active (w))
1390 1451
1391void 1452void
1392ev_idle_stop (EV_P_ struct ev_idle *w) 1453ev_idle_stop (EV_P_ struct ev_idle *w)
1393{ 1454{
1394 ev_clear_pending (EV_A_ (W)w); 1455 ev_clear_pending (EV_A_ (W)w);
1395 if (ev_is_active (w)) 1456 if (!ev_is_active (w))
1396 return; 1457 return;
1397 1458
1398 idles [((W)w)->active - 1] = idles [--idlecnt]; 1459 idles [((W)w)->active - 1] = idles [--idlecnt];
1399 ev_stop (EV_A_ (W)w); 1460 ev_stop (EV_A_ (W)w);
1400} 1461}
1412 1473
1413void 1474void
1414ev_prepare_stop (EV_P_ struct ev_prepare *w) 1475ev_prepare_stop (EV_P_ struct ev_prepare *w)
1415{ 1476{
1416 ev_clear_pending (EV_A_ (W)w); 1477 ev_clear_pending (EV_A_ (W)w);
1417 if (ev_is_active (w)) 1478 if (!ev_is_active (w))
1418 return; 1479 return;
1419 1480
1420 prepares [((W)w)->active - 1] = prepares [--preparecnt]; 1481 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1421 ev_stop (EV_A_ (W)w); 1482 ev_stop (EV_A_ (W)w);
1422} 1483}
1434 1495
1435void 1496void
1436ev_check_stop (EV_P_ struct ev_check *w) 1497ev_check_stop (EV_P_ struct ev_check *w)
1437{ 1498{
1438 ev_clear_pending (EV_A_ (W)w); 1499 ev_clear_pending (EV_A_ (W)w);
1439 if (ev_is_active (w)) 1500 if (!ev_is_active (w))
1440 return; 1501 return;
1441 1502
1442 checks [((W)w)->active - 1] = checks [--checkcnt]; 1503 checks [((W)w)->active - 1] = checks [--checkcnt];
1443 ev_stop (EV_A_ (W)w); 1504 ev_stop (EV_A_ (W)w);
1444} 1505}
1462 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init); 1523 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init);
1463 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1524 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1464 1525
1465 if (!((WL)w)->next) 1526 if (!((WL)w)->next)
1466 { 1527 {
1467#if WIN32 1528#if _WIN32
1468 signal (w->signum, sighandler); 1529 signal (w->signum, sighandler);
1469#else 1530#else
1470 struct sigaction sa; 1531 struct sigaction sa;
1471 sa.sa_handler = sighandler; 1532 sa.sa_handler = sighandler;
1472 sigfillset (&sa.sa_mask); 1533 sigfillset (&sa.sa_mask);
1505 1566
1506void 1567void
1507ev_child_stop (EV_P_ struct ev_child *w) 1568ev_child_stop (EV_P_ struct ev_child *w)
1508{ 1569{
1509 ev_clear_pending (EV_A_ (W)w); 1570 ev_clear_pending (EV_A_ (W)w);
1510 if (ev_is_active (w)) 1571 if (!ev_is_active (w))
1511 return; 1572 return;
1512 1573
1513 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1574 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1514 ev_stop (EV_A_ (W)w); 1575 ev_stop (EV_A_ (W)w);
1515} 1576}
1559 else 1620 else
1560 { 1621 {
1561 once->cb = cb; 1622 once->cb = cb;
1562 once->arg = arg; 1623 once->arg = arg;
1563 1624
1564 ev_watcher_init (&once->io, once_cb_io); 1625 ev_init (&once->io, once_cb_io);
1565 if (fd >= 0) 1626 if (fd >= 0)
1566 { 1627 {
1567 ev_io_set (&once->io, fd, events); 1628 ev_io_set (&once->io, fd, events);
1568 ev_io_start (EV_A_ &once->io); 1629 ev_io_start (EV_A_ &once->io);
1569 } 1630 }
1570 1631
1571 ev_watcher_init (&once->to, once_cb_to); 1632 ev_init (&once->to, once_cb_to);
1572 if (timeout >= 0.) 1633 if (timeout >= 0.)
1573 { 1634 {
1574 ev_timer_set (&once->to, timeout, 0.); 1635 ev_timer_set (&once->to, timeout, 0.);
1575 ev_timer_start (EV_A_ &once->to); 1636 ev_timer_start (EV_A_ &once->to);
1576 } 1637 }
1577 } 1638 }
1578} 1639}
1579 1640
1641#ifdef __cplusplus
1642}
1643#endif
1644

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines