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

Comparing libev/ev.c (file contents):
Revision 1.70 by root, Tue Nov 6 00:52:32 2007 UTC vs.
Revision 1.86 by root, Sat Nov 10 03:19:21 2007 UTC

54 54
55#endif 55#endif
56 56
57#include <math.h> 57#include <math.h>
58#include <stdlib.h> 58#include <stdlib.h>
59#include <unistd.h>
60#include <fcntl.h> 59#include <fcntl.h>
61#include <signal.h>
62#include <stddef.h> 60#include <stddef.h>
63 61
64#include <stdio.h> 62#include <stdio.h>
65 63
66#include <assert.h> 64#include <assert.h>
67#include <errno.h> 65#include <errno.h>
68#include <sys/types.h> 66#include <sys/types.h>
67#include <time.h>
68
69#include <signal.h>
70
69#ifndef WIN32 71#ifndef WIN32
72# include <unistd.h>
73# include <sys/time.h>
70# include <sys/wait.h> 74# include <sys/wait.h>
71#endif 75#endif
72#include <sys/time.h>
73#include <time.h>
74
75/**/ 76/**/
76 77
77#ifndef EV_USE_MONOTONIC 78#ifndef EV_USE_MONOTONIC
78# define EV_USE_MONOTONIC 1 79# define EV_USE_MONOTONIC 1
79#endif 80#endif
94# define EV_USE_KQUEUE 0 95# define EV_USE_KQUEUE 0
95#endif 96#endif
96 97
97#ifndef EV_USE_WIN32 98#ifndef EV_USE_WIN32
98# ifdef WIN32 99# ifdef WIN32
100# define EV_USE_WIN32 0 /* it does not exist, use select */
101# undef EV_USE_SELECT
99# define EV_USE_WIN32 1 102# define EV_USE_SELECT 1
100# else 103# else
101# define EV_USE_WIN32 0 104# define EV_USE_WIN32 0
102# endif 105# endif
103#endif 106#endif
104 107
123#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 126#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
124#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */ 127#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */
125#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ 128#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */
126/*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ 129/*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */
127 130
131#ifdef EV_H
132# include EV_H
133#else
128#include "ev.h" 134# include "ev.h"
135#endif
129 136
130#if __GNUC__ >= 3 137#if __GNUC__ >= 3
131# define expect(expr,value) __builtin_expect ((expr),(value)) 138# define expect(expr,value) __builtin_expect ((expr),(value))
132# define inline inline 139# define inline inline
133#else 140#else
145typedef struct ev_watcher_list *WL; 152typedef struct ev_watcher_list *WL;
146typedef struct ev_watcher_time *WT; 153typedef struct ev_watcher_time *WT;
147 154
148static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 155static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
149 156
150#if WIN32 157#include "ev_win32.c"
151/* note: the comment below could not be substantiated, but what would I care */
152/* MSDN says this is required to handle SIGFPE */
153volatile double SIGFPE_REQ = 0.0f;
154#endif
155 158
156/*****************************************************************************/ 159/*****************************************************************************/
157 160
158static void (*syserr_cb)(const char *msg); 161static void (*syserr_cb)(const char *msg);
159 162
216 int events; 219 int events;
217} ANPENDING; 220} ANPENDING;
218 221
219#if EV_MULTIPLICITY 222#if EV_MULTIPLICITY
220 223
221struct ev_loop 224 struct ev_loop
222{ 225 {
226 ev_tstamp ev_rt_now;
223# define VAR(name,decl) decl; 227 #define VAR(name,decl) decl;
224# include "ev_vars.h" 228 #include "ev_vars.h"
225};
226# undef VAR 229 #undef VAR
230 };
227# include "ev_wrap.h" 231 #include "ev_wrap.h"
232
233 struct ev_loop default_loop_struct;
234 static struct ev_loop *default_loop;
228 235
229#else 236#else
230 237
238 ev_tstamp ev_rt_now;
231# define VAR(name,decl) static decl; 239 #define VAR(name,decl) static decl;
232# include "ev_vars.h" 240 #include "ev_vars.h"
233# undef VAR 241 #undef VAR
242
243 static int default_loop;
234 244
235#endif 245#endif
236 246
237/*****************************************************************************/ 247/*****************************************************************************/
238 248
263#endif 273#endif
264 274
265 return ev_time (); 275 return ev_time ();
266} 276}
267 277
278#if EV_MULTIPLICITY
268ev_tstamp 279ev_tstamp
269ev_now (EV_P) 280ev_now (EV_P)
270{ 281{
271 return rt_now; 282 return ev_rt_now;
272} 283}
284#endif
273 285
274#define array_roundsize(base,n) ((n) | 4 & ~3) 286#define array_roundsize(type,n) ((n) | 4 & ~3)
275 287
276#define array_needsize(base,cur,cnt,init) \ 288#define array_needsize(type,base,cur,cnt,init) \
277 if (expect_false ((cnt) > cur)) \ 289 if (expect_false ((cnt) > cur)) \
278 { \ 290 { \
279 int newcnt = cur; \ 291 int newcnt = cur; \
280 do \ 292 do \
281 { \ 293 { \
282 newcnt = array_roundsize (base, newcnt << 1); \ 294 newcnt = array_roundsize (type, newcnt << 1); \
283 } \ 295 } \
284 while ((cnt) > newcnt); \ 296 while ((cnt) > newcnt); \
285 \ 297 \
286 base = ev_realloc (base, sizeof (*base) * (newcnt)); \ 298 base = (type *)ev_realloc (base, sizeof (type) * (newcnt));\
287 init (base + cur, newcnt - cur); \ 299 init (base + cur, newcnt - cur); \
288 cur = newcnt; \ 300 cur = newcnt; \
289 } 301 }
290 302
291#define array_slim(stem) \ 303#define array_slim(type,stem) \
292 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ 304 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \
293 { \ 305 { \
294 stem ## max = array_roundsize (stem ## cnt >> 1); \ 306 stem ## max = array_roundsize (stem ## cnt >> 1); \
295 base = ev_realloc (base, sizeof (*base) * (stem ## max)); \ 307 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\
296 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ 308 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
297 } 309 }
310
311/* microsoft's pseudo-c is quite far from C as the rest of the world and the standard knows it */
312/* bringing us everlasting joy in form of stupid extra macros that are not required in C */
313#define array_free_microshit(stem) \
314 ev_free (stem ## s); stem ## cnt = stem ## max = 0;
298 315
299#define array_free(stem, idx) \ 316#define array_free(stem, idx) \
300 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; 317 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
301 318
302/*****************************************************************************/ 319/*****************************************************************************/
312 329
313 ++base; 330 ++base;
314 } 331 }
315} 332}
316 333
317static void 334void
318event (EV_P_ W w, int events) 335ev_feed_event (EV_P_ void *w, int revents)
319{ 336{
337 W w_ = (W)w;
338
320 if (w->pending) 339 if (w_->pending)
321 { 340 {
322 pendings [ABSPRI (w)][w->pending - 1].events |= events; 341 pendings [ABSPRI (w_)][w_->pending - 1].events |= revents;
323 return; 342 return;
324 } 343 }
325 344
326 w->pending = ++pendingcnt [ABSPRI (w)]; 345 w_->pending = ++pendingcnt [ABSPRI (w_)];
327 array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], ); 346 array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], (void));
328 pendings [ABSPRI (w)][w->pending - 1].w = w; 347 pendings [ABSPRI (w_)][w_->pending - 1].w = w_;
329 pendings [ABSPRI (w)][w->pending - 1].events = events; 348 pendings [ABSPRI (w_)][w_->pending - 1].events = revents;
330} 349}
331 350
332static void 351static void
333queue_events (EV_P_ W *events, int eventcnt, int type) 352queue_events (EV_P_ W *events, int eventcnt, int type)
334{ 353{
335 int i; 354 int i;
336 355
337 for (i = 0; i < eventcnt; ++i) 356 for (i = 0; i < eventcnt; ++i)
338 event (EV_A_ events [i], type); 357 ev_feed_event (EV_A_ events [i], type);
339} 358}
340 359
341static void 360inline void
342fd_event (EV_P_ int fd, int events) 361fd_event (EV_P_ int fd, int revents)
343{ 362{
344 ANFD *anfd = anfds + fd; 363 ANFD *anfd = anfds + fd;
345 struct ev_io *w; 364 struct ev_io *w;
346 365
347 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) 366 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
348 { 367 {
349 int ev = w->events & events; 368 int ev = w->events & revents;
350 369
351 if (ev) 370 if (ev)
352 event (EV_A_ (W)w, ev); 371 ev_feed_event (EV_A_ (W)w, ev);
353 } 372 }
373}
374
375void
376ev_feed_fd_event (EV_P_ int fd, int revents)
377{
378 fd_event (EV_A_ fd, revents);
354} 379}
355 380
356/*****************************************************************************/ 381/*****************************************************************************/
357 382
358static void 383static void
387 return; 412 return;
388 413
389 anfds [fd].reify = 1; 414 anfds [fd].reify = 1;
390 415
391 ++fdchangecnt; 416 ++fdchangecnt;
392 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 417 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, (void));
393 fdchanges [fdchangecnt - 1] = fd; 418 fdchanges [fdchangecnt - 1] = fd;
394} 419}
395 420
396static void 421static void
397fd_kill (EV_P_ int fd) 422fd_kill (EV_P_ int fd)
399 struct ev_io *w; 424 struct ev_io *w;
400 425
401 while ((w = (struct ev_io *)anfds [fd].head)) 426 while ((w = (struct ev_io *)anfds [fd].head))
402 { 427 {
403 ev_io_stop (EV_A_ w); 428 ev_io_stop (EV_A_ w);
404 event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); 429 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
405 } 430 }
431}
432
433static int
434fd_valid (int fd)
435{
436#ifdef WIN32
437 return !!win32_get_osfhandle (fd);
438#else
439 return fcntl (fd, F_GETFD) != -1;
440#endif
406} 441}
407 442
408/* called on EBADF to verify fds */ 443/* called on EBADF to verify fds */
409static void 444static void
410fd_ebadf (EV_P) 445fd_ebadf (EV_P)
411{ 446{
412 int fd; 447 int fd;
413 448
414 for (fd = 0; fd < anfdmax; ++fd) 449 for (fd = 0; fd < anfdmax; ++fd)
415 if (anfds [fd].events) 450 if (anfds [fd].events)
416 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 451 if (!fd_valid (fd) == -1 && errno == EBADF)
417 fd_kill (EV_A_ fd); 452 fd_kill (EV_A_ fd);
418} 453}
419 454
420/* called on ENOMEM in select/poll to kill some fds and retry */ 455/* called on ENOMEM in select/poll to kill some fds and retry */
421static void 456static void
487 522
488 heap [k] = w; 523 heap [k] = w;
489 ((W)heap [k])->active = k + 1; 524 ((W)heap [k])->active = k + 1;
490} 525}
491 526
527inline void
528adjustheap (WT *heap, int N, int k, ev_tstamp at)
529{
530 ev_tstamp old_at = heap [k]->at;
531 heap [k]->at = at;
532
533 if (old_at < at)
534 downheap (heap, N, k);
535 else
536 upheap (heap, k);
537}
538
492/*****************************************************************************/ 539/*****************************************************************************/
493 540
494typedef struct 541typedef struct
495{ 542{
496 WL head; 543 WL head;
527 574
528 if (!gotsig) 575 if (!gotsig)
529 { 576 {
530 int old_errno = errno; 577 int old_errno = errno;
531 gotsig = 1; 578 gotsig = 1;
579#ifdef WIN32
580 send (sigpipe [1], &signum, 1, MSG_DONTWAIT);
581#else
532 write (sigpipe [1], &signum, 1); 582 write (sigpipe [1], &signum, 1);
583#endif
533 errno = old_errno; 584 errno = old_errno;
534 } 585 }
535} 586}
536 587
588void
589ev_feed_signal_event (EV_P_ int signum)
590{
591 WL w;
592
593#if EV_MULTIPLICITY
594 assert (("feeding signal events is only supported in the default loop", loop == default_loop));
595#endif
596
597 --signum;
598
599 if (signum < 0 || signum >= signalmax)
600 return;
601
602 signals [signum].gotsig = 0;
603
604 for (w = signals [signum].head; w; w = w->next)
605 ev_feed_event (EV_A_ (W)w, EV_SIGNAL);
606}
607
537static void 608static void
538sigcb (EV_P_ struct ev_io *iow, int revents) 609sigcb (EV_P_ struct ev_io *iow, int revents)
539{ 610{
540 WL w;
541 int signum; 611 int signum;
542 612
613#ifdef WIN32
614 recv (sigpipe [0], &revents, 1, MSG_DONTWAIT);
615#else
543 read (sigpipe [0], &revents, 1); 616 read (sigpipe [0], &revents, 1);
617#endif
544 gotsig = 0; 618 gotsig = 0;
545 619
546 for (signum = signalmax; signum--; ) 620 for (signum = signalmax; signum--; )
547 if (signals [signum].gotsig) 621 if (signals [signum].gotsig)
548 { 622 ev_feed_signal_event (EV_A_ signum + 1);
549 signals [signum].gotsig = 0;
550
551 for (w = signals [signum].head; w; w = w->next)
552 event (EV_A_ (W)w, EV_SIGNAL);
553 }
554} 623}
555 624
556static void 625static void
557siginit (EV_P) 626siginit (EV_P)
558{ 627{
570 ev_unref (EV_A); /* child watcher should not keep loop alive */ 639 ev_unref (EV_A); /* child watcher should not keep loop alive */
571} 640}
572 641
573/*****************************************************************************/ 642/*****************************************************************************/
574 643
644static struct ev_child *childs [PID_HASHSIZE];
645
575#ifndef WIN32 646#ifndef WIN32
576 647
577static struct ev_child *childs [PID_HASHSIZE];
578static struct ev_signal childev; 648static struct ev_signal childev;
579 649
580#ifndef WCONTINUED 650#ifndef WCONTINUED
581# define WCONTINUED 0 651# define WCONTINUED 0
582#endif 652#endif
590 if (w->pid == pid || !w->pid) 660 if (w->pid == pid || !w->pid)
591 { 661 {
592 ev_priority (w) = ev_priority (sw); /* need to do it *now* */ 662 ev_priority (w) = ev_priority (sw); /* need to do it *now* */
593 w->rpid = pid; 663 w->rpid = pid;
594 w->rstatus = status; 664 w->rstatus = status;
595 event (EV_A_ (W)w, EV_CHILD); 665 ev_feed_event (EV_A_ (W)w, EV_CHILD);
596 } 666 }
597} 667}
598 668
599static void 669static void
600childcb (EV_P_ struct ev_signal *sw, int revents) 670childcb (EV_P_ struct ev_signal *sw, int revents)
602 int pid, status; 672 int pid, status;
603 673
604 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) 674 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)))
605 { 675 {
606 /* make sure we are called again until all childs have been reaped */ 676 /* make sure we are called again until all childs have been reaped */
607 event (EV_A_ (W)sw, EV_SIGNAL); 677 ev_feed_event (EV_A_ (W)sw, EV_SIGNAL);
608 678
609 child_reap (EV_A_ sw, pid, pid, status); 679 child_reap (EV_A_ sw, pid, pid, status);
610 child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ 680 child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */
611 } 681 }
612} 682}
669 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 739 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
670 have_monotonic = 1; 740 have_monotonic = 1;
671 } 741 }
672#endif 742#endif
673 743
674 rt_now = ev_time (); 744 ev_rt_now = ev_time ();
675 mn_now = get_clock (); 745 mn_now = get_clock ();
676 now_floor = mn_now; 746 now_floor = mn_now;
677 rtmn_diff = rt_now - mn_now; 747 rtmn_diff = ev_rt_now - mn_now;
678 748
679 if (methods == EVMETHOD_AUTO) 749 if (methods == EVMETHOD_AUTO)
680 if (!enable_secure () && getenv ("LIBEV_METHODS")) 750 if (!enable_secure () && getenv ("LIBEV_METHODS"))
681 methods = atoi (getenv ("LIBEV_METHODS")); 751 methods = atoi (getenv ("LIBEV_METHODS"));
682 else 752 else
697#endif 767#endif
698#if EV_USE_SELECT 768#if EV_USE_SELECT
699 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 769 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
700#endif 770#endif
701 771
702 ev_watcher_init (&sigev, sigcb); 772 ev_init (&sigev, sigcb);
703 ev_set_priority (&sigev, EV_MAXPRI); 773 ev_set_priority (&sigev, EV_MAXPRI);
704 } 774 }
705} 775}
706 776
707void 777void
726#endif 796#endif
727 797
728 for (i = NUMPRI; i--; ) 798 for (i = NUMPRI; i--; )
729 array_free (pending, [i]); 799 array_free (pending, [i]);
730 800
801 /* have to use the microsoft-never-gets-it-right macro */
731 array_free (fdchange, ); 802 array_free_microshit (fdchange);
732 array_free (timer, ); 803 array_free_microshit (timer);
733 array_free (periodic, ); 804 array_free_microshit (periodic);
734 array_free (idle, ); 805 array_free_microshit (idle);
735 array_free (prepare, ); 806 array_free_microshit (prepare);
736 array_free (check, ); 807 array_free_microshit (check);
737 808
738 method = 0; 809 method = 0;
739} 810}
740 811
741static void 812static void
796} 867}
797 868
798#endif 869#endif
799 870
800#if EV_MULTIPLICITY 871#if EV_MULTIPLICITY
801struct ev_loop default_loop_struct;
802static struct ev_loop *default_loop;
803
804struct ev_loop * 872struct ev_loop *
805#else 873#else
806static int default_loop;
807
808int 874int
809#endif 875#endif
810ev_default_loop (int methods) 876ev_default_loop (int methods)
811{ 877{
812 if (sigpipe [0] == sigpipe [1]) 878 if (sigpipe [0] == sigpipe [1])
846{ 912{
847#if EV_MULTIPLICITY 913#if EV_MULTIPLICITY
848 struct ev_loop *loop = default_loop; 914 struct ev_loop *loop = default_loop;
849#endif 915#endif
850 916
917#ifndef WIN32
851 ev_ref (EV_A); /* child watcher */ 918 ev_ref (EV_A); /* child watcher */
852 ev_signal_stop (EV_A_ &childev); 919 ev_signal_stop (EV_A_ &childev);
920#endif
853 921
854 ev_ref (EV_A); /* signal watcher */ 922 ev_ref (EV_A); /* signal watcher */
855 ev_io_stop (EV_A_ &sigev); 923 ev_io_stop (EV_A_ &sigev);
856 924
857 close (sigpipe [0]); sigpipe [0] = 0; 925 close (sigpipe [0]); sigpipe [0] = 0;
870 if (method) 938 if (method)
871 postfork = 1; 939 postfork = 1;
872} 940}
873 941
874/*****************************************************************************/ 942/*****************************************************************************/
943
944static int
945any_pending (EV_P)
946{
947 int pri;
948
949 for (pri = NUMPRI; pri--; )
950 if (pendingcnt [pri])
951 return 1;
952
953 return 0;
954}
875 955
876static void 956static void
877call_pending (EV_P) 957call_pending (EV_P)
878{ 958{
879 int pri; 959 int pri;
884 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 964 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
885 965
886 if (p->w) 966 if (p->w)
887 { 967 {
888 p->w->pending = 0; 968 p->w->pending = 0;
889 p->w->cb (EV_A_ p->w, p->events); 969 EV_CB_INVOKE (p->w, p->events);
890 } 970 }
891 } 971 }
892} 972}
893 973
894static void 974static void
908 downheap ((WT *)timers, timercnt, 0); 988 downheap ((WT *)timers, timercnt, 0);
909 } 989 }
910 else 990 else
911 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 991 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
912 992
913 event (EV_A_ (W)w, EV_TIMEOUT); 993 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
914 } 994 }
915} 995}
916 996
917static void 997static void
918periodics_reify (EV_P) 998periodics_reify (EV_P)
919{ 999{
920 while (periodiccnt && ((WT)periodics [0])->at <= rt_now) 1000 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
921 { 1001 {
922 struct ev_periodic *w = periodics [0]; 1002 struct ev_periodic *w = periodics [0];
923 1003
924 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 1004 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
925 1005
926 /* first reschedule or stop timer */ 1006 /* first reschedule or stop timer */
927 if (w->interval) 1007 if (w->reschedule_cb)
928 { 1008 {
1009 ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001);
1010
1011 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now));
1012 downheap ((WT *)periodics, periodiccnt, 0);
1013 }
1014 else if (w->interval)
1015 {
929 ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; 1016 ((WT)w)->at += floor ((ev_rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval;
930 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now)); 1017 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > ev_rt_now));
931 downheap ((WT *)periodics, periodiccnt, 0); 1018 downheap ((WT *)periodics, periodiccnt, 0);
932 } 1019 }
933 else 1020 else
934 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 1021 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
935 1022
936 event (EV_A_ (W)w, EV_PERIODIC); 1023 ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
937 } 1024 }
938} 1025}
939 1026
940static void 1027static void
941periodics_reschedule (EV_P) 1028periodics_reschedule (EV_P)
945 /* adjust periodics after time jump */ 1032 /* adjust periodics after time jump */
946 for (i = 0; i < periodiccnt; ++i) 1033 for (i = 0; i < periodiccnt; ++i)
947 { 1034 {
948 struct ev_periodic *w = periodics [i]; 1035 struct ev_periodic *w = periodics [i];
949 1036
1037 if (w->reschedule_cb)
1038 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
950 if (w->interval) 1039 else if (w->interval)
951 {
952 ev_tstamp diff = ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; 1040 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
953
954 if (fabs (diff) >= 1e-4)
955 {
956 ev_periodic_stop (EV_A_ w);
957 ev_periodic_start (EV_A_ w);
958
959 i = 0; /* restart loop, inefficient, but time jumps should be rare */
960 }
961 }
962 } 1041 }
1042
1043 /* now rebuild the heap */
1044 for (i = periodiccnt >> 1; i--; )
1045 downheap ((WT *)periodics, periodiccnt, i);
963} 1046}
964 1047
965inline int 1048inline int
966time_update_monotonic (EV_P) 1049time_update_monotonic (EV_P)
967{ 1050{
968 mn_now = get_clock (); 1051 mn_now = get_clock ();
969 1052
970 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) 1053 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
971 { 1054 {
972 rt_now = rtmn_diff + mn_now; 1055 ev_rt_now = rtmn_diff + mn_now;
973 return 0; 1056 return 0;
974 } 1057 }
975 else 1058 else
976 { 1059 {
977 now_floor = mn_now; 1060 now_floor = mn_now;
978 rt_now = ev_time (); 1061 ev_rt_now = ev_time ();
979 return 1; 1062 return 1;
980 } 1063 }
981} 1064}
982 1065
983static void 1066static void
992 { 1075 {
993 ev_tstamp odiff = rtmn_diff; 1076 ev_tstamp odiff = rtmn_diff;
994 1077
995 for (i = 4; --i; ) /* loop a few times, before making important decisions */ 1078 for (i = 4; --i; ) /* loop a few times, before making important decisions */
996 { 1079 {
997 rtmn_diff = rt_now - mn_now; 1080 rtmn_diff = ev_rt_now - mn_now;
998 1081
999 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) 1082 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)
1000 return; /* all is well */ 1083 return; /* all is well */
1001 1084
1002 rt_now = ev_time (); 1085 ev_rt_now = ev_time ();
1003 mn_now = get_clock (); 1086 mn_now = get_clock ();
1004 now_floor = mn_now; 1087 now_floor = mn_now;
1005 } 1088 }
1006 1089
1007 periodics_reschedule (EV_A); 1090 periodics_reschedule (EV_A);
1010 } 1093 }
1011 } 1094 }
1012 else 1095 else
1013#endif 1096#endif
1014 { 1097 {
1015 rt_now = ev_time (); 1098 ev_rt_now = ev_time ();
1016 1099
1017 if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) 1100 if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
1018 { 1101 {
1019 periodics_reschedule (EV_A); 1102 periodics_reschedule (EV_A);
1020 1103
1021 /* adjust timers. this is easy, as the offset is the same for all */ 1104 /* adjust timers. this is easy, as the offset is the same for all */
1022 for (i = 0; i < timercnt; ++i) 1105 for (i = 0; i < timercnt; ++i)
1023 ((WT)timers [i])->at += rt_now - mn_now; 1106 ((WT)timers [i])->at += ev_rt_now - mn_now;
1024 } 1107 }
1025 1108
1026 mn_now = rt_now; 1109 mn_now = ev_rt_now;
1027 } 1110 }
1028} 1111}
1029 1112
1030void 1113void
1031ev_ref (EV_P) 1114ev_ref (EV_P)
1063 /* update fd-related kernel structures */ 1146 /* update fd-related kernel structures */
1064 fd_reify (EV_A); 1147 fd_reify (EV_A);
1065 1148
1066 /* calculate blocking time */ 1149 /* calculate blocking time */
1067 1150
1068 /* we only need this for !monotonic clockor timers, but as we basically 1151 /* we only need this for !monotonic clock or timers, but as we basically
1069 always have timers, we just calculate it always */ 1152 always have timers, we just calculate it always */
1070#if EV_USE_MONOTONIC 1153#if EV_USE_MONOTONIC
1071 if (expect_true (have_monotonic)) 1154 if (expect_true (have_monotonic))
1072 time_update_monotonic (EV_A); 1155 time_update_monotonic (EV_A);
1073 else 1156 else
1074#endif 1157#endif
1075 { 1158 {
1076 rt_now = ev_time (); 1159 ev_rt_now = ev_time ();
1077 mn_now = rt_now; 1160 mn_now = ev_rt_now;
1078 } 1161 }
1079 1162
1080 if (flags & EVLOOP_NONBLOCK || idlecnt) 1163 if (flags & EVLOOP_NONBLOCK || idlecnt)
1081 block = 0.; 1164 block = 0.;
1082 else 1165 else
1089 if (block > to) block = to; 1172 if (block > to) block = to;
1090 } 1173 }
1091 1174
1092 if (periodiccnt) 1175 if (periodiccnt)
1093 { 1176 {
1094 ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge; 1177 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge;
1095 if (block > to) block = to; 1178 if (block > to) block = to;
1096 } 1179 }
1097 1180
1098 if (block < 0.) block = 0.; 1181 if (block < 0.) block = 0.;
1099 } 1182 }
1100 1183
1101 method_poll (EV_A_ block); 1184 method_poll (EV_A_ block);
1102 1185
1103 /* update rt_now, do magic */ 1186 /* update ev_rt_now, do magic */
1104 time_update (EV_A); 1187 time_update (EV_A);
1105 1188
1106 /* queue pending timers and reschedule them */ 1189 /* queue pending timers and reschedule them */
1107 timers_reify (EV_A); /* relative timers called last */ 1190 timers_reify (EV_A); /* relative timers called last */
1108 periodics_reify (EV_A); /* absolute timers called first */ 1191 periodics_reify (EV_A); /* absolute timers called first */
1109 1192
1110 /* queue idle watchers unless io or timers are pending */ 1193 /* queue idle watchers unless io or timers are pending */
1111 if (!pendingcnt) 1194 if (idlecnt && !any_pending (EV_A))
1112 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1195 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
1113 1196
1114 /* queue check watchers, to be executed first */ 1197 /* queue check watchers, to be executed first */
1115 if (checkcnt) 1198 if (checkcnt)
1116 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1199 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1191 return; 1274 return;
1192 1275
1193 assert (("ev_io_start called with negative fd", fd >= 0)); 1276 assert (("ev_io_start called with negative fd", fd >= 0));
1194 1277
1195 ev_start (EV_A_ (W)w, 1); 1278 ev_start (EV_A_ (W)w, 1);
1196 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 1279 array_needsize (ANFD, anfds, anfdmax, fd + 1, anfds_init);
1197 wlist_add ((WL *)&anfds[fd].head, (WL)w); 1280 wlist_add ((WL *)&anfds[fd].head, (WL)w);
1198 1281
1199 fd_change (EV_A_ fd); 1282 fd_change (EV_A_ fd);
1200} 1283}
1201 1284
1221 ((WT)w)->at += mn_now; 1304 ((WT)w)->at += mn_now;
1222 1305
1223 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1306 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1224 1307
1225 ev_start (EV_A_ (W)w, ++timercnt); 1308 ev_start (EV_A_ (W)w, ++timercnt);
1226 array_needsize (timers, timermax, timercnt, ); 1309 array_needsize (struct ev_timer *, timers, timermax, timercnt, (void));
1227 timers [timercnt - 1] = w; 1310 timers [timercnt - 1] = w;
1228 upheap ((WT *)timers, timercnt - 1); 1311 upheap ((WT *)timers, timercnt - 1);
1229 1312
1230 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1313 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1231} 1314}
1254ev_timer_again (EV_P_ struct ev_timer *w) 1337ev_timer_again (EV_P_ struct ev_timer *w)
1255{ 1338{
1256 if (ev_is_active (w)) 1339 if (ev_is_active (w))
1257 { 1340 {
1258 if (w->repeat) 1341 if (w->repeat)
1259 {
1260 ((WT)w)->at = mn_now + w->repeat;
1261 downheap ((WT *)timers, timercnt, ((W)w)->active - 1); 1342 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1, mn_now + w->repeat);
1262 }
1263 else 1343 else
1264 ev_timer_stop (EV_A_ w); 1344 ev_timer_stop (EV_A_ w);
1265 } 1345 }
1266 else if (w->repeat) 1346 else if (w->repeat)
1267 ev_timer_start (EV_A_ w); 1347 ev_timer_start (EV_A_ w);
1271ev_periodic_start (EV_P_ struct ev_periodic *w) 1351ev_periodic_start (EV_P_ struct ev_periodic *w)
1272{ 1352{
1273 if (ev_is_active (w)) 1353 if (ev_is_active (w))
1274 return; 1354 return;
1275 1355
1356 if (w->reschedule_cb)
1357 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1358 else if (w->interval)
1359 {
1276 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); 1360 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
1277
1278 /* this formula differs from the one in periodic_reify because we do not always round up */ 1361 /* this formula differs from the one in periodic_reify because we do not always round up */
1279 if (w->interval)
1280 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; 1362 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1363 }
1281 1364
1282 ev_start (EV_A_ (W)w, ++periodiccnt); 1365 ev_start (EV_A_ (W)w, ++periodiccnt);
1283 array_needsize (periodics, periodicmax, periodiccnt, ); 1366 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void));
1284 periodics [periodiccnt - 1] = w; 1367 periodics [periodiccnt - 1] = w;
1285 upheap ((WT *)periodics, periodiccnt - 1); 1368 upheap ((WT *)periodics, periodiccnt - 1);
1286 1369
1287 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1370 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1288} 1371}
1304 1387
1305 ev_stop (EV_A_ (W)w); 1388 ev_stop (EV_A_ (W)w);
1306} 1389}
1307 1390
1308void 1391void
1392ev_periodic_again (EV_P_ struct ev_periodic *w)
1393{
1394 /* TODO: use adjustheap and recalculation */
1395 ev_periodic_stop (EV_A_ w);
1396 ev_periodic_start (EV_A_ w);
1397}
1398
1399void
1309ev_idle_start (EV_P_ struct ev_idle *w) 1400ev_idle_start (EV_P_ struct ev_idle *w)
1310{ 1401{
1311 if (ev_is_active (w)) 1402 if (ev_is_active (w))
1312 return; 1403 return;
1313 1404
1314 ev_start (EV_A_ (W)w, ++idlecnt); 1405 ev_start (EV_A_ (W)w, ++idlecnt);
1315 array_needsize (idles, idlemax, idlecnt, ); 1406 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, (void));
1316 idles [idlecnt - 1] = w; 1407 idles [idlecnt - 1] = w;
1317} 1408}
1318 1409
1319void 1410void
1320ev_idle_stop (EV_P_ struct ev_idle *w) 1411ev_idle_stop (EV_P_ struct ev_idle *w)
1332{ 1423{
1333 if (ev_is_active (w)) 1424 if (ev_is_active (w))
1334 return; 1425 return;
1335 1426
1336 ev_start (EV_A_ (W)w, ++preparecnt); 1427 ev_start (EV_A_ (W)w, ++preparecnt);
1337 array_needsize (prepares, preparemax, preparecnt, ); 1428 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, (void));
1338 prepares [preparecnt - 1] = w; 1429 prepares [preparecnt - 1] = w;
1339} 1430}
1340 1431
1341void 1432void
1342ev_prepare_stop (EV_P_ struct ev_prepare *w) 1433ev_prepare_stop (EV_P_ struct ev_prepare *w)
1354{ 1445{
1355 if (ev_is_active (w)) 1446 if (ev_is_active (w))
1356 return; 1447 return;
1357 1448
1358 ev_start (EV_A_ (W)w, ++checkcnt); 1449 ev_start (EV_A_ (W)w, ++checkcnt);
1359 array_needsize (checks, checkmax, checkcnt, ); 1450 array_needsize (struct ev_check *, checks, checkmax, checkcnt, (void));
1360 checks [checkcnt - 1] = w; 1451 checks [checkcnt - 1] = w;
1361} 1452}
1362 1453
1363void 1454void
1364ev_check_stop (EV_P_ struct ev_check *w) 1455ev_check_stop (EV_P_ struct ev_check *w)
1385 return; 1476 return;
1386 1477
1387 assert (("ev_signal_start called with illegal signal number", w->signum > 0)); 1478 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
1388 1479
1389 ev_start (EV_A_ (W)w, 1); 1480 ev_start (EV_A_ (W)w, 1);
1390 array_needsize (signals, signalmax, w->signum, signals_init); 1481 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init);
1391 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1482 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1392 1483
1393 if (!((WL)w)->next) 1484 if (!((WL)w)->next)
1394 { 1485 {
1395#if WIN32 1486#if WIN32
1478} 1569}
1479 1570
1480void 1571void
1481ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) 1572ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
1482{ 1573{
1483 struct ev_once *once = ev_malloc (sizeof (struct ev_once)); 1574 struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once));
1484 1575
1485 if (!once) 1576 if (!once)
1486 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); 1577 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1487 else 1578 else
1488 { 1579 {
1489 once->cb = cb; 1580 once->cb = cb;
1490 once->arg = arg; 1581 once->arg = arg;
1491 1582
1492 ev_watcher_init (&once->io, once_cb_io); 1583 ev_init (&once->io, once_cb_io);
1493 if (fd >= 0) 1584 if (fd >= 0)
1494 { 1585 {
1495 ev_io_set (&once->io, fd, events); 1586 ev_io_set (&once->io, fd, events);
1496 ev_io_start (EV_A_ &once->io); 1587 ev_io_start (EV_A_ &once->io);
1497 } 1588 }
1498 1589
1499 ev_watcher_init (&once->to, once_cb_to); 1590 ev_init (&once->to, once_cb_to);
1500 if (timeout >= 0.) 1591 if (timeout >= 0.)
1501 { 1592 {
1502 ev_timer_set (&once->to, timeout, 0.); 1593 ev_timer_set (&once->to, timeout, 0.);
1503 ev_timer_start (EV_A_ &once->to); 1594 ev_timer_start (EV_A_ &once->to);
1504 } 1595 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines