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

Comparing libev/ev.c (file contents):
Revision 1.98 by root, Sun Nov 11 02:05:20 2007 UTC vs.
Revision 1.120 by root, Fri Nov 16 01:54:25 2007 UTC

51 51
52# if HAVE_POLL && HAVE_POLL_H && !defined (EV_USE_POLL) 52# if HAVE_POLL && HAVE_POLL_H && !defined (EV_USE_POLL)
53# define EV_USE_POLL 1 53# define EV_USE_POLL 1
54# endif 54# endif
55 55
56# if HAVE_EPOLL && HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H && !defined (EV_USE_EPOLL) 56# if HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H && !defined (EV_USE_EPOLL)
57# define EV_USE_EPOLL 1 57# define EV_USE_EPOLL 1
58# endif 58# endif
59 59
60# if HAVE_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H && !defined (EV_USE_KQUEUE) 60# if HAVE_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H && !defined (EV_USE_KQUEUE)
61# define EV_USE_KQUEUE 1 61# define EV_USE_KQUEUE 1
62# endif
63
64# if HAVE_PORT_H && HAVE_PORT_CREATE && !defined (EV_USE_PORT)
65# define EV_USE_PORT 1
62# endif 66# endif
63 67
64#endif 68#endif
65 69
66#include <math.h> 70#include <math.h>
75#include <sys/types.h> 79#include <sys/types.h>
76#include <time.h> 80#include <time.h>
77 81
78#include <signal.h> 82#include <signal.h>
79 83
80#ifndef WIN32 84#ifndef _WIN32
81# include <unistd.h> 85# include <unistd.h>
82# include <sys/time.h> 86# include <sys/time.h>
83# include <sys/wait.h> 87# include <sys/wait.h>
88#else
89# define WIN32_LEAN_AND_MEAN
90# include <windows.h>
91# ifndef EV_SELECT_IS_WINSOCKET
92# define EV_SELECT_IS_WINSOCKET 1
84#endif 93# endif
94#endif
95
85/**/ 96/**/
86 97
87#ifndef EV_USE_MONOTONIC 98#ifndef EV_USE_MONOTONIC
88# define EV_USE_MONOTONIC 1 99# define EV_USE_MONOTONIC 1
89#endif 100#endif
90 101
102#ifndef EV_USE_REALTIME
103# define EV_USE_REALTIME 1
104#endif
105
91#ifndef EV_USE_SELECT 106#ifndef EV_USE_SELECT
92# define EV_USE_SELECT 1 107# define EV_USE_SELECT 1
108# define EV_SELECT_USE_FD_SET 1
93#endif 109#endif
94 110
95#ifndef EV_USE_POLL 111#ifndef EV_USE_POLL
96# define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */ 112# ifdef _WIN32
113# define EV_USE_POLL 0
114# else
115# define EV_USE_POLL 1
116# endif
97#endif 117#endif
98 118
99#ifndef EV_USE_EPOLL 119#ifndef EV_USE_EPOLL
100# define EV_USE_EPOLL 0 120# define EV_USE_EPOLL 0
101#endif 121#endif
102 122
103#ifndef EV_USE_KQUEUE 123#ifndef EV_USE_KQUEUE
104# define EV_USE_KQUEUE 0 124# define EV_USE_KQUEUE 0
105#endif 125#endif
106 126
107#ifndef EV_USE_WIN32
108# ifdef WIN32
109# define EV_USE_WIN32 0 /* it does not exist, use select */
110# undef EV_USE_SELECT
111# define EV_USE_SELECT 1
112# else
113# define EV_USE_WIN32 0
114# endif
115#endif
116
117#ifndef EV_USE_REALTIME 127#ifndef EV_USE_PORT
118# define EV_USE_REALTIME 1 128# define EV_USE_PORT 0
119#endif 129#endif
120 130
121/**/ 131/**/
132
133/* darwin simply cannot be helped */
134#ifdef __APPLE__
135# undef EV_USE_POLL
136# undef EV_USE_KQUEUE
137#endif
122 138
123#ifndef CLOCK_MONOTONIC 139#ifndef CLOCK_MONOTONIC
124# undef EV_USE_MONOTONIC 140# undef EV_USE_MONOTONIC
125# define EV_USE_MONOTONIC 0 141# define EV_USE_MONOTONIC 0
126#endif 142#endif
128#ifndef CLOCK_REALTIME 144#ifndef CLOCK_REALTIME
129# undef EV_USE_REALTIME 145# undef EV_USE_REALTIME
130# define EV_USE_REALTIME 0 146# define EV_USE_REALTIME 0
131#endif 147#endif
132 148
149#if EV_SELECT_IS_WINSOCKET
150# include <winsock.h>
151#endif
152
133/**/ 153/**/
134 154
135#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 155#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
136#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */ 156#define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */
137#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ 157#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */
138/*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ 158/*#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */
139 159
140#ifdef EV_H 160#ifdef EV_H
141# include EV_H 161# include EV_H
142#else 162#else
143# include "ev.h" 163# include "ev.h"
155#define expect_true(expr) expect ((expr) != 0, 1) 175#define expect_true(expr) expect ((expr) != 0, 1)
156 176
157#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) 177#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
158#define ABSPRI(w) ((w)->priority - EV_MINPRI) 178#define ABSPRI(w) ((w)->priority - EV_MINPRI)
159 179
180#define EMPTY0 /* required for microsofts broken pseudo-c compiler */
181#define EMPTY2(a,b) /* used to suppress some warnings */
182
160typedef struct ev_watcher *W; 183typedef struct ev_watcher *W;
161typedef struct ev_watcher_list *WL; 184typedef struct ev_watcher_list *WL;
162typedef struct ev_watcher_time *WT; 185typedef struct ev_watcher_time *WT;
163 186
164static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 187static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
165 188
166#ifdef WIN32 189#ifdef _WIN32
167# include "ev_win32.c" 190# include "ev_win32.c"
168#endif 191#endif
169 192
170/*****************************************************************************/ 193/*****************************************************************************/
171 194
220typedef struct 243typedef struct
221{ 244{
222 WL head; 245 WL head;
223 unsigned char events; 246 unsigned char events;
224 unsigned char reify; 247 unsigned char reify;
248#if EV_SELECT_IS_WINSOCKET
249 SOCKET handle;
250#endif
225} ANFD; 251} ANFD;
226 252
227typedef struct 253typedef struct
228{ 254{
229 W w; 255 W w;
233#if EV_MULTIPLICITY 259#if EV_MULTIPLICITY
234 260
235 struct ev_loop 261 struct ev_loop
236 { 262 {
237 ev_tstamp ev_rt_now; 263 ev_tstamp ev_rt_now;
264 #define ev_rt_now ((loop)->ev_rt_now)
238 #define VAR(name,decl) decl; 265 #define VAR(name,decl) decl;
239 #include "ev_vars.h" 266 #include "ev_vars.h"
240 #undef VAR 267 #undef VAR
241 }; 268 };
242 #include "ev_wrap.h" 269 #include "ev_wrap.h"
243 270
244 struct ev_loop default_loop_struct; 271 static struct ev_loop default_loop_struct;
245 static struct ev_loop *default_loop; 272 struct ev_loop *ev_default_loop_ptr;
246 273
247#else 274#else
248 275
249 ev_tstamp ev_rt_now; 276 ev_tstamp ev_rt_now;
250 #define VAR(name,decl) static decl; 277 #define VAR(name,decl) static decl;
251 #include "ev_vars.h" 278 #include "ev_vars.h"
252 #undef VAR 279 #undef VAR
253 280
254 static int default_loop; 281 static int ev_default_loop_ptr;
255 282
256#endif 283#endif
257 284
258/*****************************************************************************/ 285/*****************************************************************************/
259 286
292{ 319{
293 return ev_rt_now; 320 return ev_rt_now;
294} 321}
295#endif 322#endif
296 323
297#define array_roundsize(type,n) ((n) | 4 & ~3) 324#define array_roundsize(type,n) (((n) | 4) & ~3)
298 325
299#define array_needsize(type,base,cur,cnt,init) \ 326#define array_needsize(type,base,cur,cnt,init) \
300 if (expect_false ((cnt) > cur)) \ 327 if (expect_false ((cnt) > cur)) \
301 { \ 328 { \
302 int newcnt = cur; \ 329 int newcnt = cur; \
317 stem ## max = array_roundsize (stem ## cnt >> 1); \ 344 stem ## max = array_roundsize (stem ## cnt >> 1); \
318 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ 345 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\
319 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ 346 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
320 } 347 }
321 348
322/* microsoft's pseudo-c is quite far from C as the rest of the world and the standard knows it */
323/* bringing us everlasting joy in form of stupid extra macros that are not required in C */
324#define array_free_microshit(stem) \
325 ev_free (stem ## s); stem ## cnt = stem ## max = 0;
326
327#define array_free(stem, idx) \ 349#define array_free(stem, idx) \
328 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; 350 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
329 351
330/*****************************************************************************/ 352/*****************************************************************************/
331 353
352 pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; 374 pendings [ABSPRI (w_)][w_->pending - 1].events |= revents;
353 return; 375 return;
354 } 376 }
355 377
356 w_->pending = ++pendingcnt [ABSPRI (w_)]; 378 w_->pending = ++pendingcnt [ABSPRI (w_)];
357 array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], (void)); 379 array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], EMPTY2);
358 pendings [ABSPRI (w_)][w_->pending - 1].w = w_; 380 pendings [ABSPRI (w_)][w_->pending - 1].w = w_;
359 pendings [ABSPRI (w_)][w_->pending - 1].events = revents; 381 pendings [ABSPRI (w_)][w_->pending - 1].events = revents;
360} 382}
361 383
362static void 384static void
405 int events = 0; 427 int events = 0;
406 428
407 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) 429 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
408 events |= w->events; 430 events |= w->events;
409 431
432#if EV_SELECT_IS_WINSOCKET
433 if (events)
434 {
435 unsigned long argp;
436 anfd->handle = _get_osfhandle (fd);
437 assert (("libev only supports socket fds in this configuration", ioctlsocket (anfd->handle, FIONREAD, &argp) == 0));
438 }
439#endif
440
410 anfd->reify = 0; 441 anfd->reify = 0;
411 442
412 method_modify (EV_A_ fd, anfd->events, events); 443 method_modify (EV_A_ fd, anfd->events, events);
413 anfd->events = events; 444 anfd->events = events;
414 } 445 }
423 return; 454 return;
424 455
425 anfds [fd].reify = 1; 456 anfds [fd].reify = 1;
426 457
427 ++fdchangecnt; 458 ++fdchangecnt;
428 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, (void)); 459 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2);
429 fdchanges [fdchangecnt - 1] = fd; 460 fdchanges [fdchangecnt - 1] = fd;
430} 461}
431 462
432static void 463static void
433fd_kill (EV_P_ int fd) 464fd_kill (EV_P_ int fd)
442} 473}
443 474
444static int 475static int
445fd_valid (int fd) 476fd_valid (int fd)
446{ 477{
447#ifdef WIN32 478#ifdef _WIN32
448 return !!win32_get_osfhandle (fd); 479 return _get_osfhandle (fd) != -1;
449#else 480#else
450 return fcntl (fd, F_GETFD) != -1; 481 return fcntl (fd, F_GETFD) != -1;
451#endif 482#endif
452} 483}
453 484
534 heap [k] = w; 565 heap [k] = w;
535 ((W)heap [k])->active = k + 1; 566 ((W)heap [k])->active = k + 1;
536} 567}
537 568
538inline void 569inline void
539adjustheap (WT *heap, int N, int k, ev_tstamp at) 570adjustheap (WT *heap, int N, int k)
540{ 571{
541 ev_tstamp old_at = heap [k]->at; 572 upheap (heap, k);
542 heap [k]->at = at;
543
544 if (old_at < at)
545 downheap (heap, N, k); 573 downheap (heap, N, k);
546 else
547 upheap (heap, k);
548} 574}
549 575
550/*****************************************************************************/ 576/*****************************************************************************/
551 577
552typedef struct 578typedef struct
575} 601}
576 602
577static void 603static void
578sighandler (int signum) 604sighandler (int signum)
579{ 605{
580#if WIN32 606#if _WIN32
581 signal (signum, sighandler); 607 signal (signum, sighandler);
582#endif 608#endif
583 609
584 signals [signum - 1].gotsig = 1; 610 signals [signum - 1].gotsig = 1;
585 611
586 if (!gotsig) 612 if (!gotsig)
587 { 613 {
588 int old_errno = errno; 614 int old_errno = errno;
589 gotsig = 1; 615 gotsig = 1;
590#ifdef WIN32
591 send (sigpipe [1], &signum, 1, MSG_DONTWAIT);
592#else
593 write (sigpipe [1], &signum, 1); 616 write (sigpipe [1], &signum, 1);
594#endif
595 errno = old_errno; 617 errno = old_errno;
596 } 618 }
597} 619}
598 620
599void 621void
600ev_feed_signal_event (EV_P_ int signum) 622ev_feed_signal_event (EV_P_ int signum)
601{ 623{
602 WL w; 624 WL w;
603 625
604#if EV_MULTIPLICITY 626#if EV_MULTIPLICITY
605 assert (("feeding signal events is only supported in the default loop", loop == default_loop)); 627 assert (("feeding signal events is only supported in the default loop", loop == ev_default_loop_ptr));
606#endif 628#endif
607 629
608 --signum; 630 --signum;
609 631
610 if (signum < 0 || signum >= signalmax) 632 if (signum < 0 || signum >= signalmax)
619static void 641static void
620sigcb (EV_P_ struct ev_io *iow, int revents) 642sigcb (EV_P_ struct ev_io *iow, int revents)
621{ 643{
622 int signum; 644 int signum;
623 645
624#ifdef WIN32
625 recv (sigpipe [0], &revents, 1, MSG_DONTWAIT);
626#else
627 read (sigpipe [0], &revents, 1); 646 read (sigpipe [0], &revents, 1);
628#endif
629 gotsig = 0; 647 gotsig = 0;
630 648
631 for (signum = signalmax; signum--; ) 649 for (signum = signalmax; signum--; )
632 if (signals [signum].gotsig) 650 if (signals [signum].gotsig)
633 ev_feed_signal_event (EV_A_ signum + 1); 651 ev_feed_signal_event (EV_A_ signum + 1);
634} 652}
635 653
654inline void
655fd_intern (int fd)
656{
657#ifdef _WIN32
658 int arg = 1;
659 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg);
660#else
661 fcntl (fd, F_SETFD, FD_CLOEXEC);
662 fcntl (fd, F_SETFL, O_NONBLOCK);
663#endif
664}
665
636static void 666static void
637siginit (EV_P) 667siginit (EV_P)
638{ 668{
639#ifndef WIN32 669 fd_intern (sigpipe [0]);
640 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); 670 fd_intern (sigpipe [1]);
641 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC);
642
643 /* rather than sort out wether we really need nb, set it */
644 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
645 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
646#endif
647 671
648 ev_io_set (&sigev, sigpipe [0], EV_READ); 672 ev_io_set (&sigev, sigpipe [0], EV_READ);
649 ev_io_start (EV_A_ &sigev); 673 ev_io_start (EV_A_ &sigev);
650 ev_unref (EV_A); /* child watcher should not keep loop alive */ 674 ev_unref (EV_A); /* child watcher should not keep loop alive */
651} 675}
652 676
653/*****************************************************************************/ 677/*****************************************************************************/
654 678
655static struct ev_child *childs [PID_HASHSIZE]; 679static struct ev_child *childs [PID_HASHSIZE];
656 680
657#ifndef WIN32 681#ifndef _WIN32
658 682
659static struct ev_signal childev; 683static struct ev_signal childev;
660 684
661#ifndef WCONTINUED 685#ifndef WCONTINUED
662# define WCONTINUED 0 686# define WCONTINUED 0
694 718
695#endif 719#endif
696 720
697/*****************************************************************************/ 721/*****************************************************************************/
698 722
723#if EV_USE_PORT
724# include "ev_port.c"
725#endif
699#if EV_USE_KQUEUE 726#if EV_USE_KQUEUE
700# include "ev_kqueue.c" 727# include "ev_kqueue.c"
701#endif 728#endif
702#if EV_USE_EPOLL 729#if EV_USE_EPOLL
703# include "ev_epoll.c" 730# include "ev_epoll.c"
723 750
724/* return true if we are running with elevated privileges and should ignore env variables */ 751/* return true if we are running with elevated privileges and should ignore env variables */
725static int 752static int
726enable_secure (void) 753enable_secure (void)
727{ 754{
728#ifdef WIN32 755#ifdef _WIN32
729 return 0; 756 return 0;
730#else 757#else
731 return getuid () != geteuid () 758 return getuid () != geteuid ()
732 || getgid () != getegid (); 759 || getgid () != getegid ();
733#endif 760#endif
734} 761}
735 762
736int 763unsigned int
737ev_method (EV_P) 764ev_method (EV_P)
738{ 765{
739 return method; 766 return method;
740} 767}
741 768
742static void 769static void
743loop_init (EV_P_ int methods) 770loop_init (EV_P_ unsigned int flags)
744{ 771{
745 if (!method) 772 if (!method)
746 { 773 {
747#if EV_USE_MONOTONIC 774#if EV_USE_MONOTONIC
748 { 775 {
755 ev_rt_now = ev_time (); 782 ev_rt_now = ev_time ();
756 mn_now = get_clock (); 783 mn_now = get_clock ();
757 now_floor = mn_now; 784 now_floor = mn_now;
758 rtmn_diff = ev_rt_now - mn_now; 785 rtmn_diff = ev_rt_now - mn_now;
759 786
760 if (methods == EVMETHOD_AUTO) 787 if (!(flags & EVFLAG_NOENV) && !enable_secure () && getenv ("LIBEV_FLAGS"))
761 if (!enable_secure () && getenv ("LIBEV_METHODS"))
762 methods = atoi (getenv ("LIBEV_METHODS")); 788 flags = atoi (getenv ("LIBEV_FLAGS"));
763 else 789
764 methods = EVMETHOD_ANY; 790 if (!(flags & 0x0000ffff))
791 flags |= 0x0000ffff;
765 792
766 method = 0; 793 method = 0;
767#if EV_USE_WIN32 794#if EV_USE_PORT
768 if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods); 795 if (!method && (flags & EVMETHOD_PORT )) method = port_init (EV_A_ flags);
769#endif 796#endif
770#if EV_USE_KQUEUE 797#if EV_USE_KQUEUE
771 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); 798 if (!method && (flags & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ flags);
772#endif 799#endif
773#if EV_USE_EPOLL 800#if EV_USE_EPOLL
774 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); 801 if (!method && (flags & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ flags);
775#endif 802#endif
776#if EV_USE_POLL 803#if EV_USE_POLL
777 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); 804 if (!method && (flags & EVMETHOD_POLL )) method = poll_init (EV_A_ flags);
778#endif 805#endif
779#if EV_USE_SELECT 806#if EV_USE_SELECT
780 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 807 if (!method && (flags & EVMETHOD_SELECT)) method = select_init (EV_A_ flags);
781#endif 808#endif
782 809
783 ev_init (&sigev, sigcb); 810 ev_init (&sigev, sigcb);
784 ev_set_priority (&sigev, EV_MAXPRI); 811 ev_set_priority (&sigev, EV_MAXPRI);
785 } 812 }
788void 815void
789loop_destroy (EV_P) 816loop_destroy (EV_P)
790{ 817{
791 int i; 818 int i;
792 819
793#if EV_USE_WIN32 820#if EV_USE_PORT
794 if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A); 821 if (method == EVMETHOD_PORT ) port_destroy (EV_A);
795#endif 822#endif
796#if EV_USE_KQUEUE 823#if EV_USE_KQUEUE
797 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); 824 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
798#endif 825#endif
799#if EV_USE_EPOLL 826#if EV_USE_EPOLL
808 835
809 for (i = NUMPRI; i--; ) 836 for (i = NUMPRI; i--; )
810 array_free (pending, [i]); 837 array_free (pending, [i]);
811 838
812 /* have to use the microsoft-never-gets-it-right macro */ 839 /* have to use the microsoft-never-gets-it-right macro */
813 array_free_microshit (fdchange); 840 array_free (fdchange, EMPTY0);
814 array_free_microshit (timer); 841 array_free (timer, EMPTY0);
815#if EV_PERIODICS 842#if EV_PERIODICS
816 array_free_microshit (periodic); 843 array_free (periodic, EMPTY0);
817#endif 844#endif
818 array_free_microshit (idle); 845 array_free (idle, EMPTY0);
819 array_free_microshit (prepare); 846 array_free (prepare, EMPTY0);
820 array_free_microshit (check); 847 array_free (check, EMPTY0);
821 848
822 method = 0; 849 method = 0;
823} 850}
824 851
825static void 852static void
826loop_fork (EV_P) 853loop_fork (EV_P)
827{ 854{
855#if EV_USE_PORT
856 if (method == EVMETHOD_PORT ) port_fork (EV_A);
857#endif
858#if EV_USE_KQUEUE
859 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A);
860#endif
828#if EV_USE_EPOLL 861#if EV_USE_EPOLL
829 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A); 862 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A);
830#endif
831#if EV_USE_KQUEUE
832 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A);
833#endif 863#endif
834 864
835 if (ev_is_active (&sigev)) 865 if (ev_is_active (&sigev))
836 { 866 {
837 /* default loop */ 867 /* default loop */
850 postfork = 0; 880 postfork = 0;
851} 881}
852 882
853#if EV_MULTIPLICITY 883#if EV_MULTIPLICITY
854struct ev_loop * 884struct ev_loop *
855ev_loop_new (int methods) 885ev_loop_new (unsigned int flags)
856{ 886{
857 struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); 887 struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop));
858 888
859 memset (loop, 0, sizeof (struct ev_loop)); 889 memset (loop, 0, sizeof (struct ev_loop));
860 890
861 loop_init (EV_A_ methods); 891 loop_init (EV_A_ flags);
862 892
863 if (ev_method (EV_A)) 893 if (ev_method (EV_A))
864 return loop; 894 return loop;
865 895
866 return 0; 896 return 0;
881 911
882#endif 912#endif
883 913
884#if EV_MULTIPLICITY 914#if EV_MULTIPLICITY
885struct ev_loop * 915struct ev_loop *
916ev_default_loop_ (unsigned int flags)
886#else 917#else
887int 918int
919ev_default_loop (unsigned int flags)
888#endif 920#endif
889ev_default_loop (int methods)
890{ 921{
891 if (sigpipe [0] == sigpipe [1]) 922 if (sigpipe [0] == sigpipe [1])
892 if (pipe (sigpipe)) 923 if (pipe (sigpipe))
893 return 0; 924 return 0;
894 925
895 if (!default_loop) 926 if (!ev_default_loop_ptr)
896 { 927 {
897#if EV_MULTIPLICITY 928#if EV_MULTIPLICITY
898 struct ev_loop *loop = default_loop = &default_loop_struct; 929 struct ev_loop *loop = ev_default_loop_ptr = &default_loop_struct;
899#else 930#else
900 default_loop = 1; 931 ev_default_loop_ptr = 1;
901#endif 932#endif
902 933
903 loop_init (EV_A_ methods); 934 loop_init (EV_A_ flags);
904 935
905 if (ev_method (EV_A)) 936 if (ev_method (EV_A))
906 { 937 {
907 siginit (EV_A); 938 siginit (EV_A);
908 939
909#ifndef WIN32 940#ifndef _WIN32
910 ev_signal_init (&childev, childcb, SIGCHLD); 941 ev_signal_init (&childev, childcb, SIGCHLD);
911 ev_set_priority (&childev, EV_MAXPRI); 942 ev_set_priority (&childev, EV_MAXPRI);
912 ev_signal_start (EV_A_ &childev); 943 ev_signal_start (EV_A_ &childev);
913 ev_unref (EV_A); /* child watcher should not keep loop alive */ 944 ev_unref (EV_A); /* child watcher should not keep loop alive */
914#endif 945#endif
915 } 946 }
916 else 947 else
917 default_loop = 0; 948 ev_default_loop_ptr = 0;
918 } 949 }
919 950
920 return default_loop; 951 return ev_default_loop_ptr;
921} 952}
922 953
923void 954void
924ev_default_destroy (void) 955ev_default_destroy (void)
925{ 956{
926#if EV_MULTIPLICITY 957#if EV_MULTIPLICITY
927 struct ev_loop *loop = default_loop; 958 struct ev_loop *loop = ev_default_loop_ptr;
928#endif 959#endif
929 960
930#ifndef WIN32 961#ifndef _WIN32
931 ev_ref (EV_A); /* child watcher */ 962 ev_ref (EV_A); /* child watcher */
932 ev_signal_stop (EV_A_ &childev); 963 ev_signal_stop (EV_A_ &childev);
933#endif 964#endif
934 965
935 ev_ref (EV_A); /* signal watcher */ 966 ev_ref (EV_A); /* signal watcher */
943 974
944void 975void
945ev_default_fork (void) 976ev_default_fork (void)
946{ 977{
947#if EV_MULTIPLICITY 978#if EV_MULTIPLICITY
948 struct ev_loop *loop = default_loop; 979 struct ev_loop *loop = ev_default_loop_ptr;
949#endif 980#endif
950 981
951 if (method) 982 if (method)
952 postfork = 1; 983 postfork = 1;
953} 984}
1022 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 1053 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
1023 1054
1024 /* first reschedule or stop timer */ 1055 /* first reschedule or stop timer */
1025 if (w->reschedule_cb) 1056 if (w->reschedule_cb)
1026 { 1057 {
1027 ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001); 1058 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001);
1028
1029 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now)); 1059 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now));
1030 downheap ((WT *)periodics, periodiccnt, 0); 1060 downheap ((WT *)periodics, periodiccnt, 0);
1031 } 1061 }
1032 else if (w->interval) 1062 else if (w->interval)
1033 { 1063 {
1151ev_loop (EV_P_ int flags) 1181ev_loop (EV_P_ int flags)
1152{ 1182{
1153 double block; 1183 double block;
1154 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; 1184 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
1155 1185
1156 do 1186 while (activecnt)
1157 { 1187 {
1158 /* queue check watchers (and execute them) */ 1188 /* queue check watchers (and execute them) */
1159 if (expect_false (preparecnt)) 1189 if (expect_false (preparecnt))
1160 { 1190 {
1161 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 1191 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
1224 /* queue check watchers, to be executed first */ 1254 /* queue check watchers, to be executed first */
1225 if (checkcnt) 1255 if (checkcnt)
1226 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1256 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1227 1257
1228 call_pending (EV_A); 1258 call_pending (EV_A);
1259
1260 if (loop_done)
1261 break;
1229 } 1262 }
1230 while (activecnt && !loop_done);
1231 1263
1232 if (loop_done != 2) 1264 if (loop_done != 2)
1233 loop_done = 0; 1265 loop_done = 0;
1234} 1266}
1235 1267
1333 ((WT)w)->at += mn_now; 1365 ((WT)w)->at += mn_now;
1334 1366
1335 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1367 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1336 1368
1337 ev_start (EV_A_ (W)w, ++timercnt); 1369 ev_start (EV_A_ (W)w, ++timercnt);
1338 array_needsize (struct ev_timer *, timers, timermax, timercnt, (void)); 1370 array_needsize (struct ev_timer *, timers, timermax, timercnt, EMPTY2);
1339 timers [timercnt - 1] = w; 1371 timers [timercnt - 1] = w;
1340 upheap ((WT *)timers, timercnt - 1); 1372 upheap ((WT *)timers, timercnt - 1);
1341 1373
1342 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1374 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1343} 1375}
1352 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1384 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1353 1385
1354 if (((W)w)->active < timercnt--) 1386 if (((W)w)->active < timercnt--)
1355 { 1387 {
1356 timers [((W)w)->active - 1] = timers [timercnt]; 1388 timers [((W)w)->active - 1] = timers [timercnt];
1357 downheap ((WT *)timers, timercnt, ((W)w)->active - 1); 1389 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1358 } 1390 }
1359 1391
1360 ((WT)w)->at -= mn_now; 1392 ((WT)w)->at -= mn_now;
1361 1393
1362 ev_stop (EV_A_ (W)w); 1394 ev_stop (EV_A_ (W)w);
1366ev_timer_again (EV_P_ struct ev_timer *w) 1398ev_timer_again (EV_P_ struct ev_timer *w)
1367{ 1399{
1368 if (ev_is_active (w)) 1400 if (ev_is_active (w))
1369 { 1401 {
1370 if (w->repeat) 1402 if (w->repeat)
1403 {
1404 ((WT)w)->at = mn_now + w->repeat;
1371 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1, mn_now + w->repeat); 1405 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1406 }
1372 else 1407 else
1373 ev_timer_stop (EV_A_ w); 1408 ev_timer_stop (EV_A_ w);
1374 } 1409 }
1375 else if (w->repeat) 1410 else if (w->repeat)
1411 {
1412 w->at = w->repeat;
1376 ev_timer_start (EV_A_ w); 1413 ev_timer_start (EV_A_ w);
1414 }
1377} 1415}
1378 1416
1379#if EV_PERIODICS 1417#if EV_PERIODICS
1380void 1418void
1381ev_periodic_start (EV_P_ struct ev_periodic *w) 1419ev_periodic_start (EV_P_ struct ev_periodic *w)
1391 /* this formula differs from the one in periodic_reify because we do not always round up */ 1429 /* this formula differs from the one in periodic_reify because we do not always round up */
1392 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; 1430 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1393 } 1431 }
1394 1432
1395 ev_start (EV_A_ (W)w, ++periodiccnt); 1433 ev_start (EV_A_ (W)w, ++periodiccnt);
1396 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); 1434 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2);
1397 periodics [periodiccnt - 1] = w; 1435 periodics [periodiccnt - 1] = w;
1398 upheap ((WT *)periodics, periodiccnt - 1); 1436 upheap ((WT *)periodics, periodiccnt - 1);
1399 1437
1400 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1438 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1401} 1439}
1410 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1448 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1411 1449
1412 if (((W)w)->active < periodiccnt--) 1450 if (((W)w)->active < periodiccnt--)
1413 { 1451 {
1414 periodics [((W)w)->active - 1] = periodics [periodiccnt]; 1452 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1415 downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); 1453 adjustheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1416 } 1454 }
1417 1455
1418 ev_stop (EV_A_ (W)w); 1456 ev_stop (EV_A_ (W)w);
1419} 1457}
1420 1458
1432{ 1470{
1433 if (ev_is_active (w)) 1471 if (ev_is_active (w))
1434 return; 1472 return;
1435 1473
1436 ev_start (EV_A_ (W)w, ++idlecnt); 1474 ev_start (EV_A_ (W)w, ++idlecnt);
1437 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, (void)); 1475 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, EMPTY2);
1438 idles [idlecnt - 1] = w; 1476 idles [idlecnt - 1] = w;
1439} 1477}
1440 1478
1441void 1479void
1442ev_idle_stop (EV_P_ struct ev_idle *w) 1480ev_idle_stop (EV_P_ struct ev_idle *w)
1443{ 1481{
1444 ev_clear_pending (EV_A_ (W)w); 1482 ev_clear_pending (EV_A_ (W)w);
1445 if (ev_is_active (w)) 1483 if (!ev_is_active (w))
1446 return; 1484 return;
1447 1485
1448 idles [((W)w)->active - 1] = idles [--idlecnt]; 1486 idles [((W)w)->active - 1] = idles [--idlecnt];
1449 ev_stop (EV_A_ (W)w); 1487 ev_stop (EV_A_ (W)w);
1450} 1488}
1454{ 1492{
1455 if (ev_is_active (w)) 1493 if (ev_is_active (w))
1456 return; 1494 return;
1457 1495
1458 ev_start (EV_A_ (W)w, ++preparecnt); 1496 ev_start (EV_A_ (W)w, ++preparecnt);
1459 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, (void)); 1497 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, EMPTY2);
1460 prepares [preparecnt - 1] = w; 1498 prepares [preparecnt - 1] = w;
1461} 1499}
1462 1500
1463void 1501void
1464ev_prepare_stop (EV_P_ struct ev_prepare *w) 1502ev_prepare_stop (EV_P_ struct ev_prepare *w)
1465{ 1503{
1466 ev_clear_pending (EV_A_ (W)w); 1504 ev_clear_pending (EV_A_ (W)w);
1467 if (ev_is_active (w)) 1505 if (!ev_is_active (w))
1468 return; 1506 return;
1469 1507
1470 prepares [((W)w)->active - 1] = prepares [--preparecnt]; 1508 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1471 ev_stop (EV_A_ (W)w); 1509 ev_stop (EV_A_ (W)w);
1472} 1510}
1476{ 1514{
1477 if (ev_is_active (w)) 1515 if (ev_is_active (w))
1478 return; 1516 return;
1479 1517
1480 ev_start (EV_A_ (W)w, ++checkcnt); 1518 ev_start (EV_A_ (W)w, ++checkcnt);
1481 array_needsize (struct ev_check *, checks, checkmax, checkcnt, (void)); 1519 array_needsize (struct ev_check *, checks, checkmax, checkcnt, EMPTY2);
1482 checks [checkcnt - 1] = w; 1520 checks [checkcnt - 1] = w;
1483} 1521}
1484 1522
1485void 1523void
1486ev_check_stop (EV_P_ struct ev_check *w) 1524ev_check_stop (EV_P_ struct ev_check *w)
1499 1537
1500void 1538void
1501ev_signal_start (EV_P_ struct ev_signal *w) 1539ev_signal_start (EV_P_ struct ev_signal *w)
1502{ 1540{
1503#if EV_MULTIPLICITY 1541#if EV_MULTIPLICITY
1504 assert (("signal watchers are only supported in the default loop", loop == default_loop)); 1542 assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr));
1505#endif 1543#endif
1506 if (ev_is_active (w)) 1544 if (ev_is_active (w))
1507 return; 1545 return;
1508 1546
1509 assert (("ev_signal_start called with illegal signal number", w->signum > 0)); 1547 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
1512 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init); 1550 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init);
1513 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1551 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1514 1552
1515 if (!((WL)w)->next) 1553 if (!((WL)w)->next)
1516 { 1554 {
1517#if WIN32 1555#if _WIN32
1518 signal (w->signum, sighandler); 1556 signal (w->signum, sighandler);
1519#else 1557#else
1520 struct sigaction sa; 1558 struct sigaction sa;
1521 sa.sa_handler = sighandler; 1559 sa.sa_handler = sighandler;
1522 sigfillset (&sa.sa_mask); 1560 sigfillset (&sa.sa_mask);
1542 1580
1543void 1581void
1544ev_child_start (EV_P_ struct ev_child *w) 1582ev_child_start (EV_P_ struct ev_child *w)
1545{ 1583{
1546#if EV_MULTIPLICITY 1584#if EV_MULTIPLICITY
1547 assert (("child watchers are only supported in the default loop", loop == default_loop)); 1585 assert (("child watchers are only supported in the default loop", loop == ev_default_loop_ptr));
1548#endif 1586#endif
1549 if (ev_is_active (w)) 1587 if (ev_is_active (w))
1550 return; 1588 return;
1551 1589
1552 ev_start (EV_A_ (W)w, 1); 1590 ev_start (EV_A_ (W)w, 1);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines