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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines