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

Comparing libev/ev.c (file contents):
Revision 1.96 by root, Sun Nov 11 01:50:36 2007 UTC vs.
Revision 1.115 by root, Wed Nov 14 04:53:21 2007 UTC

35 35
36#ifndef EV_STANDALONE 36#ifndef EV_STANDALONE
37# include "config.h" 37# include "config.h"
38 38
39# if HAVE_CLOCK_GETTIME 39# if HAVE_CLOCK_GETTIME
40# ifndef EV_USE_MONOTONIC
40# define EV_USE_MONOTONIC 1 41# define EV_USE_MONOTONIC 1
42# endif
43# ifndef EV_USE_REALTIME
41# define EV_USE_REALTIME 1 44# define EV_USE_REALTIME 1
45# endif
42# endif 46# endif
43 47
44# if HAVE_SELECT && HAVE_SYS_SELECT_H && !defined (EV_USE_SELECT) 48# if HAVE_SELECT && HAVE_SYS_SELECT_H && !defined (EV_USE_SELECT)
45# define EV_USE_SELECT 1 49# define EV_USE_SELECT 1
46# endif 50# endif
47 51
48# if HAVE_POLL && HAVE_POLL_H && !defined (EV_USE_POLL) 52# if HAVE_POLL && HAVE_POLL_H && !defined (EV_USE_POLL)
49# define EV_USE_POLL 1 53# define EV_USE_POLL 1
50# endif 54# endif
51 55
52# 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)
53# define EV_USE_EPOLL 1 57# define EV_USE_EPOLL 1
54# endif 58# endif
55 59
56# 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)
57# define EV_USE_KQUEUE 1 61# define EV_USE_KQUEUE 1
71#include <sys/types.h> 75#include <sys/types.h>
72#include <time.h> 76#include <time.h>
73 77
74#include <signal.h> 78#include <signal.h>
75 79
76#ifndef WIN32 80#ifndef _WIN32
77# include <unistd.h> 81# include <unistd.h>
78# include <sys/time.h> 82# include <sys/time.h>
79# 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
80#endif 89# endif
90#endif
91
81/**/ 92/**/
82 93
83#ifndef EV_USE_MONOTONIC 94#ifndef EV_USE_MONOTONIC
84# define EV_USE_MONOTONIC 1 95# define EV_USE_MONOTONIC 1
85#endif 96#endif
86 97
87#ifndef EV_USE_SELECT 98#ifndef EV_USE_SELECT
88# define EV_USE_SELECT 1 99# define EV_USE_SELECT 1
100# define EV_SELECT_USE_FD_SET 1
89#endif 101#endif
90 102
91#ifndef EV_USE_POLL 103#ifndef EV_USE_POLL
92# 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
93#endif 109#endif
94 110
95#ifndef EV_USE_EPOLL 111#ifndef EV_USE_EPOLL
96# define EV_USE_EPOLL 0 112# define EV_USE_EPOLL 0
97#endif 113#endif
98 114
99#ifndef EV_USE_KQUEUE 115#ifndef EV_USE_KQUEUE
100# define EV_USE_KQUEUE 0 116# define EV_USE_KQUEUE 0
101#endif 117#endif
102 118
103#ifndef EV_USE_WIN32
104# ifdef WIN32
105# define EV_USE_WIN32 0 /* it does not exist, use select */
106# undef EV_USE_SELECT
107# define EV_USE_SELECT 1
108# else
109# define EV_USE_WIN32 0
110# endif
111#endif
112
113#ifndef EV_USE_REALTIME 119#ifndef EV_USE_REALTIME
114# define EV_USE_REALTIME 1 120# define EV_USE_REALTIME 1
115#endif 121#endif
116 122
117/**/ 123/**/
124
125/* darwin simply cannot be helped */
126#ifdef __APPLE__
127# undef EV_USE_POLL
128# undef EV_USE_KQUEUE
129#endif
118 130
119#ifndef CLOCK_MONOTONIC 131#ifndef CLOCK_MONOTONIC
120# undef EV_USE_MONOTONIC 132# undef EV_USE_MONOTONIC
121# define EV_USE_MONOTONIC 0 133# define EV_USE_MONOTONIC 0
122#endif 134#endif
123 135
124#ifndef CLOCK_REALTIME 136#ifndef CLOCK_REALTIME
125# undef EV_USE_REALTIME 137# undef EV_USE_REALTIME
126# define EV_USE_REALTIME 0 138# define EV_USE_REALTIME 0
139#endif
140
141#if EV_SELECT_IS_WINSOCKET
142# include <winsock.h>
127#endif 143#endif
128 144
129/**/ 145/**/
130 146
131#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) */
151#define expect_true(expr) expect ((expr) != 0, 1) 167#define expect_true(expr) expect ((expr) != 0, 1)
152 168
153#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) 169#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
154#define ABSPRI(w) ((w)->priority - EV_MINPRI) 170#define ABSPRI(w) ((w)->priority - EV_MINPRI)
155 171
172#define EMPTY0 /* required for microsofts broken pseudo-c compiler */
173#define EMPTY2(a,b) /* used to suppress some warnings */
174
156typedef struct ev_watcher *W; 175typedef struct ev_watcher *W;
157typedef struct ev_watcher_list *WL; 176typedef struct ev_watcher_list *WL;
158typedef struct ev_watcher_time *WT; 177typedef struct ev_watcher_time *WT;
159 178
160static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 179static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
161 180
181#ifdef _WIN32
162#include "ev_win32.c" 182# include "ev_win32.c"
183#endif
163 184
164/*****************************************************************************/ 185/*****************************************************************************/
165 186
166static void (*syserr_cb)(const char *msg); 187static void (*syserr_cb)(const char *msg);
167 188
214typedef struct 235typedef struct
215{ 236{
216 WL head; 237 WL head;
217 unsigned char events; 238 unsigned char events;
218 unsigned char reify; 239 unsigned char reify;
240#if EV_SELECT_IS_WINSOCKET
241 SOCKET handle;
242#endif
219} ANFD; 243} ANFD;
220 244
221typedef struct 245typedef struct
222{ 246{
223 W w; 247 W w;
227#if EV_MULTIPLICITY 251#if EV_MULTIPLICITY
228 252
229 struct ev_loop 253 struct ev_loop
230 { 254 {
231 ev_tstamp ev_rt_now; 255 ev_tstamp ev_rt_now;
256 #define ev_rt_now ((loop)->ev_rt_now)
232 #define VAR(name,decl) decl; 257 #define VAR(name,decl) decl;
233 #include "ev_vars.h" 258 #include "ev_vars.h"
234 #undef VAR 259 #undef VAR
235 }; 260 };
236 #include "ev_wrap.h" 261 #include "ev_wrap.h"
286{ 311{
287 return ev_rt_now; 312 return ev_rt_now;
288} 313}
289#endif 314#endif
290 315
291#define array_roundsize(type,n) ((n) | 4 & ~3) 316#define array_roundsize(type,n) (((n) | 4) & ~3)
292 317
293#define array_needsize(type,base,cur,cnt,init) \ 318#define array_needsize(type,base,cur,cnt,init) \
294 if (expect_false ((cnt) > cur)) \ 319 if (expect_false ((cnt) > cur)) \
295 { \ 320 { \
296 int newcnt = cur; \ 321 int newcnt = cur; \
311 stem ## max = array_roundsize (stem ## cnt >> 1); \ 336 stem ## max = array_roundsize (stem ## cnt >> 1); \
312 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ 337 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\
313 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ 338 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
314 } 339 }
315 340
316/* microsoft's pseudo-c is quite far from C as the rest of the world and the standard knows it */
317/* bringing us everlasting joy in form of stupid extra macros that are not required in C */
318#define array_free_microshit(stem) \
319 ev_free (stem ## s); stem ## cnt = stem ## max = 0;
320
321#define array_free(stem, idx) \ 341#define array_free(stem, idx) \
322 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; 342 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
323 343
324/*****************************************************************************/ 344/*****************************************************************************/
325 345
346 pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; 366 pendings [ABSPRI (w_)][w_->pending - 1].events |= revents;
347 return; 367 return;
348 } 368 }
349 369
350 w_->pending = ++pendingcnt [ABSPRI (w_)]; 370 w_->pending = ++pendingcnt [ABSPRI (w_)];
351 array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], (void)); 371 array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], EMPTY2);
352 pendings [ABSPRI (w_)][w_->pending - 1].w = w_; 372 pendings [ABSPRI (w_)][w_->pending - 1].w = w_;
353 pendings [ABSPRI (w_)][w_->pending - 1].events = revents; 373 pendings [ABSPRI (w_)][w_->pending - 1].events = revents;
354} 374}
355 375
356static void 376static void
399 int events = 0; 419 int events = 0;
400 420
401 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) 421 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
402 events |= w->events; 422 events |= w->events;
403 423
424#if EV_SELECT_IS_WINSOCKET
425 if (events)
426 {
427 unsigned long argp;
428 anfd->handle = _get_osfhandle (fd);
429 assert (("libev only supports socket fds in this configuration", ioctlsocket (anfd->handle, FIONREAD, &argp) == 0));
430 }
431#endif
432
404 anfd->reify = 0; 433 anfd->reify = 0;
405 434
406 method_modify (EV_A_ fd, anfd->events, events); 435 method_modify (EV_A_ fd, anfd->events, events);
407 anfd->events = events; 436 anfd->events = events;
408 } 437 }
417 return; 446 return;
418 447
419 anfds [fd].reify = 1; 448 anfds [fd].reify = 1;
420 449
421 ++fdchangecnt; 450 ++fdchangecnt;
422 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, (void)); 451 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2);
423 fdchanges [fdchangecnt - 1] = fd; 452 fdchanges [fdchangecnt - 1] = fd;
424} 453}
425 454
426static void 455static void
427fd_kill (EV_P_ int fd) 456fd_kill (EV_P_ int fd)
436} 465}
437 466
438static int 467static int
439fd_valid (int fd) 468fd_valid (int fd)
440{ 469{
441#ifdef WIN32 470#ifdef _WIN32
442 return !!win32_get_osfhandle (fd); 471 return _get_osfhandle (fd) != -1;
443#else 472#else
444 return fcntl (fd, F_GETFD) != -1; 473 return fcntl (fd, F_GETFD) != -1;
445#endif 474#endif
446} 475}
447 476
528 heap [k] = w; 557 heap [k] = w;
529 ((W)heap [k])->active = k + 1; 558 ((W)heap [k])->active = k + 1;
530} 559}
531 560
532inline void 561inline void
533adjustheap (WT *heap, int N, int k, ev_tstamp at) 562adjustheap (WT *heap, int N, int k)
534{ 563{
535 ev_tstamp old_at = heap [k]->at; 564 upheap (heap, k);
536 heap [k]->at = at;
537
538 if (old_at < at)
539 downheap (heap, N, k); 565 downheap (heap, N, k);
540 else
541 upheap (heap, k);
542} 566}
543 567
544/*****************************************************************************/ 568/*****************************************************************************/
545 569
546typedef struct 570typedef struct
569} 593}
570 594
571static void 595static void
572sighandler (int signum) 596sighandler (int signum)
573{ 597{
574#if WIN32 598#if _WIN32
575 signal (signum, sighandler); 599 signal (signum, sighandler);
576#endif 600#endif
577 601
578 signals [signum - 1].gotsig = 1; 602 signals [signum - 1].gotsig = 1;
579 603
580 if (!gotsig) 604 if (!gotsig)
581 { 605 {
582 int old_errno = errno; 606 int old_errno = errno;
583 gotsig = 1; 607 gotsig = 1;
584#ifdef WIN32
585 send (sigpipe [1], &signum, 1, MSG_DONTWAIT);
586#else
587 write (sigpipe [1], &signum, 1); 608 write (sigpipe [1], &signum, 1);
588#endif
589 errno = old_errno; 609 errno = old_errno;
590 } 610 }
591} 611}
592 612
593void 613void
613static void 633static void
614sigcb (EV_P_ struct ev_io *iow, int revents) 634sigcb (EV_P_ struct ev_io *iow, int revents)
615{ 635{
616 int signum; 636 int signum;
617 637
618#ifdef WIN32
619 recv (sigpipe [0], &revents, 1, MSG_DONTWAIT);
620#else
621 read (sigpipe [0], &revents, 1); 638 read (sigpipe [0], &revents, 1);
622#endif
623 gotsig = 0; 639 gotsig = 0;
624 640
625 for (signum = signalmax; signum--; ) 641 for (signum = signalmax; signum--; )
626 if (signals [signum].gotsig) 642 if (signals [signum].gotsig)
627 ev_feed_signal_event (EV_A_ signum + 1); 643 ev_feed_signal_event (EV_A_ signum + 1);
628} 644}
629 645
646inline void
647fd_intern (int fd)
648{
649#ifdef _WIN32
650 int arg = 1;
651 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg);
652#else
653 fcntl (fd, F_SETFD, FD_CLOEXEC);
654 fcntl (fd, F_SETFL, O_NONBLOCK);
655#endif
656}
657
630static void 658static void
631siginit (EV_P) 659siginit (EV_P)
632{ 660{
633#ifndef WIN32 661 fd_intern (sigpipe [0]);
634 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); 662 fd_intern (sigpipe [1]);
635 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC);
636
637 /* rather than sort out wether we really need nb, set it */
638 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
639 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
640#endif
641 663
642 ev_io_set (&sigev, sigpipe [0], EV_READ); 664 ev_io_set (&sigev, sigpipe [0], EV_READ);
643 ev_io_start (EV_A_ &sigev); 665 ev_io_start (EV_A_ &sigev);
644 ev_unref (EV_A); /* child watcher should not keep loop alive */ 666 ev_unref (EV_A); /* child watcher should not keep loop alive */
645} 667}
646 668
647/*****************************************************************************/ 669/*****************************************************************************/
648 670
649static struct ev_child *childs [PID_HASHSIZE]; 671static struct ev_child *childs [PID_HASHSIZE];
650 672
651#ifndef WIN32 673#ifndef _WIN32
652 674
653static struct ev_signal childev; 675static struct ev_signal childev;
654 676
655#ifndef WCONTINUED 677#ifndef WCONTINUED
656# define WCONTINUED 0 678# define WCONTINUED 0
717 739
718/* return true if we are running with elevated privileges and should ignore env variables */ 740/* return true if we are running with elevated privileges and should ignore env variables */
719static int 741static int
720enable_secure (void) 742enable_secure (void)
721{ 743{
722#ifdef WIN32 744#ifdef _WIN32
723 return 0; 745 return 0;
724#else 746#else
725 return getuid () != geteuid () 747 return getuid () != geteuid ()
726 || getgid () != getegid (); 748 || getgid () != getegid ();
727#endif 749#endif
728} 750}
729 751
730int 752unsigned int
731ev_method (EV_P) 753ev_method (EV_P)
732{ 754{
733 return method; 755 return method;
734} 756}
735 757
736static void 758static void
737loop_init (EV_P_ int methods) 759loop_init (EV_P_ unsigned int flags)
738{ 760{
739 if (!method) 761 if (!method)
740 { 762 {
741#if EV_USE_MONOTONIC 763#if EV_USE_MONOTONIC
742 { 764 {
749 ev_rt_now = ev_time (); 771 ev_rt_now = ev_time ();
750 mn_now = get_clock (); 772 mn_now = get_clock ();
751 now_floor = mn_now; 773 now_floor = mn_now;
752 rtmn_diff = ev_rt_now - mn_now; 774 rtmn_diff = ev_rt_now - mn_now;
753 775
754 if (methods == EVMETHOD_AUTO) 776 if (!(flags & EVFLAG_NOENV) && !enable_secure () && getenv ("LIBEV_FLAGS"))
755 if (!enable_secure () && getenv ("LIBEV_METHODS"))
756 methods = atoi (getenv ("LIBEV_METHODS")); 777 flags = atoi (getenv ("LIBEV_FLAGS"));
757 else 778
758 methods = EVMETHOD_ANY; 779 if (!(flags & 0x0000ffff))
780 flags |= 0x0000ffff;
759 781
760 method = 0; 782 method = 0;
761#if EV_USE_WIN32
762 if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods);
763#endif
764#if EV_USE_KQUEUE 783#if EV_USE_KQUEUE
765 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); 784 if (!method && (flags & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ flags);
766#endif 785#endif
767#if EV_USE_EPOLL 786#if EV_USE_EPOLL
768 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); 787 if (!method && (flags & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ flags);
769#endif 788#endif
770#if EV_USE_POLL 789#if EV_USE_POLL
771 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); 790 if (!method && (flags & EVMETHOD_POLL )) method = poll_init (EV_A_ flags);
772#endif 791#endif
773#if EV_USE_SELECT 792#if EV_USE_SELECT
774 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 793 if (!method && (flags & EVMETHOD_SELECT)) method = select_init (EV_A_ flags);
775#endif 794#endif
776 795
777 ev_init (&sigev, sigcb); 796 ev_init (&sigev, sigcb);
778 ev_set_priority (&sigev, EV_MAXPRI); 797 ev_set_priority (&sigev, EV_MAXPRI);
779 } 798 }
782void 801void
783loop_destroy (EV_P) 802loop_destroy (EV_P)
784{ 803{
785 int i; 804 int i;
786 805
787#if EV_USE_WIN32
788 if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A);
789#endif
790#if EV_USE_KQUEUE 806#if EV_USE_KQUEUE
791 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); 807 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
792#endif 808#endif
793#if EV_USE_EPOLL 809#if EV_USE_EPOLL
794 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); 810 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A);
802 818
803 for (i = NUMPRI; i--; ) 819 for (i = NUMPRI; i--; )
804 array_free (pending, [i]); 820 array_free (pending, [i]);
805 821
806 /* have to use the microsoft-never-gets-it-right macro */ 822 /* have to use the microsoft-never-gets-it-right macro */
807 array_free_microshit (fdchange); 823 array_free (fdchange, EMPTY0);
808 array_free_microshit (timer); 824 array_free (timer, EMPTY0);
809#if EV_PERIODICS 825#if EV_PERIODICS
810 array_free_microshit (periodic); 826 array_free (periodic, EMPTY0);
811#endif 827#endif
812 array_free_microshit (idle); 828 array_free (idle, EMPTY0);
813 array_free_microshit (prepare); 829 array_free (prepare, EMPTY0);
814 array_free_microshit (check); 830 array_free (check, EMPTY0);
815 831
816 method = 0; 832 method = 0;
817} 833}
818 834
819static void 835static void
844 postfork = 0; 860 postfork = 0;
845} 861}
846 862
847#if EV_MULTIPLICITY 863#if EV_MULTIPLICITY
848struct ev_loop * 864struct ev_loop *
849ev_loop_new (int methods) 865ev_loop_new (unsigned int flags)
850{ 866{
851 struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); 867 struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop));
852 868
853 memset (loop, 0, sizeof (struct ev_loop)); 869 memset (loop, 0, sizeof (struct ev_loop));
854 870
855 loop_init (EV_A_ methods); 871 loop_init (EV_A_ flags);
856 872
857 if (ev_method (EV_A)) 873 if (ev_method (EV_A))
858 return loop; 874 return loop;
859 875
860 return 0; 876 return 0;
878#if EV_MULTIPLICITY 894#if EV_MULTIPLICITY
879struct ev_loop * 895struct ev_loop *
880#else 896#else
881int 897int
882#endif 898#endif
883ev_default_loop (int methods) 899ev_default_loop (unsigned int flags)
884{ 900{
885 if (sigpipe [0] == sigpipe [1]) 901 if (sigpipe [0] == sigpipe [1])
886 if (pipe (sigpipe)) 902 if (pipe (sigpipe))
887 return 0; 903 return 0;
888 904
892 struct ev_loop *loop = default_loop = &default_loop_struct; 908 struct ev_loop *loop = default_loop = &default_loop_struct;
893#else 909#else
894 default_loop = 1; 910 default_loop = 1;
895#endif 911#endif
896 912
897 loop_init (EV_A_ methods); 913 loop_init (EV_A_ flags);
898 914
899 if (ev_method (EV_A)) 915 if (ev_method (EV_A))
900 { 916 {
901 siginit (EV_A); 917 siginit (EV_A);
902 918
903#ifndef WIN32 919#ifndef _WIN32
904 ev_signal_init (&childev, childcb, SIGCHLD); 920 ev_signal_init (&childev, childcb, SIGCHLD);
905 ev_set_priority (&childev, EV_MAXPRI); 921 ev_set_priority (&childev, EV_MAXPRI);
906 ev_signal_start (EV_A_ &childev); 922 ev_signal_start (EV_A_ &childev);
907 ev_unref (EV_A); /* child watcher should not keep loop alive */ 923 ev_unref (EV_A); /* child watcher should not keep loop alive */
908#endif 924#endif
919{ 935{
920#if EV_MULTIPLICITY 936#if EV_MULTIPLICITY
921 struct ev_loop *loop = default_loop; 937 struct ev_loop *loop = default_loop;
922#endif 938#endif
923 939
924#ifndef WIN32 940#ifndef _WIN32
925 ev_ref (EV_A); /* child watcher */ 941 ev_ref (EV_A); /* child watcher */
926 ev_signal_stop (EV_A_ &childev); 942 ev_signal_stop (EV_A_ &childev);
927#endif 943#endif
928 944
929 ev_ref (EV_A); /* signal watcher */ 945 ev_ref (EV_A); /* signal watcher */
1016 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 1032 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
1017 1033
1018 /* first reschedule or stop timer */ 1034 /* first reschedule or stop timer */
1019 if (w->reschedule_cb) 1035 if (w->reschedule_cb)
1020 { 1036 {
1021 ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001); 1037 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001);
1022
1023 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now)); 1038 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now));
1024 downheap ((WT *)periodics, periodiccnt, 0); 1039 downheap ((WT *)periodics, periodiccnt, 0);
1025 } 1040 }
1026 else if (w->interval) 1041 else if (w->interval)
1027 { 1042 {
1145ev_loop (EV_P_ int flags) 1160ev_loop (EV_P_ int flags)
1146{ 1161{
1147 double block; 1162 double block;
1148 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; 1163 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
1149 1164
1150 do 1165 while (activecnt)
1151 { 1166 {
1152 /* queue check watchers (and execute them) */ 1167 /* queue check watchers (and execute them) */
1153 if (expect_false (preparecnt)) 1168 if (expect_false (preparecnt))
1154 { 1169 {
1155 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 1170 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
1218 /* queue check watchers, to be executed first */ 1233 /* queue check watchers, to be executed first */
1219 if (checkcnt) 1234 if (checkcnt)
1220 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1235 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1221 1236
1222 call_pending (EV_A); 1237 call_pending (EV_A);
1238
1239 if (loop_done)
1240 break;
1223 } 1241 }
1224 while (activecnt && !loop_done);
1225 1242
1226 if (loop_done != 2) 1243 if (loop_done != 2)
1227 loop_done = 0; 1244 loop_done = 0;
1228} 1245}
1229 1246
1327 ((WT)w)->at += mn_now; 1344 ((WT)w)->at += mn_now;
1328 1345
1329 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1346 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1330 1347
1331 ev_start (EV_A_ (W)w, ++timercnt); 1348 ev_start (EV_A_ (W)w, ++timercnt);
1332 array_needsize (struct ev_timer *, timers, timermax, timercnt, (void)); 1349 array_needsize (struct ev_timer *, timers, timermax, timercnt, EMPTY2);
1333 timers [timercnt - 1] = w; 1350 timers [timercnt - 1] = w;
1334 upheap ((WT *)timers, timercnt - 1); 1351 upheap ((WT *)timers, timercnt - 1);
1335 1352
1336 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1353 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1337} 1354}
1346 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1363 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1347 1364
1348 if (((W)w)->active < timercnt--) 1365 if (((W)w)->active < timercnt--)
1349 { 1366 {
1350 timers [((W)w)->active - 1] = timers [timercnt]; 1367 timers [((W)w)->active - 1] = timers [timercnt];
1351 downheap ((WT *)timers, timercnt, ((W)w)->active - 1); 1368 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1352 } 1369 }
1353 1370
1354 ((WT)w)->at -= mn_now; 1371 ((WT)w)->at -= mn_now;
1355 1372
1356 ev_stop (EV_A_ (W)w); 1373 ev_stop (EV_A_ (W)w);
1360ev_timer_again (EV_P_ struct ev_timer *w) 1377ev_timer_again (EV_P_ struct ev_timer *w)
1361{ 1378{
1362 if (ev_is_active (w)) 1379 if (ev_is_active (w))
1363 { 1380 {
1364 if (w->repeat) 1381 if (w->repeat)
1382 {
1383 ((WT)w)->at = mn_now + w->repeat;
1365 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1, mn_now + w->repeat); 1384 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1385 }
1366 else 1386 else
1367 ev_timer_stop (EV_A_ w); 1387 ev_timer_stop (EV_A_ w);
1368 } 1388 }
1369 else if (w->repeat) 1389 else if (w->repeat)
1390 {
1391 w->at = w->repeat;
1370 ev_timer_start (EV_A_ w); 1392 ev_timer_start (EV_A_ w);
1393 }
1371} 1394}
1372 1395
1373#if EV_PERIODICS 1396#if EV_PERIODICS
1374void 1397void
1375ev_periodic_start (EV_P_ struct ev_periodic *w) 1398ev_periodic_start (EV_P_ struct ev_periodic *w)
1385 /* this formula differs from the one in periodic_reify because we do not always round up */ 1408 /* this formula differs from the one in periodic_reify because we do not always round up */
1386 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; 1409 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1387 } 1410 }
1388 1411
1389 ev_start (EV_A_ (W)w, ++periodiccnt); 1412 ev_start (EV_A_ (W)w, ++periodiccnt);
1390 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); 1413 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2);
1391 periodics [periodiccnt - 1] = w; 1414 periodics [periodiccnt - 1] = w;
1392 upheap ((WT *)periodics, periodiccnt - 1); 1415 upheap ((WT *)periodics, periodiccnt - 1);
1393 1416
1394 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1417 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1395} 1418}
1404 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1427 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1405 1428
1406 if (((W)w)->active < periodiccnt--) 1429 if (((W)w)->active < periodiccnt--)
1407 { 1430 {
1408 periodics [((W)w)->active - 1] = periodics [periodiccnt]; 1431 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1409 downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); 1432 adjustheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1410 } 1433 }
1411 1434
1412 ev_stop (EV_A_ (W)w); 1435 ev_stop (EV_A_ (W)w);
1413} 1436}
1414 1437
1426{ 1449{
1427 if (ev_is_active (w)) 1450 if (ev_is_active (w))
1428 return; 1451 return;
1429 1452
1430 ev_start (EV_A_ (W)w, ++idlecnt); 1453 ev_start (EV_A_ (W)w, ++idlecnt);
1431 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, (void)); 1454 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, EMPTY2);
1432 idles [idlecnt - 1] = w; 1455 idles [idlecnt - 1] = w;
1433} 1456}
1434 1457
1435void 1458void
1436ev_idle_stop (EV_P_ struct ev_idle *w) 1459ev_idle_stop (EV_P_ struct ev_idle *w)
1437{ 1460{
1438 ev_clear_pending (EV_A_ (W)w); 1461 ev_clear_pending (EV_A_ (W)w);
1439 if (ev_is_active (w)) 1462 if (!ev_is_active (w))
1440 return; 1463 return;
1441 1464
1442 idles [((W)w)->active - 1] = idles [--idlecnt]; 1465 idles [((W)w)->active - 1] = idles [--idlecnt];
1443 ev_stop (EV_A_ (W)w); 1466 ev_stop (EV_A_ (W)w);
1444} 1467}
1448{ 1471{
1449 if (ev_is_active (w)) 1472 if (ev_is_active (w))
1450 return; 1473 return;
1451 1474
1452 ev_start (EV_A_ (W)w, ++preparecnt); 1475 ev_start (EV_A_ (W)w, ++preparecnt);
1453 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, (void)); 1476 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, EMPTY2);
1454 prepares [preparecnt - 1] = w; 1477 prepares [preparecnt - 1] = w;
1455} 1478}
1456 1479
1457void 1480void
1458ev_prepare_stop (EV_P_ struct ev_prepare *w) 1481ev_prepare_stop (EV_P_ struct ev_prepare *w)
1459{ 1482{
1460 ev_clear_pending (EV_A_ (W)w); 1483 ev_clear_pending (EV_A_ (W)w);
1461 if (ev_is_active (w)) 1484 if (!ev_is_active (w))
1462 return; 1485 return;
1463 1486
1464 prepares [((W)w)->active - 1] = prepares [--preparecnt]; 1487 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1465 ev_stop (EV_A_ (W)w); 1488 ev_stop (EV_A_ (W)w);
1466} 1489}
1470{ 1493{
1471 if (ev_is_active (w)) 1494 if (ev_is_active (w))
1472 return; 1495 return;
1473 1496
1474 ev_start (EV_A_ (W)w, ++checkcnt); 1497 ev_start (EV_A_ (W)w, ++checkcnt);
1475 array_needsize (struct ev_check *, checks, checkmax, checkcnt, (void)); 1498 array_needsize (struct ev_check *, checks, checkmax, checkcnt, EMPTY2);
1476 checks [checkcnt - 1] = w; 1499 checks [checkcnt - 1] = w;
1477} 1500}
1478 1501
1479void 1502void
1480ev_check_stop (EV_P_ struct ev_check *w) 1503ev_check_stop (EV_P_ struct ev_check *w)
1506 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init); 1529 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init);
1507 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1530 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1508 1531
1509 if (!((WL)w)->next) 1532 if (!((WL)w)->next)
1510 { 1533 {
1511#if WIN32 1534#if _WIN32
1512 signal (w->signum, sighandler); 1535 signal (w->signum, sighandler);
1513#else 1536#else
1514 struct sigaction sa; 1537 struct sigaction sa;
1515 sa.sa_handler = sighandler; 1538 sa.sa_handler = sighandler;
1516 sigfillset (&sa.sa_mask); 1539 sigfillset (&sa.sa_mask);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines