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

Comparing libev/ev.c (file contents):
Revision 1.97 by root, Sun Nov 11 01:53:07 2007 UTC vs.
Revision 1.118 by root, Fri Nov 16 01:33:54 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_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
127 143
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
147#endif
148
149#if EV_SELECT_IS_WINSOCKET
150# include <winsock.h>
131#endif 151#endif
132 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) */
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
189#ifdef _WIN32
166#include "ev_win32.c" 190# include "ev_win32.c"
191#endif
167 192
168/*****************************************************************************/ 193/*****************************************************************************/
169 194
170static void (*syserr_cb)(const char *msg); 195static void (*syserr_cb)(const char *msg);
171 196
218typedef struct 243typedef struct
219{ 244{
220 WL head; 245 WL head;
221 unsigned char events; 246 unsigned char events;
222 unsigned char reify; 247 unsigned char reify;
248#if EV_SELECT_IS_WINSOCKET
249 SOCKET handle;
250#endif
223} ANFD; 251} ANFD;
224 252
225typedef struct 253typedef struct
226{ 254{
227 W w; 255 W w;
231#if EV_MULTIPLICITY 259#if EV_MULTIPLICITY
232 260
233 struct ev_loop 261 struct ev_loop
234 { 262 {
235 ev_tstamp ev_rt_now; 263 ev_tstamp ev_rt_now;
264 #define ev_rt_now ((loop)->ev_rt_now)
236 #define VAR(name,decl) decl; 265 #define VAR(name,decl) decl;
237 #include "ev_vars.h" 266 #include "ev_vars.h"
238 #undef VAR 267 #undef VAR
239 }; 268 };
240 #include "ev_wrap.h" 269 #include "ev_wrap.h"
241 270
242 struct ev_loop default_loop_struct; 271 static struct ev_loop default_loop_struct;
243 static struct ev_loop *default_loop; 272 struct ev_loop *ev_default_loop_ptr;
244 273
245#else 274#else
246 275
247 ev_tstamp ev_rt_now; 276 ev_tstamp ev_rt_now;
248 #define VAR(name,decl) static decl; 277 #define VAR(name,decl) static decl;
249 #include "ev_vars.h" 278 #include "ev_vars.h"
250 #undef VAR 279 #undef VAR
251 280
252 static int default_loop; 281 static int ev_default_loop_ptr;
253 282
254#endif 283#endif
255 284
256/*****************************************************************************/ 285/*****************************************************************************/
257 286
290{ 319{
291 return ev_rt_now; 320 return ev_rt_now;
292} 321}
293#endif 322#endif
294 323
295#define array_roundsize(type,n) ((n) | 4 & ~3) 324#define array_roundsize(type,n) (((n) | 4) & ~3)
296 325
297#define array_needsize(type,base,cur,cnt,init) \ 326#define array_needsize(type,base,cur,cnt,init) \
298 if (expect_false ((cnt) > cur)) \ 327 if (expect_false ((cnt) > cur)) \
299 { \ 328 { \
300 int newcnt = cur; \ 329 int newcnt = cur; \
315 stem ## max = array_roundsize (stem ## cnt >> 1); \ 344 stem ## max = array_roundsize (stem ## cnt >> 1); \
316 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ 345 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\
317 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ 346 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
318 } 347 }
319 348
320/* microsoft's pseudo-c is quite far from C as the rest of the world and the standard knows it */
321/* bringing us everlasting joy in form of stupid extra macros that are not required in C */
322#define array_free_microshit(stem) \
323 ev_free (stem ## s); stem ## cnt = stem ## max = 0;
324
325#define array_free(stem, idx) \ 349#define array_free(stem, idx) \
326 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;
327 351
328/*****************************************************************************/ 352/*****************************************************************************/
329 353
350 pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; 374 pendings [ABSPRI (w_)][w_->pending - 1].events |= revents;
351 return; 375 return;
352 } 376 }
353 377
354 w_->pending = ++pendingcnt [ABSPRI (w_)]; 378 w_->pending = ++pendingcnt [ABSPRI (w_)];
355 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);
356 pendings [ABSPRI (w_)][w_->pending - 1].w = w_; 380 pendings [ABSPRI (w_)][w_->pending - 1].w = w_;
357 pendings [ABSPRI (w_)][w_->pending - 1].events = revents; 381 pendings [ABSPRI (w_)][w_->pending - 1].events = revents;
358} 382}
359 383
360static void 384static void
403 int events = 0; 427 int events = 0;
404 428
405 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)
406 events |= w->events; 430 events |= w->events;
407 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
408 anfd->reify = 0; 441 anfd->reify = 0;
409 442
410 method_modify (EV_A_ fd, anfd->events, events); 443 method_modify (EV_A_ fd, anfd->events, events);
411 anfd->events = events; 444 anfd->events = events;
412 } 445 }
421 return; 454 return;
422 455
423 anfds [fd].reify = 1; 456 anfds [fd].reify = 1;
424 457
425 ++fdchangecnt; 458 ++fdchangecnt;
426 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, (void)); 459 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2);
427 fdchanges [fdchangecnt - 1] = fd; 460 fdchanges [fdchangecnt - 1] = fd;
428} 461}
429 462
430static void 463static void
431fd_kill (EV_P_ int fd) 464fd_kill (EV_P_ int fd)
440} 473}
441 474
442static int 475static int
443fd_valid (int fd) 476fd_valid (int fd)
444{ 477{
445#ifdef WIN32 478#ifdef _WIN32
446 return !!win32_get_osfhandle (fd); 479 return _get_osfhandle (fd) != -1;
447#else 480#else
448 return fcntl (fd, F_GETFD) != -1; 481 return fcntl (fd, F_GETFD) != -1;
449#endif 482#endif
450} 483}
451 484
532 heap [k] = w; 565 heap [k] = w;
533 ((W)heap [k])->active = k + 1; 566 ((W)heap [k])->active = k + 1;
534} 567}
535 568
536inline void 569inline void
537adjustheap (WT *heap, int N, int k, ev_tstamp at) 570adjustheap (WT *heap, int N, int k)
538{ 571{
539 ev_tstamp old_at = heap [k]->at; 572 upheap (heap, k);
540 heap [k]->at = at;
541
542 if (old_at < at)
543 downheap (heap, N, k); 573 downheap (heap, N, k);
544 else
545 upheap (heap, k);
546} 574}
547 575
548/*****************************************************************************/ 576/*****************************************************************************/
549 577
550typedef struct 578typedef struct
573} 601}
574 602
575static void 603static void
576sighandler (int signum) 604sighandler (int signum)
577{ 605{
578#if WIN32 606#if _WIN32
579 signal (signum, sighandler); 607 signal (signum, sighandler);
580#endif 608#endif
581 609
582 signals [signum - 1].gotsig = 1; 610 signals [signum - 1].gotsig = 1;
583 611
584 if (!gotsig) 612 if (!gotsig)
585 { 613 {
586 int old_errno = errno; 614 int old_errno = errno;
587 gotsig = 1; 615 gotsig = 1;
588#ifdef WIN32
589 send (sigpipe [1], &signum, 1, MSG_DONTWAIT);
590#else
591 write (sigpipe [1], &signum, 1); 616 write (sigpipe [1], &signum, 1);
592#endif
593 errno = old_errno; 617 errno = old_errno;
594 } 618 }
595} 619}
596 620
597void 621void
598ev_feed_signal_event (EV_P_ int signum) 622ev_feed_signal_event (EV_P_ int signum)
599{ 623{
600 WL w; 624 WL w;
601 625
602#if EV_MULTIPLICITY 626#if EV_MULTIPLICITY
603 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));
604#endif 628#endif
605 629
606 --signum; 630 --signum;
607 631
608 if (signum < 0 || signum >= signalmax) 632 if (signum < 0 || signum >= signalmax)
617static void 641static void
618sigcb (EV_P_ struct ev_io *iow, int revents) 642sigcb (EV_P_ struct ev_io *iow, int revents)
619{ 643{
620 int signum; 644 int signum;
621 645
622#ifdef WIN32
623 recv (sigpipe [0], &revents, 1, MSG_DONTWAIT);
624#else
625 read (sigpipe [0], &revents, 1); 646 read (sigpipe [0], &revents, 1);
626#endif
627 gotsig = 0; 647 gotsig = 0;
628 648
629 for (signum = signalmax; signum--; ) 649 for (signum = signalmax; signum--; )
630 if (signals [signum].gotsig) 650 if (signals [signum].gotsig)
631 ev_feed_signal_event (EV_A_ signum + 1); 651 ev_feed_signal_event (EV_A_ signum + 1);
632} 652}
633 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
634static void 666static void
635siginit (EV_P) 667siginit (EV_P)
636{ 668{
637#ifndef WIN32 669 fd_intern (sigpipe [0]);
638 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); 670 fd_intern (sigpipe [1]);
639 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC);
640
641 /* rather than sort out wether we really need nb, set it */
642 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
643 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
644#endif
645 671
646 ev_io_set (&sigev, sigpipe [0], EV_READ); 672 ev_io_set (&sigev, sigpipe [0], EV_READ);
647 ev_io_start (EV_A_ &sigev); 673 ev_io_start (EV_A_ &sigev);
648 ev_unref (EV_A); /* child watcher should not keep loop alive */ 674 ev_unref (EV_A); /* child watcher should not keep loop alive */
649} 675}
650 676
651/*****************************************************************************/ 677/*****************************************************************************/
652 678
653static struct ev_child *childs [PID_HASHSIZE]; 679static struct ev_child *childs [PID_HASHSIZE];
654 680
655#ifndef WIN32 681#ifndef _WIN32
656 682
657static struct ev_signal childev; 683static struct ev_signal childev;
658 684
659#ifndef WCONTINUED 685#ifndef WCONTINUED
660# define WCONTINUED 0 686# define WCONTINUED 0
692 718
693#endif 719#endif
694 720
695/*****************************************************************************/ 721/*****************************************************************************/
696 722
723#if EV_USE_PORT
724# include "ev_port.c"
725#endif
697#if EV_USE_KQUEUE 726#if EV_USE_KQUEUE
698# include "ev_kqueue.c" 727# include "ev_kqueue.c"
699#endif 728#endif
700#if EV_USE_EPOLL 729#if EV_USE_EPOLL
701# include "ev_epoll.c" 730# include "ev_epoll.c"
721 750
722/* 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 */
723static int 752static int
724enable_secure (void) 753enable_secure (void)
725{ 754{
726#ifdef WIN32 755#ifdef _WIN32
727 return 0; 756 return 0;
728#else 757#else
729 return getuid () != geteuid () 758 return getuid () != geteuid ()
730 || getgid () != getegid (); 759 || getgid () != getegid ();
731#endif 760#endif
732} 761}
733 762
734int 763unsigned int
735ev_method (EV_P) 764ev_method (EV_P)
736{ 765{
737 return method; 766 return method;
738} 767}
739 768
740static void 769static void
741loop_init (EV_P_ int methods) 770loop_init (EV_P_ unsigned int flags)
742{ 771{
743 if (!method) 772 if (!method)
744 { 773 {
745#if EV_USE_MONOTONIC 774#if EV_USE_MONOTONIC
746 { 775 {
753 ev_rt_now = ev_time (); 782 ev_rt_now = ev_time ();
754 mn_now = get_clock (); 783 mn_now = get_clock ();
755 now_floor = mn_now; 784 now_floor = mn_now;
756 rtmn_diff = ev_rt_now - mn_now; 785 rtmn_diff = ev_rt_now - mn_now;
757 786
758 if (methods == EVMETHOD_AUTO) 787 if (!(flags & EVFLAG_NOENV) && !enable_secure () && getenv ("LIBEV_FLAGS"))
759 if (!enable_secure () && getenv ("LIBEV_METHODS"))
760 methods = atoi (getenv ("LIBEV_METHODS")); 788 flags = atoi (getenv ("LIBEV_FLAGS"));
761 else 789
762 methods = EVMETHOD_ANY; 790 if (!(flags & 0x0000ffff))
791 flags |= 0x0000ffff;
763 792
764 method = 0; 793 method = 0;
765#if EV_USE_WIN32 794#if EV_USE_PORT
766 if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods); 795 if (!method && (flags & EVMETHOD_PORT )) method = port_init (EV_A_ flags);
767#endif 796#endif
768#if EV_USE_KQUEUE 797#if EV_USE_KQUEUE
769 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); 798 if (!method && (flags & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ flags);
770#endif 799#endif
771#if EV_USE_EPOLL 800#if EV_USE_EPOLL
772 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); 801 if (!method && (flags & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ flags);
773#endif 802#endif
774#if EV_USE_POLL 803#if EV_USE_POLL
775 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); 804 if (!method && (flags & EVMETHOD_POLL )) method = poll_init (EV_A_ flags);
776#endif 805#endif
777#if EV_USE_SELECT 806#if EV_USE_SELECT
778 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 807 if (!method && (flags & EVMETHOD_SELECT)) method = select_init (EV_A_ flags);
779#endif 808#endif
780 809
781 ev_init (&sigev, sigcb); 810 ev_init (&sigev, sigcb);
782 ev_set_priority (&sigev, EV_MAXPRI); 811 ev_set_priority (&sigev, EV_MAXPRI);
783 } 812 }
786void 815void
787loop_destroy (EV_P) 816loop_destroy (EV_P)
788{ 817{
789 int i; 818 int i;
790 819
791#if EV_USE_WIN32 820#if EV_USE_PORT
792 if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A); 821 if (method == EVMETHOD_PORT ) port_destroy (EV_A);
793#endif 822#endif
794#if EV_USE_KQUEUE 823#if EV_USE_KQUEUE
795 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); 824 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
796#endif 825#endif
797#if EV_USE_EPOLL 826#if EV_USE_EPOLL
806 835
807 for (i = NUMPRI; i--; ) 836 for (i = NUMPRI; i--; )
808 array_free (pending, [i]); 837 array_free (pending, [i]);
809 838
810 /* have to use the microsoft-never-gets-it-right macro */ 839 /* have to use the microsoft-never-gets-it-right macro */
811 array_free_microshit (fdchange); 840 array_free (fdchange, EMPTY0);
812 array_free_microshit (timer); 841 array_free (timer, EMPTY0);
813#if EV_PERIODICS 842#if EV_PERIODICS
814 array_free_microshit (periodic); 843 array_free (periodic, EMPTY0);
815#endif 844#endif
816 array_free_microshit (idle); 845 array_free (idle, EMPTY0);
817 array_free_microshit (prepare); 846 array_free (prepare, EMPTY0);
818 array_free_microshit (check); 847 array_free (check, EMPTY0);
819 848
820 method = 0; 849 method = 0;
821} 850}
822 851
823static void 852static void
824loop_fork (EV_P) 853loop_fork (EV_P)
825{ 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
826#if EV_USE_EPOLL 861#if EV_USE_EPOLL
827 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A); 862 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A);
828#endif
829#if EV_USE_KQUEUE
830 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A);
831#endif 863#endif
832 864
833 if (ev_is_active (&sigev)) 865 if (ev_is_active (&sigev))
834 { 866 {
835 /* default loop */ 867 /* default loop */
848 postfork = 0; 880 postfork = 0;
849} 881}
850 882
851#if EV_MULTIPLICITY 883#if EV_MULTIPLICITY
852struct ev_loop * 884struct ev_loop *
853ev_loop_new (int methods) 885ev_loop_new (unsigned int flags)
854{ 886{
855 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));
856 888
857 memset (loop, 0, sizeof (struct ev_loop)); 889 memset (loop, 0, sizeof (struct ev_loop));
858 890
859 loop_init (EV_A_ methods); 891 loop_init (EV_A_ flags);
860 892
861 if (ev_method (EV_A)) 893 if (ev_method (EV_A))
862 return loop; 894 return loop;
863 895
864 return 0; 896 return 0;
879 911
880#endif 912#endif
881 913
882#if EV_MULTIPLICITY 914#if EV_MULTIPLICITY
883struct ev_loop * 915struct ev_loop *
916ev_default_loop_ (unsigned int flags)
884#else 917#else
885int 918int
919ev_default_loop (unsigned int flags)
886#endif 920#endif
887ev_default_loop (int methods)
888{ 921{
889 if (sigpipe [0] == sigpipe [1]) 922 if (sigpipe [0] == sigpipe [1])
890 if (pipe (sigpipe)) 923 if (pipe (sigpipe))
891 return 0; 924 return 0;
892 925
893 if (!default_loop) 926 if (!ev_default_loop_ptr)
894 { 927 {
895#if EV_MULTIPLICITY 928#if EV_MULTIPLICITY
896 struct ev_loop *loop = default_loop = &default_loop_struct; 929 struct ev_loop *loop = ev_default_loop_ptr = &default_loop_struct;
897#else 930#else
898 default_loop = 1; 931 ev_default_loop_ptr = 1;
899#endif 932#endif
900 933
901 loop_init (EV_A_ methods); 934 loop_init (EV_A_ flags);
902 935
903 if (ev_method (EV_A)) 936 if (ev_method (EV_A))
904 { 937 {
905 siginit (EV_A); 938 siginit (EV_A);
906 939
907#ifndef WIN32 940#ifndef _WIN32
908 ev_signal_init (&childev, childcb, SIGCHLD); 941 ev_signal_init (&childev, childcb, SIGCHLD);
909 ev_set_priority (&childev, EV_MAXPRI); 942 ev_set_priority (&childev, EV_MAXPRI);
910 ev_signal_start (EV_A_ &childev); 943 ev_signal_start (EV_A_ &childev);
911 ev_unref (EV_A); /* child watcher should not keep loop alive */ 944 ev_unref (EV_A); /* child watcher should not keep loop alive */
912#endif 945#endif
913 } 946 }
914 else 947 else
915 default_loop = 0; 948 ev_default_loop_ptr = 0;
916 } 949 }
917 950
918 return default_loop; 951 return ev_default_loop_ptr;
919} 952}
920 953
921void 954void
922ev_default_destroy (void) 955ev_default_destroy (void)
923{ 956{
924#if EV_MULTIPLICITY 957#if EV_MULTIPLICITY
925 struct ev_loop *loop = default_loop; 958 struct ev_loop *loop = ev_default_loop_ptr;
926#endif 959#endif
927 960
928#ifndef WIN32 961#ifndef _WIN32
929 ev_ref (EV_A); /* child watcher */ 962 ev_ref (EV_A); /* child watcher */
930 ev_signal_stop (EV_A_ &childev); 963 ev_signal_stop (EV_A_ &childev);
931#endif 964#endif
932 965
933 ev_ref (EV_A); /* signal watcher */ 966 ev_ref (EV_A); /* signal watcher */
941 974
942void 975void
943ev_default_fork (void) 976ev_default_fork (void)
944{ 977{
945#if EV_MULTIPLICITY 978#if EV_MULTIPLICITY
946 struct ev_loop *loop = default_loop; 979 struct ev_loop *loop = ev_default_loop_ptr;
947#endif 980#endif
948 981
949 if (method) 982 if (method)
950 postfork = 1; 983 postfork = 1;
951} 984}
1020 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 1053 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
1021 1054
1022 /* first reschedule or stop timer */ 1055 /* first reschedule or stop timer */
1023 if (w->reschedule_cb) 1056 if (w->reschedule_cb)
1024 { 1057 {
1025 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);
1026
1027 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));
1028 downheap ((WT *)periodics, periodiccnt, 0); 1060 downheap ((WT *)periodics, periodiccnt, 0);
1029 } 1061 }
1030 else if (w->interval) 1062 else if (w->interval)
1031 { 1063 {
1149ev_loop (EV_P_ int flags) 1181ev_loop (EV_P_ int flags)
1150{ 1182{
1151 double block; 1183 double block;
1152 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; 1184 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
1153 1185
1154 do 1186 while (activecnt)
1155 { 1187 {
1156 /* queue check watchers (and execute them) */ 1188 /* queue check watchers (and execute them) */
1157 if (expect_false (preparecnt)) 1189 if (expect_false (preparecnt))
1158 { 1190 {
1159 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 1191 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
1222 /* queue check watchers, to be executed first */ 1254 /* queue check watchers, to be executed first */
1223 if (checkcnt) 1255 if (checkcnt)
1224 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1256 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1225 1257
1226 call_pending (EV_A); 1258 call_pending (EV_A);
1259
1260 if (loop_done)
1261 break;
1227 } 1262 }
1228 while (activecnt && !loop_done);
1229 1263
1230 if (loop_done != 2) 1264 if (loop_done != 2)
1231 loop_done = 0; 1265 loop_done = 0;
1232} 1266}
1233 1267
1331 ((WT)w)->at += mn_now; 1365 ((WT)w)->at += mn_now;
1332 1366
1333 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.));
1334 1368
1335 ev_start (EV_A_ (W)w, ++timercnt); 1369 ev_start (EV_A_ (W)w, ++timercnt);
1336 array_needsize (struct ev_timer *, timers, timermax, timercnt, (void)); 1370 array_needsize (struct ev_timer *, timers, timermax, timercnt, EMPTY2);
1337 timers [timercnt - 1] = w; 1371 timers [timercnt - 1] = w;
1338 upheap ((WT *)timers, timercnt - 1); 1372 upheap ((WT *)timers, timercnt - 1);
1339 1373
1340 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1374 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1341} 1375}
1350 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1384 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1351 1385
1352 if (((W)w)->active < timercnt--) 1386 if (((W)w)->active < timercnt--)
1353 { 1387 {
1354 timers [((W)w)->active - 1] = timers [timercnt]; 1388 timers [((W)w)->active - 1] = timers [timercnt];
1355 downheap ((WT *)timers, timercnt, ((W)w)->active - 1); 1389 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1356 } 1390 }
1357 1391
1358 ((WT)w)->at -= mn_now; 1392 ((WT)w)->at -= mn_now;
1359 1393
1360 ev_stop (EV_A_ (W)w); 1394 ev_stop (EV_A_ (W)w);
1364ev_timer_again (EV_P_ struct ev_timer *w) 1398ev_timer_again (EV_P_ struct ev_timer *w)
1365{ 1399{
1366 if (ev_is_active (w)) 1400 if (ev_is_active (w))
1367 { 1401 {
1368 if (w->repeat) 1402 if (w->repeat)
1403 {
1404 ((WT)w)->at = mn_now + w->repeat;
1369 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1, mn_now + w->repeat); 1405 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1406 }
1370 else 1407 else
1371 ev_timer_stop (EV_A_ w); 1408 ev_timer_stop (EV_A_ w);
1372 } 1409 }
1373 else if (w->repeat) 1410 else if (w->repeat)
1411 {
1412 w->at = w->repeat;
1374 ev_timer_start (EV_A_ w); 1413 ev_timer_start (EV_A_ w);
1414 }
1375} 1415}
1376 1416
1377#if EV_PERIODICS 1417#if EV_PERIODICS
1378void 1418void
1379ev_periodic_start (EV_P_ struct ev_periodic *w) 1419ev_periodic_start (EV_P_ struct ev_periodic *w)
1389 /* 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 */
1390 ((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;
1391 } 1431 }
1392 1432
1393 ev_start (EV_A_ (W)w, ++periodiccnt); 1433 ev_start (EV_A_ (W)w, ++periodiccnt);
1394 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); 1434 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2);
1395 periodics [periodiccnt - 1] = w; 1435 periodics [periodiccnt - 1] = w;
1396 upheap ((WT *)periodics, periodiccnt - 1); 1436 upheap ((WT *)periodics, periodiccnt - 1);
1397 1437
1398 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1438 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1399} 1439}
1408 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1448 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1409 1449
1410 if (((W)w)->active < periodiccnt--) 1450 if (((W)w)->active < periodiccnt--)
1411 { 1451 {
1412 periodics [((W)w)->active - 1] = periodics [periodiccnt]; 1452 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1413 downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); 1453 adjustheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1414 } 1454 }
1415 1455
1416 ev_stop (EV_A_ (W)w); 1456 ev_stop (EV_A_ (W)w);
1417} 1457}
1418 1458
1430{ 1470{
1431 if (ev_is_active (w)) 1471 if (ev_is_active (w))
1432 return; 1472 return;
1433 1473
1434 ev_start (EV_A_ (W)w, ++idlecnt); 1474 ev_start (EV_A_ (W)w, ++idlecnt);
1435 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, (void)); 1475 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, EMPTY2);
1436 idles [idlecnt - 1] = w; 1476 idles [idlecnt - 1] = w;
1437} 1477}
1438 1478
1439void 1479void
1440ev_idle_stop (EV_P_ struct ev_idle *w) 1480ev_idle_stop (EV_P_ struct ev_idle *w)
1441{ 1481{
1442 ev_clear_pending (EV_A_ (W)w); 1482 ev_clear_pending (EV_A_ (W)w);
1443 if (ev_is_active (w)) 1483 if (!ev_is_active (w))
1444 return; 1484 return;
1445 1485
1446 idles [((W)w)->active - 1] = idles [--idlecnt]; 1486 idles [((W)w)->active - 1] = idles [--idlecnt];
1447 ev_stop (EV_A_ (W)w); 1487 ev_stop (EV_A_ (W)w);
1448} 1488}
1452{ 1492{
1453 if (ev_is_active (w)) 1493 if (ev_is_active (w))
1454 return; 1494 return;
1455 1495
1456 ev_start (EV_A_ (W)w, ++preparecnt); 1496 ev_start (EV_A_ (W)w, ++preparecnt);
1457 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, (void)); 1497 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, EMPTY2);
1458 prepares [preparecnt - 1] = w; 1498 prepares [preparecnt - 1] = w;
1459} 1499}
1460 1500
1461void 1501void
1462ev_prepare_stop (EV_P_ struct ev_prepare *w) 1502ev_prepare_stop (EV_P_ struct ev_prepare *w)
1463{ 1503{
1464 ev_clear_pending (EV_A_ (W)w); 1504 ev_clear_pending (EV_A_ (W)w);
1465 if (ev_is_active (w)) 1505 if (!ev_is_active (w))
1466 return; 1506 return;
1467 1507
1468 prepares [((W)w)->active - 1] = prepares [--preparecnt]; 1508 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1469 ev_stop (EV_A_ (W)w); 1509 ev_stop (EV_A_ (W)w);
1470} 1510}
1474{ 1514{
1475 if (ev_is_active (w)) 1515 if (ev_is_active (w))
1476 return; 1516 return;
1477 1517
1478 ev_start (EV_A_ (W)w, ++checkcnt); 1518 ev_start (EV_A_ (W)w, ++checkcnt);
1479 array_needsize (struct ev_check *, checks, checkmax, checkcnt, (void)); 1519 array_needsize (struct ev_check *, checks, checkmax, checkcnt, EMPTY2);
1480 checks [checkcnt - 1] = w; 1520 checks [checkcnt - 1] = w;
1481} 1521}
1482 1522
1483void 1523void
1484ev_check_stop (EV_P_ struct ev_check *w) 1524ev_check_stop (EV_P_ struct ev_check *w)
1497 1537
1498void 1538void
1499ev_signal_start (EV_P_ struct ev_signal *w) 1539ev_signal_start (EV_P_ struct ev_signal *w)
1500{ 1540{
1501#if EV_MULTIPLICITY 1541#if EV_MULTIPLICITY
1502 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));
1503#endif 1543#endif
1504 if (ev_is_active (w)) 1544 if (ev_is_active (w))
1505 return; 1545 return;
1506 1546
1507 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));
1510 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init); 1550 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init);
1511 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1551 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1512 1552
1513 if (!((WL)w)->next) 1553 if (!((WL)w)->next)
1514 { 1554 {
1515#if WIN32 1555#if _WIN32
1516 signal (w->signum, sighandler); 1556 signal (w->signum, sighandler);
1517#else 1557#else
1518 struct sigaction sa; 1558 struct sigaction sa;
1519 sa.sa_handler = sighandler; 1559 sa.sa_handler = sighandler;
1520 sigfillset (&sa.sa_mask); 1560 sigfillset (&sa.sa_mask);
1540 1580
1541void 1581void
1542ev_child_start (EV_P_ struct ev_child *w) 1582ev_child_start (EV_P_ struct ev_child *w)
1543{ 1583{
1544#if EV_MULTIPLICITY 1584#if EV_MULTIPLICITY
1545 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));
1546#endif 1586#endif
1547 if (ev_is_active (w)) 1587 if (ev_is_active (w))
1548 return; 1588 return;
1549 1589
1550 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