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.115 by root, Wed Nov 14 04:53:21 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
181#ifdef _WIN32
166#include "ev_win32.c" 182# include "ev_win32.c"
183#endif
167 184
168/*****************************************************************************/ 185/*****************************************************************************/
169 186
170static void (*syserr_cb)(const char *msg); 187static void (*syserr_cb)(const char *msg);
171 188
218typedef struct 235typedef struct
219{ 236{
220 WL head; 237 WL head;
221 unsigned char events; 238 unsigned char events;
222 unsigned char reify; 239 unsigned char reify;
240#if EV_SELECT_IS_WINSOCKET
241 SOCKET handle;
242#endif
223} ANFD; 243} ANFD;
224 244
225typedef struct 245typedef struct
226{ 246{
227 W w; 247 W w;
231#if EV_MULTIPLICITY 251#if EV_MULTIPLICITY
232 252
233 struct ev_loop 253 struct ev_loop
234 { 254 {
235 ev_tstamp ev_rt_now; 255 ev_tstamp ev_rt_now;
256 #define ev_rt_now ((loop)->ev_rt_now)
236 #define VAR(name,decl) decl; 257 #define VAR(name,decl) decl;
237 #include "ev_vars.h" 258 #include "ev_vars.h"
238 #undef VAR 259 #undef VAR
239 }; 260 };
240 #include "ev_wrap.h" 261 #include "ev_wrap.h"
290{ 311{
291 return ev_rt_now; 312 return ev_rt_now;
292} 313}
293#endif 314#endif
294 315
295#define array_roundsize(type,n) ((n) | 4 & ~3) 316#define array_roundsize(type,n) (((n) | 4) & ~3)
296 317
297#define array_needsize(type,base,cur,cnt,init) \ 318#define array_needsize(type,base,cur,cnt,init) \
298 if (expect_false ((cnt) > cur)) \ 319 if (expect_false ((cnt) > cur)) \
299 { \ 320 { \
300 int newcnt = cur; \ 321 int newcnt = cur; \
315 stem ## max = array_roundsize (stem ## cnt >> 1); \ 336 stem ## max = array_roundsize (stem ## cnt >> 1); \
316 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ 337 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\
317 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ 338 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
318 } 339 }
319 340
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) \ 341#define array_free(stem, idx) \
326 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;
327 343
328/*****************************************************************************/ 344/*****************************************************************************/
329 345
350 pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; 366 pendings [ABSPRI (w_)][w_->pending - 1].events |= revents;
351 return; 367 return;
352 } 368 }
353 369
354 w_->pending = ++pendingcnt [ABSPRI (w_)]; 370 w_->pending = ++pendingcnt [ABSPRI (w_)];
355 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);
356 pendings [ABSPRI (w_)][w_->pending - 1].w = w_; 372 pendings [ABSPRI (w_)][w_->pending - 1].w = w_;
357 pendings [ABSPRI (w_)][w_->pending - 1].events = revents; 373 pendings [ABSPRI (w_)][w_->pending - 1].events = revents;
358} 374}
359 375
360static void 376static void
403 int events = 0; 419 int events = 0;
404 420
405 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)
406 events |= w->events; 422 events |= w->events;
407 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
408 anfd->reify = 0; 433 anfd->reify = 0;
409 434
410 method_modify (EV_A_ fd, anfd->events, events); 435 method_modify (EV_A_ fd, anfd->events, events);
411 anfd->events = events; 436 anfd->events = events;
412 } 437 }
421 return; 446 return;
422 447
423 anfds [fd].reify = 1; 448 anfds [fd].reify = 1;
424 449
425 ++fdchangecnt; 450 ++fdchangecnt;
426 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, (void)); 451 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2);
427 fdchanges [fdchangecnt - 1] = fd; 452 fdchanges [fdchangecnt - 1] = fd;
428} 453}
429 454
430static void 455static void
431fd_kill (EV_P_ int fd) 456fd_kill (EV_P_ int fd)
440} 465}
441 466
442static int 467static int
443fd_valid (int fd) 468fd_valid (int fd)
444{ 469{
445#ifdef WIN32 470#ifdef _WIN32
446 return !!win32_get_osfhandle (fd); 471 return _get_osfhandle (fd) != -1;
447#else 472#else
448 return fcntl (fd, F_GETFD) != -1; 473 return fcntl (fd, F_GETFD) != -1;
449#endif 474#endif
450} 475}
451 476
532 heap [k] = w; 557 heap [k] = w;
533 ((W)heap [k])->active = k + 1; 558 ((W)heap [k])->active = k + 1;
534} 559}
535 560
536inline void 561inline void
537adjustheap (WT *heap, int N, int k, ev_tstamp at) 562adjustheap (WT *heap, int N, int k)
538{ 563{
539 ev_tstamp old_at = heap [k]->at; 564 upheap (heap, k);
540 heap [k]->at = at;
541
542 if (old_at < at)
543 downheap (heap, N, k); 565 downheap (heap, N, k);
544 else
545 upheap (heap, k);
546} 566}
547 567
548/*****************************************************************************/ 568/*****************************************************************************/
549 569
550typedef struct 570typedef struct
573} 593}
574 594
575static void 595static void
576sighandler (int signum) 596sighandler (int signum)
577{ 597{
578#if WIN32 598#if _WIN32
579 signal (signum, sighandler); 599 signal (signum, sighandler);
580#endif 600#endif
581 601
582 signals [signum - 1].gotsig = 1; 602 signals [signum - 1].gotsig = 1;
583 603
584 if (!gotsig) 604 if (!gotsig)
585 { 605 {
586 int old_errno = errno; 606 int old_errno = errno;
587 gotsig = 1; 607 gotsig = 1;
588#ifdef WIN32
589 send (sigpipe [1], &signum, 1, MSG_DONTWAIT);
590#else
591 write (sigpipe [1], &signum, 1); 608 write (sigpipe [1], &signum, 1);
592#endif
593 errno = old_errno; 609 errno = old_errno;
594 } 610 }
595} 611}
596 612
597void 613void
617static void 633static void
618sigcb (EV_P_ struct ev_io *iow, int revents) 634sigcb (EV_P_ struct ev_io *iow, int revents)
619{ 635{
620 int signum; 636 int signum;
621 637
622#ifdef WIN32
623 recv (sigpipe [0], &revents, 1, MSG_DONTWAIT);
624#else
625 read (sigpipe [0], &revents, 1); 638 read (sigpipe [0], &revents, 1);
626#endif
627 gotsig = 0; 639 gotsig = 0;
628 640
629 for (signum = signalmax; signum--; ) 641 for (signum = signalmax; signum--; )
630 if (signals [signum].gotsig) 642 if (signals [signum].gotsig)
631 ev_feed_signal_event (EV_A_ signum + 1); 643 ev_feed_signal_event (EV_A_ signum + 1);
632} 644}
633 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
634static void 658static void
635siginit (EV_P) 659siginit (EV_P)
636{ 660{
637#ifndef WIN32 661 fd_intern (sigpipe [0]);
638 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); 662 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 663
646 ev_io_set (&sigev, sigpipe [0], EV_READ); 664 ev_io_set (&sigev, sigpipe [0], EV_READ);
647 ev_io_start (EV_A_ &sigev); 665 ev_io_start (EV_A_ &sigev);
648 ev_unref (EV_A); /* child watcher should not keep loop alive */ 666 ev_unref (EV_A); /* child watcher should not keep loop alive */
649} 667}
650 668
651/*****************************************************************************/ 669/*****************************************************************************/
652 670
653static struct ev_child *childs [PID_HASHSIZE]; 671static struct ev_child *childs [PID_HASHSIZE];
654 672
655#ifndef WIN32 673#ifndef _WIN32
656 674
657static struct ev_signal childev; 675static struct ev_signal childev;
658 676
659#ifndef WCONTINUED 677#ifndef WCONTINUED
660# define WCONTINUED 0 678# define WCONTINUED 0
721 739
722/* 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 */
723static int 741static int
724enable_secure (void) 742enable_secure (void)
725{ 743{
726#ifdef WIN32 744#ifdef _WIN32
727 return 0; 745 return 0;
728#else 746#else
729 return getuid () != geteuid () 747 return getuid () != geteuid ()
730 || getgid () != getegid (); 748 || getgid () != getegid ();
731#endif 749#endif
732} 750}
733 751
734int 752unsigned int
735ev_method (EV_P) 753ev_method (EV_P)
736{ 754{
737 return method; 755 return method;
738} 756}
739 757
740static void 758static void
741loop_init (EV_P_ int methods) 759loop_init (EV_P_ unsigned int flags)
742{ 760{
743 if (!method) 761 if (!method)
744 { 762 {
745#if EV_USE_MONOTONIC 763#if EV_USE_MONOTONIC
746 { 764 {
753 ev_rt_now = ev_time (); 771 ev_rt_now = ev_time ();
754 mn_now = get_clock (); 772 mn_now = get_clock ();
755 now_floor = mn_now; 773 now_floor = mn_now;
756 rtmn_diff = ev_rt_now - mn_now; 774 rtmn_diff = ev_rt_now - mn_now;
757 775
758 if (methods == EVMETHOD_AUTO) 776 if (!(flags & EVFLAG_NOENV) && !enable_secure () && getenv ("LIBEV_FLAGS"))
759 if (!enable_secure () && getenv ("LIBEV_METHODS"))
760 methods = atoi (getenv ("LIBEV_METHODS")); 777 flags = atoi (getenv ("LIBEV_FLAGS"));
761 else 778
762 methods = EVMETHOD_ANY; 779 if (!(flags & 0x0000ffff))
780 flags |= 0x0000ffff;
763 781
764 method = 0; 782 method = 0;
765#if EV_USE_WIN32
766 if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods);
767#endif
768#if EV_USE_KQUEUE 783#if EV_USE_KQUEUE
769 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); 784 if (!method && (flags & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ flags);
770#endif 785#endif
771#if EV_USE_EPOLL 786#if EV_USE_EPOLL
772 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); 787 if (!method && (flags & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ flags);
773#endif 788#endif
774#if EV_USE_POLL 789#if EV_USE_POLL
775 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); 790 if (!method && (flags & EVMETHOD_POLL )) method = poll_init (EV_A_ flags);
776#endif 791#endif
777#if EV_USE_SELECT 792#if EV_USE_SELECT
778 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 793 if (!method && (flags & EVMETHOD_SELECT)) method = select_init (EV_A_ flags);
779#endif 794#endif
780 795
781 ev_init (&sigev, sigcb); 796 ev_init (&sigev, sigcb);
782 ev_set_priority (&sigev, EV_MAXPRI); 797 ev_set_priority (&sigev, EV_MAXPRI);
783 } 798 }
786void 801void
787loop_destroy (EV_P) 802loop_destroy (EV_P)
788{ 803{
789 int i; 804 int i;
790 805
791#if EV_USE_WIN32
792 if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A);
793#endif
794#if EV_USE_KQUEUE 806#if EV_USE_KQUEUE
795 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); 807 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
796#endif 808#endif
797#if EV_USE_EPOLL 809#if EV_USE_EPOLL
798 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); 810 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A);
806 818
807 for (i = NUMPRI; i--; ) 819 for (i = NUMPRI; i--; )
808 array_free (pending, [i]); 820 array_free (pending, [i]);
809 821
810 /* have to use the microsoft-never-gets-it-right macro */ 822 /* have to use the microsoft-never-gets-it-right macro */
811 array_free_microshit (fdchange); 823 array_free (fdchange, EMPTY0);
812 array_free_microshit (timer); 824 array_free (timer, EMPTY0);
813#if EV_PERIODICS 825#if EV_PERIODICS
814 array_free_microshit (periodic); 826 array_free (periodic, EMPTY0);
815#endif 827#endif
816 array_free_microshit (idle); 828 array_free (idle, EMPTY0);
817 array_free_microshit (prepare); 829 array_free (prepare, EMPTY0);
818 array_free_microshit (check); 830 array_free (check, EMPTY0);
819 831
820 method = 0; 832 method = 0;
821} 833}
822 834
823static void 835static void
848 postfork = 0; 860 postfork = 0;
849} 861}
850 862
851#if EV_MULTIPLICITY 863#if EV_MULTIPLICITY
852struct ev_loop * 864struct ev_loop *
853ev_loop_new (int methods) 865ev_loop_new (unsigned int flags)
854{ 866{
855 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));
856 868
857 memset (loop, 0, sizeof (struct ev_loop)); 869 memset (loop, 0, sizeof (struct ev_loop));
858 870
859 loop_init (EV_A_ methods); 871 loop_init (EV_A_ flags);
860 872
861 if (ev_method (EV_A)) 873 if (ev_method (EV_A))
862 return loop; 874 return loop;
863 875
864 return 0; 876 return 0;
882#if EV_MULTIPLICITY 894#if EV_MULTIPLICITY
883struct ev_loop * 895struct ev_loop *
884#else 896#else
885int 897int
886#endif 898#endif
887ev_default_loop (int methods) 899ev_default_loop (unsigned int flags)
888{ 900{
889 if (sigpipe [0] == sigpipe [1]) 901 if (sigpipe [0] == sigpipe [1])
890 if (pipe (sigpipe)) 902 if (pipe (sigpipe))
891 return 0; 903 return 0;
892 904
896 struct ev_loop *loop = default_loop = &default_loop_struct; 908 struct ev_loop *loop = default_loop = &default_loop_struct;
897#else 909#else
898 default_loop = 1; 910 default_loop = 1;
899#endif 911#endif
900 912
901 loop_init (EV_A_ methods); 913 loop_init (EV_A_ flags);
902 914
903 if (ev_method (EV_A)) 915 if (ev_method (EV_A))
904 { 916 {
905 siginit (EV_A); 917 siginit (EV_A);
906 918
907#ifndef WIN32 919#ifndef _WIN32
908 ev_signal_init (&childev, childcb, SIGCHLD); 920 ev_signal_init (&childev, childcb, SIGCHLD);
909 ev_set_priority (&childev, EV_MAXPRI); 921 ev_set_priority (&childev, EV_MAXPRI);
910 ev_signal_start (EV_A_ &childev); 922 ev_signal_start (EV_A_ &childev);
911 ev_unref (EV_A); /* child watcher should not keep loop alive */ 923 ev_unref (EV_A); /* child watcher should not keep loop alive */
912#endif 924#endif
923{ 935{
924#if EV_MULTIPLICITY 936#if EV_MULTIPLICITY
925 struct ev_loop *loop = default_loop; 937 struct ev_loop *loop = default_loop;
926#endif 938#endif
927 939
928#ifndef WIN32 940#ifndef _WIN32
929 ev_ref (EV_A); /* child watcher */ 941 ev_ref (EV_A); /* child watcher */
930 ev_signal_stop (EV_A_ &childev); 942 ev_signal_stop (EV_A_ &childev);
931#endif 943#endif
932 944
933 ev_ref (EV_A); /* signal watcher */ 945 ev_ref (EV_A); /* signal watcher */
1020 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 1032 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
1021 1033
1022 /* first reschedule or stop timer */ 1034 /* first reschedule or stop timer */
1023 if (w->reschedule_cb) 1035 if (w->reschedule_cb)
1024 { 1036 {
1025 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);
1026
1027 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));
1028 downheap ((WT *)periodics, periodiccnt, 0); 1039 downheap ((WT *)periodics, periodiccnt, 0);
1029 } 1040 }
1030 else if (w->interval) 1041 else if (w->interval)
1031 { 1042 {
1149ev_loop (EV_P_ int flags) 1160ev_loop (EV_P_ int flags)
1150{ 1161{
1151 double block; 1162 double block;
1152 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; 1163 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
1153 1164
1154 do 1165 while (activecnt)
1155 { 1166 {
1156 /* queue check watchers (and execute them) */ 1167 /* queue check watchers (and execute them) */
1157 if (expect_false (preparecnt)) 1168 if (expect_false (preparecnt))
1158 { 1169 {
1159 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 1170 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
1222 /* queue check watchers, to be executed first */ 1233 /* queue check watchers, to be executed first */
1223 if (checkcnt) 1234 if (checkcnt)
1224 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1235 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1225 1236
1226 call_pending (EV_A); 1237 call_pending (EV_A);
1238
1239 if (loop_done)
1240 break;
1227 } 1241 }
1228 while (activecnt && !loop_done);
1229 1242
1230 if (loop_done != 2) 1243 if (loop_done != 2)
1231 loop_done = 0; 1244 loop_done = 0;
1232} 1245}
1233 1246
1331 ((WT)w)->at += mn_now; 1344 ((WT)w)->at += mn_now;
1332 1345
1333 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.));
1334 1347
1335 ev_start (EV_A_ (W)w, ++timercnt); 1348 ev_start (EV_A_ (W)w, ++timercnt);
1336 array_needsize (struct ev_timer *, timers, timermax, timercnt, (void)); 1349 array_needsize (struct ev_timer *, timers, timermax, timercnt, EMPTY2);
1337 timers [timercnt - 1] = w; 1350 timers [timercnt - 1] = w;
1338 upheap ((WT *)timers, timercnt - 1); 1351 upheap ((WT *)timers, timercnt - 1);
1339 1352
1340 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1353 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1341} 1354}
1350 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1363 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1351 1364
1352 if (((W)w)->active < timercnt--) 1365 if (((W)w)->active < timercnt--)
1353 { 1366 {
1354 timers [((W)w)->active - 1] = timers [timercnt]; 1367 timers [((W)w)->active - 1] = timers [timercnt];
1355 downheap ((WT *)timers, timercnt, ((W)w)->active - 1); 1368 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1356 } 1369 }
1357 1370
1358 ((WT)w)->at -= mn_now; 1371 ((WT)w)->at -= mn_now;
1359 1372
1360 ev_stop (EV_A_ (W)w); 1373 ev_stop (EV_A_ (W)w);
1364ev_timer_again (EV_P_ struct ev_timer *w) 1377ev_timer_again (EV_P_ struct ev_timer *w)
1365{ 1378{
1366 if (ev_is_active (w)) 1379 if (ev_is_active (w))
1367 { 1380 {
1368 if (w->repeat) 1381 if (w->repeat)
1382 {
1383 ((WT)w)->at = mn_now + w->repeat;
1369 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1, mn_now + w->repeat); 1384 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1385 }
1370 else 1386 else
1371 ev_timer_stop (EV_A_ w); 1387 ev_timer_stop (EV_A_ w);
1372 } 1388 }
1373 else if (w->repeat) 1389 else if (w->repeat)
1390 {
1391 w->at = w->repeat;
1374 ev_timer_start (EV_A_ w); 1392 ev_timer_start (EV_A_ w);
1393 }
1375} 1394}
1376 1395
1377#if EV_PERIODICS 1396#if EV_PERIODICS
1378void 1397void
1379ev_periodic_start (EV_P_ struct ev_periodic *w) 1398ev_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 */ 1408 /* 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; 1409 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1391 } 1410 }
1392 1411
1393 ev_start (EV_A_ (W)w, ++periodiccnt); 1412 ev_start (EV_A_ (W)w, ++periodiccnt);
1394 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); 1413 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2);
1395 periodics [periodiccnt - 1] = w; 1414 periodics [periodiccnt - 1] = w;
1396 upheap ((WT *)periodics, periodiccnt - 1); 1415 upheap ((WT *)periodics, periodiccnt - 1);
1397 1416
1398 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1417 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1399} 1418}
1408 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1427 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1409 1428
1410 if (((W)w)->active < periodiccnt--) 1429 if (((W)w)->active < periodiccnt--)
1411 { 1430 {
1412 periodics [((W)w)->active - 1] = periodics [periodiccnt]; 1431 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1413 downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); 1432 adjustheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1414 } 1433 }
1415 1434
1416 ev_stop (EV_A_ (W)w); 1435 ev_stop (EV_A_ (W)w);
1417} 1436}
1418 1437
1430{ 1449{
1431 if (ev_is_active (w)) 1450 if (ev_is_active (w))
1432 return; 1451 return;
1433 1452
1434 ev_start (EV_A_ (W)w, ++idlecnt); 1453 ev_start (EV_A_ (W)w, ++idlecnt);
1435 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, (void)); 1454 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, EMPTY2);
1436 idles [idlecnt - 1] = w; 1455 idles [idlecnt - 1] = w;
1437} 1456}
1438 1457
1439void 1458void
1440ev_idle_stop (EV_P_ struct ev_idle *w) 1459ev_idle_stop (EV_P_ struct ev_idle *w)
1441{ 1460{
1442 ev_clear_pending (EV_A_ (W)w); 1461 ev_clear_pending (EV_A_ (W)w);
1443 if (ev_is_active (w)) 1462 if (!ev_is_active (w))
1444 return; 1463 return;
1445 1464
1446 idles [((W)w)->active - 1] = idles [--idlecnt]; 1465 idles [((W)w)->active - 1] = idles [--idlecnt];
1447 ev_stop (EV_A_ (W)w); 1466 ev_stop (EV_A_ (W)w);
1448} 1467}
1452{ 1471{
1453 if (ev_is_active (w)) 1472 if (ev_is_active (w))
1454 return; 1473 return;
1455 1474
1456 ev_start (EV_A_ (W)w, ++preparecnt); 1475 ev_start (EV_A_ (W)w, ++preparecnt);
1457 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, (void)); 1476 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, EMPTY2);
1458 prepares [preparecnt - 1] = w; 1477 prepares [preparecnt - 1] = w;
1459} 1478}
1460 1479
1461void 1480void
1462ev_prepare_stop (EV_P_ struct ev_prepare *w) 1481ev_prepare_stop (EV_P_ struct ev_prepare *w)
1463{ 1482{
1464 ev_clear_pending (EV_A_ (W)w); 1483 ev_clear_pending (EV_A_ (W)w);
1465 if (ev_is_active (w)) 1484 if (!ev_is_active (w))
1466 return; 1485 return;
1467 1486
1468 prepares [((W)w)->active - 1] = prepares [--preparecnt]; 1487 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1469 ev_stop (EV_A_ (W)w); 1488 ev_stop (EV_A_ (W)w);
1470} 1489}
1474{ 1493{
1475 if (ev_is_active (w)) 1494 if (ev_is_active (w))
1476 return; 1495 return;
1477 1496
1478 ev_start (EV_A_ (W)w, ++checkcnt); 1497 ev_start (EV_A_ (W)w, ++checkcnt);
1479 array_needsize (struct ev_check *, checks, checkmax, checkcnt, (void)); 1498 array_needsize (struct ev_check *, checks, checkmax, checkcnt, EMPTY2);
1480 checks [checkcnt - 1] = w; 1499 checks [checkcnt - 1] = w;
1481} 1500}
1482 1501
1483void 1502void
1484ev_check_stop (EV_P_ struct ev_check *w) 1503ev_check_stop (EV_P_ struct ev_check *w)
1510 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init); 1529 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init);
1511 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1530 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1512 1531
1513 if (!((WL)w)->next) 1532 if (!((WL)w)->next)
1514 { 1533 {
1515#if WIN32 1534#if _WIN32
1516 signal (w->signum, sighandler); 1535 signal (w->signum, sighandler);
1517#else 1536#else
1518 struct sigaction sa; 1537 struct sigaction sa;
1519 sa.sa_handler = sighandler; 1538 sa.sa_handler = sighandler;
1520 sigfillset (&sa.sa_mask); 1539 sigfillset (&sa.sa_mask);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines