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.117 by ayin, Thu Nov 15 17:15:56 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
75#include <sys/types.h> 75#include <sys/types.h>
76#include <time.h> 76#include <time.h>
77 77
78#include <signal.h> 78#include <signal.h>
79 79
80#ifndef WIN32 80#ifndef _WIN32
81# include <unistd.h> 81# include <unistd.h>
82# include <sys/time.h> 82# include <sys/time.h>
83# 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
84#endif 89# endif
90#endif
91
85/**/ 92/**/
86 93
87#ifndef EV_USE_MONOTONIC 94#ifndef EV_USE_MONOTONIC
88# define EV_USE_MONOTONIC 1 95# define EV_USE_MONOTONIC 1
89#endif 96#endif
90 97
91#ifndef EV_USE_SELECT 98#ifndef EV_USE_SELECT
92# define EV_USE_SELECT 1 99# define EV_USE_SELECT 1
100# define EV_SELECT_USE_FD_SET 1
93#endif 101#endif
94 102
95#ifndef EV_USE_POLL 103#ifndef EV_USE_POLL
96# 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
97#endif 109#endif
98 110
99#ifndef EV_USE_EPOLL 111#ifndef EV_USE_EPOLL
100# define EV_USE_EPOLL 0 112# define EV_USE_EPOLL 0
101#endif 113#endif
102 114
103#ifndef EV_USE_KQUEUE 115#ifndef EV_USE_KQUEUE
104# define EV_USE_KQUEUE 0 116# define EV_USE_KQUEUE 0
105#endif 117#endif
106 118
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 119#ifndef EV_USE_REALTIME
118# define EV_USE_REALTIME 1 120# define EV_USE_REALTIME 1
119#endif 121#endif
120 122
121/**/ 123/**/
124
125/* darwin simply cannot be helped */
126#ifdef __APPLE__
127# undef EV_USE_POLL
128# undef EV_USE_KQUEUE
129#endif
122 130
123#ifndef CLOCK_MONOTONIC 131#ifndef CLOCK_MONOTONIC
124# undef EV_USE_MONOTONIC 132# undef EV_USE_MONOTONIC
125# define EV_USE_MONOTONIC 0 133# define EV_USE_MONOTONIC 0
126#endif 134#endif
127 135
128#ifndef CLOCK_REALTIME 136#ifndef CLOCK_REALTIME
129# undef EV_USE_REALTIME 137# undef EV_USE_REALTIME
130# define EV_USE_REALTIME 0 138# define EV_USE_REALTIME 0
139#endif
140
141#if EV_SELECT_IS_WINSOCKET
142# include <winsock.h>
131#endif 143#endif
132 144
133/**/ 145/**/
134 146
135#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) */
155#define expect_true(expr) expect ((expr) != 0, 1) 167#define expect_true(expr) expect ((expr) != 0, 1)
156 168
157#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) 169#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
158#define ABSPRI(w) ((w)->priority - EV_MINPRI) 170#define ABSPRI(w) ((w)->priority - EV_MINPRI)
159 171
172#define EMPTY0 /* required for microsofts broken pseudo-c compiler */
173#define EMPTY2(a,b) /* used to suppress some warnings */
174
160typedef struct ev_watcher *W; 175typedef struct ev_watcher *W;
161typedef struct ev_watcher_list *WL; 176typedef struct ev_watcher_list *WL;
162typedef struct ev_watcher_time *WT; 177typedef struct ev_watcher_time *WT;
163 178
164static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 179static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
165 180
166#ifdef WIN32 181#ifdef _WIN32
167# include "ev_win32.c" 182# include "ev_win32.c"
168#endif 183#endif
169 184
170/*****************************************************************************/ 185/*****************************************************************************/
171 186
220typedef struct 235typedef struct
221{ 236{
222 WL head; 237 WL head;
223 unsigned char events; 238 unsigned char events;
224 unsigned char reify; 239 unsigned char reify;
240#if EV_SELECT_IS_WINSOCKET
241 SOCKET handle;
242#endif
225} ANFD; 243} ANFD;
226 244
227typedef struct 245typedef struct
228{ 246{
229 W w; 247 W w;
233#if EV_MULTIPLICITY 251#if EV_MULTIPLICITY
234 252
235 struct ev_loop 253 struct ev_loop
236 { 254 {
237 ev_tstamp ev_rt_now; 255 ev_tstamp ev_rt_now;
256 #define ev_rt_now ((loop)->ev_rt_now)
238 #define VAR(name,decl) decl; 257 #define VAR(name,decl) decl;
239 #include "ev_vars.h" 258 #include "ev_vars.h"
240 #undef VAR 259 #undef VAR
241 }; 260 };
242 #include "ev_wrap.h" 261 #include "ev_wrap.h"
243 262
244 struct ev_loop default_loop_struct; 263 static struct ev_loop default_loop_struct;
245 static struct ev_loop *default_loop; 264 struct ev_loop *ev_default_loop_ptr;
246 265
247#else 266#else
248 267
249 ev_tstamp ev_rt_now; 268 ev_tstamp ev_rt_now;
250 #define VAR(name,decl) static decl; 269 #define VAR(name,decl) static decl;
251 #include "ev_vars.h" 270 #include "ev_vars.h"
252 #undef VAR 271 #undef VAR
253 272
254 static int default_loop; 273 static int ev_default_loop_ptr;
255 274
256#endif 275#endif
257 276
258/*****************************************************************************/ 277/*****************************************************************************/
259 278
292{ 311{
293 return ev_rt_now; 312 return ev_rt_now;
294} 313}
295#endif 314#endif
296 315
297#define array_roundsize(type,n) ((n) | 4 & ~3) 316#define array_roundsize(type,n) (((n) | 4) & ~3)
298 317
299#define array_needsize(type,base,cur,cnt,init) \ 318#define array_needsize(type,base,cur,cnt,init) \
300 if (expect_false ((cnt) > cur)) \ 319 if (expect_false ((cnt) > cur)) \
301 { \ 320 { \
302 int newcnt = cur; \ 321 int newcnt = cur; \
317 stem ## max = array_roundsize (stem ## cnt >> 1); \ 336 stem ## max = array_roundsize (stem ## cnt >> 1); \
318 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ 337 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\
319 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ 338 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
320 } 339 }
321 340
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) \ 341#define array_free(stem, idx) \
328 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;
329 343
330/*****************************************************************************/ 344/*****************************************************************************/
331 345
352 pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; 366 pendings [ABSPRI (w_)][w_->pending - 1].events |= revents;
353 return; 367 return;
354 } 368 }
355 369
356 w_->pending = ++pendingcnt [ABSPRI (w_)]; 370 w_->pending = ++pendingcnt [ABSPRI (w_)];
357 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);
358 pendings [ABSPRI (w_)][w_->pending - 1].w = w_; 372 pendings [ABSPRI (w_)][w_->pending - 1].w = w_;
359 pendings [ABSPRI (w_)][w_->pending - 1].events = revents; 373 pendings [ABSPRI (w_)][w_->pending - 1].events = revents;
360} 374}
361 375
362static void 376static void
405 int events = 0; 419 int events = 0;
406 420
407 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)
408 events |= w->events; 422 events |= w->events;
409 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
410 anfd->reify = 0; 433 anfd->reify = 0;
411 434
412 method_modify (EV_A_ fd, anfd->events, events); 435 method_modify (EV_A_ fd, anfd->events, events);
413 anfd->events = events; 436 anfd->events = events;
414 } 437 }
423 return; 446 return;
424 447
425 anfds [fd].reify = 1; 448 anfds [fd].reify = 1;
426 449
427 ++fdchangecnt; 450 ++fdchangecnt;
428 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, (void)); 451 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2);
429 fdchanges [fdchangecnt - 1] = fd; 452 fdchanges [fdchangecnt - 1] = fd;
430} 453}
431 454
432static void 455static void
433fd_kill (EV_P_ int fd) 456fd_kill (EV_P_ int fd)
442} 465}
443 466
444static int 467static int
445fd_valid (int fd) 468fd_valid (int fd)
446{ 469{
447#ifdef WIN32 470#ifdef _WIN32
448 return !!win32_get_osfhandle (fd); 471 return _get_osfhandle (fd) != -1;
449#else 472#else
450 return fcntl (fd, F_GETFD) != -1; 473 return fcntl (fd, F_GETFD) != -1;
451#endif 474#endif
452} 475}
453 476
534 heap [k] = w; 557 heap [k] = w;
535 ((W)heap [k])->active = k + 1; 558 ((W)heap [k])->active = k + 1;
536} 559}
537 560
538inline void 561inline void
539adjustheap (WT *heap, int N, int k, ev_tstamp at) 562adjustheap (WT *heap, int N, int k)
540{ 563{
541 ev_tstamp old_at = heap [k]->at; 564 upheap (heap, k);
542 heap [k]->at = at;
543
544 if (old_at < at)
545 downheap (heap, N, k); 565 downheap (heap, N, k);
546 else
547 upheap (heap, k);
548} 566}
549 567
550/*****************************************************************************/ 568/*****************************************************************************/
551 569
552typedef struct 570typedef struct
575} 593}
576 594
577static void 595static void
578sighandler (int signum) 596sighandler (int signum)
579{ 597{
580#if WIN32 598#if _WIN32
581 signal (signum, sighandler); 599 signal (signum, sighandler);
582#endif 600#endif
583 601
584 signals [signum - 1].gotsig = 1; 602 signals [signum - 1].gotsig = 1;
585 603
586 if (!gotsig) 604 if (!gotsig)
587 { 605 {
588 int old_errno = errno; 606 int old_errno = errno;
589 gotsig = 1; 607 gotsig = 1;
590#ifdef WIN32
591 send (sigpipe [1], &signum, 1, MSG_DONTWAIT);
592#else
593 write (sigpipe [1], &signum, 1); 608 write (sigpipe [1], &signum, 1);
594#endif
595 errno = old_errno; 609 errno = old_errno;
596 } 610 }
597} 611}
598 612
599void 613void
600ev_feed_signal_event (EV_P_ int signum) 614ev_feed_signal_event (EV_P_ int signum)
601{ 615{
602 WL w; 616 WL w;
603 617
604#if EV_MULTIPLICITY 618#if EV_MULTIPLICITY
605 assert (("feeding signal events is only supported in the default loop", loop == default_loop)); 619 assert (("feeding signal events is only supported in the default loop", loop == ev_default_loop_ptr));
606#endif 620#endif
607 621
608 --signum; 622 --signum;
609 623
610 if (signum < 0 || signum >= signalmax) 624 if (signum < 0 || signum >= signalmax)
619static void 633static void
620sigcb (EV_P_ struct ev_io *iow, int revents) 634sigcb (EV_P_ struct ev_io *iow, int revents)
621{ 635{
622 int signum; 636 int signum;
623 637
624#ifdef WIN32
625 recv (sigpipe [0], &revents, 1, MSG_DONTWAIT);
626#else
627 read (sigpipe [0], &revents, 1); 638 read (sigpipe [0], &revents, 1);
628#endif
629 gotsig = 0; 639 gotsig = 0;
630 640
631 for (signum = signalmax; signum--; ) 641 for (signum = signalmax; signum--; )
632 if (signals [signum].gotsig) 642 if (signals [signum].gotsig)
633 ev_feed_signal_event (EV_A_ signum + 1); 643 ev_feed_signal_event (EV_A_ signum + 1);
634} 644}
635 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
636static void 658static void
637siginit (EV_P) 659siginit (EV_P)
638{ 660{
639#ifndef WIN32 661 fd_intern (sigpipe [0]);
640 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); 662 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 663
648 ev_io_set (&sigev, sigpipe [0], EV_READ); 664 ev_io_set (&sigev, sigpipe [0], EV_READ);
649 ev_io_start (EV_A_ &sigev); 665 ev_io_start (EV_A_ &sigev);
650 ev_unref (EV_A); /* child watcher should not keep loop alive */ 666 ev_unref (EV_A); /* child watcher should not keep loop alive */
651} 667}
652 668
653/*****************************************************************************/ 669/*****************************************************************************/
654 670
655static struct ev_child *childs [PID_HASHSIZE]; 671static struct ev_child *childs [PID_HASHSIZE];
656 672
657#ifndef WIN32 673#ifndef _WIN32
658 674
659static struct ev_signal childev; 675static struct ev_signal childev;
660 676
661#ifndef WCONTINUED 677#ifndef WCONTINUED
662# define WCONTINUED 0 678# define WCONTINUED 0
723 739
724/* 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 */
725static int 741static int
726enable_secure (void) 742enable_secure (void)
727{ 743{
728#ifdef WIN32 744#ifdef _WIN32
729 return 0; 745 return 0;
730#else 746#else
731 return getuid () != geteuid () 747 return getuid () != geteuid ()
732 || getgid () != getegid (); 748 || getgid () != getegid ();
733#endif 749#endif
734} 750}
735 751
736int 752unsigned int
737ev_method (EV_P) 753ev_method (EV_P)
738{ 754{
739 return method; 755 return method;
740} 756}
741 757
742static void 758static void
743loop_init (EV_P_ int methods) 759loop_init (EV_P_ unsigned int flags)
744{ 760{
745 if (!method) 761 if (!method)
746 { 762 {
747#if EV_USE_MONOTONIC 763#if EV_USE_MONOTONIC
748 { 764 {
755 ev_rt_now = ev_time (); 771 ev_rt_now = ev_time ();
756 mn_now = get_clock (); 772 mn_now = get_clock ();
757 now_floor = mn_now; 773 now_floor = mn_now;
758 rtmn_diff = ev_rt_now - mn_now; 774 rtmn_diff = ev_rt_now - mn_now;
759 775
760 if (methods == EVMETHOD_AUTO) 776 if (!(flags & EVFLAG_NOENV) && !enable_secure () && getenv ("LIBEV_FLAGS"))
761 if (!enable_secure () && getenv ("LIBEV_METHODS"))
762 methods = atoi (getenv ("LIBEV_METHODS")); 777 flags = atoi (getenv ("LIBEV_FLAGS"));
763 else 778
764 methods = EVMETHOD_ANY; 779 if (!(flags & 0x0000ffff))
780 flags |= 0x0000ffff;
765 781
766 method = 0; 782 method = 0;
767#if EV_USE_WIN32
768 if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods);
769#endif
770#if EV_USE_KQUEUE 783#if EV_USE_KQUEUE
771 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); 784 if (!method && (flags & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ flags);
772#endif 785#endif
773#if EV_USE_EPOLL 786#if EV_USE_EPOLL
774 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); 787 if (!method && (flags & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ flags);
775#endif 788#endif
776#if EV_USE_POLL 789#if EV_USE_POLL
777 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); 790 if (!method && (flags & EVMETHOD_POLL )) method = poll_init (EV_A_ flags);
778#endif 791#endif
779#if EV_USE_SELECT 792#if EV_USE_SELECT
780 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 793 if (!method && (flags & EVMETHOD_SELECT)) method = select_init (EV_A_ flags);
781#endif 794#endif
782 795
783 ev_init (&sigev, sigcb); 796 ev_init (&sigev, sigcb);
784 ev_set_priority (&sigev, EV_MAXPRI); 797 ev_set_priority (&sigev, EV_MAXPRI);
785 } 798 }
788void 801void
789loop_destroy (EV_P) 802loop_destroy (EV_P)
790{ 803{
791 int i; 804 int i;
792 805
793#if EV_USE_WIN32
794 if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A);
795#endif
796#if EV_USE_KQUEUE 806#if EV_USE_KQUEUE
797 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); 807 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
798#endif 808#endif
799#if EV_USE_EPOLL 809#if EV_USE_EPOLL
800 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); 810 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A);
808 818
809 for (i = NUMPRI; i--; ) 819 for (i = NUMPRI; i--; )
810 array_free (pending, [i]); 820 array_free (pending, [i]);
811 821
812 /* have to use the microsoft-never-gets-it-right macro */ 822 /* have to use the microsoft-never-gets-it-right macro */
813 array_free_microshit (fdchange); 823 array_free (fdchange, EMPTY0);
814 array_free_microshit (timer); 824 array_free (timer, EMPTY0);
815#if EV_PERIODICS 825#if EV_PERIODICS
816 array_free_microshit (periodic); 826 array_free (periodic, EMPTY0);
817#endif 827#endif
818 array_free_microshit (idle); 828 array_free (idle, EMPTY0);
819 array_free_microshit (prepare); 829 array_free (prepare, EMPTY0);
820 array_free_microshit (check); 830 array_free (check, EMPTY0);
821 831
822 method = 0; 832 method = 0;
823} 833}
824 834
825static void 835static void
850 postfork = 0; 860 postfork = 0;
851} 861}
852 862
853#if EV_MULTIPLICITY 863#if EV_MULTIPLICITY
854struct ev_loop * 864struct ev_loop *
855ev_loop_new (int methods) 865ev_loop_new (unsigned int flags)
856{ 866{
857 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));
858 868
859 memset (loop, 0, sizeof (struct ev_loop)); 869 memset (loop, 0, sizeof (struct ev_loop));
860 870
861 loop_init (EV_A_ methods); 871 loop_init (EV_A_ flags);
862 872
863 if (ev_method (EV_A)) 873 if (ev_method (EV_A))
864 return loop; 874 return loop;
865 875
866 return 0; 876 return 0;
881 891
882#endif 892#endif
883 893
884#if EV_MULTIPLICITY 894#if EV_MULTIPLICITY
885struct ev_loop * 895struct ev_loop *
896ev_default_loop_ (unsigned int flags)
886#else 897#else
887int 898int
899ev_default_loop (unsigned int flags)
888#endif 900#endif
889ev_default_loop (int methods)
890{ 901{
891 if (sigpipe [0] == sigpipe [1]) 902 if (sigpipe [0] == sigpipe [1])
892 if (pipe (sigpipe)) 903 if (pipe (sigpipe))
893 return 0; 904 return 0;
894 905
895 if (!default_loop) 906 if (!ev_default_loop_ptr)
896 { 907 {
897#if EV_MULTIPLICITY 908#if EV_MULTIPLICITY
898 struct ev_loop *loop = default_loop = &default_loop_struct; 909 struct ev_loop *loop = ev_default_loop_ptr = &default_loop_struct;
899#else 910#else
900 default_loop = 1; 911 ev_default_loop_ptr = 1;
901#endif 912#endif
902 913
903 loop_init (EV_A_ methods); 914 loop_init (EV_A_ flags);
904 915
905 if (ev_method (EV_A)) 916 if (ev_method (EV_A))
906 { 917 {
907 siginit (EV_A); 918 siginit (EV_A);
908 919
909#ifndef WIN32 920#ifndef _WIN32
910 ev_signal_init (&childev, childcb, SIGCHLD); 921 ev_signal_init (&childev, childcb, SIGCHLD);
911 ev_set_priority (&childev, EV_MAXPRI); 922 ev_set_priority (&childev, EV_MAXPRI);
912 ev_signal_start (EV_A_ &childev); 923 ev_signal_start (EV_A_ &childev);
913 ev_unref (EV_A); /* child watcher should not keep loop alive */ 924 ev_unref (EV_A); /* child watcher should not keep loop alive */
914#endif 925#endif
915 } 926 }
916 else 927 else
917 default_loop = 0; 928 ev_default_loop_ptr = 0;
918 } 929 }
919 930
920 return default_loop; 931 return ev_default_loop_ptr;
921} 932}
922 933
923void 934void
924ev_default_destroy (void) 935ev_default_destroy (void)
925{ 936{
926#if EV_MULTIPLICITY 937#if EV_MULTIPLICITY
927 struct ev_loop *loop = default_loop; 938 struct ev_loop *loop = ev_default_loop_ptr;
928#endif 939#endif
929 940
930#ifndef WIN32 941#ifndef _WIN32
931 ev_ref (EV_A); /* child watcher */ 942 ev_ref (EV_A); /* child watcher */
932 ev_signal_stop (EV_A_ &childev); 943 ev_signal_stop (EV_A_ &childev);
933#endif 944#endif
934 945
935 ev_ref (EV_A); /* signal watcher */ 946 ev_ref (EV_A); /* signal watcher */
943 954
944void 955void
945ev_default_fork (void) 956ev_default_fork (void)
946{ 957{
947#if EV_MULTIPLICITY 958#if EV_MULTIPLICITY
948 struct ev_loop *loop = default_loop; 959 struct ev_loop *loop = ev_default_loop_ptr;
949#endif 960#endif
950 961
951 if (method) 962 if (method)
952 postfork = 1; 963 postfork = 1;
953} 964}
1022 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 1033 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
1023 1034
1024 /* first reschedule or stop timer */ 1035 /* first reschedule or stop timer */
1025 if (w->reschedule_cb) 1036 if (w->reschedule_cb)
1026 { 1037 {
1027 ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001); 1038 ((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)); 1039 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now));
1030 downheap ((WT *)periodics, periodiccnt, 0); 1040 downheap ((WT *)periodics, periodiccnt, 0);
1031 } 1041 }
1032 else if (w->interval) 1042 else if (w->interval)
1033 { 1043 {
1151ev_loop (EV_P_ int flags) 1161ev_loop (EV_P_ int flags)
1152{ 1162{
1153 double block; 1163 double block;
1154 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; 1164 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
1155 1165
1156 do 1166 while (activecnt)
1157 { 1167 {
1158 /* queue check watchers (and execute them) */ 1168 /* queue check watchers (and execute them) */
1159 if (expect_false (preparecnt)) 1169 if (expect_false (preparecnt))
1160 { 1170 {
1161 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 1171 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
1224 /* queue check watchers, to be executed first */ 1234 /* queue check watchers, to be executed first */
1225 if (checkcnt) 1235 if (checkcnt)
1226 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1236 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1227 1237
1228 call_pending (EV_A); 1238 call_pending (EV_A);
1239
1240 if (loop_done)
1241 break;
1229 } 1242 }
1230 while (activecnt && !loop_done);
1231 1243
1232 if (loop_done != 2) 1244 if (loop_done != 2)
1233 loop_done = 0; 1245 loop_done = 0;
1234} 1246}
1235 1247
1333 ((WT)w)->at += mn_now; 1345 ((WT)w)->at += mn_now;
1334 1346
1335 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1347 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1336 1348
1337 ev_start (EV_A_ (W)w, ++timercnt); 1349 ev_start (EV_A_ (W)w, ++timercnt);
1338 array_needsize (struct ev_timer *, timers, timermax, timercnt, (void)); 1350 array_needsize (struct ev_timer *, timers, timermax, timercnt, EMPTY2);
1339 timers [timercnt - 1] = w; 1351 timers [timercnt - 1] = w;
1340 upheap ((WT *)timers, timercnt - 1); 1352 upheap ((WT *)timers, timercnt - 1);
1341 1353
1342 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1354 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1343} 1355}
1352 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1364 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1353 1365
1354 if (((W)w)->active < timercnt--) 1366 if (((W)w)->active < timercnt--)
1355 { 1367 {
1356 timers [((W)w)->active - 1] = timers [timercnt]; 1368 timers [((W)w)->active - 1] = timers [timercnt];
1357 downheap ((WT *)timers, timercnt, ((W)w)->active - 1); 1369 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1358 } 1370 }
1359 1371
1360 ((WT)w)->at -= mn_now; 1372 ((WT)w)->at -= mn_now;
1361 1373
1362 ev_stop (EV_A_ (W)w); 1374 ev_stop (EV_A_ (W)w);
1366ev_timer_again (EV_P_ struct ev_timer *w) 1378ev_timer_again (EV_P_ struct ev_timer *w)
1367{ 1379{
1368 if (ev_is_active (w)) 1380 if (ev_is_active (w))
1369 { 1381 {
1370 if (w->repeat) 1382 if (w->repeat)
1383 {
1384 ((WT)w)->at = mn_now + w->repeat;
1371 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1, mn_now + w->repeat); 1385 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1386 }
1372 else 1387 else
1373 ev_timer_stop (EV_A_ w); 1388 ev_timer_stop (EV_A_ w);
1374 } 1389 }
1375 else if (w->repeat) 1390 else if (w->repeat)
1391 {
1392 w->at = w->repeat;
1376 ev_timer_start (EV_A_ w); 1393 ev_timer_start (EV_A_ w);
1394 }
1377} 1395}
1378 1396
1379#if EV_PERIODICS 1397#if EV_PERIODICS
1380void 1398void
1381ev_periodic_start (EV_P_ struct ev_periodic *w) 1399ev_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 */ 1409 /* 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; 1410 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1393 } 1411 }
1394 1412
1395 ev_start (EV_A_ (W)w, ++periodiccnt); 1413 ev_start (EV_A_ (W)w, ++periodiccnt);
1396 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); 1414 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2);
1397 periodics [periodiccnt - 1] = w; 1415 periodics [periodiccnt - 1] = w;
1398 upheap ((WT *)periodics, periodiccnt - 1); 1416 upheap ((WT *)periodics, periodiccnt - 1);
1399 1417
1400 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1418 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1401} 1419}
1410 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1428 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1411 1429
1412 if (((W)w)->active < periodiccnt--) 1430 if (((W)w)->active < periodiccnt--)
1413 { 1431 {
1414 periodics [((W)w)->active - 1] = periodics [periodiccnt]; 1432 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1415 downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); 1433 adjustheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1416 } 1434 }
1417 1435
1418 ev_stop (EV_A_ (W)w); 1436 ev_stop (EV_A_ (W)w);
1419} 1437}
1420 1438
1432{ 1450{
1433 if (ev_is_active (w)) 1451 if (ev_is_active (w))
1434 return; 1452 return;
1435 1453
1436 ev_start (EV_A_ (W)w, ++idlecnt); 1454 ev_start (EV_A_ (W)w, ++idlecnt);
1437 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, (void)); 1455 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, EMPTY2);
1438 idles [idlecnt - 1] = w; 1456 idles [idlecnt - 1] = w;
1439} 1457}
1440 1458
1441void 1459void
1442ev_idle_stop (EV_P_ struct ev_idle *w) 1460ev_idle_stop (EV_P_ struct ev_idle *w)
1443{ 1461{
1444 ev_clear_pending (EV_A_ (W)w); 1462 ev_clear_pending (EV_A_ (W)w);
1445 if (ev_is_active (w)) 1463 if (!ev_is_active (w))
1446 return; 1464 return;
1447 1465
1448 idles [((W)w)->active - 1] = idles [--idlecnt]; 1466 idles [((W)w)->active - 1] = idles [--idlecnt];
1449 ev_stop (EV_A_ (W)w); 1467 ev_stop (EV_A_ (W)w);
1450} 1468}
1454{ 1472{
1455 if (ev_is_active (w)) 1473 if (ev_is_active (w))
1456 return; 1474 return;
1457 1475
1458 ev_start (EV_A_ (W)w, ++preparecnt); 1476 ev_start (EV_A_ (W)w, ++preparecnt);
1459 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, (void)); 1477 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, EMPTY2);
1460 prepares [preparecnt - 1] = w; 1478 prepares [preparecnt - 1] = w;
1461} 1479}
1462 1480
1463void 1481void
1464ev_prepare_stop (EV_P_ struct ev_prepare *w) 1482ev_prepare_stop (EV_P_ struct ev_prepare *w)
1465{ 1483{
1466 ev_clear_pending (EV_A_ (W)w); 1484 ev_clear_pending (EV_A_ (W)w);
1467 if (ev_is_active (w)) 1485 if (!ev_is_active (w))
1468 return; 1486 return;
1469 1487
1470 prepares [((W)w)->active - 1] = prepares [--preparecnt]; 1488 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1471 ev_stop (EV_A_ (W)w); 1489 ev_stop (EV_A_ (W)w);
1472} 1490}
1476{ 1494{
1477 if (ev_is_active (w)) 1495 if (ev_is_active (w))
1478 return; 1496 return;
1479 1497
1480 ev_start (EV_A_ (W)w, ++checkcnt); 1498 ev_start (EV_A_ (W)w, ++checkcnt);
1481 array_needsize (struct ev_check *, checks, checkmax, checkcnt, (void)); 1499 array_needsize (struct ev_check *, checks, checkmax, checkcnt, EMPTY2);
1482 checks [checkcnt - 1] = w; 1500 checks [checkcnt - 1] = w;
1483} 1501}
1484 1502
1485void 1503void
1486ev_check_stop (EV_P_ struct ev_check *w) 1504ev_check_stop (EV_P_ struct ev_check *w)
1499 1517
1500void 1518void
1501ev_signal_start (EV_P_ struct ev_signal *w) 1519ev_signal_start (EV_P_ struct ev_signal *w)
1502{ 1520{
1503#if EV_MULTIPLICITY 1521#if EV_MULTIPLICITY
1504 assert (("signal watchers are only supported in the default loop", loop == default_loop)); 1522 assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr));
1505#endif 1523#endif
1506 if (ev_is_active (w)) 1524 if (ev_is_active (w))
1507 return; 1525 return;
1508 1526
1509 assert (("ev_signal_start called with illegal signal number", w->signum > 0)); 1527 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
1512 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init); 1530 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init);
1513 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1531 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1514 1532
1515 if (!((WL)w)->next) 1533 if (!((WL)w)->next)
1516 { 1534 {
1517#if WIN32 1535#if _WIN32
1518 signal (w->signum, sighandler); 1536 signal (w->signum, sighandler);
1519#else 1537#else
1520 struct sigaction sa; 1538 struct sigaction sa;
1521 sa.sa_handler = sighandler; 1539 sa.sa_handler = sighandler;
1522 sigfillset (&sa.sa_mask); 1540 sigfillset (&sa.sa_mask);
1542 1560
1543void 1561void
1544ev_child_start (EV_P_ struct ev_child *w) 1562ev_child_start (EV_P_ struct ev_child *w)
1545{ 1563{
1546#if EV_MULTIPLICITY 1564#if EV_MULTIPLICITY
1547 assert (("child watchers are only supported in the default loop", loop == default_loop)); 1565 assert (("child watchers are only supported in the default loop", loop == ev_default_loop_ptr));
1548#endif 1566#endif
1549 if (ev_is_active (w)) 1567 if (ev_is_active (w))
1550 return; 1568 return;
1551 1569
1552 ev_start (EV_A_ (W)w, 1); 1570 ev_start (EV_A_ (W)w, 1);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines