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

Comparing libev/ev.c (file contents):
Revision 1.71 by root, Tue Nov 6 13:17:55 2007 UTC vs.
Revision 1.88 by root, Sat Nov 10 04:40:27 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
64#include <assert.h> 69#include <assert.h>
65#include <errno.h> 70#include <errno.h>
66#include <sys/types.h> 71#include <sys/types.h>
67#include <time.h> 72#include <time.h>
68 73
69#ifndef PERL
70# include <signal.h> 74#include <signal.h>
71#endif
72 75
73#ifndef WIN32 76#ifndef WIN32
74# include <unistd.h> 77# include <unistd.h>
75# include <sys/time.h> 78# include <sys/time.h>
76# include <sys/wait.h> 79# include <sys/wait.h>
128#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) */
129#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) */
130#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 */
131/*#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 */
132 135
136#ifdef EV_H
137# include EV_H
138#else
133#include "ev.h" 139# include "ev.h"
140#endif
134 141
135#if __GNUC__ >= 3 142#if __GNUC__ >= 3
136# define expect(expr,value) __builtin_expect ((expr),(value)) 143# define expect(expr,value) __builtin_expect ((expr),(value))
137# define inline inline 144# define inline inline
138#else 145#else
150typedef struct ev_watcher_list *WL; 157typedef struct ev_watcher_list *WL;
151typedef struct ev_watcher_time *WT; 158typedef struct ev_watcher_time *WT;
152 159
153static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 160static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
154 161
155#if WIN32 162#include "ev_win32.c"
156/* note: the comment below could not be substantiated, but what would I care */
157/* MSDN says this is required to handle SIGFPE */
158volatile double SIGFPE_REQ = 0.0f;
159#endif
160 163
161/*****************************************************************************/ 164/*****************************************************************************/
162 165
163static void (*syserr_cb)(const char *msg); 166static void (*syserr_cb)(const char *msg);
164 167
221 int events; 224 int events;
222} ANPENDING; 225} ANPENDING;
223 226
224#if EV_MULTIPLICITY 227#if EV_MULTIPLICITY
225 228
226struct ev_loop 229 struct ev_loop
227{ 230 {
231 ev_tstamp ev_rt_now;
228# define VAR(name,decl) decl; 232 #define VAR(name,decl) decl;
229# include "ev_vars.h" 233 #include "ev_vars.h"
230};
231# undef VAR 234 #undef VAR
235 };
232# include "ev_wrap.h" 236 #include "ev_wrap.h"
237
238 struct ev_loop default_loop_struct;
239 static struct ev_loop *default_loop;
233 240
234#else 241#else
235 242
243 ev_tstamp ev_rt_now;
236# define VAR(name,decl) static decl; 244 #define VAR(name,decl) static decl;
237# include "ev_vars.h" 245 #include "ev_vars.h"
238# undef VAR 246 #undef VAR
247
248 static int default_loop;
239 249
240#endif 250#endif
241 251
242/*****************************************************************************/ 252/*****************************************************************************/
243 253
268#endif 278#endif
269 279
270 return ev_time (); 280 return ev_time ();
271} 281}
272 282
283#if EV_MULTIPLICITY
273ev_tstamp 284ev_tstamp
274ev_now (EV_P) 285ev_now (EV_P)
275{ 286{
276 return rt_now; 287 return ev_rt_now;
277} 288}
289#endif
278 290
279#define array_roundsize(base,n) ((n) | 4 & ~3) 291#define array_roundsize(type,n) ((n) | 4 & ~3)
280 292
281#define array_needsize(base,cur,cnt,init) \ 293#define array_needsize(type,base,cur,cnt,init) \
282 if (expect_false ((cnt) > cur)) \ 294 if (expect_false ((cnt) > cur)) \
283 { \ 295 { \
284 int newcnt = cur; \ 296 int newcnt = cur; \
285 do \ 297 do \
286 { \ 298 { \
287 newcnt = array_roundsize (base, newcnt << 1); \ 299 newcnt = array_roundsize (type, newcnt << 1); \
288 } \ 300 } \
289 while ((cnt) > newcnt); \ 301 while ((cnt) > newcnt); \
290 \ 302 \
291 base = ev_realloc (base, sizeof (*base) * (newcnt)); \ 303 base = (type *)ev_realloc (base, sizeof (type) * (newcnt));\
292 init (base + cur, newcnt - cur); \ 304 init (base + cur, newcnt - cur); \
293 cur = newcnt; \ 305 cur = newcnt; \
294 } 306 }
295 307
296#define array_slim(stem) \ 308#define array_slim(type,stem) \
297 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ 309 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \
298 { \ 310 { \
299 stem ## max = array_roundsize (stem ## cnt >> 1); \ 311 stem ## max = array_roundsize (stem ## cnt >> 1); \
300 base = ev_realloc (base, sizeof (*base) * (stem ## max)); \ 312 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\
301 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ 313 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
302 } 314 }
303 315
304/* microsoft's pseudo-c is quite far from C as the rest of the world and the standard knows it */ 316/* microsoft's pseudo-c is quite far from C as the rest of the world and the standard knows it */
305/* bringing us everlasting joy in form of stupid extra macros that are not required in C */ 317/* bringing us everlasting joy in form of stupid extra macros that are not required in C */
322 334
323 ++base; 335 ++base;
324 } 336 }
325} 337}
326 338
327static void 339void
328event (EV_P_ W w, int events) 340ev_feed_event (EV_P_ void *w, int revents)
329{ 341{
342 W w_ = (W)w;
343
330 if (w->pending) 344 if (w_->pending)
331 { 345 {
332 pendings [ABSPRI (w)][w->pending - 1].events |= events; 346 pendings [ABSPRI (w_)][w_->pending - 1].events |= revents;
333 return; 347 return;
334 } 348 }
335 349
336 w->pending = ++pendingcnt [ABSPRI (w)]; 350 w_->pending = ++pendingcnt [ABSPRI (w_)];
337 array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], (void)); 351 array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], (void));
338 pendings [ABSPRI (w)][w->pending - 1].w = w; 352 pendings [ABSPRI (w_)][w_->pending - 1].w = w_;
339 pendings [ABSPRI (w)][w->pending - 1].events = events; 353 pendings [ABSPRI (w_)][w_->pending - 1].events = revents;
340} 354}
341 355
342static void 356static void
343queue_events (EV_P_ W *events, int eventcnt, int type) 357queue_events (EV_P_ W *events, int eventcnt, int type)
344{ 358{
345 int i; 359 int i;
346 360
347 for (i = 0; i < eventcnt; ++i) 361 for (i = 0; i < eventcnt; ++i)
348 event (EV_A_ events [i], type); 362 ev_feed_event (EV_A_ events [i], type);
349} 363}
350 364
351static void 365inline void
352fd_event (EV_P_ int fd, int events) 366fd_event (EV_P_ int fd, int revents)
353{ 367{
354 ANFD *anfd = anfds + fd; 368 ANFD *anfd = anfds + fd;
355 struct ev_io *w; 369 struct ev_io *w;
356 370
357 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)
358 { 372 {
359 int ev = w->events & events; 373 int ev = w->events & revents;
360 374
361 if (ev) 375 if (ev)
362 event (EV_A_ (W)w, ev); 376 ev_feed_event (EV_A_ (W)w, ev);
363 } 377 }
378}
379
380void
381ev_feed_fd_event (EV_P_ int fd, int revents)
382{
383 fd_event (EV_A_ fd, revents);
364} 384}
365 385
366/*****************************************************************************/ 386/*****************************************************************************/
367 387
368static void 388static void
397 return; 417 return;
398 418
399 anfds [fd].reify = 1; 419 anfds [fd].reify = 1;
400 420
401 ++fdchangecnt; 421 ++fdchangecnt;
402 array_needsize (fdchanges, fdchangemax, fdchangecnt, (void)); 422 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, (void));
403 fdchanges [fdchangecnt - 1] = fd; 423 fdchanges [fdchangecnt - 1] = fd;
404} 424}
405 425
406static void 426static void
407fd_kill (EV_P_ int fd) 427fd_kill (EV_P_ int fd)
409 struct ev_io *w; 429 struct ev_io *w;
410 430
411 while ((w = (struct ev_io *)anfds [fd].head)) 431 while ((w = (struct ev_io *)anfds [fd].head))
412 { 432 {
413 ev_io_stop (EV_A_ w); 433 ev_io_stop (EV_A_ w);
414 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);
415 } 435 }
416} 436}
417 437
418static int 438static int
419fd_valid (int fd) 439fd_valid (int fd)
507 527
508 heap [k] = w; 528 heap [k] = w;
509 ((W)heap [k])->active = k + 1; 529 ((W)heap [k])->active = k + 1;
510} 530}
511 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
512/*****************************************************************************/ 544/*****************************************************************************/
513 545
514typedef struct 546typedef struct
515{ 547{
516 WL head; 548 WL head;
547 579
548 if (!gotsig) 580 if (!gotsig)
549 { 581 {
550 int old_errno = errno; 582 int old_errno = errno;
551 gotsig = 1; 583 gotsig = 1;
584#ifdef WIN32
585 send (sigpipe [1], &signum, 1, MSG_DONTWAIT);
586#else
552 write (sigpipe [1], &signum, 1); 587 write (sigpipe [1], &signum, 1);
588#endif
553 errno = old_errno; 589 errno = old_errno;
554 } 590 }
555} 591}
556 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
557static void 613static void
558sigcb (EV_P_ struct ev_io *iow, int revents) 614sigcb (EV_P_ struct ev_io *iow, int revents)
559{ 615{
560 WL w;
561 int signum; 616 int signum;
562 617
618#ifdef WIN32
619 recv (sigpipe [0], &revents, 1, MSG_DONTWAIT);
620#else
563 read (sigpipe [0], &revents, 1); 621 read (sigpipe [0], &revents, 1);
622#endif
564 gotsig = 0; 623 gotsig = 0;
565 624
566 for (signum = signalmax; signum--; ) 625 for (signum = signalmax; signum--; )
567 if (signals [signum].gotsig) 626 if (signals [signum].gotsig)
568 { 627 ev_feed_signal_event (EV_A_ signum + 1);
569 signals [signum].gotsig = 0;
570
571 for (w = signals [signum].head; w; w = w->next)
572 event (EV_A_ (W)w, EV_SIGNAL);
573 }
574} 628}
575 629
576static void 630static void
577siginit (EV_P) 631siginit (EV_P)
578{ 632{
611 if (w->pid == pid || !w->pid) 665 if (w->pid == pid || !w->pid)
612 { 666 {
613 ev_priority (w) = ev_priority (sw); /* need to do it *now* */ 667 ev_priority (w) = ev_priority (sw); /* need to do it *now* */
614 w->rpid = pid; 668 w->rpid = pid;
615 w->rstatus = status; 669 w->rstatus = status;
616 event (EV_A_ (W)w, EV_CHILD); 670 ev_feed_event (EV_A_ (W)w, EV_CHILD);
617 } 671 }
618} 672}
619 673
620static void 674static void
621childcb (EV_P_ struct ev_signal *sw, int revents) 675childcb (EV_P_ struct ev_signal *sw, int revents)
623 int pid, status; 677 int pid, status;
624 678
625 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) 679 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)))
626 { 680 {
627 /* 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 */
628 event (EV_A_ (W)sw, EV_SIGNAL); 682 ev_feed_event (EV_A_ (W)sw, EV_SIGNAL);
629 683
630 child_reap (EV_A_ sw, pid, pid, status); 684 child_reap (EV_A_ sw, pid, pid, status);
631 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 */
632 } 686 }
633} 687}
690 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 744 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
691 have_monotonic = 1; 745 have_monotonic = 1;
692 } 746 }
693#endif 747#endif
694 748
695 rt_now = ev_time (); 749 ev_rt_now = ev_time ();
696 mn_now = get_clock (); 750 mn_now = get_clock ();
697 now_floor = mn_now; 751 now_floor = mn_now;
698 rtmn_diff = rt_now - mn_now; 752 rtmn_diff = ev_rt_now - mn_now;
699 753
700 if (methods == EVMETHOD_AUTO) 754 if (methods == EVMETHOD_AUTO)
701 if (!enable_secure () && getenv ("LIBEV_METHODS")) 755 if (!enable_secure () && getenv ("LIBEV_METHODS"))
702 methods = atoi (getenv ("LIBEV_METHODS")); 756 methods = atoi (getenv ("LIBEV_METHODS"));
703 else 757 else
718#endif 772#endif
719#if EV_USE_SELECT 773#if EV_USE_SELECT
720 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 774 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
721#endif 775#endif
722 776
723 ev_watcher_init (&sigev, sigcb); 777 ev_init (&sigev, sigcb);
724 ev_set_priority (&sigev, EV_MAXPRI); 778 ev_set_priority (&sigev, EV_MAXPRI);
725 } 779 }
726} 780}
727 781
728void 782void
818} 872}
819 873
820#endif 874#endif
821 875
822#if EV_MULTIPLICITY 876#if EV_MULTIPLICITY
823struct ev_loop default_loop_struct;
824static struct ev_loop *default_loop;
825
826struct ev_loop * 877struct ev_loop *
827#else 878#else
828static int default_loop;
829
830int 879int
831#endif 880#endif
832ev_default_loop (int methods) 881ev_default_loop (int methods)
833{ 882{
834 if (sigpipe [0] == sigpipe [1]) 883 if (sigpipe [0] == sigpipe [1])
895 postfork = 1; 944 postfork = 1;
896} 945}
897 946
898/*****************************************************************************/ 947/*****************************************************************************/
899 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}
960
900static void 961static void
901call_pending (EV_P) 962call_pending (EV_P)
902{ 963{
903 int pri; 964 int pri;
904 965
908 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 969 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
909 970
910 if (p->w) 971 if (p->w)
911 { 972 {
912 p->w->pending = 0; 973 p->w->pending = 0;
913 p->w->cb (EV_A_ p->w, p->events); 974 EV_CB_INVOKE (p->w, p->events);
914 } 975 }
915 } 976 }
916} 977}
917 978
918static void 979static void
932 downheap ((WT *)timers, timercnt, 0); 993 downheap ((WT *)timers, timercnt, 0);
933 } 994 }
934 else 995 else
935 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 996 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
936 997
937 event (EV_A_ (W)w, EV_TIMEOUT); 998 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
938 } 999 }
939} 1000}
940 1001
941static void 1002static void
942periodics_reify (EV_P) 1003periodics_reify (EV_P)
943{ 1004{
944 while (periodiccnt && ((WT)periodics [0])->at <= rt_now) 1005 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
945 { 1006 {
946 struct ev_periodic *w = periodics [0]; 1007 struct ev_periodic *w = periodics [0];
947 1008
948 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 1009 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
949 1010
950 /* first reschedule or stop timer */ 1011 /* first reschedule or stop timer */
951 if (w->interval) 1012 if (w->reschedule_cb)
952 { 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 {
953 ((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;
954 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));
955 downheap ((WT *)periodics, periodiccnt, 0); 1023 downheap ((WT *)periodics, periodiccnt, 0);
956 } 1024 }
957 else 1025 else
958 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 1026 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
959 1027
960 event (EV_A_ (W)w, EV_PERIODIC); 1028 ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
961 } 1029 }
962} 1030}
963 1031
964static void 1032static void
965periodics_reschedule (EV_P) 1033periodics_reschedule (EV_P)
969 /* adjust periodics after time jump */ 1037 /* adjust periodics after time jump */
970 for (i = 0; i < periodiccnt; ++i) 1038 for (i = 0; i < periodiccnt; ++i)
971 { 1039 {
972 struct ev_periodic *w = periodics [i]; 1040 struct ev_periodic *w = periodics [i];
973 1041
1042 if (w->reschedule_cb)
1043 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
974 if (w->interval) 1044 else if (w->interval)
975 {
976 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;
977
978 if (fabs (diff) >= 1e-4)
979 {
980 ev_periodic_stop (EV_A_ w);
981 ev_periodic_start (EV_A_ w);
982
983 i = 0; /* restart loop, inefficient, but time jumps should be rare */
984 }
985 }
986 } 1046 }
1047
1048 /* now rebuild the heap */
1049 for (i = periodiccnt >> 1; i--; )
1050 downheap ((WT *)periodics, periodiccnt, i);
987} 1051}
988 1052
989inline int 1053inline int
990time_update_monotonic (EV_P) 1054time_update_monotonic (EV_P)
991{ 1055{
992 mn_now = get_clock (); 1056 mn_now = get_clock ();
993 1057
994 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) 1058 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
995 { 1059 {
996 rt_now = rtmn_diff + mn_now; 1060 ev_rt_now = rtmn_diff + mn_now;
997 return 0; 1061 return 0;
998 } 1062 }
999 else 1063 else
1000 { 1064 {
1001 now_floor = mn_now; 1065 now_floor = mn_now;
1002 rt_now = ev_time (); 1066 ev_rt_now = ev_time ();
1003 return 1; 1067 return 1;
1004 } 1068 }
1005} 1069}
1006 1070
1007static void 1071static void
1016 { 1080 {
1017 ev_tstamp odiff = rtmn_diff; 1081 ev_tstamp odiff = rtmn_diff;
1018 1082
1019 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 */
1020 { 1084 {
1021 rtmn_diff = rt_now - mn_now; 1085 rtmn_diff = ev_rt_now - mn_now;
1022 1086
1023 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) 1087 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)
1024 return; /* all is well */ 1088 return; /* all is well */
1025 1089
1026 rt_now = ev_time (); 1090 ev_rt_now = ev_time ();
1027 mn_now = get_clock (); 1091 mn_now = get_clock ();
1028 now_floor = mn_now; 1092 now_floor = mn_now;
1029 } 1093 }
1030 1094
1031 periodics_reschedule (EV_A); 1095 periodics_reschedule (EV_A);
1034 } 1098 }
1035 } 1099 }
1036 else 1100 else
1037#endif 1101#endif
1038 { 1102 {
1039 rt_now = ev_time (); 1103 ev_rt_now = ev_time ();
1040 1104
1041 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))
1042 { 1106 {
1043 periodics_reschedule (EV_A); 1107 periodics_reschedule (EV_A);
1044 1108
1045 /* 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 */
1046 for (i = 0; i < timercnt; ++i) 1110 for (i = 0; i < timercnt; ++i)
1047 ((WT)timers [i])->at += rt_now - mn_now; 1111 ((WT)timers [i])->at += ev_rt_now - mn_now;
1048 } 1112 }
1049 1113
1050 mn_now = rt_now; 1114 mn_now = ev_rt_now;
1051 } 1115 }
1052} 1116}
1053 1117
1054void 1118void
1055ev_ref (EV_P) 1119ev_ref (EV_P)
1087 /* update fd-related kernel structures */ 1151 /* update fd-related kernel structures */
1088 fd_reify (EV_A); 1152 fd_reify (EV_A);
1089 1153
1090 /* calculate blocking time */ 1154 /* calculate blocking time */
1091 1155
1092 /* 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
1093 always have timers, we just calculate it always */ 1157 always have timers, we just calculate it always */
1094#if EV_USE_MONOTONIC 1158#if EV_USE_MONOTONIC
1095 if (expect_true (have_monotonic)) 1159 if (expect_true (have_monotonic))
1096 time_update_monotonic (EV_A); 1160 time_update_monotonic (EV_A);
1097 else 1161 else
1098#endif 1162#endif
1099 { 1163 {
1100 rt_now = ev_time (); 1164 ev_rt_now = ev_time ();
1101 mn_now = rt_now; 1165 mn_now = ev_rt_now;
1102 } 1166 }
1103 1167
1104 if (flags & EVLOOP_NONBLOCK || idlecnt) 1168 if (flags & EVLOOP_NONBLOCK || idlecnt)
1105 block = 0.; 1169 block = 0.;
1106 else 1170 else
1113 if (block > to) block = to; 1177 if (block > to) block = to;
1114 } 1178 }
1115 1179
1116 if (periodiccnt) 1180 if (periodiccnt)
1117 { 1181 {
1118 ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge; 1182 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge;
1119 if (block > to) block = to; 1183 if (block > to) block = to;
1120 } 1184 }
1121 1185
1122 if (block < 0.) block = 0.; 1186 if (block < 0.) block = 0.;
1123 } 1187 }
1124 1188
1125 method_poll (EV_A_ block); 1189 method_poll (EV_A_ block);
1126 1190
1127 /* update rt_now, do magic */ 1191 /* update ev_rt_now, do magic */
1128 time_update (EV_A); 1192 time_update (EV_A);
1129 1193
1130 /* queue pending timers and reschedule them */ 1194 /* queue pending timers and reschedule them */
1131 timers_reify (EV_A); /* relative timers called last */ 1195 timers_reify (EV_A); /* relative timers called last */
1132 periodics_reify (EV_A); /* absolute timers called first */ 1196 periodics_reify (EV_A); /* absolute timers called first */
1133 1197
1134 /* queue idle watchers unless io or timers are pending */ 1198 /* queue idle watchers unless io or timers are pending */
1135 if (!pendingcnt) 1199 if (idlecnt && !any_pending (EV_A))
1136 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1200 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
1137 1201
1138 /* queue check watchers, to be executed first */ 1202 /* queue check watchers, to be executed first */
1139 if (checkcnt) 1203 if (checkcnt)
1140 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1204 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1215 return; 1279 return;
1216 1280
1217 assert (("ev_io_start called with negative fd", fd >= 0)); 1281 assert (("ev_io_start called with negative fd", fd >= 0));
1218 1282
1219 ev_start (EV_A_ (W)w, 1); 1283 ev_start (EV_A_ (W)w, 1);
1220 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 1284 array_needsize (ANFD, anfds, anfdmax, fd + 1, anfds_init);
1221 wlist_add ((WL *)&anfds[fd].head, (WL)w); 1285 wlist_add ((WL *)&anfds[fd].head, (WL)w);
1222 1286
1223 fd_change (EV_A_ fd); 1287 fd_change (EV_A_ fd);
1224} 1288}
1225 1289
1245 ((WT)w)->at += mn_now; 1309 ((WT)w)->at += mn_now;
1246 1310
1247 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1311 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1248 1312
1249 ev_start (EV_A_ (W)w, ++timercnt); 1313 ev_start (EV_A_ (W)w, ++timercnt);
1250 array_needsize (timers, timermax, timercnt, (void)); 1314 array_needsize (struct ev_timer *, timers, timermax, timercnt, (void));
1251 timers [timercnt - 1] = w; 1315 timers [timercnt - 1] = w;
1252 upheap ((WT *)timers, timercnt - 1); 1316 upheap ((WT *)timers, timercnt - 1);
1253 1317
1254 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1318 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1255} 1319}
1278ev_timer_again (EV_P_ struct ev_timer *w) 1342ev_timer_again (EV_P_ struct ev_timer *w)
1279{ 1343{
1280 if (ev_is_active (w)) 1344 if (ev_is_active (w))
1281 { 1345 {
1282 if (w->repeat) 1346 if (w->repeat)
1283 {
1284 ((WT)w)->at = mn_now + w->repeat;
1285 downheap ((WT *)timers, timercnt, ((W)w)->active - 1); 1347 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1, mn_now + w->repeat);
1286 }
1287 else 1348 else
1288 ev_timer_stop (EV_A_ w); 1349 ev_timer_stop (EV_A_ w);
1289 } 1350 }
1290 else if (w->repeat) 1351 else if (w->repeat)
1291 ev_timer_start (EV_A_ w); 1352 ev_timer_start (EV_A_ w);
1295ev_periodic_start (EV_P_ struct ev_periodic *w) 1356ev_periodic_start (EV_P_ struct ev_periodic *w)
1296{ 1357{
1297 if (ev_is_active (w)) 1358 if (ev_is_active (w))
1298 return; 1359 return;
1299 1360
1361 if (w->reschedule_cb)
1362 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1363 else if (w->interval)
1364 {
1300 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); 1365 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
1301
1302 /* this formula differs from the one in periodic_reify because we do not always round up */ 1366 /* this formula differs from the one in periodic_reify because we do not always round up */
1303 if (w->interval)
1304 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; 1367 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1368 }
1305 1369
1306 ev_start (EV_A_ (W)w, ++periodiccnt); 1370 ev_start (EV_A_ (W)w, ++periodiccnt);
1307 array_needsize (periodics, periodicmax, periodiccnt, (void)); 1371 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void));
1308 periodics [periodiccnt - 1] = w; 1372 periodics [periodiccnt - 1] = w;
1309 upheap ((WT *)periodics, periodiccnt - 1); 1373 upheap ((WT *)periodics, periodiccnt - 1);
1310 1374
1311 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1375 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1312} 1376}
1328 1392
1329 ev_stop (EV_A_ (W)w); 1393 ev_stop (EV_A_ (W)w);
1330} 1394}
1331 1395
1332void 1396void
1397ev_periodic_again (EV_P_ struct ev_periodic *w)
1398{
1399 /* TODO: use adjustheap and recalculation */
1400 ev_periodic_stop (EV_A_ w);
1401 ev_periodic_start (EV_A_ w);
1402}
1403
1404void
1333ev_idle_start (EV_P_ struct ev_idle *w) 1405ev_idle_start (EV_P_ struct ev_idle *w)
1334{ 1406{
1335 if (ev_is_active (w)) 1407 if (ev_is_active (w))
1336 return; 1408 return;
1337 1409
1338 ev_start (EV_A_ (W)w, ++idlecnt); 1410 ev_start (EV_A_ (W)w, ++idlecnt);
1339 array_needsize (idles, idlemax, idlecnt, (void)); 1411 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, (void));
1340 idles [idlecnt - 1] = w; 1412 idles [idlecnt - 1] = w;
1341} 1413}
1342 1414
1343void 1415void
1344ev_idle_stop (EV_P_ struct ev_idle *w) 1416ev_idle_stop (EV_P_ struct ev_idle *w)
1356{ 1428{
1357 if (ev_is_active (w)) 1429 if (ev_is_active (w))
1358 return; 1430 return;
1359 1431
1360 ev_start (EV_A_ (W)w, ++preparecnt); 1432 ev_start (EV_A_ (W)w, ++preparecnt);
1361 array_needsize (prepares, preparemax, preparecnt, (void)); 1433 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, (void));
1362 prepares [preparecnt - 1] = w; 1434 prepares [preparecnt - 1] = w;
1363} 1435}
1364 1436
1365void 1437void
1366ev_prepare_stop (EV_P_ struct ev_prepare *w) 1438ev_prepare_stop (EV_P_ struct ev_prepare *w)
1378{ 1450{
1379 if (ev_is_active (w)) 1451 if (ev_is_active (w))
1380 return; 1452 return;
1381 1453
1382 ev_start (EV_A_ (W)w, ++checkcnt); 1454 ev_start (EV_A_ (W)w, ++checkcnt);
1383 array_needsize (checks, checkmax, checkcnt, (void)); 1455 array_needsize (struct ev_check *, checks, checkmax, checkcnt, (void));
1384 checks [checkcnt - 1] = w; 1456 checks [checkcnt - 1] = w;
1385} 1457}
1386 1458
1387void 1459void
1388ev_check_stop (EV_P_ struct ev_check *w) 1460ev_check_stop (EV_P_ struct ev_check *w)
1409 return; 1481 return;
1410 1482
1411 assert (("ev_signal_start called with illegal signal number", w->signum > 0)); 1483 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
1412 1484
1413 ev_start (EV_A_ (W)w, 1); 1485 ev_start (EV_A_ (W)w, 1);
1414 array_needsize (signals, signalmax, w->signum, signals_init); 1486 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init);
1415 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1487 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1416 1488
1417 if (!((WL)w)->next) 1489 if (!((WL)w)->next)
1418 { 1490 {
1419#if WIN32 1491#if WIN32
1502} 1574}
1503 1575
1504void 1576void
1505ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) 1577ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
1506{ 1578{
1507 struct ev_once *once = ev_malloc (sizeof (struct ev_once)); 1579 struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once));
1508 1580
1509 if (!once) 1581 if (!once)
1510 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); 1582 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1511 else 1583 else
1512 { 1584 {
1513 once->cb = cb; 1585 once->cb = cb;
1514 once->arg = arg; 1586 once->arg = arg;
1515 1587
1516 ev_watcher_init (&once->io, once_cb_io); 1588 ev_init (&once->io, once_cb_io);
1517 if (fd >= 0) 1589 if (fd >= 0)
1518 { 1590 {
1519 ev_io_set (&once->io, fd, events); 1591 ev_io_set (&once->io, fd, events);
1520 ev_io_start (EV_A_ &once->io); 1592 ev_io_start (EV_A_ &once->io);
1521 } 1593 }
1522 1594
1523 ev_watcher_init (&once->to, once_cb_to); 1595 ev_init (&once->to, once_cb_to);
1524 if (timeout >= 0.) 1596 if (timeout >= 0.)
1525 { 1597 {
1526 ev_timer_set (&once->to, timeout, 0.); 1598 ev_timer_set (&once->to, timeout, 0.);
1527 ev_timer_start (EV_A_ &once->to); 1599 ev_timer_start (EV_A_ &once->to);
1528 } 1600 }
1529 } 1601 }
1530} 1602}
1531 1603
1604#ifdef __cplusplus
1605}
1606#endif
1607

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines