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

Comparing libev/ev.c (file contents):
Revision 1.88 by root, Sat Nov 10 04:40:27 2007 UTC vs.
Revision 1.104 by root, Mon Nov 12 00:39:45 2007 UTC

35 35
36#ifndef EV_STANDALONE 36#ifndef EV_STANDALONE
37# include "config.h" 37# include "config.h"
38 38
39# if HAVE_CLOCK_GETTIME 39# if HAVE_CLOCK_GETTIME
40# ifndef EV_USE_MONOTONIC
40# define EV_USE_MONOTONIC 1 41# define EV_USE_MONOTONIC 1
42# endif
43# ifndef EV_USE_REALTIME
41# define EV_USE_REALTIME 1 44# define EV_USE_REALTIME 1
45# endif
42# endif 46# endif
43 47
44# if HAVE_SELECT && HAVE_SYS_SELECT_H 48# if HAVE_SELECT && HAVE_SYS_SELECT_H && !defined (EV_USE_SELECT)
45# define EV_USE_SELECT 1 49# define EV_USE_SELECT 1
46# endif 50# endif
47 51
48# if HAVE_POLL && HAVE_POLL_H 52# if HAVE_POLL && HAVE_POLL_H && !defined (EV_USE_POLL)
49# define EV_USE_POLL 1 53# define EV_USE_POLL 1
50# endif 54# endif
51 55
52# if HAVE_EPOLL && HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H 56# if HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H && !defined (EV_USE_EPOLL)
53# define EV_USE_EPOLL 1 57# define EV_USE_EPOLL 1
54# endif 58# endif
55 59
56# if HAVE_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H 60# if HAVE_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H && !defined (EV_USE_KQUEUE)
57# define EV_USE_KQUEUE 1 61# define EV_USE_KQUEUE 1
58# endif 62# endif
59 63
60#endif 64#endif
61 65
71#include <sys/types.h> 75#include <sys/types.h>
72#include <time.h> 76#include <time.h>
73 77
74#include <signal.h> 78#include <signal.h>
75 79
76#ifndef WIN32 80#ifndef _WIN32
77# include <unistd.h> 81# include <unistd.h>
78# include <sys/time.h> 82# include <sys/time.h>
79# include <sys/wait.h> 83# include <sys/wait.h>
84#else
85# define WIN32_LEAN_AND_MEAN
86# include <windows.h>
87# ifndef EV_SELECT_IS_WINSOCKET
88# define EV_SELECT_IS_WINSOCKET 1
80#endif 89# endif
90#endif
91
81/**/ 92/**/
82 93
83#ifndef EV_USE_MONOTONIC 94#ifndef EV_USE_MONOTONIC
84# define EV_USE_MONOTONIC 1 95# define EV_USE_MONOTONIC 1
85#endif 96#endif
86 97
87#ifndef EV_USE_SELECT 98#ifndef EV_USE_SELECT
88# define EV_USE_SELECT 1 99# define EV_USE_SELECT 1
100# define EV_SELECT_USE_FD_SET 1
89#endif 101#endif
90 102
91#ifndef EV_USE_POLL 103#ifndef EV_USE_POLL
92# define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */ 104# ifdef _WIN32
105# define EV_USE_POLL 0
106# else
107# define EV_USE_POLL 1
108# endif
93#endif 109#endif
94 110
95#ifndef EV_USE_EPOLL 111#ifndef EV_USE_EPOLL
96# define EV_USE_EPOLL 0 112# define EV_USE_EPOLL 0
97#endif 113#endif
98 114
99#ifndef EV_USE_KQUEUE 115#ifndef EV_USE_KQUEUE
100# define EV_USE_KQUEUE 0 116# define EV_USE_KQUEUE 0
101#endif
102
103#ifndef EV_USE_WIN32
104# ifdef WIN32
105# define EV_USE_WIN32 0 /* it does not exist, use select */
106# undef EV_USE_SELECT
107# define EV_USE_SELECT 1
108# else
109# define EV_USE_WIN32 0
110# endif
111#endif 117#endif
112 118
113#ifndef EV_USE_REALTIME 119#ifndef EV_USE_REALTIME
114# define EV_USE_REALTIME 1 120# define EV_USE_REALTIME 1
115#endif 121#endif
122#endif 128#endif
123 129
124#ifndef CLOCK_REALTIME 130#ifndef CLOCK_REALTIME
125# undef EV_USE_REALTIME 131# undef EV_USE_REALTIME
126# define EV_USE_REALTIME 0 132# define EV_USE_REALTIME 0
133#endif
134
135#if EV_SELECT_IS_WINSOCKET
136# include <winsock.h>
127#endif 137#endif
128 138
129/**/ 139/**/
130 140
131#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 141#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
151#define expect_true(expr) expect ((expr) != 0, 1) 161#define expect_true(expr) expect ((expr) != 0, 1)
152 162
153#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) 163#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
154#define ABSPRI(w) ((w)->priority - EV_MINPRI) 164#define ABSPRI(w) ((w)->priority - EV_MINPRI)
155 165
166#define EMPTY /* required for microsofts broken pseudo-c compiler */
167
156typedef struct ev_watcher *W; 168typedef struct ev_watcher *W;
157typedef struct ev_watcher_list *WL; 169typedef struct ev_watcher_list *WL;
158typedef struct ev_watcher_time *WT; 170typedef struct ev_watcher_time *WT;
159 171
160static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 172static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
161 173
174#ifdef _WIN32
162#include "ev_win32.c" 175# include "ev_win32.c"
176#endif
163 177
164/*****************************************************************************/ 178/*****************************************************************************/
165 179
166static void (*syserr_cb)(const char *msg); 180static void (*syserr_cb)(const char *msg);
167 181
214typedef struct 228typedef struct
215{ 229{
216 WL head; 230 WL head;
217 unsigned char events; 231 unsigned char events;
218 unsigned char reify; 232 unsigned char reify;
233#if EV_SELECT_IS_WINSOCKET
234 SOCKET handle;
235#endif
219} ANFD; 236} ANFD;
220 237
221typedef struct 238typedef struct
222{ 239{
223 W w; 240 W w;
227#if EV_MULTIPLICITY 244#if EV_MULTIPLICITY
228 245
229 struct ev_loop 246 struct ev_loop
230 { 247 {
231 ev_tstamp ev_rt_now; 248 ev_tstamp ev_rt_now;
249 #define ev_rt_now ((loop)->ev_rt_now)
232 #define VAR(name,decl) decl; 250 #define VAR(name,decl) decl;
233 #include "ev_vars.h" 251 #include "ev_vars.h"
234 #undef VAR 252 #undef VAR
235 }; 253 };
236 #include "ev_wrap.h" 254 #include "ev_wrap.h"
249 267
250#endif 268#endif
251 269
252/*****************************************************************************/ 270/*****************************************************************************/
253 271
254inline ev_tstamp 272ev_tstamp
255ev_time (void) 273ev_time (void)
256{ 274{
257#if EV_USE_REALTIME 275#if EV_USE_REALTIME
258 struct timespec ts; 276 struct timespec ts;
259 clock_gettime (CLOCK_REALTIME, &ts); 277 clock_gettime (CLOCK_REALTIME, &ts);
311 stem ## max = array_roundsize (stem ## cnt >> 1); \ 329 stem ## max = array_roundsize (stem ## cnt >> 1); \
312 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ 330 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\
313 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ 331 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
314 } 332 }
315 333
316/* microsoft's pseudo-c is quite far from C as the rest of the world and the standard knows it */
317/* bringing us everlasting joy in form of stupid extra macros that are not required in C */
318#define array_free_microshit(stem) \
319 ev_free (stem ## s); stem ## cnt = stem ## max = 0;
320
321#define array_free(stem, idx) \ 334#define array_free(stem, idx) \
322 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; 335 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
323 336
324/*****************************************************************************/ 337/*****************************************************************************/
325 338
399 int events = 0; 412 int events = 0;
400 413
401 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) 414 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
402 events |= w->events; 415 events |= w->events;
403 416
417#if EV_SELECT_IS_WINSOCKET
418 if (events)
419 {
420 unsigned long argp;
421 anfd->handle = _get_osfhandle (fd);
422 assert (("libev only supports socket fds in this configuration", ioctlsocket (anfd->handle, FIONREAD, &argp) == 0));
423 }
424#endif
425
404 anfd->reify = 0; 426 anfd->reify = 0;
405 427
406 method_modify (EV_A_ fd, anfd->events, events); 428 method_modify (EV_A_ fd, anfd->events, events);
407 anfd->events = events; 429 anfd->events = events;
408 } 430 }
436} 458}
437 459
438static int 460static int
439fd_valid (int fd) 461fd_valid (int fd)
440{ 462{
441#ifdef WIN32 463#ifdef _WIN32
442 return !!win32_get_osfhandle (fd); 464 return _get_osfhandle (fd) != -1;
443#else 465#else
444 return fcntl (fd, F_GETFD) != -1; 466 return fcntl (fd, F_GETFD) != -1;
445#endif 467#endif
446} 468}
447 469
528 heap [k] = w; 550 heap [k] = w;
529 ((W)heap [k])->active = k + 1; 551 ((W)heap [k])->active = k + 1;
530} 552}
531 553
532inline void 554inline void
533adjustheap (WT *heap, int N, int k, ev_tstamp at) 555adjustheap (WT *heap, int N, int k)
534{ 556{
535 ev_tstamp old_at = heap [k]->at; 557 upheap (heap, k);
536 heap [k]->at = at;
537
538 if (old_at < at)
539 downheap (heap, N, k); 558 downheap (heap, N, k);
540 else
541 upheap (heap, k);
542} 559}
543 560
544/*****************************************************************************/ 561/*****************************************************************************/
545 562
546typedef struct 563typedef struct
569} 586}
570 587
571static void 588static void
572sighandler (int signum) 589sighandler (int signum)
573{ 590{
574#if WIN32 591#if _WIN32
575 signal (signum, sighandler); 592 signal (signum, sighandler);
576#endif 593#endif
577 594
578 signals [signum - 1].gotsig = 1; 595 signals [signum - 1].gotsig = 1;
579 596
580 if (!gotsig) 597 if (!gotsig)
581 { 598 {
582 int old_errno = errno; 599 int old_errno = errno;
583 gotsig = 1; 600 gotsig = 1;
584#ifdef WIN32
585 send (sigpipe [1], &signum, 1, MSG_DONTWAIT);
586#else
587 write (sigpipe [1], &signum, 1); 601 write (sigpipe [1], &signum, 1);
588#endif
589 errno = old_errno; 602 errno = old_errno;
590 } 603 }
591} 604}
592 605
593void 606void
613static void 626static void
614sigcb (EV_P_ struct ev_io *iow, int revents) 627sigcb (EV_P_ struct ev_io *iow, int revents)
615{ 628{
616 int signum; 629 int signum;
617 630
618#ifdef WIN32
619 recv (sigpipe [0], &revents, 1, MSG_DONTWAIT);
620#else
621 read (sigpipe [0], &revents, 1); 631 read (sigpipe [0], &revents, 1);
622#endif
623 gotsig = 0; 632 gotsig = 0;
624 633
625 for (signum = signalmax; signum--; ) 634 for (signum = signalmax; signum--; )
626 if (signals [signum].gotsig) 635 if (signals [signum].gotsig)
627 ev_feed_signal_event (EV_A_ signum + 1); 636 ev_feed_signal_event (EV_A_ signum + 1);
628} 637}
629 638
639inline void
640fd_intern (int fd)
641{
642#ifdef _WIN32
643 int arg = 1;
644 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg);
645#else
646 fcntl (fd, F_SETFD, FD_CLOEXEC);
647 fcntl (fd, F_SETFL, O_NONBLOCK);
648#endif
649}
650
630static void 651static void
631siginit (EV_P) 652siginit (EV_P)
632{ 653{
633#ifndef WIN32 654 fd_intern (sigpipe [0]);
634 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); 655 fd_intern (sigpipe [1]);
635 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC);
636
637 /* rather than sort out wether we really need nb, set it */
638 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
639 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
640#endif
641 656
642 ev_io_set (&sigev, sigpipe [0], EV_READ); 657 ev_io_set (&sigev, sigpipe [0], EV_READ);
643 ev_io_start (EV_A_ &sigev); 658 ev_io_start (EV_A_ &sigev);
644 ev_unref (EV_A); /* child watcher should not keep loop alive */ 659 ev_unref (EV_A); /* child watcher should not keep loop alive */
645} 660}
646 661
647/*****************************************************************************/ 662/*****************************************************************************/
648 663
649static struct ev_child *childs [PID_HASHSIZE]; 664static struct ev_child *childs [PID_HASHSIZE];
650 665
651#ifndef WIN32 666#ifndef _WIN32
652 667
653static struct ev_signal childev; 668static struct ev_signal childev;
654 669
655#ifndef WCONTINUED 670#ifndef WCONTINUED
656# define WCONTINUED 0 671# define WCONTINUED 0
717 732
718/* return true if we are running with elevated privileges and should ignore env variables */ 733/* return true if we are running with elevated privileges and should ignore env variables */
719static int 734static int
720enable_secure (void) 735enable_secure (void)
721{ 736{
722#ifdef WIN32 737#ifdef _WIN32
723 return 0; 738 return 0;
724#else 739#else
725 return getuid () != geteuid () 740 return getuid () != geteuid ()
726 || getgid () != getegid (); 741 || getgid () != getegid ();
727#endif 742#endif
756 methods = atoi (getenv ("LIBEV_METHODS")); 771 methods = atoi (getenv ("LIBEV_METHODS"));
757 else 772 else
758 methods = EVMETHOD_ANY; 773 methods = EVMETHOD_ANY;
759 774
760 method = 0; 775 method = 0;
761#if EV_USE_WIN32
762 if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods);
763#endif
764#if EV_USE_KQUEUE 776#if EV_USE_KQUEUE
765 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); 777 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods);
766#endif 778#endif
767#if EV_USE_EPOLL 779#if EV_USE_EPOLL
768 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); 780 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods);
782void 794void
783loop_destroy (EV_P) 795loop_destroy (EV_P)
784{ 796{
785 int i; 797 int i;
786 798
787#if EV_USE_WIN32
788 if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A);
789#endif
790#if EV_USE_KQUEUE 799#if EV_USE_KQUEUE
791 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); 800 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
792#endif 801#endif
793#if EV_USE_EPOLL 802#if EV_USE_EPOLL
794 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); 803 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A);
802 811
803 for (i = NUMPRI; i--; ) 812 for (i = NUMPRI; i--; )
804 array_free (pending, [i]); 813 array_free (pending, [i]);
805 814
806 /* have to use the microsoft-never-gets-it-right macro */ 815 /* have to use the microsoft-never-gets-it-right macro */
807 array_free_microshit (fdchange); 816 array_free (fdchange, EMPTY);
808 array_free_microshit (timer); 817 array_free (timer, EMPTY);
809 array_free_microshit (periodic); 818#if EV_PERIODICS
810 array_free_microshit (idle); 819 array_free (periodic, EMPTY);
811 array_free_microshit (prepare); 820#endif
812 array_free_microshit (check); 821 array_free (idle, EMPTY);
822 array_free (prepare, EMPTY);
823 array_free (check, EMPTY);
813 824
814 method = 0; 825 method = 0;
815} 826}
816 827
817static void 828static void
896 907
897 if (ev_method (EV_A)) 908 if (ev_method (EV_A))
898 { 909 {
899 siginit (EV_A); 910 siginit (EV_A);
900 911
901#ifndef WIN32 912#ifndef _WIN32
902 ev_signal_init (&childev, childcb, SIGCHLD); 913 ev_signal_init (&childev, childcb, SIGCHLD);
903 ev_set_priority (&childev, EV_MAXPRI); 914 ev_set_priority (&childev, EV_MAXPRI);
904 ev_signal_start (EV_A_ &childev); 915 ev_signal_start (EV_A_ &childev);
905 ev_unref (EV_A); /* child watcher should not keep loop alive */ 916 ev_unref (EV_A); /* child watcher should not keep loop alive */
906#endif 917#endif
917{ 928{
918#if EV_MULTIPLICITY 929#if EV_MULTIPLICITY
919 struct ev_loop *loop = default_loop; 930 struct ev_loop *loop = default_loop;
920#endif 931#endif
921 932
922#ifndef WIN32 933#ifndef _WIN32
923 ev_ref (EV_A); /* child watcher */ 934 ev_ref (EV_A); /* child watcher */
924 ev_signal_stop (EV_A_ &childev); 935 ev_signal_stop (EV_A_ &childev);
925#endif 936#endif
926 937
927 ev_ref (EV_A); /* signal watcher */ 938 ev_ref (EV_A); /* signal watcher */
987 998
988 /* first reschedule or stop timer */ 999 /* first reschedule or stop timer */
989 if (w->repeat) 1000 if (w->repeat)
990 { 1001 {
991 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); 1002 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
1003
992 ((WT)w)->at = mn_now + w->repeat; 1004 ((WT)w)->at += w->repeat;
1005 if (((WT)w)->at < mn_now)
1006 ((WT)w)->at = mn_now;
1007
993 downheap ((WT *)timers, timercnt, 0); 1008 downheap ((WT *)timers, timercnt, 0);
994 } 1009 }
995 else 1010 else
996 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 1011 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
997 1012
998 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); 1013 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
999 } 1014 }
1000} 1015}
1001 1016
1017#if EV_PERIODICS
1002static void 1018static void
1003periodics_reify (EV_P) 1019periodics_reify (EV_P)
1004{ 1020{
1005 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) 1021 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
1006 { 1022 {
1047 1063
1048 /* now rebuild the heap */ 1064 /* now rebuild the heap */
1049 for (i = periodiccnt >> 1; i--; ) 1065 for (i = periodiccnt >> 1; i--; )
1050 downheap ((WT *)periodics, periodiccnt, i); 1066 downheap ((WT *)periodics, periodiccnt, i);
1051} 1067}
1068#endif
1052 1069
1053inline int 1070inline int
1054time_update_monotonic (EV_P) 1071time_update_monotonic (EV_P)
1055{ 1072{
1056 mn_now = get_clock (); 1073 mn_now = get_clock ();
1090 ev_rt_now = ev_time (); 1107 ev_rt_now = ev_time ();
1091 mn_now = get_clock (); 1108 mn_now = get_clock ();
1092 now_floor = mn_now; 1109 now_floor = mn_now;
1093 } 1110 }
1094 1111
1112# if EV_PERIODICS
1095 periodics_reschedule (EV_A); 1113 periodics_reschedule (EV_A);
1114# endif
1096 /* no timer adjustment, as the monotonic clock doesn't jump */ 1115 /* no timer adjustment, as the monotonic clock doesn't jump */
1097 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ 1116 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */
1098 } 1117 }
1099 } 1118 }
1100 else 1119 else
1102 { 1121 {
1103 ev_rt_now = ev_time (); 1122 ev_rt_now = ev_time ();
1104 1123
1105 if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) 1124 if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
1106 { 1125 {
1126#if EV_PERIODICS
1107 periodics_reschedule (EV_A); 1127 periodics_reschedule (EV_A);
1128#endif
1108 1129
1109 /* adjust timers. this is easy, as the offset is the same for all */ 1130 /* adjust timers. this is easy, as the offset is the same for all */
1110 for (i = 0; i < timercnt; ++i) 1131 for (i = 0; i < timercnt; ++i)
1111 ((WT)timers [i])->at += ev_rt_now - mn_now; 1132 ((WT)timers [i])->at += ev_rt_now - mn_now;
1112 } 1133 }
1175 { 1196 {
1176 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; 1197 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge;
1177 if (block > to) block = to; 1198 if (block > to) block = to;
1178 } 1199 }
1179 1200
1201#if EV_PERIODICS
1180 if (periodiccnt) 1202 if (periodiccnt)
1181 { 1203 {
1182 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge; 1204 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge;
1183 if (block > to) block = to; 1205 if (block > to) block = to;
1184 } 1206 }
1207#endif
1185 1208
1186 if (block < 0.) block = 0.; 1209 if (block < 0.) block = 0.;
1187 } 1210 }
1188 1211
1189 method_poll (EV_A_ block); 1212 method_poll (EV_A_ block);
1191 /* update ev_rt_now, do magic */ 1214 /* update ev_rt_now, do magic */
1192 time_update (EV_A); 1215 time_update (EV_A);
1193 1216
1194 /* queue pending timers and reschedule them */ 1217 /* queue pending timers and reschedule them */
1195 timers_reify (EV_A); /* relative timers called last */ 1218 timers_reify (EV_A); /* relative timers called last */
1219#if EV_PERIODICS
1196 periodics_reify (EV_A); /* absolute timers called first */ 1220 periodics_reify (EV_A); /* absolute timers called first */
1221#endif
1197 1222
1198 /* queue idle watchers unless io or timers are pending */ 1223 /* queue idle watchers unless io or timers are pending */
1199 if (idlecnt && !any_pending (EV_A)) 1224 if (idlecnt && !any_pending (EV_A))
1200 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1225 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
1201 1226
1292{ 1317{
1293 ev_clear_pending (EV_A_ (W)w); 1318 ev_clear_pending (EV_A_ (W)w);
1294 if (!ev_is_active (w)) 1319 if (!ev_is_active (w))
1295 return; 1320 return;
1296 1321
1322 assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax));
1323
1297 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 1324 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
1298 ev_stop (EV_A_ (W)w); 1325 ev_stop (EV_A_ (W)w);
1299 1326
1300 fd_change (EV_A_ w->fd); 1327 fd_change (EV_A_ w->fd);
1301} 1328}
1328 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1355 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1329 1356
1330 if (((W)w)->active < timercnt--) 1357 if (((W)w)->active < timercnt--)
1331 { 1358 {
1332 timers [((W)w)->active - 1] = timers [timercnt]; 1359 timers [((W)w)->active - 1] = timers [timercnt];
1333 downheap ((WT *)timers, timercnt, ((W)w)->active - 1); 1360 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1334 } 1361 }
1335 1362
1336 ((WT)w)->at = w->repeat; 1363 ((WT)w)->at -= mn_now;
1337 1364
1338 ev_stop (EV_A_ (W)w); 1365 ev_stop (EV_A_ (W)w);
1339} 1366}
1340 1367
1341void 1368void
1342ev_timer_again (EV_P_ struct ev_timer *w) 1369ev_timer_again (EV_P_ struct ev_timer *w)
1343{ 1370{
1344 if (ev_is_active (w)) 1371 if (ev_is_active (w))
1345 { 1372 {
1346 if (w->repeat) 1373 if (w->repeat)
1374 {
1375 ((WT)w)->at = mn_now + w->repeat;
1347 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1, mn_now + w->repeat); 1376 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1377 }
1348 else 1378 else
1349 ev_timer_stop (EV_A_ w); 1379 ev_timer_stop (EV_A_ w);
1350 } 1380 }
1351 else if (w->repeat) 1381 else if (w->repeat)
1352 ev_timer_start (EV_A_ w); 1382 ev_timer_start (EV_A_ w);
1353} 1383}
1354 1384
1385#if EV_PERIODICS
1355void 1386void
1356ev_periodic_start (EV_P_ struct ev_periodic *w) 1387ev_periodic_start (EV_P_ struct ev_periodic *w)
1357{ 1388{
1358 if (ev_is_active (w)) 1389 if (ev_is_active (w))
1359 return; 1390 return;
1385 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1416 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1386 1417
1387 if (((W)w)->active < periodiccnt--) 1418 if (((W)w)->active < periodiccnt--)
1388 { 1419 {
1389 periodics [((W)w)->active - 1] = periodics [periodiccnt]; 1420 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1390 downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); 1421 adjustheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1391 } 1422 }
1392 1423
1393 ev_stop (EV_A_ (W)w); 1424 ev_stop (EV_A_ (W)w);
1394} 1425}
1395 1426
1398{ 1429{
1399 /* TODO: use adjustheap and recalculation */ 1430 /* TODO: use adjustheap and recalculation */
1400 ev_periodic_stop (EV_A_ w); 1431 ev_periodic_stop (EV_A_ w);
1401 ev_periodic_start (EV_A_ w); 1432 ev_periodic_start (EV_A_ w);
1402} 1433}
1434#endif
1403 1435
1404void 1436void
1405ev_idle_start (EV_P_ struct ev_idle *w) 1437ev_idle_start (EV_P_ struct ev_idle *w)
1406{ 1438{
1407 if (ev_is_active (w)) 1439 if (ev_is_active (w))
1414 1446
1415void 1447void
1416ev_idle_stop (EV_P_ struct ev_idle *w) 1448ev_idle_stop (EV_P_ struct ev_idle *w)
1417{ 1449{
1418 ev_clear_pending (EV_A_ (W)w); 1450 ev_clear_pending (EV_A_ (W)w);
1419 if (ev_is_active (w)) 1451 if (!ev_is_active (w))
1420 return; 1452 return;
1421 1453
1422 idles [((W)w)->active - 1] = idles [--idlecnt]; 1454 idles [((W)w)->active - 1] = idles [--idlecnt];
1423 ev_stop (EV_A_ (W)w); 1455 ev_stop (EV_A_ (W)w);
1424} 1456}
1436 1468
1437void 1469void
1438ev_prepare_stop (EV_P_ struct ev_prepare *w) 1470ev_prepare_stop (EV_P_ struct ev_prepare *w)
1439{ 1471{
1440 ev_clear_pending (EV_A_ (W)w); 1472 ev_clear_pending (EV_A_ (W)w);
1441 if (ev_is_active (w)) 1473 if (!ev_is_active (w))
1442 return; 1474 return;
1443 1475
1444 prepares [((W)w)->active - 1] = prepares [--preparecnt]; 1476 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1445 ev_stop (EV_A_ (W)w); 1477 ev_stop (EV_A_ (W)w);
1446} 1478}
1458 1490
1459void 1491void
1460ev_check_stop (EV_P_ struct ev_check *w) 1492ev_check_stop (EV_P_ struct ev_check *w)
1461{ 1493{
1462 ev_clear_pending (EV_A_ (W)w); 1494 ev_clear_pending (EV_A_ (W)w);
1463 if (ev_is_active (w)) 1495 if (!ev_is_active (w))
1464 return; 1496 return;
1465 1497
1466 checks [((W)w)->active - 1] = checks [--checkcnt]; 1498 checks [((W)w)->active - 1] = checks [--checkcnt];
1467 ev_stop (EV_A_ (W)w); 1499 ev_stop (EV_A_ (W)w);
1468} 1500}
1486 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init); 1518 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init);
1487 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1519 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1488 1520
1489 if (!((WL)w)->next) 1521 if (!((WL)w)->next)
1490 { 1522 {
1491#if WIN32 1523#if _WIN32
1492 signal (w->signum, sighandler); 1524 signal (w->signum, sighandler);
1493#else 1525#else
1494 struct sigaction sa; 1526 struct sigaction sa;
1495 sa.sa_handler = sighandler; 1527 sa.sa_handler = sighandler;
1496 sigfillset (&sa.sa_mask); 1528 sigfillset (&sa.sa_mask);
1529 1561
1530void 1562void
1531ev_child_stop (EV_P_ struct ev_child *w) 1563ev_child_stop (EV_P_ struct ev_child *w)
1532{ 1564{
1533 ev_clear_pending (EV_A_ (W)w); 1565 ev_clear_pending (EV_A_ (W)w);
1534 if (ev_is_active (w)) 1566 if (!ev_is_active (w))
1535 return; 1567 return;
1536 1568
1537 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1569 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1538 ev_stop (EV_A_ (W)w); 1570 ev_stop (EV_A_ (W)w);
1539} 1571}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines