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

Comparing libev/ev.c (file contents):
Revision 1.87 by root, Sat Nov 10 03:36:15 2007 UTC vs.
Revision 1.107 by root, Mon Nov 12 01:20:25 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_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)
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 117#endif
102 118
103#ifndef EV_USE_WIN32
104# ifdef WIN32
105# define EV_USE_WIN32 0 /* it does not exist, use select */
106# undef EV_USE_SELECT
107# define EV_USE_SELECT 1
108# else
109# define EV_USE_WIN32 0
110# endif
111#endif
112
113#ifndef EV_USE_REALTIME 119#ifndef EV_USE_REALTIME
114# define EV_USE_REALTIME 1 120# define EV_USE_REALTIME 1
115#endif 121#endif
116 122
117/**/ 123/**/
124
125/* darwin simply cannot be helped */
126#ifdef __APPLE__
127# undef EV_USE_POLL
128# undef EV_USE_KQUEUE
129#endif
118 130
119#ifndef CLOCK_MONOTONIC 131#ifndef CLOCK_MONOTONIC
120# undef EV_USE_MONOTONIC 132# undef EV_USE_MONOTONIC
121# define EV_USE_MONOTONIC 0 133# define EV_USE_MONOTONIC 0
122#endif 134#endif
123 135
124#ifndef CLOCK_REALTIME 136#ifndef CLOCK_REALTIME
125# undef EV_USE_REALTIME 137# undef EV_USE_REALTIME
126# define EV_USE_REALTIME 0 138# define EV_USE_REALTIME 0
139#endif
140
141#if EV_SELECT_IS_WINSOCKET
142# include <winsock.h>
127#endif 143#endif
128 144
129/**/ 145/**/
130 146
131#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 147#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
151#define expect_true(expr) expect ((expr) != 0, 1) 167#define expect_true(expr) expect ((expr) != 0, 1)
152 168
153#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) 169#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
154#define ABSPRI(w) ((w)->priority - EV_MINPRI) 170#define ABSPRI(w) ((w)->priority - EV_MINPRI)
155 171
172#define EMPTY /* required for microsofts broken pseudo-c compiler */
173
156typedef struct ev_watcher *W; 174typedef struct ev_watcher *W;
157typedef struct ev_watcher_list *WL; 175typedef struct ev_watcher_list *WL;
158typedef struct ev_watcher_time *WT; 176typedef struct ev_watcher_time *WT;
159 177
160static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 178static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
161 179
180#ifdef _WIN32
162#include "ev_win32.c" 181# include "ev_win32.c"
182#endif
163 183
164/*****************************************************************************/ 184/*****************************************************************************/
165 185
166static void (*syserr_cb)(const char *msg); 186static void (*syserr_cb)(const char *msg);
167 187
214typedef struct 234typedef struct
215{ 235{
216 WL head; 236 WL head;
217 unsigned char events; 237 unsigned char events;
218 unsigned char reify; 238 unsigned char reify;
239#if EV_SELECT_IS_WINSOCKET
240 SOCKET handle;
241#endif
219} ANFD; 242} ANFD;
220 243
221typedef struct 244typedef struct
222{ 245{
223 W w; 246 W w;
227#if EV_MULTIPLICITY 250#if EV_MULTIPLICITY
228 251
229 struct ev_loop 252 struct ev_loop
230 { 253 {
231 ev_tstamp ev_rt_now; 254 ev_tstamp ev_rt_now;
255 #define ev_rt_now ((loop)->ev_rt_now)
232 #define VAR(name,decl) decl; 256 #define VAR(name,decl) decl;
233 #include "ev_vars.h" 257 #include "ev_vars.h"
234 #undef VAR 258 #undef VAR
235 }; 259 };
236 #include "ev_wrap.h" 260 #include "ev_wrap.h"
249 273
250#endif 274#endif
251 275
252/*****************************************************************************/ 276/*****************************************************************************/
253 277
254inline ev_tstamp 278ev_tstamp
255ev_time (void) 279ev_time (void)
256{ 280{
257#if EV_USE_REALTIME 281#if EV_USE_REALTIME
258 struct timespec ts; 282 struct timespec ts;
259 clock_gettime (CLOCK_REALTIME, &ts); 283 clock_gettime (CLOCK_REALTIME, &ts);
311 stem ## max = array_roundsize (stem ## cnt >> 1); \ 335 stem ## max = array_roundsize (stem ## cnt >> 1); \
312 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ 336 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\
313 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ 337 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
314 } 338 }
315 339
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) \ 340#define array_free(stem, idx) \
322 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; 341 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
323 342
324/*****************************************************************************/ 343/*****************************************************************************/
325 344
399 int events = 0; 418 int events = 0;
400 419
401 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) 420 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
402 events |= w->events; 421 events |= w->events;
403 422
423#if EV_SELECT_IS_WINSOCKET
424 if (events)
425 {
426 unsigned long argp;
427 anfd->handle = _get_osfhandle (fd);
428 assert (("libev only supports socket fds in this configuration", ioctlsocket (anfd->handle, FIONREAD, &argp) == 0));
429 }
430#endif
431
404 anfd->reify = 0; 432 anfd->reify = 0;
405 433
406 method_modify (EV_A_ fd, anfd->events, events); 434 method_modify (EV_A_ fd, anfd->events, events);
407 anfd->events = events; 435 anfd->events = events;
408 } 436 }
436} 464}
437 465
438static int 466static int
439fd_valid (int fd) 467fd_valid (int fd)
440{ 468{
441#ifdef WIN32 469#ifdef _WIN32
442 return !!win32_get_osfhandle (fd); 470 return _get_osfhandle (fd) != -1;
443#else 471#else
444 return fcntl (fd, F_GETFD) != -1; 472 return fcntl (fd, F_GETFD) != -1;
445#endif 473#endif
446} 474}
447 475
528 heap [k] = w; 556 heap [k] = w;
529 ((W)heap [k])->active = k + 1; 557 ((W)heap [k])->active = k + 1;
530} 558}
531 559
532inline void 560inline void
533adjustheap (WT *heap, int N, int k, ev_tstamp at) 561adjustheap (WT *heap, int N, int k)
534{ 562{
535 ev_tstamp old_at = heap [k]->at; 563 upheap (heap, k);
536 heap [k]->at = at;
537
538 if (old_at < at)
539 downheap (heap, N, k); 564 downheap (heap, N, k);
540 else
541 upheap (heap, k);
542} 565}
543 566
544/*****************************************************************************/ 567/*****************************************************************************/
545 568
546typedef struct 569typedef struct
569} 592}
570 593
571static void 594static void
572sighandler (int signum) 595sighandler (int signum)
573{ 596{
574#if WIN32 597#if _WIN32
575 signal (signum, sighandler); 598 signal (signum, sighandler);
576#endif 599#endif
577 600
578 signals [signum - 1].gotsig = 1; 601 signals [signum - 1].gotsig = 1;
579 602
580 if (!gotsig) 603 if (!gotsig)
581 { 604 {
582 int old_errno = errno; 605 int old_errno = errno;
583 gotsig = 1; 606 gotsig = 1;
584#ifdef WIN32
585 send (sigpipe [1], &signum, 1, MSG_DONTWAIT);
586#else
587 write (sigpipe [1], &signum, 1); 607 write (sigpipe [1], &signum, 1);
588#endif
589 errno = old_errno; 608 errno = old_errno;
590 } 609 }
591} 610}
592 611
593void 612void
613static void 632static void
614sigcb (EV_P_ struct ev_io *iow, int revents) 633sigcb (EV_P_ struct ev_io *iow, int revents)
615{ 634{
616 int signum; 635 int signum;
617 636
618#ifdef WIN32
619 recv (sigpipe [0], &revents, 1, MSG_DONTWAIT);
620#else
621 read (sigpipe [0], &revents, 1); 637 read (sigpipe [0], &revents, 1);
622#endif
623 gotsig = 0; 638 gotsig = 0;
624 639
625 for (signum = signalmax; signum--; ) 640 for (signum = signalmax; signum--; )
626 if (signals [signum].gotsig) 641 if (signals [signum].gotsig)
627 ev_feed_signal_event (EV_A_ signum + 1); 642 ev_feed_signal_event (EV_A_ signum + 1);
628} 643}
629 644
645inline void
646fd_intern (int fd)
647{
648#ifdef _WIN32
649 int arg = 1;
650 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg);
651#else
652 fcntl (fd, F_SETFD, FD_CLOEXEC);
653 fcntl (fd, F_SETFL, O_NONBLOCK);
654#endif
655}
656
630static void 657static void
631siginit (EV_P) 658siginit (EV_P)
632{ 659{
633#ifndef WIN32 660 fd_intern (sigpipe [0]);
634 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); 661 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 662
642 ev_io_set (&sigev, sigpipe [0], EV_READ); 663 ev_io_set (&sigev, sigpipe [0], EV_READ);
643 ev_io_start (EV_A_ &sigev); 664 ev_io_start (EV_A_ &sigev);
644 ev_unref (EV_A); /* child watcher should not keep loop alive */ 665 ev_unref (EV_A); /* child watcher should not keep loop alive */
645} 666}
646 667
647/*****************************************************************************/ 668/*****************************************************************************/
648 669
649static struct ev_child *childs [PID_HASHSIZE]; 670static struct ev_child *childs [PID_HASHSIZE];
650 671
651#ifndef WIN32 672#ifndef _WIN32
652 673
653static struct ev_signal childev; 674static struct ev_signal childev;
654 675
655#ifndef WCONTINUED 676#ifndef WCONTINUED
656# define WCONTINUED 0 677# define WCONTINUED 0
717 738
718/* return true if we are running with elevated privileges and should ignore env variables */ 739/* return true if we are running with elevated privileges and should ignore env variables */
719static int 740static int
720enable_secure (void) 741enable_secure (void)
721{ 742{
722#ifdef WIN32 743#ifdef _WIN32
723 return 0; 744 return 0;
724#else 745#else
725 return getuid () != geteuid () 746 return getuid () != geteuid ()
726 || getgid () != getegid (); 747 || getgid () != getegid ();
727#endif 748#endif
756 methods = atoi (getenv ("LIBEV_METHODS")); 777 methods = atoi (getenv ("LIBEV_METHODS"));
757 else 778 else
758 methods = EVMETHOD_ANY; 779 methods = EVMETHOD_ANY;
759 780
760 method = 0; 781 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 782#if EV_USE_KQUEUE
765 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); 783 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods);
766#endif 784#endif
767#if EV_USE_EPOLL 785#if EV_USE_EPOLL
768 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); 786 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods);
782void 800void
783loop_destroy (EV_P) 801loop_destroy (EV_P)
784{ 802{
785 int i; 803 int i;
786 804
787#if EV_USE_WIN32
788 if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A);
789#endif
790#if EV_USE_KQUEUE 805#if EV_USE_KQUEUE
791 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); 806 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
792#endif 807#endif
793#if EV_USE_EPOLL 808#if EV_USE_EPOLL
794 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); 809 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A);
802 817
803 for (i = NUMPRI; i--; ) 818 for (i = NUMPRI; i--; )
804 array_free (pending, [i]); 819 array_free (pending, [i]);
805 820
806 /* have to use the microsoft-never-gets-it-right macro */ 821 /* have to use the microsoft-never-gets-it-right macro */
807 array_free_microshit (fdchange); 822 array_free (fdchange, EMPTY);
808 array_free_microshit (timer); 823 array_free (timer, EMPTY);
809 array_free_microshit (periodic); 824#if EV_PERIODICS
810 array_free_microshit (idle); 825 array_free (periodic, EMPTY);
811 array_free_microshit (prepare); 826#endif
812 array_free_microshit (check); 827 array_free (idle, EMPTY);
828 array_free (prepare, EMPTY);
829 array_free (check, EMPTY);
813 830
814 method = 0; 831 method = 0;
815} 832}
816 833
817static void 834static void
896 913
897 if (ev_method (EV_A)) 914 if (ev_method (EV_A))
898 { 915 {
899 siginit (EV_A); 916 siginit (EV_A);
900 917
901#ifndef WIN32 918#ifndef _WIN32
902 ev_signal_init (&childev, childcb, SIGCHLD); 919 ev_signal_init (&childev, childcb, SIGCHLD);
903 ev_set_priority (&childev, EV_MAXPRI); 920 ev_set_priority (&childev, EV_MAXPRI);
904 ev_signal_start (EV_A_ &childev); 921 ev_signal_start (EV_A_ &childev);
905 ev_unref (EV_A); /* child watcher should not keep loop alive */ 922 ev_unref (EV_A); /* child watcher should not keep loop alive */
906#endif 923#endif
917{ 934{
918#if EV_MULTIPLICITY 935#if EV_MULTIPLICITY
919 struct ev_loop *loop = default_loop; 936 struct ev_loop *loop = default_loop;
920#endif 937#endif
921 938
922#ifndef WIN32 939#ifndef _WIN32
923 ev_ref (EV_A); /* child watcher */ 940 ev_ref (EV_A); /* child watcher */
924 ev_signal_stop (EV_A_ &childev); 941 ev_signal_stop (EV_A_ &childev);
925#endif 942#endif
926 943
927 ev_ref (EV_A); /* signal watcher */ 944 ev_ref (EV_A); /* signal watcher */
987 1004
988 /* first reschedule or stop timer */ 1005 /* first reschedule or stop timer */
989 if (w->repeat) 1006 if (w->repeat)
990 { 1007 {
991 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); 1008 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
1009
992 ((WT)w)->at = mn_now + w->repeat; 1010 ((WT)w)->at += w->repeat;
1011 if (((WT)w)->at < mn_now)
1012 ((WT)w)->at = mn_now;
1013
993 downheap ((WT *)timers, timercnt, 0); 1014 downheap ((WT *)timers, timercnt, 0);
994 } 1015 }
995 else 1016 else
996 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 1017 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
997 1018
998 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); 1019 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
999 } 1020 }
1000} 1021}
1001 1022
1023#if EV_PERIODICS
1002static void 1024static void
1003periodics_reify (EV_P) 1025periodics_reify (EV_P)
1004{ 1026{
1005 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) 1027 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
1006 { 1028 {
1047 1069
1048 /* now rebuild the heap */ 1070 /* now rebuild the heap */
1049 for (i = periodiccnt >> 1; i--; ) 1071 for (i = periodiccnt >> 1; i--; )
1050 downheap ((WT *)periodics, periodiccnt, i); 1072 downheap ((WT *)periodics, periodiccnt, i);
1051} 1073}
1074#endif
1052 1075
1053inline int 1076inline int
1054time_update_monotonic (EV_P) 1077time_update_monotonic (EV_P)
1055{ 1078{
1056 mn_now = get_clock (); 1079 mn_now = get_clock ();
1090 ev_rt_now = ev_time (); 1113 ev_rt_now = ev_time ();
1091 mn_now = get_clock (); 1114 mn_now = get_clock ();
1092 now_floor = mn_now; 1115 now_floor = mn_now;
1093 } 1116 }
1094 1117
1118# if EV_PERIODICS
1095 periodics_reschedule (EV_A); 1119 periodics_reschedule (EV_A);
1120# endif
1096 /* no timer adjustment, as the monotonic clock doesn't jump */ 1121 /* no timer adjustment, as the monotonic clock doesn't jump */
1097 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ 1122 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */
1098 } 1123 }
1099 } 1124 }
1100 else 1125 else
1102 { 1127 {
1103 ev_rt_now = ev_time (); 1128 ev_rt_now = ev_time ();
1104 1129
1105 if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) 1130 if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
1106 { 1131 {
1132#if EV_PERIODICS
1107 periodics_reschedule (EV_A); 1133 periodics_reschedule (EV_A);
1134#endif
1108 1135
1109 /* adjust timers. this is easy, as the offset is the same for all */ 1136 /* adjust timers. this is easy, as the offset is the same for all */
1110 for (i = 0; i < timercnt; ++i) 1137 for (i = 0; i < timercnt; ++i)
1111 ((WT)timers [i])->at += ev_rt_now - mn_now; 1138 ((WT)timers [i])->at += ev_rt_now - mn_now;
1112 } 1139 }
1175 { 1202 {
1176 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; 1203 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge;
1177 if (block > to) block = to; 1204 if (block > to) block = to;
1178 } 1205 }
1179 1206
1207#if EV_PERIODICS
1180 if (periodiccnt) 1208 if (periodiccnt)
1181 { 1209 {
1182 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge; 1210 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge;
1183 if (block > to) block = to; 1211 if (block > to) block = to;
1184 } 1212 }
1213#endif
1185 1214
1186 if (block < 0.) block = 0.; 1215 if (block < 0.) block = 0.;
1187 } 1216 }
1188 1217
1189 method_poll (EV_A_ block); 1218 method_poll (EV_A_ block);
1191 /* update ev_rt_now, do magic */ 1220 /* update ev_rt_now, do magic */
1192 time_update (EV_A); 1221 time_update (EV_A);
1193 1222
1194 /* queue pending timers and reschedule them */ 1223 /* queue pending timers and reschedule them */
1195 timers_reify (EV_A); /* relative timers called last */ 1224 timers_reify (EV_A); /* relative timers called last */
1225#if EV_PERIODICS
1196 periodics_reify (EV_A); /* absolute timers called first */ 1226 periodics_reify (EV_A); /* absolute timers called first */
1227#endif
1197 1228
1198 /* queue idle watchers unless io or timers are pending */ 1229 /* queue idle watchers unless io or timers are pending */
1199 if (idlecnt && !any_pending (EV_A)) 1230 if (idlecnt && !any_pending (EV_A))
1200 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1231 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
1201 1232
1292{ 1323{
1293 ev_clear_pending (EV_A_ (W)w); 1324 ev_clear_pending (EV_A_ (W)w);
1294 if (!ev_is_active (w)) 1325 if (!ev_is_active (w))
1295 return; 1326 return;
1296 1327
1328 assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax));
1329
1297 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 1330 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
1298 ev_stop (EV_A_ (W)w); 1331 ev_stop (EV_A_ (W)w);
1299 1332
1300 fd_change (EV_A_ w->fd); 1333 fd_change (EV_A_ w->fd);
1301} 1334}
1328 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1361 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1329 1362
1330 if (((W)w)->active < timercnt--) 1363 if (((W)w)->active < timercnt--)
1331 { 1364 {
1332 timers [((W)w)->active - 1] = timers [timercnt]; 1365 timers [((W)w)->active - 1] = timers [timercnt];
1333 downheap ((WT *)timers, timercnt, ((W)w)->active - 1); 1366 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1334 } 1367 }
1335 1368
1336 ((WT)w)->at = w->repeat; 1369 ((WT)w)->at -= mn_now;
1337 1370
1338 ev_stop (EV_A_ (W)w); 1371 ev_stop (EV_A_ (W)w);
1339} 1372}
1340 1373
1341void 1374void
1342ev_timer_again (EV_P_ struct ev_timer *w) 1375ev_timer_again (EV_P_ struct ev_timer *w)
1343{ 1376{
1344 if (ev_is_active (w)) 1377 if (ev_is_active (w))
1345 { 1378 {
1346 if (w->repeat) 1379 if (w->repeat)
1380 {
1381 ((WT)w)->at = mn_now + w->repeat;
1347 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1, mn_now + w->repeat); 1382 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1383 }
1348 else 1384 else
1349 ev_timer_stop (EV_A_ w); 1385 ev_timer_stop (EV_A_ w);
1350 } 1386 }
1351 else if (w->repeat) 1387 else if (w->repeat)
1352 ev_timer_start (EV_A_ w); 1388 ev_timer_start (EV_A_ w);
1353} 1389}
1354 1390
1391#if EV_PERIODICS
1355void 1392void
1356ev_periodic_start (EV_P_ struct ev_periodic *w) 1393ev_periodic_start (EV_P_ struct ev_periodic *w)
1357{ 1394{
1358 if (ev_is_active (w)) 1395 if (ev_is_active (w))
1359 return; 1396 return;
1385 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1422 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1386 1423
1387 if (((W)w)->active < periodiccnt--) 1424 if (((W)w)->active < periodiccnt--)
1388 { 1425 {
1389 periodics [((W)w)->active - 1] = periodics [periodiccnt]; 1426 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1390 downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); 1427 adjustheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1391 } 1428 }
1392 1429
1393 ev_stop (EV_A_ (W)w); 1430 ev_stop (EV_A_ (W)w);
1394} 1431}
1395 1432
1398{ 1435{
1399 /* TODO: use adjustheap and recalculation */ 1436 /* TODO: use adjustheap and recalculation */
1400 ev_periodic_stop (EV_A_ w); 1437 ev_periodic_stop (EV_A_ w);
1401 ev_periodic_start (EV_A_ w); 1438 ev_periodic_start (EV_A_ w);
1402} 1439}
1440#endif
1403 1441
1404void 1442void
1405ev_idle_start (EV_P_ struct ev_idle *w) 1443ev_idle_start (EV_P_ struct ev_idle *w)
1406{ 1444{
1407 if (ev_is_active (w)) 1445 if (ev_is_active (w))
1414 1452
1415void 1453void
1416ev_idle_stop (EV_P_ struct ev_idle *w) 1454ev_idle_stop (EV_P_ struct ev_idle *w)
1417{ 1455{
1418 ev_clear_pending (EV_A_ (W)w); 1456 ev_clear_pending (EV_A_ (W)w);
1419 if (ev_is_active (w)) 1457 if (!ev_is_active (w))
1420 return; 1458 return;
1421 1459
1422 idles [((W)w)->active - 1] = idles [--idlecnt]; 1460 idles [((W)w)->active - 1] = idles [--idlecnt];
1423 ev_stop (EV_A_ (W)w); 1461 ev_stop (EV_A_ (W)w);
1424} 1462}
1436 1474
1437void 1475void
1438ev_prepare_stop (EV_P_ struct ev_prepare *w) 1476ev_prepare_stop (EV_P_ struct ev_prepare *w)
1439{ 1477{
1440 ev_clear_pending (EV_A_ (W)w); 1478 ev_clear_pending (EV_A_ (W)w);
1441 if (ev_is_active (w)) 1479 if (!ev_is_active (w))
1442 return; 1480 return;
1443 1481
1444 prepares [((W)w)->active - 1] = prepares [--preparecnt]; 1482 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1445 ev_stop (EV_A_ (W)w); 1483 ev_stop (EV_A_ (W)w);
1446} 1484}
1458 1496
1459void 1497void
1460ev_check_stop (EV_P_ struct ev_check *w) 1498ev_check_stop (EV_P_ struct ev_check *w)
1461{ 1499{
1462 ev_clear_pending (EV_A_ (W)w); 1500 ev_clear_pending (EV_A_ (W)w);
1463 if (ev_is_active (w)) 1501 if (!ev_is_active (w))
1464 return; 1502 return;
1465 1503
1466 checks [((W)w)->active - 1] = checks [--checkcnt]; 1504 checks [((W)w)->active - 1] = checks [--checkcnt];
1467 ev_stop (EV_A_ (W)w); 1505 ev_stop (EV_A_ (W)w);
1468} 1506}
1486 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init); 1524 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init);
1487 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1525 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1488 1526
1489 if (!((WL)w)->next) 1527 if (!((WL)w)->next)
1490 { 1528 {
1491#if WIN32 1529#if _WIN32
1492 signal (w->signum, sighandler); 1530 signal (w->signum, sighandler);
1493#else 1531#else
1494 struct sigaction sa; 1532 struct sigaction sa;
1495 sa.sa_handler = sighandler; 1533 sa.sa_handler = sighandler;
1496 sigfillset (&sa.sa_mask); 1534 sigfillset (&sa.sa_mask);
1529 1567
1530void 1568void
1531ev_child_stop (EV_P_ struct ev_child *w) 1569ev_child_stop (EV_P_ struct ev_child *w)
1532{ 1570{
1533 ev_clear_pending (EV_A_ (W)w); 1571 ev_clear_pending (EV_A_ (W)w);
1534 if (ev_is_active (w)) 1572 if (!ev_is_active (w))
1535 return; 1573 return;
1536 1574
1537 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1575 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1538 ev_stop (EV_A_ (W)w); 1576 ev_stop (EV_A_ (W)w);
1539} 1577}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines