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

Comparing libev/ev.c (file contents):
Revision 1.79 by root, Fri Nov 9 15:15:20 2007 UTC vs.
Revision 1.103 by root, Mon Nov 12 00:31:08 2007 UTC

26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */ 30 */
31
32#ifdef __cplusplus
33extern "C" {
34#endif
35
31#ifndef EV_STANDALONE 36#ifndef EV_STANDALONE
32# include "config.h" 37# include "config.h"
33 38
34# if HAVE_CLOCK_GETTIME 39# if HAVE_CLOCK_GETTIME
40# ifndef EV_USE_MONOTONIC
35# define EV_USE_MONOTONIC 1 41# define EV_USE_MONOTONIC 1
42# endif
43# ifndef EV_USE_REALTIME
36# define EV_USE_REALTIME 1 44# define EV_USE_REALTIME 1
45# endif
37# endif 46# endif
38 47
39# if HAVE_SELECT && HAVE_SYS_SELECT_H 48# if HAVE_SELECT && HAVE_SYS_SELECT_H && !defined (EV_USE_SELECT)
40# define EV_USE_SELECT 1 49# define EV_USE_SELECT 1
41# endif 50# endif
42 51
43# if HAVE_POLL && HAVE_POLL_H 52# if HAVE_POLL && HAVE_POLL_H && !defined (EV_USE_POLL)
44# define EV_USE_POLL 1 53# define EV_USE_POLL 1
45# endif 54# endif
46 55
47# if HAVE_EPOLL && HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H 56# if HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H && !defined (EV_USE_EPOLL)
48# define EV_USE_EPOLL 1 57# define EV_USE_EPOLL 1
49# endif 58# endif
50 59
51# if HAVE_KQUEUE && HAVE_WORKING_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H 60# if HAVE_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H && !defined (EV_USE_KQUEUE)
52# define EV_USE_KQUEUE 1 61# define EV_USE_KQUEUE 1
53# endif 62# endif
54 63
55#endif 64#endif
56 65
66#include <sys/types.h> 75#include <sys/types.h>
67#include <time.h> 76#include <time.h>
68 77
69#include <signal.h> 78#include <signal.h>
70 79
71#ifndef WIN32 80#ifndef _WIN32
72# include <unistd.h> 81# include <unistd.h>
73# include <sys/time.h> 82# include <sys/time.h>
74# 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
75#endif 89# endif
90#endif
91
76/**/ 92/**/
77 93
78#ifndef EV_USE_MONOTONIC 94#ifndef EV_USE_MONOTONIC
79# define EV_USE_MONOTONIC 1 95# define EV_USE_MONOTONIC 1
80#endif 96#endif
91# define EV_USE_EPOLL 0 107# define EV_USE_EPOLL 0
92#endif 108#endif
93 109
94#ifndef EV_USE_KQUEUE 110#ifndef EV_USE_KQUEUE
95# define EV_USE_KQUEUE 0 111# define EV_USE_KQUEUE 0
96#endif
97
98#ifndef EV_USE_WIN32
99# ifdef WIN32
100# define EV_USE_WIN32 0 /* it does not exist, use select */
101# undef EV_USE_SELECT
102# define EV_USE_SELECT 1
103# else
104# define EV_USE_WIN32 0
105# endif
106#endif 112#endif
107 113
108#ifndef EV_USE_REALTIME 114#ifndef EV_USE_REALTIME
109# define EV_USE_REALTIME 1 115# define EV_USE_REALTIME 1
110#endif 116#endif
117#endif 123#endif
118 124
119#ifndef CLOCK_REALTIME 125#ifndef CLOCK_REALTIME
120# undef EV_USE_REALTIME 126# undef EV_USE_REALTIME
121# define EV_USE_REALTIME 0 127# define EV_USE_REALTIME 0
128#endif
129
130#if EV_SELECT_IS_WINSOCKET
131# include <winsock.h>
122#endif 132#endif
123 133
124/**/ 134/**/
125 135
126#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 136#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
127#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */ 137#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */
128#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ 138#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */
129/*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ 139/*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */
130 140
141#ifdef EV_H
142# include EV_H
143#else
131#include "ev.h" 144# include "ev.h"
145#endif
132 146
133#if __GNUC__ >= 3 147#if __GNUC__ >= 3
134# define expect(expr,value) __builtin_expect ((expr),(value)) 148# define expect(expr,value) __builtin_expect ((expr),(value))
135# define inline inline 149# define inline inline
136#else 150#else
142#define expect_true(expr) expect ((expr) != 0, 1) 156#define expect_true(expr) expect ((expr) != 0, 1)
143 157
144#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) 158#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
145#define ABSPRI(w) ((w)->priority - EV_MINPRI) 159#define ABSPRI(w) ((w)->priority - EV_MINPRI)
146 160
161#define EMPTY /* required for microsofts broken pseudo-c compiler */
162
147typedef struct ev_watcher *W; 163typedef struct ev_watcher *W;
148typedef struct ev_watcher_list *WL; 164typedef struct ev_watcher_list *WL;
149typedef struct ev_watcher_time *WT; 165typedef struct ev_watcher_time *WT;
150 166
151static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 167static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
152 168
169#ifdef _WIN32
153#include "ev_win32.c" 170# include "ev_win32.c"
171#endif
154 172
155/*****************************************************************************/ 173/*****************************************************************************/
156 174
157static void (*syserr_cb)(const char *msg); 175static void (*syserr_cb)(const char *msg);
158 176
205typedef struct 223typedef struct
206{ 224{
207 WL head; 225 WL head;
208 unsigned char events; 226 unsigned char events;
209 unsigned char reify; 227 unsigned char reify;
228#if EV_SELECT_IS_WINSOCKET
229 SOCKET handle;
230#endif
210} ANFD; 231} ANFD;
211 232
212typedef struct 233typedef struct
213{ 234{
214 W w; 235 W w;
215 int events; 236 int events;
216} ANPENDING; 237} ANPENDING;
217 238
218#if EV_MULTIPLICITY 239#if EV_MULTIPLICITY
219 240
220struct ev_loop 241 struct ev_loop
221{ 242 {
243 ev_tstamp ev_rt_now;
244 #define ev_rt_now ((loop)->ev_rt_now)
222# define VAR(name,decl) decl; 245 #define VAR(name,decl) decl;
223# include "ev_vars.h" 246 #include "ev_vars.h"
224};
225# undef VAR 247 #undef VAR
248 };
226# include "ev_wrap.h" 249 #include "ev_wrap.h"
250
251 struct ev_loop default_loop_struct;
252 static struct ev_loop *default_loop;
227 253
228#else 254#else
229 255
256 ev_tstamp ev_rt_now;
230# define VAR(name,decl) static decl; 257 #define VAR(name,decl) static decl;
231# include "ev_vars.h" 258 #include "ev_vars.h"
232# undef VAR 259 #undef VAR
260
261 static int default_loop;
233 262
234#endif 263#endif
235 264
236/*****************************************************************************/ 265/*****************************************************************************/
237 266
238inline ev_tstamp 267ev_tstamp
239ev_time (void) 268ev_time (void)
240{ 269{
241#if EV_USE_REALTIME 270#if EV_USE_REALTIME
242 struct timespec ts; 271 struct timespec ts;
243 clock_gettime (CLOCK_REALTIME, &ts); 272 clock_gettime (CLOCK_REALTIME, &ts);
262#endif 291#endif
263 292
264 return ev_time (); 293 return ev_time ();
265} 294}
266 295
296#if EV_MULTIPLICITY
267ev_tstamp 297ev_tstamp
268ev_now (EV_P) 298ev_now (EV_P)
269{ 299{
270 return rt_now; 300 return ev_rt_now;
271} 301}
302#endif
272 303
273#define array_roundsize(type,n) ((n) | 4 & ~3) 304#define array_roundsize(type,n) ((n) | 4 & ~3)
274 305
275#define array_needsize(type,base,cur,cnt,init) \ 306#define array_needsize(type,base,cur,cnt,init) \
276 if (expect_false ((cnt) > cur)) \ 307 if (expect_false ((cnt) > cur)) \
293 stem ## max = array_roundsize (stem ## cnt >> 1); \ 324 stem ## max = array_roundsize (stem ## cnt >> 1); \
294 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ 325 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\
295 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ 326 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
296 } 327 }
297 328
298/* microsoft's pseudo-c is quite far from C as the rest of the world and the standard knows it */
299/* bringing us everlasting joy in form of stupid extra macros that are not required in C */
300#define array_free_microshit(stem) \
301 ev_free (stem ## s); stem ## cnt = stem ## max = 0;
302
303#define array_free(stem, idx) \ 329#define array_free(stem, idx) \
304 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; 330 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
305 331
306/*****************************************************************************/ 332/*****************************************************************************/
307 333
381 int events = 0; 407 int events = 0;
382 408
383 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) 409 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
384 events |= w->events; 410 events |= w->events;
385 411
412#if EV_SELECT_IS_WINSOCKET
413 if (events)
414 {
415 unsigned long argp;
416 anfd->handle = _get_osfhandle (fd);
417 assert (("libev only supports socket fds in this configuration", ioctlsocket (anfd->handle, FIONREAD, &argp) == 0));
418 }
419#endif
420
386 anfd->reify = 0; 421 anfd->reify = 0;
387 422
388 method_modify (EV_A_ fd, anfd->events, events); 423 method_modify (EV_A_ fd, anfd->events, events);
389 anfd->events = events; 424 anfd->events = events;
390 } 425 }
418} 453}
419 454
420static int 455static int
421fd_valid (int fd) 456fd_valid (int fd)
422{ 457{
423#ifdef WIN32 458#ifdef _WIN32
424 return !!win32_get_osfhandle (fd); 459 return _get_osfhandle (fd) != -1;
425#else 460#else
426 return fcntl (fd, F_GETFD) != -1; 461 return fcntl (fd, F_GETFD) != -1;
427#endif 462#endif
428} 463}
429 464
509 544
510 heap [k] = w; 545 heap [k] = w;
511 ((W)heap [k])->active = k + 1; 546 ((W)heap [k])->active = k + 1;
512} 547}
513 548
549inline void
550adjustheap (WT *heap, int N, int k)
551{
552 upheap (heap, k);
553 downheap (heap, N, k);
554}
555
514/*****************************************************************************/ 556/*****************************************************************************/
515 557
516typedef struct 558typedef struct
517{ 559{
518 WL head; 560 WL head;
539} 581}
540 582
541static void 583static void
542sighandler (int signum) 584sighandler (int signum)
543{ 585{
544#if WIN32 586#if _WIN32
545 signal (signum, sighandler); 587 signal (signum, sighandler);
546#endif 588#endif
547 589
548 signals [signum - 1].gotsig = 1; 590 signals [signum - 1].gotsig = 1;
549 591
550 if (!gotsig) 592 if (!gotsig)
551 { 593 {
552 int old_errno = errno; 594 int old_errno = errno;
553 gotsig = 1; 595 gotsig = 1;
554#ifdef WIN32
555 send (sigpipe [1], &signum, 1, MSG_DONTWAIT);
556#else
557 write (sigpipe [1], &signum, 1); 596 write (sigpipe [1], &signum, 1);
558#endif
559 errno = old_errno; 597 errno = old_errno;
560 } 598 }
561} 599}
562 600
563void 601void
564ev_feed_signal_event (EV_P_ int signum) 602ev_feed_signal_event (EV_P_ int signum)
565{ 603{
604 WL w;
605
566#if EV_MULTIPLICITY 606#if EV_MULTIPLICITY
567 assert (("feeding signal events is only supported in the default loop", loop == default_loop)); 607 assert (("feeding signal events is only supported in the default loop", loop == default_loop));
568#endif 608#endif
569 609
570 --signum; 610 --signum;
579} 619}
580 620
581static void 621static void
582sigcb (EV_P_ struct ev_io *iow, int revents) 622sigcb (EV_P_ struct ev_io *iow, int revents)
583{ 623{
584 WL w;
585 int signum; 624 int signum;
586 625
587#ifdef WIN32
588 recv (sigpipe [0], &revents, 1, MSG_DONTWAIT);
589#else
590 read (sigpipe [0], &revents, 1); 626 read (sigpipe [0], &revents, 1);
591#endif
592 gotsig = 0; 627 gotsig = 0;
593 628
594 for (signum = signalmax; signum--; ) 629 for (signum = signalmax; signum--; )
595 if (signals [signum].gotsig) 630 if (signals [signum].gotsig)
596 sigevent (EV_A_ signum + 1); 631 ev_feed_signal_event (EV_A_ signum + 1);
632}
633
634inline void
635fd_intern (int fd)
636{
637#ifdef _WIN32
638 int arg = 1;
639 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg);
640#else
641 fcntl (fd, F_SETFD, FD_CLOEXEC);
642 fcntl (fd, F_SETFL, O_NONBLOCK);
643#endif
597} 644}
598 645
599static void 646static void
600siginit (EV_P) 647siginit (EV_P)
601{ 648{
602#ifndef WIN32 649 fd_intern (sigpipe [0]);
603 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); 650 fd_intern (sigpipe [1]);
604 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC);
605
606 /* rather than sort out wether we really need nb, set it */
607 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
608 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
609#endif
610 651
611 ev_io_set (&sigev, sigpipe [0], EV_READ); 652 ev_io_set (&sigev, sigpipe [0], EV_READ);
612 ev_io_start (EV_A_ &sigev); 653 ev_io_start (EV_A_ &sigev);
613 ev_unref (EV_A); /* child watcher should not keep loop alive */ 654 ev_unref (EV_A); /* child watcher should not keep loop alive */
614} 655}
615 656
616/*****************************************************************************/ 657/*****************************************************************************/
617 658
618static struct ev_child *childs [PID_HASHSIZE]; 659static struct ev_child *childs [PID_HASHSIZE];
619 660
620#ifndef WIN32 661#ifndef _WIN32
621 662
622static struct ev_signal childev; 663static struct ev_signal childev;
623 664
624#ifndef WCONTINUED 665#ifndef WCONTINUED
625# define WCONTINUED 0 666# define WCONTINUED 0
686 727
687/* return true if we are running with elevated privileges and should ignore env variables */ 728/* return true if we are running with elevated privileges and should ignore env variables */
688static int 729static int
689enable_secure (void) 730enable_secure (void)
690{ 731{
691#ifdef WIN32 732#ifdef _WIN32
692 return 0; 733 return 0;
693#else 734#else
694 return getuid () != geteuid () 735 return getuid () != geteuid ()
695 || getgid () != getegid (); 736 || getgid () != getegid ();
696#endif 737#endif
713 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 754 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
714 have_monotonic = 1; 755 have_monotonic = 1;
715 } 756 }
716#endif 757#endif
717 758
718 rt_now = ev_time (); 759 ev_rt_now = ev_time ();
719 mn_now = get_clock (); 760 mn_now = get_clock ();
720 now_floor = mn_now; 761 now_floor = mn_now;
721 rtmn_diff = rt_now - mn_now; 762 rtmn_diff = ev_rt_now - mn_now;
722 763
723 if (methods == EVMETHOD_AUTO) 764 if (methods == EVMETHOD_AUTO)
724 if (!enable_secure () && getenv ("LIBEV_METHODS")) 765 if (!enable_secure () && getenv ("LIBEV_METHODS"))
725 methods = atoi (getenv ("LIBEV_METHODS")); 766 methods = atoi (getenv ("LIBEV_METHODS"));
726 else 767 else
727 methods = EVMETHOD_ANY; 768 methods = EVMETHOD_ANY;
728 769
729 method = 0; 770 method = 0;
730#if EV_USE_WIN32
731 if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods);
732#endif
733#if EV_USE_KQUEUE 771#if EV_USE_KQUEUE
734 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); 772 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods);
735#endif 773#endif
736#if EV_USE_EPOLL 774#if EV_USE_EPOLL
737 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); 775 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods);
741#endif 779#endif
742#if EV_USE_SELECT 780#if EV_USE_SELECT
743 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 781 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
744#endif 782#endif
745 783
746 ev_watcher_init (&sigev, sigcb); 784 ev_init (&sigev, sigcb);
747 ev_set_priority (&sigev, EV_MAXPRI); 785 ev_set_priority (&sigev, EV_MAXPRI);
748 } 786 }
749} 787}
750 788
751void 789void
752loop_destroy (EV_P) 790loop_destroy (EV_P)
753{ 791{
754 int i; 792 int i;
755 793
756#if EV_USE_WIN32
757 if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A);
758#endif
759#if EV_USE_KQUEUE 794#if EV_USE_KQUEUE
760 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); 795 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
761#endif 796#endif
762#if EV_USE_EPOLL 797#if EV_USE_EPOLL
763 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); 798 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A);
771 806
772 for (i = NUMPRI; i--; ) 807 for (i = NUMPRI; i--; )
773 array_free (pending, [i]); 808 array_free (pending, [i]);
774 809
775 /* have to use the microsoft-never-gets-it-right macro */ 810 /* have to use the microsoft-never-gets-it-right macro */
776 array_free_microshit (fdchange); 811 array_free (fdchange, EMPTY);
777 array_free_microshit (timer); 812 array_free (timer, EMPTY);
778 array_free_microshit (periodic); 813#if EV_PERIODICS
779 array_free_microshit (idle); 814 array_free (periodic, EMPTY);
780 array_free_microshit (prepare); 815#endif
781 array_free_microshit (check); 816 array_free (idle, EMPTY);
817 array_free (prepare, EMPTY);
818 array_free (check, EMPTY);
782 819
783 method = 0; 820 method = 0;
784} 821}
785 822
786static void 823static void
841} 878}
842 879
843#endif 880#endif
844 881
845#if EV_MULTIPLICITY 882#if EV_MULTIPLICITY
846struct ev_loop default_loop_struct;
847static struct ev_loop *default_loop;
848
849struct ev_loop * 883struct ev_loop *
850#else 884#else
851static int default_loop;
852
853int 885int
854#endif 886#endif
855ev_default_loop (int methods) 887ev_default_loop (int methods)
856{ 888{
857 if (sigpipe [0] == sigpipe [1]) 889 if (sigpipe [0] == sigpipe [1])
870 902
871 if (ev_method (EV_A)) 903 if (ev_method (EV_A))
872 { 904 {
873 siginit (EV_A); 905 siginit (EV_A);
874 906
875#ifndef WIN32 907#ifndef _WIN32
876 ev_signal_init (&childev, childcb, SIGCHLD); 908 ev_signal_init (&childev, childcb, SIGCHLD);
877 ev_set_priority (&childev, EV_MAXPRI); 909 ev_set_priority (&childev, EV_MAXPRI);
878 ev_signal_start (EV_A_ &childev); 910 ev_signal_start (EV_A_ &childev);
879 ev_unref (EV_A); /* child watcher should not keep loop alive */ 911 ev_unref (EV_A); /* child watcher should not keep loop alive */
880#endif 912#endif
891{ 923{
892#if EV_MULTIPLICITY 924#if EV_MULTIPLICITY
893 struct ev_loop *loop = default_loop; 925 struct ev_loop *loop = default_loop;
894#endif 926#endif
895 927
896#ifndef WIN32 928#ifndef _WIN32
897 ev_ref (EV_A); /* child watcher */ 929 ev_ref (EV_A); /* child watcher */
898 ev_signal_stop (EV_A_ &childev); 930 ev_signal_stop (EV_A_ &childev);
899#endif 931#endif
900 932
901 ev_ref (EV_A); /* signal watcher */ 933 ev_ref (EV_A); /* signal watcher */
943 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 975 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
944 976
945 if (p->w) 977 if (p->w)
946 { 978 {
947 p->w->pending = 0; 979 p->w->pending = 0;
948 p->w->cb (EV_A_ p->w, p->events); 980 EV_CB_INVOKE (p->w, p->events);
949 } 981 }
950 } 982 }
951} 983}
952 984
953static void 985static void
961 993
962 /* first reschedule or stop timer */ 994 /* first reschedule or stop timer */
963 if (w->repeat) 995 if (w->repeat)
964 { 996 {
965 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); 997 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
998
966 ((WT)w)->at = mn_now + w->repeat; 999 ((WT)w)->at += w->repeat;
1000 if (((WT)w)->at < mn_now)
1001 ((WT)w)->at = mn_now;
1002
967 downheap ((WT *)timers, timercnt, 0); 1003 downheap ((WT *)timers, timercnt, 0);
968 } 1004 }
969 else 1005 else
970 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 1006 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
971 1007
972 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); 1008 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
973 } 1009 }
974} 1010}
975 1011
1012#if EV_PERIODICS
976static void 1013static void
977periodics_reify (EV_P) 1014periodics_reify (EV_P)
978{ 1015{
979 while (periodiccnt && ((WT)periodics [0])->at <= rt_now) 1016 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
980 { 1017 {
981 struct ev_periodic *w = periodics [0]; 1018 struct ev_periodic *w = periodics [0];
982 1019
983 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 1020 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
984 1021
985 /* first reschedule or stop timer */ 1022 /* first reschedule or stop timer */
986 if (w->reschedule_cb) 1023 if (w->reschedule_cb)
987 { 1024 {
988 ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, rt_now + 0.0001); 1025 ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001);
989 1026
990 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > rt_now)); 1027 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now));
991 downheap ((WT *)periodics, periodiccnt, 0); 1028 downheap ((WT *)periodics, periodiccnt, 0);
992 } 1029 }
993 else if (w->interval) 1030 else if (w->interval)
994 { 1031 {
995 ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; 1032 ((WT)w)->at += floor ((ev_rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval;
996 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now)); 1033 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > ev_rt_now));
997 downheap ((WT *)periodics, periodiccnt, 0); 1034 downheap ((WT *)periodics, periodiccnt, 0);
998 } 1035 }
999 else 1036 else
1000 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 1037 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
1001 1038
1012 for (i = 0; i < periodiccnt; ++i) 1049 for (i = 0; i < periodiccnt; ++i)
1013 { 1050 {
1014 struct ev_periodic *w = periodics [i]; 1051 struct ev_periodic *w = periodics [i];
1015 1052
1016 if (w->reschedule_cb) 1053 if (w->reschedule_cb)
1017 ((WT)w)->at = w->reschedule_cb (w, rt_now); 1054 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1018 else if (w->interval) 1055 else if (w->interval)
1019 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; 1056 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1020 } 1057 }
1021 1058
1022 /* now rebuild the heap */ 1059 /* now rebuild the heap */
1023 for (i = periodiccnt >> 1; i--; ) 1060 for (i = periodiccnt >> 1; i--; )
1024 downheap ((WT *)periodics, periodiccnt, i); 1061 downheap ((WT *)periodics, periodiccnt, i);
1025} 1062}
1063#endif
1026 1064
1027inline int 1065inline int
1028time_update_monotonic (EV_P) 1066time_update_monotonic (EV_P)
1029{ 1067{
1030 mn_now = get_clock (); 1068 mn_now = get_clock ();
1031 1069
1032 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) 1070 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
1033 { 1071 {
1034 rt_now = rtmn_diff + mn_now; 1072 ev_rt_now = rtmn_diff + mn_now;
1035 return 0; 1073 return 0;
1036 } 1074 }
1037 else 1075 else
1038 { 1076 {
1039 now_floor = mn_now; 1077 now_floor = mn_now;
1040 rt_now = ev_time (); 1078 ev_rt_now = ev_time ();
1041 return 1; 1079 return 1;
1042 } 1080 }
1043} 1081}
1044 1082
1045static void 1083static void
1054 { 1092 {
1055 ev_tstamp odiff = rtmn_diff; 1093 ev_tstamp odiff = rtmn_diff;
1056 1094
1057 for (i = 4; --i; ) /* loop a few times, before making important decisions */ 1095 for (i = 4; --i; ) /* loop a few times, before making important decisions */
1058 { 1096 {
1059 rtmn_diff = rt_now - mn_now; 1097 rtmn_diff = ev_rt_now - mn_now;
1060 1098
1061 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) 1099 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)
1062 return; /* all is well */ 1100 return; /* all is well */
1063 1101
1064 rt_now = ev_time (); 1102 ev_rt_now = ev_time ();
1065 mn_now = get_clock (); 1103 mn_now = get_clock ();
1066 now_floor = mn_now; 1104 now_floor = mn_now;
1067 } 1105 }
1068 1106
1107# if EV_PERIODICS
1069 periodics_reschedule (EV_A); 1108 periodics_reschedule (EV_A);
1109# endif
1070 /* no timer adjustment, as the monotonic clock doesn't jump */ 1110 /* no timer adjustment, as the monotonic clock doesn't jump */
1071 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ 1111 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */
1072 } 1112 }
1073 } 1113 }
1074 else 1114 else
1075#endif 1115#endif
1076 { 1116 {
1077 rt_now = ev_time (); 1117 ev_rt_now = ev_time ();
1078 1118
1079 if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) 1119 if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
1080 { 1120 {
1121#if EV_PERIODICS
1081 periodics_reschedule (EV_A); 1122 periodics_reschedule (EV_A);
1123#endif
1082 1124
1083 /* adjust timers. this is easy, as the offset is the same for all */ 1125 /* adjust timers. this is easy, as the offset is the same for all */
1084 for (i = 0; i < timercnt; ++i) 1126 for (i = 0; i < timercnt; ++i)
1085 ((WT)timers [i])->at += rt_now - mn_now; 1127 ((WT)timers [i])->at += ev_rt_now - mn_now;
1086 } 1128 }
1087 1129
1088 mn_now = rt_now; 1130 mn_now = ev_rt_now;
1089 } 1131 }
1090} 1132}
1091 1133
1092void 1134void
1093ev_ref (EV_P) 1135ev_ref (EV_P)
1133 if (expect_true (have_monotonic)) 1175 if (expect_true (have_monotonic))
1134 time_update_monotonic (EV_A); 1176 time_update_monotonic (EV_A);
1135 else 1177 else
1136#endif 1178#endif
1137 { 1179 {
1138 rt_now = ev_time (); 1180 ev_rt_now = ev_time ();
1139 mn_now = rt_now; 1181 mn_now = ev_rt_now;
1140 } 1182 }
1141 1183
1142 if (flags & EVLOOP_NONBLOCK || idlecnt) 1184 if (flags & EVLOOP_NONBLOCK || idlecnt)
1143 block = 0.; 1185 block = 0.;
1144 else 1186 else
1149 { 1191 {
1150 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; 1192 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge;
1151 if (block > to) block = to; 1193 if (block > to) block = to;
1152 } 1194 }
1153 1195
1196#if EV_PERIODICS
1154 if (periodiccnt) 1197 if (periodiccnt)
1155 { 1198 {
1156 ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge; 1199 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge;
1157 if (block > to) block = to; 1200 if (block > to) block = to;
1158 } 1201 }
1202#endif
1159 1203
1160 if (block < 0.) block = 0.; 1204 if (block < 0.) block = 0.;
1161 } 1205 }
1162 1206
1163 method_poll (EV_A_ block); 1207 method_poll (EV_A_ block);
1164 1208
1165 /* update rt_now, do magic */ 1209 /* update ev_rt_now, do magic */
1166 time_update (EV_A); 1210 time_update (EV_A);
1167 1211
1168 /* queue pending timers and reschedule them */ 1212 /* queue pending timers and reschedule them */
1169 timers_reify (EV_A); /* relative timers called last */ 1213 timers_reify (EV_A); /* relative timers called last */
1214#if EV_PERIODICS
1170 periodics_reify (EV_A); /* absolute timers called first */ 1215 periodics_reify (EV_A); /* absolute timers called first */
1216#endif
1171 1217
1172 /* queue idle watchers unless io or timers are pending */ 1218 /* queue idle watchers unless io or timers are pending */
1173 if (idlecnt && !any_pending (EV_A)) 1219 if (idlecnt && !any_pending (EV_A))
1174 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1220 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
1175 1221
1266{ 1312{
1267 ev_clear_pending (EV_A_ (W)w); 1313 ev_clear_pending (EV_A_ (W)w);
1268 if (!ev_is_active (w)) 1314 if (!ev_is_active (w))
1269 return; 1315 return;
1270 1316
1317 assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax));
1318
1271 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 1319 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
1272 ev_stop (EV_A_ (W)w); 1320 ev_stop (EV_A_ (W)w);
1273 1321
1274 fd_change (EV_A_ w->fd); 1322 fd_change (EV_A_ w->fd);
1275} 1323}
1302 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1350 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1303 1351
1304 if (((W)w)->active < timercnt--) 1352 if (((W)w)->active < timercnt--)
1305 { 1353 {
1306 timers [((W)w)->active - 1] = timers [timercnt]; 1354 timers [((W)w)->active - 1] = timers [timercnt];
1307 downheap ((WT *)timers, timercnt, ((W)w)->active - 1); 1355 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1308 } 1356 }
1309 1357
1310 ((WT)w)->at = w->repeat; 1358 ((WT)w)->at -= mn_now;
1311 1359
1312 ev_stop (EV_A_ (W)w); 1360 ev_stop (EV_A_ (W)w);
1313} 1361}
1314 1362
1315void 1363void
1318 if (ev_is_active (w)) 1366 if (ev_is_active (w))
1319 { 1367 {
1320 if (w->repeat) 1368 if (w->repeat)
1321 { 1369 {
1322 ((WT)w)->at = mn_now + w->repeat; 1370 ((WT)w)->at = mn_now + w->repeat;
1323 downheap ((WT *)timers, timercnt, ((W)w)->active - 1); 1371 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1324 } 1372 }
1325 else 1373 else
1326 ev_timer_stop (EV_A_ w); 1374 ev_timer_stop (EV_A_ w);
1327 } 1375 }
1328 else if (w->repeat) 1376 else if (w->repeat)
1329 ev_timer_start (EV_A_ w); 1377 ev_timer_start (EV_A_ w);
1330} 1378}
1331 1379
1380#if EV_PERIODICS
1332void 1381void
1333ev_periodic_start (EV_P_ struct ev_periodic *w) 1382ev_periodic_start (EV_P_ struct ev_periodic *w)
1334{ 1383{
1335 if (ev_is_active (w)) 1384 if (ev_is_active (w))
1336 return; 1385 return;
1337 1386
1338 if (w->reschedule_cb) 1387 if (w->reschedule_cb)
1339 ((WT)w)->at = w->reschedule_cb (w, rt_now); 1388 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1340 else if (w->interval) 1389 else if (w->interval)
1341 { 1390 {
1342 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); 1391 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
1343 /* this formula differs from the one in periodic_reify because we do not always round up */ 1392 /* this formula differs from the one in periodic_reify because we do not always round up */
1344 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; 1393 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1345 } 1394 }
1346 1395
1347 ev_start (EV_A_ (W)w, ++periodiccnt); 1396 ev_start (EV_A_ (W)w, ++periodiccnt);
1348 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); 1397 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void));
1349 periodics [periodiccnt - 1] = w; 1398 periodics [periodiccnt - 1] = w;
1362 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1411 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1363 1412
1364 if (((W)w)->active < periodiccnt--) 1413 if (((W)w)->active < periodiccnt--)
1365 { 1414 {
1366 periodics [((W)w)->active - 1] = periodics [periodiccnt]; 1415 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1367 downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); 1416 adjustheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1368 } 1417 }
1369 1418
1370 ev_stop (EV_A_ (W)w); 1419 ev_stop (EV_A_ (W)w);
1371} 1420}
1372 1421
1373void 1422void
1374ev_periodic_again (EV_P_ struct ev_periodic *w) 1423ev_periodic_again (EV_P_ struct ev_periodic *w)
1375{ 1424{
1425 /* TODO: use adjustheap and recalculation */
1376 ev_periodic_stop (EV_A_ w); 1426 ev_periodic_stop (EV_A_ w);
1377 ev_periodic_start (EV_A_ w); 1427 ev_periodic_start (EV_A_ w);
1378} 1428}
1429#endif
1379 1430
1380void 1431void
1381ev_idle_start (EV_P_ struct ev_idle *w) 1432ev_idle_start (EV_P_ struct ev_idle *w)
1382{ 1433{
1383 if (ev_is_active (w)) 1434 if (ev_is_active (w))
1390 1441
1391void 1442void
1392ev_idle_stop (EV_P_ struct ev_idle *w) 1443ev_idle_stop (EV_P_ struct ev_idle *w)
1393{ 1444{
1394 ev_clear_pending (EV_A_ (W)w); 1445 ev_clear_pending (EV_A_ (W)w);
1395 if (ev_is_active (w)) 1446 if (!ev_is_active (w))
1396 return; 1447 return;
1397 1448
1398 idles [((W)w)->active - 1] = idles [--idlecnt]; 1449 idles [((W)w)->active - 1] = idles [--idlecnt];
1399 ev_stop (EV_A_ (W)w); 1450 ev_stop (EV_A_ (W)w);
1400} 1451}
1412 1463
1413void 1464void
1414ev_prepare_stop (EV_P_ struct ev_prepare *w) 1465ev_prepare_stop (EV_P_ struct ev_prepare *w)
1415{ 1466{
1416 ev_clear_pending (EV_A_ (W)w); 1467 ev_clear_pending (EV_A_ (W)w);
1417 if (ev_is_active (w)) 1468 if (!ev_is_active (w))
1418 return; 1469 return;
1419 1470
1420 prepares [((W)w)->active - 1] = prepares [--preparecnt]; 1471 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1421 ev_stop (EV_A_ (W)w); 1472 ev_stop (EV_A_ (W)w);
1422} 1473}
1434 1485
1435void 1486void
1436ev_check_stop (EV_P_ struct ev_check *w) 1487ev_check_stop (EV_P_ struct ev_check *w)
1437{ 1488{
1438 ev_clear_pending (EV_A_ (W)w); 1489 ev_clear_pending (EV_A_ (W)w);
1439 if (ev_is_active (w)) 1490 if (!ev_is_active (w))
1440 return; 1491 return;
1441 1492
1442 checks [((W)w)->active - 1] = checks [--checkcnt]; 1493 checks [((W)w)->active - 1] = checks [--checkcnt];
1443 ev_stop (EV_A_ (W)w); 1494 ev_stop (EV_A_ (W)w);
1444} 1495}
1462 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init); 1513 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init);
1463 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1514 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1464 1515
1465 if (!((WL)w)->next) 1516 if (!((WL)w)->next)
1466 { 1517 {
1467#if WIN32 1518#if _WIN32
1468 signal (w->signum, sighandler); 1519 signal (w->signum, sighandler);
1469#else 1520#else
1470 struct sigaction sa; 1521 struct sigaction sa;
1471 sa.sa_handler = sighandler; 1522 sa.sa_handler = sighandler;
1472 sigfillset (&sa.sa_mask); 1523 sigfillset (&sa.sa_mask);
1505 1556
1506void 1557void
1507ev_child_stop (EV_P_ struct ev_child *w) 1558ev_child_stop (EV_P_ struct ev_child *w)
1508{ 1559{
1509 ev_clear_pending (EV_A_ (W)w); 1560 ev_clear_pending (EV_A_ (W)w);
1510 if (ev_is_active (w)) 1561 if (!ev_is_active (w))
1511 return; 1562 return;
1512 1563
1513 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1564 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1514 ev_stop (EV_A_ (W)w); 1565 ev_stop (EV_A_ (W)w);
1515} 1566}
1559 else 1610 else
1560 { 1611 {
1561 once->cb = cb; 1612 once->cb = cb;
1562 once->arg = arg; 1613 once->arg = arg;
1563 1614
1564 ev_watcher_init (&once->io, once_cb_io); 1615 ev_init (&once->io, once_cb_io);
1565 if (fd >= 0) 1616 if (fd >= 0)
1566 { 1617 {
1567 ev_io_set (&once->io, fd, events); 1618 ev_io_set (&once->io, fd, events);
1568 ev_io_start (EV_A_ &once->io); 1619 ev_io_start (EV_A_ &once->io);
1569 } 1620 }
1570 1621
1571 ev_watcher_init (&once->to, once_cb_to); 1622 ev_init (&once->to, once_cb_to);
1572 if (timeout >= 0.) 1623 if (timeout >= 0.)
1573 { 1624 {
1574 ev_timer_set (&once->to, timeout, 0.); 1625 ev_timer_set (&once->to, timeout, 0.);
1575 ev_timer_start (EV_A_ &once->to); 1626 ev_timer_start (EV_A_ &once->to);
1576 } 1627 }
1577 } 1628 }
1578} 1629}
1579 1630
1631#ifdef __cplusplus
1632}
1633#endif
1634

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines