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

Comparing libev/ev.c (file contents):
Revision 1.72 by root, Tue Nov 6 16:09:37 2007 UTC vs.
Revision 1.86 by root, Sat Nov 10 03:19:21 2007 UTC

126#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) */
127#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) */
128#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 */
129/*#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 */
130 130
131#ifdef EV_H
132# include EV_H
133#else
131#include "ev.h" 134# include "ev.h"
135#endif
132 136
133#if __GNUC__ >= 3 137#if __GNUC__ >= 3
134# define expect(expr,value) __builtin_expect ((expr),(value)) 138# define expect(expr,value) __builtin_expect ((expr),(value))
135# define inline inline 139# define inline inline
136#else 140#else
148typedef struct ev_watcher_list *WL; 152typedef struct ev_watcher_list *WL;
149typedef struct ev_watcher_time *WT; 153typedef struct ev_watcher_time *WT;
150 154
151static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 155static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
152 156
153#if WIN32 157#include "ev_win32.c"
154/* note: the comment below could not be substantiated, but what would I care */
155/* MSDN says this is required to handle SIGFPE */
156volatile double SIGFPE_REQ = 0.0f;
157
158static int
159ev_socketpair_tcp (int filedes [2])
160{
161 struct sockaddr_in addr = { 0 };
162 int addr_size = sizeof (addr);
163 SOCKET listener;
164 SOCKET sock [2] = { -1, -1 };
165
166 if ((listener = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
167 return -1;
168
169 addr.sin_family = AF_INET;
170 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
171 addr.sin_port = 0;
172
173 if (bind (listener, (struct sockaddr *)&addr, addr_size))
174 goto fail;
175
176 if (getsockname(listener, (struct sockaddr *)&addr, &addr_size))
177 goto fail;
178
179 if (listen (listener, 1))
180 goto fail;
181
182 if ((sock [0] = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
183 goto fail;
184
185 if (connect (sock[0], (struct sockaddr *)&addr, addr_size))
186 goto fail;
187
188 if ((sock[1] = accept (listener, 0, 0)) < 0)
189 goto fail;
190
191 closesocket (listener);
192
193 filedes [0] = sock [0];
194 filedes [1] = sock [1];
195
196 return 0;
197
198fail:
199 closesocket (listener);
200
201 if (sock [0] != INVALID_SOCKET) closesocket (sock [0]);
202 if (sock [1] != INVALID_SOCKET) closesocket (sock [1]);
203
204 return -1;
205}
206
207# define ev_pipe(filedes) ev_socketpair_tcp (filedes)
208#else
209# define ev_pipe(filedes) pipe (filedes)
210#endif
211 158
212/*****************************************************************************/ 159/*****************************************************************************/
213 160
214static void (*syserr_cb)(const char *msg); 161static void (*syserr_cb)(const char *msg);
215 162
272 int events; 219 int events;
273} ANPENDING; 220} ANPENDING;
274 221
275#if EV_MULTIPLICITY 222#if EV_MULTIPLICITY
276 223
277struct ev_loop 224 struct ev_loop
278{ 225 {
226 ev_tstamp ev_rt_now;
279# define VAR(name,decl) decl; 227 #define VAR(name,decl) decl;
280# include "ev_vars.h" 228 #include "ev_vars.h"
281};
282# undef VAR 229 #undef VAR
230 };
283# include "ev_wrap.h" 231 #include "ev_wrap.h"
232
233 struct ev_loop default_loop_struct;
234 static struct ev_loop *default_loop;
284 235
285#else 236#else
286 237
238 ev_tstamp ev_rt_now;
287# define VAR(name,decl) static decl; 239 #define VAR(name,decl) static decl;
288# include "ev_vars.h" 240 #include "ev_vars.h"
289# undef VAR 241 #undef VAR
242
243 static int default_loop;
290 244
291#endif 245#endif
292 246
293/*****************************************************************************/ 247/*****************************************************************************/
294 248
319#endif 273#endif
320 274
321 return ev_time (); 275 return ev_time ();
322} 276}
323 277
278#if EV_MULTIPLICITY
324ev_tstamp 279ev_tstamp
325ev_now (EV_P) 280ev_now (EV_P)
326{ 281{
327 return rt_now; 282 return ev_rt_now;
328} 283}
284#endif
329 285
330#define array_roundsize(base,n) ((n) | 4 & ~3) 286#define array_roundsize(type,n) ((n) | 4 & ~3)
331 287
332#define array_needsize(base,cur,cnt,init) \ 288#define array_needsize(type,base,cur,cnt,init) \
333 if (expect_false ((cnt) > cur)) \ 289 if (expect_false ((cnt) > cur)) \
334 { \ 290 { \
335 int newcnt = cur; \ 291 int newcnt = cur; \
336 do \ 292 do \
337 { \ 293 { \
338 newcnt = array_roundsize (base, newcnt << 1); \ 294 newcnt = array_roundsize (type, newcnt << 1); \
339 } \ 295 } \
340 while ((cnt) > newcnt); \ 296 while ((cnt) > newcnt); \
341 \ 297 \
342 base = ev_realloc (base, sizeof (*base) * (newcnt)); \ 298 base = (type *)ev_realloc (base, sizeof (type) * (newcnt));\
343 init (base + cur, newcnt - cur); \ 299 init (base + cur, newcnt - cur); \
344 cur = newcnt; \ 300 cur = newcnt; \
345 } 301 }
346 302
347#define array_slim(stem) \ 303#define array_slim(type,stem) \
348 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ 304 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \
349 { \ 305 { \
350 stem ## max = array_roundsize (stem ## cnt >> 1); \ 306 stem ## max = array_roundsize (stem ## cnt >> 1); \
351 base = ev_realloc (base, sizeof (*base) * (stem ## max)); \ 307 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\
352 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ 308 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
353 } 309 }
354 310
355/* microsoft's pseudo-c is quite far from C as the rest of the world and the standard knows it */ 311/* microsoft's pseudo-c is quite far from C as the rest of the world and the standard knows it */
356/* bringing us everlasting joy in form of stupid extra macros that are not required in C */ 312/* bringing us everlasting joy in form of stupid extra macros that are not required in C */
373 329
374 ++base; 330 ++base;
375 } 331 }
376} 332}
377 333
378static void 334void
379event (EV_P_ W w, int events) 335ev_feed_event (EV_P_ void *w, int revents)
380{ 336{
337 W w_ = (W)w;
338
381 if (w->pending) 339 if (w_->pending)
382 { 340 {
383 pendings [ABSPRI (w)][w->pending - 1].events |= events; 341 pendings [ABSPRI (w_)][w_->pending - 1].events |= revents;
384 return; 342 return;
385 } 343 }
386 344
387 w->pending = ++pendingcnt [ABSPRI (w)]; 345 w_->pending = ++pendingcnt [ABSPRI (w_)];
388 array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], (void)); 346 array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], (void));
389 pendings [ABSPRI (w)][w->pending - 1].w = w; 347 pendings [ABSPRI (w_)][w_->pending - 1].w = w_;
390 pendings [ABSPRI (w)][w->pending - 1].events = events; 348 pendings [ABSPRI (w_)][w_->pending - 1].events = revents;
391} 349}
392 350
393static void 351static void
394queue_events (EV_P_ W *events, int eventcnt, int type) 352queue_events (EV_P_ W *events, int eventcnt, int type)
395{ 353{
396 int i; 354 int i;
397 355
398 for (i = 0; i < eventcnt; ++i) 356 for (i = 0; i < eventcnt; ++i)
399 event (EV_A_ events [i], type); 357 ev_feed_event (EV_A_ events [i], type);
400} 358}
401 359
402static void 360inline void
403fd_event (EV_P_ int fd, int events) 361fd_event (EV_P_ int fd, int revents)
404{ 362{
405 ANFD *anfd = anfds + fd; 363 ANFD *anfd = anfds + fd;
406 struct ev_io *w; 364 struct ev_io *w;
407 365
408 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)
409 { 367 {
410 int ev = w->events & events; 368 int ev = w->events & revents;
411 369
412 if (ev) 370 if (ev)
413 event (EV_A_ (W)w, ev); 371 ev_feed_event (EV_A_ (W)w, ev);
414 } 372 }
373}
374
375void
376ev_feed_fd_event (EV_P_ int fd, int revents)
377{
378 fd_event (EV_A_ fd, revents);
415} 379}
416 380
417/*****************************************************************************/ 381/*****************************************************************************/
418 382
419static void 383static void
448 return; 412 return;
449 413
450 anfds [fd].reify = 1; 414 anfds [fd].reify = 1;
451 415
452 ++fdchangecnt; 416 ++fdchangecnt;
453 array_needsize (fdchanges, fdchangemax, fdchangecnt, (void)); 417 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, (void));
454 fdchanges [fdchangecnt - 1] = fd; 418 fdchanges [fdchangecnt - 1] = fd;
455} 419}
456 420
457static void 421static void
458fd_kill (EV_P_ int fd) 422fd_kill (EV_P_ int fd)
460 struct ev_io *w; 424 struct ev_io *w;
461 425
462 while ((w = (struct ev_io *)anfds [fd].head)) 426 while ((w = (struct ev_io *)anfds [fd].head))
463 { 427 {
464 ev_io_stop (EV_A_ w); 428 ev_io_stop (EV_A_ w);
465 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);
466 } 430 }
467} 431}
468 432
469static int 433static int
470fd_valid (int fd) 434fd_valid (int fd)
558 522
559 heap [k] = w; 523 heap [k] = w;
560 ((W)heap [k])->active = k + 1; 524 ((W)heap [k])->active = k + 1;
561} 525}
562 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
563/*****************************************************************************/ 539/*****************************************************************************/
564 540
565typedef struct 541typedef struct
566{ 542{
567 WL head; 543 WL head;
598 574
599 if (!gotsig) 575 if (!gotsig)
600 { 576 {
601 int old_errno = errno; 577 int old_errno = errno;
602 gotsig = 1; 578 gotsig = 1;
579#ifdef WIN32
580 send (sigpipe [1], &signum, 1, MSG_DONTWAIT);
581#else
603 write (sigpipe [1], &signum, 1); 582 write (sigpipe [1], &signum, 1);
583#endif
604 errno = old_errno; 584 errno = old_errno;
605 } 585 }
606} 586}
607 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
608static void 608static void
609sigcb (EV_P_ struct ev_io *iow, int revents) 609sigcb (EV_P_ struct ev_io *iow, int revents)
610{ 610{
611 WL w;
612 int signum; 611 int signum;
613 612
613#ifdef WIN32
614 recv (sigpipe [0], &revents, 1, MSG_DONTWAIT);
615#else
614 read (sigpipe [0], &revents, 1); 616 read (sigpipe [0], &revents, 1);
617#endif
615 gotsig = 0; 618 gotsig = 0;
616 619
617 for (signum = signalmax; signum--; ) 620 for (signum = signalmax; signum--; )
618 if (signals [signum].gotsig) 621 if (signals [signum].gotsig)
619 { 622 ev_feed_signal_event (EV_A_ signum + 1);
620 signals [signum].gotsig = 0;
621
622 for (w = signals [signum].head; w; w = w->next)
623 event (EV_A_ (W)w, EV_SIGNAL);
624 }
625} 623}
626 624
627static void 625static void
628siginit (EV_P) 626siginit (EV_P)
629{ 627{
662 if (w->pid == pid || !w->pid) 660 if (w->pid == pid || !w->pid)
663 { 661 {
664 ev_priority (w) = ev_priority (sw); /* need to do it *now* */ 662 ev_priority (w) = ev_priority (sw); /* need to do it *now* */
665 w->rpid = pid; 663 w->rpid = pid;
666 w->rstatus = status; 664 w->rstatus = status;
667 event (EV_A_ (W)w, EV_CHILD); 665 ev_feed_event (EV_A_ (W)w, EV_CHILD);
668 } 666 }
669} 667}
670 668
671static void 669static void
672childcb (EV_P_ struct ev_signal *sw, int revents) 670childcb (EV_P_ struct ev_signal *sw, int revents)
674 int pid, status; 672 int pid, status;
675 673
676 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) 674 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)))
677 { 675 {
678 /* 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 */
679 event (EV_A_ (W)sw, EV_SIGNAL); 677 ev_feed_event (EV_A_ (W)sw, EV_SIGNAL);
680 678
681 child_reap (EV_A_ sw, pid, pid, status); 679 child_reap (EV_A_ sw, pid, pid, status);
682 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 */
683 } 681 }
684} 682}
741 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 739 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
742 have_monotonic = 1; 740 have_monotonic = 1;
743 } 741 }
744#endif 742#endif
745 743
746 rt_now = ev_time (); 744 ev_rt_now = ev_time ();
747 mn_now = get_clock (); 745 mn_now = get_clock ();
748 now_floor = mn_now; 746 now_floor = mn_now;
749 rtmn_diff = rt_now - mn_now; 747 rtmn_diff = ev_rt_now - mn_now;
750 748
751 if (methods == EVMETHOD_AUTO) 749 if (methods == EVMETHOD_AUTO)
752 if (!enable_secure () && getenv ("LIBEV_METHODS")) 750 if (!enable_secure () && getenv ("LIBEV_METHODS"))
753 methods = atoi (getenv ("LIBEV_METHODS")); 751 methods = atoi (getenv ("LIBEV_METHODS"));
754 else 752 else
769#endif 767#endif
770#if EV_USE_SELECT 768#if EV_USE_SELECT
771 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 769 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
772#endif 770#endif
773 771
774 ev_watcher_init (&sigev, sigcb); 772 ev_init (&sigev, sigcb);
775 ev_set_priority (&sigev, EV_MAXPRI); 773 ev_set_priority (&sigev, EV_MAXPRI);
776 } 774 }
777} 775}
778 776
779void 777void
828 ev_ref (EV_A); 826 ev_ref (EV_A);
829 ev_io_stop (EV_A_ &sigev); 827 ev_io_stop (EV_A_ &sigev);
830 close (sigpipe [0]); 828 close (sigpipe [0]);
831 close (sigpipe [1]); 829 close (sigpipe [1]);
832 830
833 while (ev_pipe (sigpipe)) 831 while (pipe (sigpipe))
834 syserr ("(libev) error creating pipe"); 832 syserr ("(libev) error creating pipe");
835 833
836 siginit (EV_A); 834 siginit (EV_A);
837 } 835 }
838 836
869} 867}
870 868
871#endif 869#endif
872 870
873#if EV_MULTIPLICITY 871#if EV_MULTIPLICITY
874struct ev_loop default_loop_struct;
875static struct ev_loop *default_loop;
876
877struct ev_loop * 872struct ev_loop *
878#else 873#else
879static int default_loop;
880
881int 874int
882#endif 875#endif
883ev_default_loop (int methods) 876ev_default_loop (int methods)
884{ 877{
885 if (sigpipe [0] == sigpipe [1]) 878 if (sigpipe [0] == sigpipe [1])
886 if (ev_pipe (sigpipe)) 879 if (pipe (sigpipe))
887 return 0; 880 return 0;
888 881
889 if (!default_loop) 882 if (!default_loop)
890 { 883 {
891#if EV_MULTIPLICITY 884#if EV_MULTIPLICITY
946 postfork = 1; 939 postfork = 1;
947} 940}
948 941
949/*****************************************************************************/ 942/*****************************************************************************/
950 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}
955
951static void 956static void
952call_pending (EV_P) 957call_pending (EV_P)
953{ 958{
954 int pri; 959 int pri;
955 960
959 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 964 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
960 965
961 if (p->w) 966 if (p->w)
962 { 967 {
963 p->w->pending = 0; 968 p->w->pending = 0;
964 p->w->cb (EV_A_ p->w, p->events); 969 EV_CB_INVOKE (p->w, p->events);
965 } 970 }
966 } 971 }
967} 972}
968 973
969static void 974static void
983 downheap ((WT *)timers, timercnt, 0); 988 downheap ((WT *)timers, timercnt, 0);
984 } 989 }
985 else 990 else
986 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 991 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
987 992
988 event (EV_A_ (W)w, EV_TIMEOUT); 993 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
989 } 994 }
990} 995}
991 996
992static void 997static void
993periodics_reify (EV_P) 998periodics_reify (EV_P)
994{ 999{
995 while (periodiccnt && ((WT)periodics [0])->at <= rt_now) 1000 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
996 { 1001 {
997 struct ev_periodic *w = periodics [0]; 1002 struct ev_periodic *w = periodics [0];
998 1003
999 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 1004 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
1000 1005
1001 /* first reschedule or stop timer */ 1006 /* first reschedule or stop timer */
1002 if (w->interval) 1007 if (w->reschedule_cb)
1003 { 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 {
1004 ((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;
1005 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));
1006 downheap ((WT *)periodics, periodiccnt, 0); 1018 downheap ((WT *)periodics, periodiccnt, 0);
1007 } 1019 }
1008 else 1020 else
1009 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 1021 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
1010 1022
1011 event (EV_A_ (W)w, EV_PERIODIC); 1023 ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
1012 } 1024 }
1013} 1025}
1014 1026
1015static void 1027static void
1016periodics_reschedule (EV_P) 1028periodics_reschedule (EV_P)
1020 /* adjust periodics after time jump */ 1032 /* adjust periodics after time jump */
1021 for (i = 0; i < periodiccnt; ++i) 1033 for (i = 0; i < periodiccnt; ++i)
1022 { 1034 {
1023 struct ev_periodic *w = periodics [i]; 1035 struct ev_periodic *w = periodics [i];
1024 1036
1037 if (w->reschedule_cb)
1038 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1025 if (w->interval) 1039 else if (w->interval)
1026 {
1027 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;
1028
1029 if (fabs (diff) >= 1e-4)
1030 {
1031 ev_periodic_stop (EV_A_ w);
1032 ev_periodic_start (EV_A_ w);
1033
1034 i = 0; /* restart loop, inefficient, but time jumps should be rare */
1035 }
1036 }
1037 } 1041 }
1042
1043 /* now rebuild the heap */
1044 for (i = periodiccnt >> 1; i--; )
1045 downheap ((WT *)periodics, periodiccnt, i);
1038} 1046}
1039 1047
1040inline int 1048inline int
1041time_update_monotonic (EV_P) 1049time_update_monotonic (EV_P)
1042{ 1050{
1043 mn_now = get_clock (); 1051 mn_now = get_clock ();
1044 1052
1045 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) 1053 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
1046 { 1054 {
1047 rt_now = rtmn_diff + mn_now; 1055 ev_rt_now = rtmn_diff + mn_now;
1048 return 0; 1056 return 0;
1049 } 1057 }
1050 else 1058 else
1051 { 1059 {
1052 now_floor = mn_now; 1060 now_floor = mn_now;
1053 rt_now = ev_time (); 1061 ev_rt_now = ev_time ();
1054 return 1; 1062 return 1;
1055 } 1063 }
1056} 1064}
1057 1065
1058static void 1066static void
1067 { 1075 {
1068 ev_tstamp odiff = rtmn_diff; 1076 ev_tstamp odiff = rtmn_diff;
1069 1077
1070 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 */
1071 { 1079 {
1072 rtmn_diff = rt_now - mn_now; 1080 rtmn_diff = ev_rt_now - mn_now;
1073 1081
1074 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) 1082 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)
1075 return; /* all is well */ 1083 return; /* all is well */
1076 1084
1077 rt_now = ev_time (); 1085 ev_rt_now = ev_time ();
1078 mn_now = get_clock (); 1086 mn_now = get_clock ();
1079 now_floor = mn_now; 1087 now_floor = mn_now;
1080 } 1088 }
1081 1089
1082 periodics_reschedule (EV_A); 1090 periodics_reschedule (EV_A);
1085 } 1093 }
1086 } 1094 }
1087 else 1095 else
1088#endif 1096#endif
1089 { 1097 {
1090 rt_now = ev_time (); 1098 ev_rt_now = ev_time ();
1091 1099
1092 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))
1093 { 1101 {
1094 periodics_reschedule (EV_A); 1102 periodics_reschedule (EV_A);
1095 1103
1096 /* 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 */
1097 for (i = 0; i < timercnt; ++i) 1105 for (i = 0; i < timercnt; ++i)
1098 ((WT)timers [i])->at += rt_now - mn_now; 1106 ((WT)timers [i])->at += ev_rt_now - mn_now;
1099 } 1107 }
1100 1108
1101 mn_now = rt_now; 1109 mn_now = ev_rt_now;
1102 } 1110 }
1103} 1111}
1104 1112
1105void 1113void
1106ev_ref (EV_P) 1114ev_ref (EV_P)
1138 /* update fd-related kernel structures */ 1146 /* update fd-related kernel structures */
1139 fd_reify (EV_A); 1147 fd_reify (EV_A);
1140 1148
1141 /* calculate blocking time */ 1149 /* calculate blocking time */
1142 1150
1143 /* 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
1144 always have timers, we just calculate it always */ 1152 always have timers, we just calculate it always */
1145#if EV_USE_MONOTONIC 1153#if EV_USE_MONOTONIC
1146 if (expect_true (have_monotonic)) 1154 if (expect_true (have_monotonic))
1147 time_update_monotonic (EV_A); 1155 time_update_monotonic (EV_A);
1148 else 1156 else
1149#endif 1157#endif
1150 { 1158 {
1151 rt_now = ev_time (); 1159 ev_rt_now = ev_time ();
1152 mn_now = rt_now; 1160 mn_now = ev_rt_now;
1153 } 1161 }
1154 1162
1155 if (flags & EVLOOP_NONBLOCK || idlecnt) 1163 if (flags & EVLOOP_NONBLOCK || idlecnt)
1156 block = 0.; 1164 block = 0.;
1157 else 1165 else
1164 if (block > to) block = to; 1172 if (block > to) block = to;
1165 } 1173 }
1166 1174
1167 if (periodiccnt) 1175 if (periodiccnt)
1168 { 1176 {
1169 ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge; 1177 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge;
1170 if (block > to) block = to; 1178 if (block > to) block = to;
1171 } 1179 }
1172 1180
1173 if (block < 0.) block = 0.; 1181 if (block < 0.) block = 0.;
1174 } 1182 }
1175 1183
1176 method_poll (EV_A_ block); 1184 method_poll (EV_A_ block);
1177 1185
1178 /* update rt_now, do magic */ 1186 /* update ev_rt_now, do magic */
1179 time_update (EV_A); 1187 time_update (EV_A);
1180 1188
1181 /* queue pending timers and reschedule them */ 1189 /* queue pending timers and reschedule them */
1182 timers_reify (EV_A); /* relative timers called last */ 1190 timers_reify (EV_A); /* relative timers called last */
1183 periodics_reify (EV_A); /* absolute timers called first */ 1191 periodics_reify (EV_A); /* absolute timers called first */
1184 1192
1185 /* queue idle watchers unless io or timers are pending */ 1193 /* queue idle watchers unless io or timers are pending */
1186 if (!pendingcnt) 1194 if (idlecnt && !any_pending (EV_A))
1187 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1195 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
1188 1196
1189 /* queue check watchers, to be executed first */ 1197 /* queue check watchers, to be executed first */
1190 if (checkcnt) 1198 if (checkcnt)
1191 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1199 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1266 return; 1274 return;
1267 1275
1268 assert (("ev_io_start called with negative fd", fd >= 0)); 1276 assert (("ev_io_start called with negative fd", fd >= 0));
1269 1277
1270 ev_start (EV_A_ (W)w, 1); 1278 ev_start (EV_A_ (W)w, 1);
1271 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 1279 array_needsize (ANFD, anfds, anfdmax, fd + 1, anfds_init);
1272 wlist_add ((WL *)&anfds[fd].head, (WL)w); 1280 wlist_add ((WL *)&anfds[fd].head, (WL)w);
1273 1281
1274 fd_change (EV_A_ fd); 1282 fd_change (EV_A_ fd);
1275} 1283}
1276 1284
1296 ((WT)w)->at += mn_now; 1304 ((WT)w)->at += mn_now;
1297 1305
1298 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.));
1299 1307
1300 ev_start (EV_A_ (W)w, ++timercnt); 1308 ev_start (EV_A_ (W)w, ++timercnt);
1301 array_needsize (timers, timermax, timercnt, (void)); 1309 array_needsize (struct ev_timer *, timers, timermax, timercnt, (void));
1302 timers [timercnt - 1] = w; 1310 timers [timercnt - 1] = w;
1303 upheap ((WT *)timers, timercnt - 1); 1311 upheap ((WT *)timers, timercnt - 1);
1304 1312
1305 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1313 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1306} 1314}
1329ev_timer_again (EV_P_ struct ev_timer *w) 1337ev_timer_again (EV_P_ struct ev_timer *w)
1330{ 1338{
1331 if (ev_is_active (w)) 1339 if (ev_is_active (w))
1332 { 1340 {
1333 if (w->repeat) 1341 if (w->repeat)
1334 {
1335 ((WT)w)->at = mn_now + w->repeat;
1336 downheap ((WT *)timers, timercnt, ((W)w)->active - 1); 1342 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1, mn_now + w->repeat);
1337 }
1338 else 1343 else
1339 ev_timer_stop (EV_A_ w); 1344 ev_timer_stop (EV_A_ w);
1340 } 1345 }
1341 else if (w->repeat) 1346 else if (w->repeat)
1342 ev_timer_start (EV_A_ w); 1347 ev_timer_start (EV_A_ w);
1346ev_periodic_start (EV_P_ struct ev_periodic *w) 1351ev_periodic_start (EV_P_ struct ev_periodic *w)
1347{ 1352{
1348 if (ev_is_active (w)) 1353 if (ev_is_active (w))
1349 return; 1354 return;
1350 1355
1356 if (w->reschedule_cb)
1357 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1358 else if (w->interval)
1359 {
1351 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.));
1352
1353 /* 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 */
1354 if (w->interval)
1355 ((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 }
1356 1364
1357 ev_start (EV_A_ (W)w, ++periodiccnt); 1365 ev_start (EV_A_ (W)w, ++periodiccnt);
1358 array_needsize (periodics, periodicmax, periodiccnt, (void)); 1366 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void));
1359 periodics [periodiccnt - 1] = w; 1367 periodics [periodiccnt - 1] = w;
1360 upheap ((WT *)periodics, periodiccnt - 1); 1368 upheap ((WT *)periodics, periodiccnt - 1);
1361 1369
1362 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1370 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1363} 1371}
1379 1387
1380 ev_stop (EV_A_ (W)w); 1388 ev_stop (EV_A_ (W)w);
1381} 1389}
1382 1390
1383void 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
1384ev_idle_start (EV_P_ struct ev_idle *w) 1400ev_idle_start (EV_P_ struct ev_idle *w)
1385{ 1401{
1386 if (ev_is_active (w)) 1402 if (ev_is_active (w))
1387 return; 1403 return;
1388 1404
1389 ev_start (EV_A_ (W)w, ++idlecnt); 1405 ev_start (EV_A_ (W)w, ++idlecnt);
1390 array_needsize (idles, idlemax, idlecnt, (void)); 1406 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, (void));
1391 idles [idlecnt - 1] = w; 1407 idles [idlecnt - 1] = w;
1392} 1408}
1393 1409
1394void 1410void
1395ev_idle_stop (EV_P_ struct ev_idle *w) 1411ev_idle_stop (EV_P_ struct ev_idle *w)
1407{ 1423{
1408 if (ev_is_active (w)) 1424 if (ev_is_active (w))
1409 return; 1425 return;
1410 1426
1411 ev_start (EV_A_ (W)w, ++preparecnt); 1427 ev_start (EV_A_ (W)w, ++preparecnt);
1412 array_needsize (prepares, preparemax, preparecnt, (void)); 1428 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, (void));
1413 prepares [preparecnt - 1] = w; 1429 prepares [preparecnt - 1] = w;
1414} 1430}
1415 1431
1416void 1432void
1417ev_prepare_stop (EV_P_ struct ev_prepare *w) 1433ev_prepare_stop (EV_P_ struct ev_prepare *w)
1429{ 1445{
1430 if (ev_is_active (w)) 1446 if (ev_is_active (w))
1431 return; 1447 return;
1432 1448
1433 ev_start (EV_A_ (W)w, ++checkcnt); 1449 ev_start (EV_A_ (W)w, ++checkcnt);
1434 array_needsize (checks, checkmax, checkcnt, (void)); 1450 array_needsize (struct ev_check *, checks, checkmax, checkcnt, (void));
1435 checks [checkcnt - 1] = w; 1451 checks [checkcnt - 1] = w;
1436} 1452}
1437 1453
1438void 1454void
1439ev_check_stop (EV_P_ struct ev_check *w) 1455ev_check_stop (EV_P_ struct ev_check *w)
1460 return; 1476 return;
1461 1477
1462 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));
1463 1479
1464 ev_start (EV_A_ (W)w, 1); 1480 ev_start (EV_A_ (W)w, 1);
1465 array_needsize (signals, signalmax, w->signum, signals_init); 1481 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init);
1466 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1482 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1467 1483
1468 if (!((WL)w)->next) 1484 if (!((WL)w)->next)
1469 { 1485 {
1470#if WIN32 1486#if WIN32
1553} 1569}
1554 1570
1555void 1571void
1556ev_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)
1557{ 1573{
1558 struct ev_once *once = ev_malloc (sizeof (struct ev_once)); 1574 struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once));
1559 1575
1560 if (!once) 1576 if (!once)
1561 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); 1577 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1562 else 1578 else
1563 { 1579 {
1564 once->cb = cb; 1580 once->cb = cb;
1565 once->arg = arg; 1581 once->arg = arg;
1566 1582
1567 ev_watcher_init (&once->io, once_cb_io); 1583 ev_init (&once->io, once_cb_io);
1568 if (fd >= 0) 1584 if (fd >= 0)
1569 { 1585 {
1570 ev_io_set (&once->io, fd, events); 1586 ev_io_set (&once->io, fd, events);
1571 ev_io_start (EV_A_ &once->io); 1587 ev_io_start (EV_A_ &once->io);
1572 } 1588 }
1573 1589
1574 ev_watcher_init (&once->to, once_cb_to); 1590 ev_init (&once->to, once_cb_to);
1575 if (timeout >= 0.) 1591 if (timeout >= 0.)
1576 { 1592 {
1577 ev_timer_set (&once->to, timeout, 0.); 1593 ev_timer_set (&once->to, timeout, 0.);
1578 ev_timer_start (EV_A_ &once->to); 1594 ev_timer_start (EV_A_ &once->to);
1579 } 1595 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines