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.85 by root, Sat Nov 10 03:13:50 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 {
279# define VAR(name,decl) decl; 226 #define VAR(name,decl) decl;
280# include "ev_vars.h" 227 #include "ev_vars.h"
281};
282# undef VAR 228 #undef VAR
229 };
283# include "ev_wrap.h" 230 #include "ev_wrap.h"
231
232 struct ev_loop default_loop_struct;
233 static struct ev_loop *default_loop;
284 234
285#else 235#else
286 236
287# define VAR(name,decl) static decl; 237 #define VAR(name,decl) static decl;
288# include "ev_vars.h" 238 #include "ev_vars.h"
289# undef VAR 239 #undef VAR
240
241 static int default_loop;
290 242
291#endif 243#endif
292 244
293/*****************************************************************************/ 245/*****************************************************************************/
294 246
319#endif 271#endif
320 272
321 return ev_time (); 273 return ev_time ();
322} 274}
323 275
276#if EV_MULTIPLICITY
324ev_tstamp 277ev_tstamp
325ev_now (EV_P) 278ev_now (EV_P)
326{ 279{
327 return rt_now; 280 return ev_rt_now;
328} 281}
282#endif
329 283
330#define array_roundsize(base,n) ((n) | 4 & ~3) 284#define array_roundsize(type,n) ((n) | 4 & ~3)
331 285
332#define array_needsize(base,cur,cnt,init) \ 286#define array_needsize(type,base,cur,cnt,init) \
333 if (expect_false ((cnt) > cur)) \ 287 if (expect_false ((cnt) > cur)) \
334 { \ 288 { \
335 int newcnt = cur; \ 289 int newcnt = cur; \
336 do \ 290 do \
337 { \ 291 { \
338 newcnt = array_roundsize (base, newcnt << 1); \ 292 newcnt = array_roundsize (type, newcnt << 1); \
339 } \ 293 } \
340 while ((cnt) > newcnt); \ 294 while ((cnt) > newcnt); \
341 \ 295 \
342 base = ev_realloc (base, sizeof (*base) * (newcnt)); \ 296 base = (type *)ev_realloc (base, sizeof (type) * (newcnt));\
343 init (base + cur, newcnt - cur); \ 297 init (base + cur, newcnt - cur); \
344 cur = newcnt; \ 298 cur = newcnt; \
345 } 299 }
346 300
347#define array_slim(stem) \ 301#define array_slim(type,stem) \
348 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ 302 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \
349 { \ 303 { \
350 stem ## max = array_roundsize (stem ## cnt >> 1); \ 304 stem ## max = array_roundsize (stem ## cnt >> 1); \
351 base = ev_realloc (base, sizeof (*base) * (stem ## max)); \ 305 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\
352 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ 306 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
353 } 307 }
354 308
355/* microsoft's pseudo-c is quite far from C as the rest of the world and the standard knows it */ 309/* 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 */ 310/* bringing us everlasting joy in form of stupid extra macros that are not required in C */
373 327
374 ++base; 328 ++base;
375 } 329 }
376} 330}
377 331
378static void 332void
379event (EV_P_ W w, int events) 333ev_feed_event (EV_P_ void *w, int revents)
380{ 334{
335 W w_ = (W)w;
336
381 if (w->pending) 337 if (w_->pending)
382 { 338 {
383 pendings [ABSPRI (w)][w->pending - 1].events |= events; 339 pendings [ABSPRI (w_)][w_->pending - 1].events |= revents;
384 return; 340 return;
385 } 341 }
386 342
387 w->pending = ++pendingcnt [ABSPRI (w)]; 343 w_->pending = ++pendingcnt [ABSPRI (w_)];
388 array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], (void)); 344 array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], (void));
389 pendings [ABSPRI (w)][w->pending - 1].w = w; 345 pendings [ABSPRI (w_)][w_->pending - 1].w = w_;
390 pendings [ABSPRI (w)][w->pending - 1].events = events; 346 pendings [ABSPRI (w_)][w_->pending - 1].events = revents;
391} 347}
392 348
393static void 349static void
394queue_events (EV_P_ W *events, int eventcnt, int type) 350queue_events (EV_P_ W *events, int eventcnt, int type)
395{ 351{
396 int i; 352 int i;
397 353
398 for (i = 0; i < eventcnt; ++i) 354 for (i = 0; i < eventcnt; ++i)
399 event (EV_A_ events [i], type); 355 ev_feed_event (EV_A_ events [i], type);
400} 356}
401 357
402static void 358inline void
403fd_event (EV_P_ int fd, int events) 359fd_event (EV_P_ int fd, int revents)
404{ 360{
405 ANFD *anfd = anfds + fd; 361 ANFD *anfd = anfds + fd;
406 struct ev_io *w; 362 struct ev_io *w;
407 363
408 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) 364 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
409 { 365 {
410 int ev = w->events & events; 366 int ev = w->events & revents;
411 367
412 if (ev) 368 if (ev)
413 event (EV_A_ (W)w, ev); 369 ev_feed_event (EV_A_ (W)w, ev);
414 } 370 }
371}
372
373void
374ev_feed_fd_event (EV_P_ int fd, int revents)
375{
376 fd_event (EV_A_ fd, revents);
415} 377}
416 378
417/*****************************************************************************/ 379/*****************************************************************************/
418 380
419static void 381static void
448 return; 410 return;
449 411
450 anfds [fd].reify = 1; 412 anfds [fd].reify = 1;
451 413
452 ++fdchangecnt; 414 ++fdchangecnt;
453 array_needsize (fdchanges, fdchangemax, fdchangecnt, (void)); 415 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, (void));
454 fdchanges [fdchangecnt - 1] = fd; 416 fdchanges [fdchangecnt - 1] = fd;
455} 417}
456 418
457static void 419static void
458fd_kill (EV_P_ int fd) 420fd_kill (EV_P_ int fd)
460 struct ev_io *w; 422 struct ev_io *w;
461 423
462 while ((w = (struct ev_io *)anfds [fd].head)) 424 while ((w = (struct ev_io *)anfds [fd].head))
463 { 425 {
464 ev_io_stop (EV_A_ w); 426 ev_io_stop (EV_A_ w);
465 event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); 427 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
466 } 428 }
467} 429}
468 430
469static int 431static int
470fd_valid (int fd) 432fd_valid (int fd)
558 520
559 heap [k] = w; 521 heap [k] = w;
560 ((W)heap [k])->active = k + 1; 522 ((W)heap [k])->active = k + 1;
561} 523}
562 524
525inline void
526adjustheap (WT *heap, int N, int k, ev_tstamp at)
527{
528 ev_tstamp old_at = heap [k]->at;
529 heap [k]->at = at;
530
531 if (old_at < at)
532 downheap (heap, N, k);
533 else
534 upheap (heap, k);
535}
536
563/*****************************************************************************/ 537/*****************************************************************************/
564 538
565typedef struct 539typedef struct
566{ 540{
567 WL head; 541 WL head;
598 572
599 if (!gotsig) 573 if (!gotsig)
600 { 574 {
601 int old_errno = errno; 575 int old_errno = errno;
602 gotsig = 1; 576 gotsig = 1;
577#ifdef WIN32
578 send (sigpipe [1], &signum, 1, MSG_DONTWAIT);
579#else
603 write (sigpipe [1], &signum, 1); 580 write (sigpipe [1], &signum, 1);
581#endif
604 errno = old_errno; 582 errno = old_errno;
605 } 583 }
606} 584}
607 585
586void
587ev_feed_signal_event (EV_P_ int signum)
588{
589 WL w;
590
591#if EV_MULTIPLICITY
592 assert (("feeding signal events is only supported in the default loop", loop == default_loop));
593#endif
594
595 --signum;
596
597 if (signum < 0 || signum >= signalmax)
598 return;
599
600 signals [signum].gotsig = 0;
601
602 for (w = signals [signum].head; w; w = w->next)
603 ev_feed_event (EV_A_ (W)w, EV_SIGNAL);
604}
605
608static void 606static void
609sigcb (EV_P_ struct ev_io *iow, int revents) 607sigcb (EV_P_ struct ev_io *iow, int revents)
610{ 608{
611 WL w;
612 int signum; 609 int signum;
613 610
611#ifdef WIN32
612 recv (sigpipe [0], &revents, 1, MSG_DONTWAIT);
613#else
614 read (sigpipe [0], &revents, 1); 614 read (sigpipe [0], &revents, 1);
615#endif
615 gotsig = 0; 616 gotsig = 0;
616 617
617 for (signum = signalmax; signum--; ) 618 for (signum = signalmax; signum--; )
618 if (signals [signum].gotsig) 619 if (signals [signum].gotsig)
619 { 620 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} 621}
626 622
627static void 623static void
628siginit (EV_P) 624siginit (EV_P)
629{ 625{
662 if (w->pid == pid || !w->pid) 658 if (w->pid == pid || !w->pid)
663 { 659 {
664 ev_priority (w) = ev_priority (sw); /* need to do it *now* */ 660 ev_priority (w) = ev_priority (sw); /* need to do it *now* */
665 w->rpid = pid; 661 w->rpid = pid;
666 w->rstatus = status; 662 w->rstatus = status;
667 event (EV_A_ (W)w, EV_CHILD); 663 ev_feed_event (EV_A_ (W)w, EV_CHILD);
668 } 664 }
669} 665}
670 666
671static void 667static void
672childcb (EV_P_ struct ev_signal *sw, int revents) 668childcb (EV_P_ struct ev_signal *sw, int revents)
674 int pid, status; 670 int pid, status;
675 671
676 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) 672 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)))
677 { 673 {
678 /* make sure we are called again until all childs have been reaped */ 674 /* make sure we are called again until all childs have been reaped */
679 event (EV_A_ (W)sw, EV_SIGNAL); 675 ev_feed_event (EV_A_ (W)sw, EV_SIGNAL);
680 676
681 child_reap (EV_A_ sw, pid, pid, status); 677 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 */ 678 child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */
683 } 679 }
684} 680}
741 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 737 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
742 have_monotonic = 1; 738 have_monotonic = 1;
743 } 739 }
744#endif 740#endif
745 741
746 rt_now = ev_time (); 742 ev_rt_now = ev_time ();
747 mn_now = get_clock (); 743 mn_now = get_clock ();
748 now_floor = mn_now; 744 now_floor = mn_now;
749 rtmn_diff = rt_now - mn_now; 745 rtmn_diff = ev_rt_now - mn_now;
750 746
751 if (methods == EVMETHOD_AUTO) 747 if (methods == EVMETHOD_AUTO)
752 if (!enable_secure () && getenv ("LIBEV_METHODS")) 748 if (!enable_secure () && getenv ("LIBEV_METHODS"))
753 methods = atoi (getenv ("LIBEV_METHODS")); 749 methods = atoi (getenv ("LIBEV_METHODS"));
754 else 750 else
769#endif 765#endif
770#if EV_USE_SELECT 766#if EV_USE_SELECT
771 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 767 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
772#endif 768#endif
773 769
774 ev_watcher_init (&sigev, sigcb); 770 ev_init (&sigev, sigcb);
775 ev_set_priority (&sigev, EV_MAXPRI); 771 ev_set_priority (&sigev, EV_MAXPRI);
776 } 772 }
777} 773}
778 774
779void 775void
828 ev_ref (EV_A); 824 ev_ref (EV_A);
829 ev_io_stop (EV_A_ &sigev); 825 ev_io_stop (EV_A_ &sigev);
830 close (sigpipe [0]); 826 close (sigpipe [0]);
831 close (sigpipe [1]); 827 close (sigpipe [1]);
832 828
833 while (ev_pipe (sigpipe)) 829 while (pipe (sigpipe))
834 syserr ("(libev) error creating pipe"); 830 syserr ("(libev) error creating pipe");
835 831
836 siginit (EV_A); 832 siginit (EV_A);
837 } 833 }
838 834
869} 865}
870 866
871#endif 867#endif
872 868
873#if EV_MULTIPLICITY 869#if EV_MULTIPLICITY
874struct ev_loop default_loop_struct;
875static struct ev_loop *default_loop;
876
877struct ev_loop * 870struct ev_loop *
878#else 871#else
879static int default_loop;
880
881int 872int
882#endif 873#endif
883ev_default_loop (int methods) 874ev_default_loop (int methods)
884{ 875{
885 if (sigpipe [0] == sigpipe [1]) 876 if (sigpipe [0] == sigpipe [1])
886 if (ev_pipe (sigpipe)) 877 if (pipe (sigpipe))
887 return 0; 878 return 0;
888 879
889 if (!default_loop) 880 if (!default_loop)
890 { 881 {
891#if EV_MULTIPLICITY 882#if EV_MULTIPLICITY
946 postfork = 1; 937 postfork = 1;
947} 938}
948 939
949/*****************************************************************************/ 940/*****************************************************************************/
950 941
942static int
943any_pending (EV_P)
944{
945 int pri;
946
947 for (pri = NUMPRI; pri--; )
948 if (pendingcnt [pri])
949 return 1;
950
951 return 0;
952}
953
951static void 954static void
952call_pending (EV_P) 955call_pending (EV_P)
953{ 956{
954 int pri; 957 int pri;
955 958
959 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 962 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
960 963
961 if (p->w) 964 if (p->w)
962 { 965 {
963 p->w->pending = 0; 966 p->w->pending = 0;
964 p->w->cb (EV_A_ p->w, p->events); 967 EV_CB_INVOKE (p->w, p->events);
965 } 968 }
966 } 969 }
967} 970}
968 971
969static void 972static void
983 downheap ((WT *)timers, timercnt, 0); 986 downheap ((WT *)timers, timercnt, 0);
984 } 987 }
985 else 988 else
986 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 989 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
987 990
988 event (EV_A_ (W)w, EV_TIMEOUT); 991 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
989 } 992 }
990} 993}
991 994
992static void 995static void
993periodics_reify (EV_P) 996periodics_reify (EV_P)
994{ 997{
995 while (periodiccnt && ((WT)periodics [0])->at <= rt_now) 998 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
996 { 999 {
997 struct ev_periodic *w = periodics [0]; 1000 struct ev_periodic *w = periodics [0];
998 1001
999 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 1002 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
1000 1003
1001 /* first reschedule or stop timer */ 1004 /* first reschedule or stop timer */
1002 if (w->interval) 1005 if (w->reschedule_cb)
1003 { 1006 {
1007 ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001);
1008
1009 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now));
1010 downheap ((WT *)periodics, periodiccnt, 0);
1011 }
1012 else if (w->interval)
1013 {
1004 ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; 1014 ((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)); 1015 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); 1016 downheap ((WT *)periodics, periodiccnt, 0);
1007 } 1017 }
1008 else 1018 else
1009 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 1019 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
1010 1020
1011 event (EV_A_ (W)w, EV_PERIODIC); 1021 ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
1012 } 1022 }
1013} 1023}
1014 1024
1015static void 1025static void
1016periodics_reschedule (EV_P) 1026periodics_reschedule (EV_P)
1020 /* adjust periodics after time jump */ 1030 /* adjust periodics after time jump */
1021 for (i = 0; i < periodiccnt; ++i) 1031 for (i = 0; i < periodiccnt; ++i)
1022 { 1032 {
1023 struct ev_periodic *w = periodics [i]; 1033 struct ev_periodic *w = periodics [i];
1024 1034
1035 if (w->reschedule_cb)
1036 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1025 if (w->interval) 1037 else if (w->interval)
1026 {
1027 ev_tstamp diff = ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; 1038 ((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 } 1039 }
1040
1041 /* now rebuild the heap */
1042 for (i = periodiccnt >> 1; i--; )
1043 downheap ((WT *)periodics, periodiccnt, i);
1038} 1044}
1039 1045
1040inline int 1046inline int
1041time_update_monotonic (EV_P) 1047time_update_monotonic (EV_P)
1042{ 1048{
1043 mn_now = get_clock (); 1049 mn_now = get_clock ();
1044 1050
1045 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) 1051 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
1046 { 1052 {
1047 rt_now = rtmn_diff + mn_now; 1053 ev_rt_now = rtmn_diff + mn_now;
1048 return 0; 1054 return 0;
1049 } 1055 }
1050 else 1056 else
1051 { 1057 {
1052 now_floor = mn_now; 1058 now_floor = mn_now;
1053 rt_now = ev_time (); 1059 ev_rt_now = ev_time ();
1054 return 1; 1060 return 1;
1055 } 1061 }
1056} 1062}
1057 1063
1058static void 1064static void
1067 { 1073 {
1068 ev_tstamp odiff = rtmn_diff; 1074 ev_tstamp odiff = rtmn_diff;
1069 1075
1070 for (i = 4; --i; ) /* loop a few times, before making important decisions */ 1076 for (i = 4; --i; ) /* loop a few times, before making important decisions */
1071 { 1077 {
1072 rtmn_diff = rt_now - mn_now; 1078 rtmn_diff = ev_rt_now - mn_now;
1073 1079
1074 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) 1080 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)
1075 return; /* all is well */ 1081 return; /* all is well */
1076 1082
1077 rt_now = ev_time (); 1083 ev_rt_now = ev_time ();
1078 mn_now = get_clock (); 1084 mn_now = get_clock ();
1079 now_floor = mn_now; 1085 now_floor = mn_now;
1080 } 1086 }
1081 1087
1082 periodics_reschedule (EV_A); 1088 periodics_reschedule (EV_A);
1085 } 1091 }
1086 } 1092 }
1087 else 1093 else
1088#endif 1094#endif
1089 { 1095 {
1090 rt_now = ev_time (); 1096 ev_rt_now = ev_time ();
1091 1097
1092 if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) 1098 if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
1093 { 1099 {
1094 periodics_reschedule (EV_A); 1100 periodics_reschedule (EV_A);
1095 1101
1096 /* adjust timers. this is easy, as the offset is the same for all */ 1102 /* adjust timers. this is easy, as the offset is the same for all */
1097 for (i = 0; i < timercnt; ++i) 1103 for (i = 0; i < timercnt; ++i)
1098 ((WT)timers [i])->at += rt_now - mn_now; 1104 ((WT)timers [i])->at += ev_rt_now - mn_now;
1099 } 1105 }
1100 1106
1101 mn_now = rt_now; 1107 mn_now = ev_rt_now;
1102 } 1108 }
1103} 1109}
1104 1110
1105void 1111void
1106ev_ref (EV_P) 1112ev_ref (EV_P)
1138 /* update fd-related kernel structures */ 1144 /* update fd-related kernel structures */
1139 fd_reify (EV_A); 1145 fd_reify (EV_A);
1140 1146
1141 /* calculate blocking time */ 1147 /* calculate blocking time */
1142 1148
1143 /* we only need this for !monotonic clockor timers, but as we basically 1149 /* we only need this for !monotonic clock or timers, but as we basically
1144 always have timers, we just calculate it always */ 1150 always have timers, we just calculate it always */
1145#if EV_USE_MONOTONIC 1151#if EV_USE_MONOTONIC
1146 if (expect_true (have_monotonic)) 1152 if (expect_true (have_monotonic))
1147 time_update_monotonic (EV_A); 1153 time_update_monotonic (EV_A);
1148 else 1154 else
1149#endif 1155#endif
1150 { 1156 {
1151 rt_now = ev_time (); 1157 ev_rt_now = ev_time ();
1152 mn_now = rt_now; 1158 mn_now = ev_rt_now;
1153 } 1159 }
1154 1160
1155 if (flags & EVLOOP_NONBLOCK || idlecnt) 1161 if (flags & EVLOOP_NONBLOCK || idlecnt)
1156 block = 0.; 1162 block = 0.;
1157 else 1163 else
1164 if (block > to) block = to; 1170 if (block > to) block = to;
1165 } 1171 }
1166 1172
1167 if (periodiccnt) 1173 if (periodiccnt)
1168 { 1174 {
1169 ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge; 1175 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge;
1170 if (block > to) block = to; 1176 if (block > to) block = to;
1171 } 1177 }
1172 1178
1173 if (block < 0.) block = 0.; 1179 if (block < 0.) block = 0.;
1174 } 1180 }
1175 1181
1176 method_poll (EV_A_ block); 1182 method_poll (EV_A_ block);
1177 1183
1178 /* update rt_now, do magic */ 1184 /* update ev_rt_now, do magic */
1179 time_update (EV_A); 1185 time_update (EV_A);
1180 1186
1181 /* queue pending timers and reschedule them */ 1187 /* queue pending timers and reschedule them */
1182 timers_reify (EV_A); /* relative timers called last */ 1188 timers_reify (EV_A); /* relative timers called last */
1183 periodics_reify (EV_A); /* absolute timers called first */ 1189 periodics_reify (EV_A); /* absolute timers called first */
1184 1190
1185 /* queue idle watchers unless io or timers are pending */ 1191 /* queue idle watchers unless io or timers are pending */
1186 if (!pendingcnt) 1192 if (idlecnt && !any_pending (EV_A))
1187 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1193 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
1188 1194
1189 /* queue check watchers, to be executed first */ 1195 /* queue check watchers, to be executed first */
1190 if (checkcnt) 1196 if (checkcnt)
1191 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1197 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1266 return; 1272 return;
1267 1273
1268 assert (("ev_io_start called with negative fd", fd >= 0)); 1274 assert (("ev_io_start called with negative fd", fd >= 0));
1269 1275
1270 ev_start (EV_A_ (W)w, 1); 1276 ev_start (EV_A_ (W)w, 1);
1271 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 1277 array_needsize (ANFD, anfds, anfdmax, fd + 1, anfds_init);
1272 wlist_add ((WL *)&anfds[fd].head, (WL)w); 1278 wlist_add ((WL *)&anfds[fd].head, (WL)w);
1273 1279
1274 fd_change (EV_A_ fd); 1280 fd_change (EV_A_ fd);
1275} 1281}
1276 1282
1296 ((WT)w)->at += mn_now; 1302 ((WT)w)->at += mn_now;
1297 1303
1298 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1304 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1299 1305
1300 ev_start (EV_A_ (W)w, ++timercnt); 1306 ev_start (EV_A_ (W)w, ++timercnt);
1301 array_needsize (timers, timermax, timercnt, (void)); 1307 array_needsize (struct ev_timer *, timers, timermax, timercnt, (void));
1302 timers [timercnt - 1] = w; 1308 timers [timercnt - 1] = w;
1303 upheap ((WT *)timers, timercnt - 1); 1309 upheap ((WT *)timers, timercnt - 1);
1304 1310
1305 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1311 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1306} 1312}
1329ev_timer_again (EV_P_ struct ev_timer *w) 1335ev_timer_again (EV_P_ struct ev_timer *w)
1330{ 1336{
1331 if (ev_is_active (w)) 1337 if (ev_is_active (w))
1332 { 1338 {
1333 if (w->repeat) 1339 if (w->repeat)
1334 {
1335 ((WT)w)->at = mn_now + w->repeat;
1336 downheap ((WT *)timers, timercnt, ((W)w)->active - 1); 1340 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1, mn_now + w->repeat);
1337 }
1338 else 1341 else
1339 ev_timer_stop (EV_A_ w); 1342 ev_timer_stop (EV_A_ w);
1340 } 1343 }
1341 else if (w->repeat) 1344 else if (w->repeat)
1342 ev_timer_start (EV_A_ w); 1345 ev_timer_start (EV_A_ w);
1346ev_periodic_start (EV_P_ struct ev_periodic *w) 1349ev_periodic_start (EV_P_ struct ev_periodic *w)
1347{ 1350{
1348 if (ev_is_active (w)) 1351 if (ev_is_active (w))
1349 return; 1352 return;
1350 1353
1354 if (w->reschedule_cb)
1355 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1356 else if (w->interval)
1357 {
1351 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); 1358 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 */ 1359 /* 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; 1360 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1361 }
1356 1362
1357 ev_start (EV_A_ (W)w, ++periodiccnt); 1363 ev_start (EV_A_ (W)w, ++periodiccnt);
1358 array_needsize (periodics, periodicmax, periodiccnt, (void)); 1364 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void));
1359 periodics [periodiccnt - 1] = w; 1365 periodics [periodiccnt - 1] = w;
1360 upheap ((WT *)periodics, periodiccnt - 1); 1366 upheap ((WT *)periodics, periodiccnt - 1);
1361 1367
1362 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1368 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1363} 1369}
1379 1385
1380 ev_stop (EV_A_ (W)w); 1386 ev_stop (EV_A_ (W)w);
1381} 1387}
1382 1388
1383void 1389void
1390ev_periodic_again (EV_P_ struct ev_periodic *w)
1391{
1392 /* TODO: use adjustheap and recalculation */
1393 ev_periodic_stop (EV_A_ w);
1394 ev_periodic_start (EV_A_ w);
1395}
1396
1397void
1384ev_idle_start (EV_P_ struct ev_idle *w) 1398ev_idle_start (EV_P_ struct ev_idle *w)
1385{ 1399{
1386 if (ev_is_active (w)) 1400 if (ev_is_active (w))
1387 return; 1401 return;
1388 1402
1389 ev_start (EV_A_ (W)w, ++idlecnt); 1403 ev_start (EV_A_ (W)w, ++idlecnt);
1390 array_needsize (idles, idlemax, idlecnt, (void)); 1404 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, (void));
1391 idles [idlecnt - 1] = w; 1405 idles [idlecnt - 1] = w;
1392} 1406}
1393 1407
1394void 1408void
1395ev_idle_stop (EV_P_ struct ev_idle *w) 1409ev_idle_stop (EV_P_ struct ev_idle *w)
1407{ 1421{
1408 if (ev_is_active (w)) 1422 if (ev_is_active (w))
1409 return; 1423 return;
1410 1424
1411 ev_start (EV_A_ (W)w, ++preparecnt); 1425 ev_start (EV_A_ (W)w, ++preparecnt);
1412 array_needsize (prepares, preparemax, preparecnt, (void)); 1426 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, (void));
1413 prepares [preparecnt - 1] = w; 1427 prepares [preparecnt - 1] = w;
1414} 1428}
1415 1429
1416void 1430void
1417ev_prepare_stop (EV_P_ struct ev_prepare *w) 1431ev_prepare_stop (EV_P_ struct ev_prepare *w)
1429{ 1443{
1430 if (ev_is_active (w)) 1444 if (ev_is_active (w))
1431 return; 1445 return;
1432 1446
1433 ev_start (EV_A_ (W)w, ++checkcnt); 1447 ev_start (EV_A_ (W)w, ++checkcnt);
1434 array_needsize (checks, checkmax, checkcnt, (void)); 1448 array_needsize (struct ev_check *, checks, checkmax, checkcnt, (void));
1435 checks [checkcnt - 1] = w; 1449 checks [checkcnt - 1] = w;
1436} 1450}
1437 1451
1438void 1452void
1439ev_check_stop (EV_P_ struct ev_check *w) 1453ev_check_stop (EV_P_ struct ev_check *w)
1460 return; 1474 return;
1461 1475
1462 assert (("ev_signal_start called with illegal signal number", w->signum > 0)); 1476 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
1463 1477
1464 ev_start (EV_A_ (W)w, 1); 1478 ev_start (EV_A_ (W)w, 1);
1465 array_needsize (signals, signalmax, w->signum, signals_init); 1479 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init);
1466 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1480 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1467 1481
1468 if (!((WL)w)->next) 1482 if (!((WL)w)->next)
1469 { 1483 {
1470#if WIN32 1484#if WIN32
1553} 1567}
1554 1568
1555void 1569void
1556ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) 1570ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
1557{ 1571{
1558 struct ev_once *once = ev_malloc (sizeof (struct ev_once)); 1572 struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once));
1559 1573
1560 if (!once) 1574 if (!once)
1561 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); 1575 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1562 else 1576 else
1563 { 1577 {
1564 once->cb = cb; 1578 once->cb = cb;
1565 once->arg = arg; 1579 once->arg = arg;
1566 1580
1567 ev_watcher_init (&once->io, once_cb_io); 1581 ev_init (&once->io, once_cb_io);
1568 if (fd >= 0) 1582 if (fd >= 0)
1569 { 1583 {
1570 ev_io_set (&once->io, fd, events); 1584 ev_io_set (&once->io, fd, events);
1571 ev_io_start (EV_A_ &once->io); 1585 ev_io_start (EV_A_ &once->io);
1572 } 1586 }
1573 1587
1574 ev_watcher_init (&once->to, once_cb_to); 1588 ev_init (&once->to, once_cb_to);
1575 if (timeout >= 0.) 1589 if (timeout >= 0.)
1576 { 1590 {
1577 ev_timer_set (&once->to, timeout, 0.); 1591 ev_timer_set (&once->to, timeout, 0.);
1578 ev_timer_start (EV_A_ &once->to); 1592 ev_timer_start (EV_A_ &once->to);
1579 } 1593 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines