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.104 by root, Mon Nov 12 00:39:45 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
81 97
82#ifndef EV_USE_SELECT 98#ifndef EV_USE_SELECT
83# define EV_USE_SELECT 1 99# define EV_USE_SELECT 1
100# define EV_SELECT_USE_FD_SET 1
84#endif 101#endif
85 102
86#ifndef EV_USE_POLL 103#ifndef EV_USE_POLL
87# 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
88#endif 109#endif
89 110
90#ifndef EV_USE_EPOLL 111#ifndef EV_USE_EPOLL
91# define EV_USE_EPOLL 0 112# define EV_USE_EPOLL 0
92#endif 113#endif
93 114
94#ifndef EV_USE_KQUEUE 115#ifndef EV_USE_KQUEUE
95# define EV_USE_KQUEUE 0 116# 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 117#endif
107 118
108#ifndef EV_USE_REALTIME 119#ifndef EV_USE_REALTIME
109# define EV_USE_REALTIME 1 120# define EV_USE_REALTIME 1
110#endif 121#endif
117#endif 128#endif
118 129
119#ifndef CLOCK_REALTIME 130#ifndef CLOCK_REALTIME
120# undef EV_USE_REALTIME 131# undef EV_USE_REALTIME
121# define EV_USE_REALTIME 0 132# define EV_USE_REALTIME 0
133#endif
134
135#if EV_SELECT_IS_WINSOCKET
136# include <winsock.h>
122#endif 137#endif
123 138
124/**/ 139/**/
125 140
126#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) */
127#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */ 142#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 */ 143#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 */ 144/*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */
130 145
146#ifdef EV_H
147# include EV_H
148#else
131#include "ev.h" 149# include "ev.h"
150#endif
132 151
133#if __GNUC__ >= 3 152#if __GNUC__ >= 3
134# define expect(expr,value) __builtin_expect ((expr),(value)) 153# define expect(expr,value) __builtin_expect ((expr),(value))
135# define inline inline 154# define inline inline
136#else 155#else
142#define expect_true(expr) expect ((expr) != 0, 1) 161#define expect_true(expr) expect ((expr) != 0, 1)
143 162
144#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) 163#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
145#define ABSPRI(w) ((w)->priority - EV_MINPRI) 164#define ABSPRI(w) ((w)->priority - EV_MINPRI)
146 165
166#define EMPTY /* required for microsofts broken pseudo-c compiler */
167
147typedef struct ev_watcher *W; 168typedef struct ev_watcher *W;
148typedef struct ev_watcher_list *WL; 169typedef struct ev_watcher_list *WL;
149typedef struct ev_watcher_time *WT; 170typedef struct ev_watcher_time *WT;
150 171
151static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 172static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
152 173
174#ifdef _WIN32
153#include "ev_win32.c" 175# include "ev_win32.c"
176#endif
154 177
155/*****************************************************************************/ 178/*****************************************************************************/
156 179
157static void (*syserr_cb)(const char *msg); 180static void (*syserr_cb)(const char *msg);
158 181
205typedef struct 228typedef struct
206{ 229{
207 WL head; 230 WL head;
208 unsigned char events; 231 unsigned char events;
209 unsigned char reify; 232 unsigned char reify;
233#if EV_SELECT_IS_WINSOCKET
234 SOCKET handle;
235#endif
210} ANFD; 236} ANFD;
211 237
212typedef struct 238typedef struct
213{ 239{
214 W w; 240 W w;
215 int events; 241 int events;
216} ANPENDING; 242} ANPENDING;
217 243
218#if EV_MULTIPLICITY 244#if EV_MULTIPLICITY
219 245
220struct ev_loop 246 struct ev_loop
221{ 247 {
248 ev_tstamp ev_rt_now;
249 #define ev_rt_now ((loop)->ev_rt_now)
222# define VAR(name,decl) decl; 250 #define VAR(name,decl) decl;
223# include "ev_vars.h" 251 #include "ev_vars.h"
224};
225# undef VAR 252 #undef VAR
253 };
226# include "ev_wrap.h" 254 #include "ev_wrap.h"
255
256 struct ev_loop default_loop_struct;
257 static struct ev_loop *default_loop;
227 258
228#else 259#else
229 260
261 ev_tstamp ev_rt_now;
230# define VAR(name,decl) static decl; 262 #define VAR(name,decl) static decl;
231# include "ev_vars.h" 263 #include "ev_vars.h"
232# undef VAR 264 #undef VAR
265
266 static int default_loop;
233 267
234#endif 268#endif
235 269
236/*****************************************************************************/ 270/*****************************************************************************/
237 271
238inline ev_tstamp 272ev_tstamp
239ev_time (void) 273ev_time (void)
240{ 274{
241#if EV_USE_REALTIME 275#if EV_USE_REALTIME
242 struct timespec ts; 276 struct timespec ts;
243 clock_gettime (CLOCK_REALTIME, &ts); 277 clock_gettime (CLOCK_REALTIME, &ts);
262#endif 296#endif
263 297
264 return ev_time (); 298 return ev_time ();
265} 299}
266 300
301#if EV_MULTIPLICITY
267ev_tstamp 302ev_tstamp
268ev_now (EV_P) 303ev_now (EV_P)
269{ 304{
270 return rt_now; 305 return ev_rt_now;
271} 306}
307#endif
272 308
273#define array_roundsize(type,n) ((n) | 4 & ~3) 309#define array_roundsize(type,n) ((n) | 4 & ~3)
274 310
275#define array_needsize(type,base,cur,cnt,init) \ 311#define array_needsize(type,base,cur,cnt,init) \
276 if (expect_false ((cnt) > cur)) \ 312 if (expect_false ((cnt) > cur)) \
293 stem ## max = array_roundsize (stem ## cnt >> 1); \ 329 stem ## max = array_roundsize (stem ## cnt >> 1); \
294 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ 330 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\
295 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ 331 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
296 } 332 }
297 333
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) \ 334#define array_free(stem, idx) \
304 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;
305 336
306/*****************************************************************************/ 337/*****************************************************************************/
307 338
381 int events = 0; 412 int events = 0;
382 413
383 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)
384 events |= w->events; 415 events |= w->events;
385 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
386 anfd->reify = 0; 426 anfd->reify = 0;
387 427
388 method_modify (EV_A_ fd, anfd->events, events); 428 method_modify (EV_A_ fd, anfd->events, events);
389 anfd->events = events; 429 anfd->events = events;
390 } 430 }
418} 458}
419 459
420static int 460static int
421fd_valid (int fd) 461fd_valid (int fd)
422{ 462{
423#ifdef WIN32 463#ifdef _WIN32
424 return !!win32_get_osfhandle (fd); 464 return _get_osfhandle (fd) != -1;
425#else 465#else
426 return fcntl (fd, F_GETFD) != -1; 466 return fcntl (fd, F_GETFD) != -1;
427#endif 467#endif
428} 468}
429 469
509 549
510 heap [k] = w; 550 heap [k] = w;
511 ((W)heap [k])->active = k + 1; 551 ((W)heap [k])->active = k + 1;
512} 552}
513 553
554inline void
555adjustheap (WT *heap, int N, int k)
556{
557 upheap (heap, k);
558 downheap (heap, N, k);
559}
560
514/*****************************************************************************/ 561/*****************************************************************************/
515 562
516typedef struct 563typedef struct
517{ 564{
518 WL head; 565 WL head;
539} 586}
540 587
541static void 588static void
542sighandler (int signum) 589sighandler (int signum)
543{ 590{
544#if WIN32 591#if _WIN32
545 signal (signum, sighandler); 592 signal (signum, sighandler);
546#endif 593#endif
547 594
548 signals [signum - 1].gotsig = 1; 595 signals [signum - 1].gotsig = 1;
549 596
550 if (!gotsig) 597 if (!gotsig)
551 { 598 {
552 int old_errno = errno; 599 int old_errno = errno;
553 gotsig = 1; 600 gotsig = 1;
554#ifdef WIN32
555 send (sigpipe [1], &signum, 1, MSG_DONTWAIT);
556#else
557 write (sigpipe [1], &signum, 1); 601 write (sigpipe [1], &signum, 1);
558#endif
559 errno = old_errno; 602 errno = old_errno;
560 } 603 }
561} 604}
562 605
563void 606void
564ev_feed_signal_event (EV_P_ int signum) 607ev_feed_signal_event (EV_P_ int signum)
565{ 608{
609 WL w;
610
566#if EV_MULTIPLICITY 611#if EV_MULTIPLICITY
567 assert (("feeding signal events is only supported in the default loop", loop == default_loop)); 612 assert (("feeding signal events is only supported in the default loop", loop == default_loop));
568#endif 613#endif
569 614
570 --signum; 615 --signum;
579} 624}
580 625
581static void 626static void
582sigcb (EV_P_ struct ev_io *iow, int revents) 627sigcb (EV_P_ struct ev_io *iow, int revents)
583{ 628{
584 WL w;
585 int signum; 629 int signum;
586 630
587#ifdef WIN32
588 recv (sigpipe [0], &revents, 1, MSG_DONTWAIT);
589#else
590 read (sigpipe [0], &revents, 1); 631 read (sigpipe [0], &revents, 1);
591#endif
592 gotsig = 0; 632 gotsig = 0;
593 633
594 for (signum = signalmax; signum--; ) 634 for (signum = signalmax; signum--; )
595 if (signals [signum].gotsig) 635 if (signals [signum].gotsig)
596 sigevent (EV_A_ signum + 1); 636 ev_feed_signal_event (EV_A_ signum + 1);
637}
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
597} 649}
598 650
599static void 651static void
600siginit (EV_P) 652siginit (EV_P)
601{ 653{
602#ifndef WIN32 654 fd_intern (sigpipe [0]);
603 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); 655 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 656
611 ev_io_set (&sigev, sigpipe [0], EV_READ); 657 ev_io_set (&sigev, sigpipe [0], EV_READ);
612 ev_io_start (EV_A_ &sigev); 658 ev_io_start (EV_A_ &sigev);
613 ev_unref (EV_A); /* child watcher should not keep loop alive */ 659 ev_unref (EV_A); /* child watcher should not keep loop alive */
614} 660}
615 661
616/*****************************************************************************/ 662/*****************************************************************************/
617 663
618static struct ev_child *childs [PID_HASHSIZE]; 664static struct ev_child *childs [PID_HASHSIZE];
619 665
620#ifndef WIN32 666#ifndef _WIN32
621 667
622static struct ev_signal childev; 668static struct ev_signal childev;
623 669
624#ifndef WCONTINUED 670#ifndef WCONTINUED
625# define WCONTINUED 0 671# define WCONTINUED 0
686 732
687/* 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 */
688static int 734static int
689enable_secure (void) 735enable_secure (void)
690{ 736{
691#ifdef WIN32 737#ifdef _WIN32
692 return 0; 738 return 0;
693#else 739#else
694 return getuid () != geteuid () 740 return getuid () != geteuid ()
695 || getgid () != getegid (); 741 || getgid () != getegid ();
696#endif 742#endif
713 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 759 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
714 have_monotonic = 1; 760 have_monotonic = 1;
715 } 761 }
716#endif 762#endif
717 763
718 rt_now = ev_time (); 764 ev_rt_now = ev_time ();
719 mn_now = get_clock (); 765 mn_now = get_clock ();
720 now_floor = mn_now; 766 now_floor = mn_now;
721 rtmn_diff = rt_now - mn_now; 767 rtmn_diff = ev_rt_now - mn_now;
722 768
723 if (methods == EVMETHOD_AUTO) 769 if (methods == EVMETHOD_AUTO)
724 if (!enable_secure () && getenv ("LIBEV_METHODS")) 770 if (!enable_secure () && getenv ("LIBEV_METHODS"))
725 methods = atoi (getenv ("LIBEV_METHODS")); 771 methods = atoi (getenv ("LIBEV_METHODS"));
726 else 772 else
727 methods = EVMETHOD_ANY; 773 methods = EVMETHOD_ANY;
728 774
729 method = 0; 775 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 776#if EV_USE_KQUEUE
734 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); 777 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods);
735#endif 778#endif
736#if EV_USE_EPOLL 779#if EV_USE_EPOLL
737 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); 780 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods);
741#endif 784#endif
742#if EV_USE_SELECT 785#if EV_USE_SELECT
743 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 786 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
744#endif 787#endif
745 788
746 ev_watcher_init (&sigev, sigcb); 789 ev_init (&sigev, sigcb);
747 ev_set_priority (&sigev, EV_MAXPRI); 790 ev_set_priority (&sigev, EV_MAXPRI);
748 } 791 }
749} 792}
750 793
751void 794void
752loop_destroy (EV_P) 795loop_destroy (EV_P)
753{ 796{
754 int i; 797 int i;
755 798
756#if EV_USE_WIN32
757 if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A);
758#endif
759#if EV_USE_KQUEUE 799#if EV_USE_KQUEUE
760 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); 800 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
761#endif 801#endif
762#if EV_USE_EPOLL 802#if EV_USE_EPOLL
763 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); 803 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A);
771 811
772 for (i = NUMPRI; i--; ) 812 for (i = NUMPRI; i--; )
773 array_free (pending, [i]); 813 array_free (pending, [i]);
774 814
775 /* have to use the microsoft-never-gets-it-right macro */ 815 /* have to use the microsoft-never-gets-it-right macro */
776 array_free_microshit (fdchange); 816 array_free (fdchange, EMPTY);
777 array_free_microshit (timer); 817 array_free (timer, EMPTY);
778 array_free_microshit (periodic); 818#if EV_PERIODICS
779 array_free_microshit (idle); 819 array_free (periodic, EMPTY);
780 array_free_microshit (prepare); 820#endif
781 array_free_microshit (check); 821 array_free (idle, EMPTY);
822 array_free (prepare, EMPTY);
823 array_free (check, EMPTY);
782 824
783 method = 0; 825 method = 0;
784} 826}
785 827
786static void 828static void
841} 883}
842 884
843#endif 885#endif
844 886
845#if EV_MULTIPLICITY 887#if EV_MULTIPLICITY
846struct ev_loop default_loop_struct;
847static struct ev_loop *default_loop;
848
849struct ev_loop * 888struct ev_loop *
850#else 889#else
851static int default_loop;
852
853int 890int
854#endif 891#endif
855ev_default_loop (int methods) 892ev_default_loop (int methods)
856{ 893{
857 if (sigpipe [0] == sigpipe [1]) 894 if (sigpipe [0] == sigpipe [1])
870 907
871 if (ev_method (EV_A)) 908 if (ev_method (EV_A))
872 { 909 {
873 siginit (EV_A); 910 siginit (EV_A);
874 911
875#ifndef WIN32 912#ifndef _WIN32
876 ev_signal_init (&childev, childcb, SIGCHLD); 913 ev_signal_init (&childev, childcb, SIGCHLD);
877 ev_set_priority (&childev, EV_MAXPRI); 914 ev_set_priority (&childev, EV_MAXPRI);
878 ev_signal_start (EV_A_ &childev); 915 ev_signal_start (EV_A_ &childev);
879 ev_unref (EV_A); /* child watcher should not keep loop alive */ 916 ev_unref (EV_A); /* child watcher should not keep loop alive */
880#endif 917#endif
891{ 928{
892#if EV_MULTIPLICITY 929#if EV_MULTIPLICITY
893 struct ev_loop *loop = default_loop; 930 struct ev_loop *loop = default_loop;
894#endif 931#endif
895 932
896#ifndef WIN32 933#ifndef _WIN32
897 ev_ref (EV_A); /* child watcher */ 934 ev_ref (EV_A); /* child watcher */
898 ev_signal_stop (EV_A_ &childev); 935 ev_signal_stop (EV_A_ &childev);
899#endif 936#endif
900 937
901 ev_ref (EV_A); /* signal watcher */ 938 ev_ref (EV_A); /* signal watcher */
943 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 980 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
944 981
945 if (p->w) 982 if (p->w)
946 { 983 {
947 p->w->pending = 0; 984 p->w->pending = 0;
948 p->w->cb (EV_A_ p->w, p->events); 985 EV_CB_INVOKE (p->w, p->events);
949 } 986 }
950 } 987 }
951} 988}
952 989
953static void 990static void
961 998
962 /* first reschedule or stop timer */ 999 /* first reschedule or stop timer */
963 if (w->repeat) 1000 if (w->repeat)
964 { 1001 {
965 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
966 ((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
967 downheap ((WT *)timers, timercnt, 0); 1008 downheap ((WT *)timers, timercnt, 0);
968 } 1009 }
969 else 1010 else
970 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 1011 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
971 1012
972 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); 1013 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
973 } 1014 }
974} 1015}
975 1016
1017#if EV_PERIODICS
976static void 1018static void
977periodics_reify (EV_P) 1019periodics_reify (EV_P)
978{ 1020{
979 while (periodiccnt && ((WT)periodics [0])->at <= rt_now) 1021 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
980 { 1022 {
981 struct ev_periodic *w = periodics [0]; 1023 struct ev_periodic *w = periodics [0];
982 1024
983 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 1025 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
984 1026
985 /* first reschedule or stop timer */ 1027 /* first reschedule or stop timer */
986 if (w->reschedule_cb) 1028 if (w->reschedule_cb)
987 { 1029 {
988 ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, rt_now + 0.0001); 1030 ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001);
989 1031
990 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > rt_now)); 1032 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now));
991 downheap ((WT *)periodics, periodiccnt, 0); 1033 downheap ((WT *)periodics, periodiccnt, 0);
992 } 1034 }
993 else if (w->interval) 1035 else if (w->interval)
994 { 1036 {
995 ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; 1037 ((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)); 1038 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); 1039 downheap ((WT *)periodics, periodiccnt, 0);
998 } 1040 }
999 else 1041 else
1000 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 1042 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
1001 1043
1012 for (i = 0; i < periodiccnt; ++i) 1054 for (i = 0; i < periodiccnt; ++i)
1013 { 1055 {
1014 struct ev_periodic *w = periodics [i]; 1056 struct ev_periodic *w = periodics [i];
1015 1057
1016 if (w->reschedule_cb) 1058 if (w->reschedule_cb)
1017 ((WT)w)->at = w->reschedule_cb (w, rt_now); 1059 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1018 else if (w->interval) 1060 else if (w->interval)
1019 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; 1061 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1020 } 1062 }
1021 1063
1022 /* now rebuild the heap */ 1064 /* now rebuild the heap */
1023 for (i = periodiccnt >> 1; i--; ) 1065 for (i = periodiccnt >> 1; i--; )
1024 downheap ((WT *)periodics, periodiccnt, i); 1066 downheap ((WT *)periodics, periodiccnt, i);
1025} 1067}
1068#endif
1026 1069
1027inline int 1070inline int
1028time_update_monotonic (EV_P) 1071time_update_monotonic (EV_P)
1029{ 1072{
1030 mn_now = get_clock (); 1073 mn_now = get_clock ();
1031 1074
1032 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) 1075 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
1033 { 1076 {
1034 rt_now = rtmn_diff + mn_now; 1077 ev_rt_now = rtmn_diff + mn_now;
1035 return 0; 1078 return 0;
1036 } 1079 }
1037 else 1080 else
1038 { 1081 {
1039 now_floor = mn_now; 1082 now_floor = mn_now;
1040 rt_now = ev_time (); 1083 ev_rt_now = ev_time ();
1041 return 1; 1084 return 1;
1042 } 1085 }
1043} 1086}
1044 1087
1045static void 1088static void
1054 { 1097 {
1055 ev_tstamp odiff = rtmn_diff; 1098 ev_tstamp odiff = rtmn_diff;
1056 1099
1057 for (i = 4; --i; ) /* loop a few times, before making important decisions */ 1100 for (i = 4; --i; ) /* loop a few times, before making important decisions */
1058 { 1101 {
1059 rtmn_diff = rt_now - mn_now; 1102 rtmn_diff = ev_rt_now - mn_now;
1060 1103
1061 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) 1104 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)
1062 return; /* all is well */ 1105 return; /* all is well */
1063 1106
1064 rt_now = ev_time (); 1107 ev_rt_now = ev_time ();
1065 mn_now = get_clock (); 1108 mn_now = get_clock ();
1066 now_floor = mn_now; 1109 now_floor = mn_now;
1067 } 1110 }
1068 1111
1112# if EV_PERIODICS
1069 periodics_reschedule (EV_A); 1113 periodics_reschedule (EV_A);
1114# endif
1070 /* no timer adjustment, as the monotonic clock doesn't jump */ 1115 /* no timer adjustment, as the monotonic clock doesn't jump */
1071 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ 1116 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */
1072 } 1117 }
1073 } 1118 }
1074 else 1119 else
1075#endif 1120#endif
1076 { 1121 {
1077 rt_now = ev_time (); 1122 ev_rt_now = ev_time ();
1078 1123
1079 if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) 1124 if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
1080 { 1125 {
1126#if EV_PERIODICS
1081 periodics_reschedule (EV_A); 1127 periodics_reschedule (EV_A);
1128#endif
1082 1129
1083 /* 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 */
1084 for (i = 0; i < timercnt; ++i) 1131 for (i = 0; i < timercnt; ++i)
1085 ((WT)timers [i])->at += rt_now - mn_now; 1132 ((WT)timers [i])->at += ev_rt_now - mn_now;
1086 } 1133 }
1087 1134
1088 mn_now = rt_now; 1135 mn_now = ev_rt_now;
1089 } 1136 }
1090} 1137}
1091 1138
1092void 1139void
1093ev_ref (EV_P) 1140ev_ref (EV_P)
1133 if (expect_true (have_monotonic)) 1180 if (expect_true (have_monotonic))
1134 time_update_monotonic (EV_A); 1181 time_update_monotonic (EV_A);
1135 else 1182 else
1136#endif 1183#endif
1137 { 1184 {
1138 rt_now = ev_time (); 1185 ev_rt_now = ev_time ();
1139 mn_now = rt_now; 1186 mn_now = ev_rt_now;
1140 } 1187 }
1141 1188
1142 if (flags & EVLOOP_NONBLOCK || idlecnt) 1189 if (flags & EVLOOP_NONBLOCK || idlecnt)
1143 block = 0.; 1190 block = 0.;
1144 else 1191 else
1149 { 1196 {
1150 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; 1197 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge;
1151 if (block > to) block = to; 1198 if (block > to) block = to;
1152 } 1199 }
1153 1200
1201#if EV_PERIODICS
1154 if (periodiccnt) 1202 if (periodiccnt)
1155 { 1203 {
1156 ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge; 1204 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge;
1157 if (block > to) block = to; 1205 if (block > to) block = to;
1158 } 1206 }
1207#endif
1159 1208
1160 if (block < 0.) block = 0.; 1209 if (block < 0.) block = 0.;
1161 } 1210 }
1162 1211
1163 method_poll (EV_A_ block); 1212 method_poll (EV_A_ block);
1164 1213
1165 /* update rt_now, do magic */ 1214 /* update ev_rt_now, do magic */
1166 time_update (EV_A); 1215 time_update (EV_A);
1167 1216
1168 /* queue pending timers and reschedule them */ 1217 /* queue pending timers and reschedule them */
1169 timers_reify (EV_A); /* relative timers called last */ 1218 timers_reify (EV_A); /* relative timers called last */
1219#if EV_PERIODICS
1170 periodics_reify (EV_A); /* absolute timers called first */ 1220 periodics_reify (EV_A); /* absolute timers called first */
1221#endif
1171 1222
1172 /* queue idle watchers unless io or timers are pending */ 1223 /* queue idle watchers unless io or timers are pending */
1173 if (idlecnt && !any_pending (EV_A)) 1224 if (idlecnt && !any_pending (EV_A))
1174 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1225 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
1175 1226
1266{ 1317{
1267 ev_clear_pending (EV_A_ (W)w); 1318 ev_clear_pending (EV_A_ (W)w);
1268 if (!ev_is_active (w)) 1319 if (!ev_is_active (w))
1269 return; 1320 return;
1270 1321
1322 assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax));
1323
1271 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 1324 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
1272 ev_stop (EV_A_ (W)w); 1325 ev_stop (EV_A_ (W)w);
1273 1326
1274 fd_change (EV_A_ w->fd); 1327 fd_change (EV_A_ w->fd);
1275} 1328}
1302 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1355 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1303 1356
1304 if (((W)w)->active < timercnt--) 1357 if (((W)w)->active < timercnt--)
1305 { 1358 {
1306 timers [((W)w)->active - 1] = timers [timercnt]; 1359 timers [((W)w)->active - 1] = timers [timercnt];
1307 downheap ((WT *)timers, timercnt, ((W)w)->active - 1); 1360 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1308 } 1361 }
1309 1362
1310 ((WT)w)->at = w->repeat; 1363 ((WT)w)->at -= mn_now;
1311 1364
1312 ev_stop (EV_A_ (W)w); 1365 ev_stop (EV_A_ (W)w);
1313} 1366}
1314 1367
1315void 1368void
1318 if (ev_is_active (w)) 1371 if (ev_is_active (w))
1319 { 1372 {
1320 if (w->repeat) 1373 if (w->repeat)
1321 { 1374 {
1322 ((WT)w)->at = mn_now + w->repeat; 1375 ((WT)w)->at = mn_now + w->repeat;
1323 downheap ((WT *)timers, timercnt, ((W)w)->active - 1); 1376 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1324 } 1377 }
1325 else 1378 else
1326 ev_timer_stop (EV_A_ w); 1379 ev_timer_stop (EV_A_ w);
1327 } 1380 }
1328 else if (w->repeat) 1381 else if (w->repeat)
1329 ev_timer_start (EV_A_ w); 1382 ev_timer_start (EV_A_ w);
1330} 1383}
1331 1384
1385#if EV_PERIODICS
1332void 1386void
1333ev_periodic_start (EV_P_ struct ev_periodic *w) 1387ev_periodic_start (EV_P_ struct ev_periodic *w)
1334{ 1388{
1335 if (ev_is_active (w)) 1389 if (ev_is_active (w))
1336 return; 1390 return;
1337 1391
1338 if (w->reschedule_cb) 1392 if (w->reschedule_cb)
1339 ((WT)w)->at = w->reschedule_cb (w, rt_now); 1393 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1340 else if (w->interval) 1394 else if (w->interval)
1341 { 1395 {
1342 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); 1396 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 */ 1397 /* 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; 1398 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1345 } 1399 }
1346 1400
1347 ev_start (EV_A_ (W)w, ++periodiccnt); 1401 ev_start (EV_A_ (W)w, ++periodiccnt);
1348 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); 1402 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void));
1349 periodics [periodiccnt - 1] = w; 1403 periodics [periodiccnt - 1] = w;
1362 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1416 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1363 1417
1364 if (((W)w)->active < periodiccnt--) 1418 if (((W)w)->active < periodiccnt--)
1365 { 1419 {
1366 periodics [((W)w)->active - 1] = periodics [periodiccnt]; 1420 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1367 downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); 1421 adjustheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1368 } 1422 }
1369 1423
1370 ev_stop (EV_A_ (W)w); 1424 ev_stop (EV_A_ (W)w);
1371} 1425}
1372 1426
1373void 1427void
1374ev_periodic_again (EV_P_ struct ev_periodic *w) 1428ev_periodic_again (EV_P_ struct ev_periodic *w)
1375{ 1429{
1430 /* TODO: use adjustheap and recalculation */
1376 ev_periodic_stop (EV_A_ w); 1431 ev_periodic_stop (EV_A_ w);
1377 ev_periodic_start (EV_A_ w); 1432 ev_periodic_start (EV_A_ w);
1378} 1433}
1434#endif
1379 1435
1380void 1436void
1381ev_idle_start (EV_P_ struct ev_idle *w) 1437ev_idle_start (EV_P_ struct ev_idle *w)
1382{ 1438{
1383 if (ev_is_active (w)) 1439 if (ev_is_active (w))
1390 1446
1391void 1447void
1392ev_idle_stop (EV_P_ struct ev_idle *w) 1448ev_idle_stop (EV_P_ struct ev_idle *w)
1393{ 1449{
1394 ev_clear_pending (EV_A_ (W)w); 1450 ev_clear_pending (EV_A_ (W)w);
1395 if (ev_is_active (w)) 1451 if (!ev_is_active (w))
1396 return; 1452 return;
1397 1453
1398 idles [((W)w)->active - 1] = idles [--idlecnt]; 1454 idles [((W)w)->active - 1] = idles [--idlecnt];
1399 ev_stop (EV_A_ (W)w); 1455 ev_stop (EV_A_ (W)w);
1400} 1456}
1412 1468
1413void 1469void
1414ev_prepare_stop (EV_P_ struct ev_prepare *w) 1470ev_prepare_stop (EV_P_ struct ev_prepare *w)
1415{ 1471{
1416 ev_clear_pending (EV_A_ (W)w); 1472 ev_clear_pending (EV_A_ (W)w);
1417 if (ev_is_active (w)) 1473 if (!ev_is_active (w))
1418 return; 1474 return;
1419 1475
1420 prepares [((W)w)->active - 1] = prepares [--preparecnt]; 1476 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1421 ev_stop (EV_A_ (W)w); 1477 ev_stop (EV_A_ (W)w);
1422} 1478}
1434 1490
1435void 1491void
1436ev_check_stop (EV_P_ struct ev_check *w) 1492ev_check_stop (EV_P_ struct ev_check *w)
1437{ 1493{
1438 ev_clear_pending (EV_A_ (W)w); 1494 ev_clear_pending (EV_A_ (W)w);
1439 if (ev_is_active (w)) 1495 if (!ev_is_active (w))
1440 return; 1496 return;
1441 1497
1442 checks [((W)w)->active - 1] = checks [--checkcnt]; 1498 checks [((W)w)->active - 1] = checks [--checkcnt];
1443 ev_stop (EV_A_ (W)w); 1499 ev_stop (EV_A_ (W)w);
1444} 1500}
1462 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init); 1518 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init);
1463 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1519 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1464 1520
1465 if (!((WL)w)->next) 1521 if (!((WL)w)->next)
1466 { 1522 {
1467#if WIN32 1523#if _WIN32
1468 signal (w->signum, sighandler); 1524 signal (w->signum, sighandler);
1469#else 1525#else
1470 struct sigaction sa; 1526 struct sigaction sa;
1471 sa.sa_handler = sighandler; 1527 sa.sa_handler = sighandler;
1472 sigfillset (&sa.sa_mask); 1528 sigfillset (&sa.sa_mask);
1505 1561
1506void 1562void
1507ev_child_stop (EV_P_ struct ev_child *w) 1563ev_child_stop (EV_P_ struct ev_child *w)
1508{ 1564{
1509 ev_clear_pending (EV_A_ (W)w); 1565 ev_clear_pending (EV_A_ (W)w);
1510 if (ev_is_active (w)) 1566 if (!ev_is_active (w))
1511 return; 1567 return;
1512 1568
1513 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1569 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1514 ev_stop (EV_A_ (W)w); 1570 ev_stop (EV_A_ (W)w);
1515} 1571}
1559 else 1615 else
1560 { 1616 {
1561 once->cb = cb; 1617 once->cb = cb;
1562 once->arg = arg; 1618 once->arg = arg;
1563 1619
1564 ev_watcher_init (&once->io, once_cb_io); 1620 ev_init (&once->io, once_cb_io);
1565 if (fd >= 0) 1621 if (fd >= 0)
1566 { 1622 {
1567 ev_io_set (&once->io, fd, events); 1623 ev_io_set (&once->io, fd, events);
1568 ev_io_start (EV_A_ &once->io); 1624 ev_io_start (EV_A_ &once->io);
1569 } 1625 }
1570 1626
1571 ev_watcher_init (&once->to, once_cb_to); 1627 ev_init (&once->to, once_cb_to);
1572 if (timeout >= 0.) 1628 if (timeout >= 0.)
1573 { 1629 {
1574 ev_timer_set (&once->to, timeout, 0.); 1630 ev_timer_set (&once->to, timeout, 0.);
1575 ev_timer_start (EV_A_ &once->to); 1631 ev_timer_start (EV_A_ &once->to);
1576 } 1632 }
1577 } 1633 }
1578} 1634}
1579 1635
1636#ifdef __cplusplus
1637}
1638#endif
1639

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines