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

Comparing libev/ev.c (file contents):
Revision 1.70 by root, Tue Nov 6 00:52:32 2007 UTC vs.
Revision 1.80 by root, Fri Nov 9 15:30:59 2007 UTC

54 54
55#endif 55#endif
56 56
57#include <math.h> 57#include <math.h>
58#include <stdlib.h> 58#include <stdlib.h>
59#include <unistd.h>
60#include <fcntl.h> 59#include <fcntl.h>
61#include <signal.h>
62#include <stddef.h> 60#include <stddef.h>
63 61
64#include <stdio.h> 62#include <stdio.h>
65 63
66#include <assert.h> 64#include <assert.h>
67#include <errno.h> 65#include <errno.h>
68#include <sys/types.h> 66#include <sys/types.h>
67#include <time.h>
68
69#include <signal.h>
70
69#ifndef WIN32 71#ifndef WIN32
72# include <unistd.h>
73# include <sys/time.h>
70# include <sys/wait.h> 74# include <sys/wait.h>
71#endif 75#endif
72#include <sys/time.h>
73#include <time.h>
74
75/**/ 76/**/
76 77
77#ifndef EV_USE_MONOTONIC 78#ifndef EV_USE_MONOTONIC
78# define EV_USE_MONOTONIC 1 79# define EV_USE_MONOTONIC 1
79#endif 80#endif
94# define EV_USE_KQUEUE 0 95# define EV_USE_KQUEUE 0
95#endif 96#endif
96 97
97#ifndef EV_USE_WIN32 98#ifndef EV_USE_WIN32
98# ifdef WIN32 99# ifdef WIN32
100# define EV_USE_WIN32 0 /* it does not exist, use select */
101# undef EV_USE_SELECT
99# define EV_USE_WIN32 1 102# define EV_USE_SELECT 1
100# else 103# else
101# define EV_USE_WIN32 0 104# define EV_USE_WIN32 0
102# endif 105# endif
103#endif 106#endif
104 107
145typedef struct ev_watcher_list *WL; 148typedef struct ev_watcher_list *WL;
146typedef struct ev_watcher_time *WT; 149typedef struct ev_watcher_time *WT;
147 150
148static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 151static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
149 152
150#if WIN32 153#include "ev_win32.c"
151/* note: the comment below could not be substantiated, but what would I care */
152/* MSDN says this is required to handle SIGFPE */
153volatile double SIGFPE_REQ = 0.0f;
154#endif
155 154
156/*****************************************************************************/ 155/*****************************************************************************/
157 156
158static void (*syserr_cb)(const char *msg); 157static void (*syserr_cb)(const char *msg);
159 158
216 int events; 215 int events;
217} ANPENDING; 216} ANPENDING;
218 217
219#if EV_MULTIPLICITY 218#if EV_MULTIPLICITY
220 219
221struct ev_loop 220 struct ev_loop
222{ 221 {
223# define VAR(name,decl) decl; 222 #define VAR(name,decl) decl;
224# include "ev_vars.h" 223 #include "ev_vars.h"
225};
226# undef VAR 224 #undef VAR
225 };
227# include "ev_wrap.h" 226 #include "ev_wrap.h"
227
228 struct ev_loop default_loop_struct;
229 static struct ev_loop *default_loop;
228 230
229#else 231#else
230 232
231# define VAR(name,decl) static decl; 233 #define VAR(name,decl) static decl;
232# include "ev_vars.h" 234 #include "ev_vars.h"
233# undef VAR 235 #undef VAR
236
237 static int default_loop;
234 238
235#endif 239#endif
236 240
237/*****************************************************************************/ 241/*****************************************************************************/
238 242
269ev_now (EV_P) 273ev_now (EV_P)
270{ 274{
271 return rt_now; 275 return rt_now;
272} 276}
273 277
274#define array_roundsize(base,n) ((n) | 4 & ~3) 278#define array_roundsize(type,n) ((n) | 4 & ~3)
275 279
276#define array_needsize(base,cur,cnt,init) \ 280#define array_needsize(type,base,cur,cnt,init) \
277 if (expect_false ((cnt) > cur)) \ 281 if (expect_false ((cnt) > cur)) \
278 { \ 282 { \
279 int newcnt = cur; \ 283 int newcnt = cur; \
280 do \ 284 do \
281 { \ 285 { \
282 newcnt = array_roundsize (base, newcnt << 1); \ 286 newcnt = array_roundsize (type, newcnt << 1); \
283 } \ 287 } \
284 while ((cnt) > newcnt); \ 288 while ((cnt) > newcnt); \
285 \ 289 \
286 base = ev_realloc (base, sizeof (*base) * (newcnt)); \ 290 base = (type *)ev_realloc (base, sizeof (type) * (newcnt));\
287 init (base + cur, newcnt - cur); \ 291 init (base + cur, newcnt - cur); \
288 cur = newcnt; \ 292 cur = newcnt; \
289 } 293 }
290 294
291#define array_slim(stem) \ 295#define array_slim(type,stem) \
292 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ 296 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \
293 { \ 297 { \
294 stem ## max = array_roundsize (stem ## cnt >> 1); \ 298 stem ## max = array_roundsize (stem ## cnt >> 1); \
295 base = ev_realloc (base, sizeof (*base) * (stem ## max)); \ 299 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\
296 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ 300 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
297 } 301 }
302
303/* microsoft's pseudo-c is quite far from C as the rest of the world and the standard knows it */
304/* bringing us everlasting joy in form of stupid extra macros that are not required in C */
305#define array_free_microshit(stem) \
306 ev_free (stem ## s); stem ## cnt = stem ## max = 0;
298 307
299#define array_free(stem, idx) \ 308#define array_free(stem, idx) \
300 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; 309 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
301 310
302/*****************************************************************************/ 311/*****************************************************************************/
312 321
313 ++base; 322 ++base;
314 } 323 }
315} 324}
316 325
317static void 326void
318event (EV_P_ W w, int events) 327ev_feed_event (EV_P_ void *w, int revents)
319{ 328{
329 W w_ = (W)w;
330
320 if (w->pending) 331 if (w_->pending)
321 { 332 {
322 pendings [ABSPRI (w)][w->pending - 1].events |= events; 333 pendings [ABSPRI (w_)][w_->pending - 1].events |= revents;
323 return; 334 return;
324 } 335 }
325 336
326 w->pending = ++pendingcnt [ABSPRI (w)]; 337 w_->pending = ++pendingcnt [ABSPRI (w_)];
327 array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], ); 338 array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], (void));
328 pendings [ABSPRI (w)][w->pending - 1].w = w; 339 pendings [ABSPRI (w_)][w_->pending - 1].w = w_;
329 pendings [ABSPRI (w)][w->pending - 1].events = events; 340 pendings [ABSPRI (w_)][w_->pending - 1].events = revents;
330} 341}
331 342
332static void 343static void
333queue_events (EV_P_ W *events, int eventcnt, int type) 344queue_events (EV_P_ W *events, int eventcnt, int type)
334{ 345{
335 int i; 346 int i;
336 347
337 for (i = 0; i < eventcnt; ++i) 348 for (i = 0; i < eventcnt; ++i)
338 event (EV_A_ events [i], type); 349 ev_feed_event (EV_A_ events [i], type);
339} 350}
340 351
341static void 352inline void
342fd_event (EV_P_ int fd, int events) 353fd_event (EV_P_ int fd, int revents)
343{ 354{
344 ANFD *anfd = anfds + fd; 355 ANFD *anfd = anfds + fd;
345 struct ev_io *w; 356 struct ev_io *w;
346 357
347 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) 358 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
348 { 359 {
349 int ev = w->events & events; 360 int ev = w->events & revents;
350 361
351 if (ev) 362 if (ev)
352 event (EV_A_ (W)w, ev); 363 ev_feed_event (EV_A_ (W)w, ev);
353 } 364 }
365}
366
367void
368ev_feed_fd_event (EV_P_ int fd, int revents)
369{
370 fd_event (EV_A_ fd, revents);
354} 371}
355 372
356/*****************************************************************************/ 373/*****************************************************************************/
357 374
358static void 375static void
387 return; 404 return;
388 405
389 anfds [fd].reify = 1; 406 anfds [fd].reify = 1;
390 407
391 ++fdchangecnt; 408 ++fdchangecnt;
392 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 409 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, (void));
393 fdchanges [fdchangecnt - 1] = fd; 410 fdchanges [fdchangecnt - 1] = fd;
394} 411}
395 412
396static void 413static void
397fd_kill (EV_P_ int fd) 414fd_kill (EV_P_ int fd)
399 struct ev_io *w; 416 struct ev_io *w;
400 417
401 while ((w = (struct ev_io *)anfds [fd].head)) 418 while ((w = (struct ev_io *)anfds [fd].head))
402 { 419 {
403 ev_io_stop (EV_A_ w); 420 ev_io_stop (EV_A_ w);
404 event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); 421 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
405 } 422 }
423}
424
425static int
426fd_valid (int fd)
427{
428#ifdef WIN32
429 return !!win32_get_osfhandle (fd);
430#else
431 return fcntl (fd, F_GETFD) != -1;
432#endif
406} 433}
407 434
408/* called on EBADF to verify fds */ 435/* called on EBADF to verify fds */
409static void 436static void
410fd_ebadf (EV_P) 437fd_ebadf (EV_P)
411{ 438{
412 int fd; 439 int fd;
413 440
414 for (fd = 0; fd < anfdmax; ++fd) 441 for (fd = 0; fd < anfdmax; ++fd)
415 if (anfds [fd].events) 442 if (anfds [fd].events)
416 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 443 if (!fd_valid (fd) == -1 && errno == EBADF)
417 fd_kill (EV_A_ fd); 444 fd_kill (EV_A_ fd);
418} 445}
419 446
420/* called on ENOMEM in select/poll to kill some fds and retry */ 447/* called on ENOMEM in select/poll to kill some fds and retry */
421static void 448static void
527 554
528 if (!gotsig) 555 if (!gotsig)
529 { 556 {
530 int old_errno = errno; 557 int old_errno = errno;
531 gotsig = 1; 558 gotsig = 1;
559#ifdef WIN32
560 send (sigpipe [1], &signum, 1, MSG_DONTWAIT);
561#else
532 write (sigpipe [1], &signum, 1); 562 write (sigpipe [1], &signum, 1);
563#endif
533 errno = old_errno; 564 errno = old_errno;
534 } 565 }
535} 566}
536 567
568void
569ev_feed_signal_event (EV_P_ int signum)
570{
571 WL w;
572
573#if EV_MULTIPLICITY
574 assert (("feeding signal events is only supported in the default loop", loop == default_loop));
575#endif
576
577 --signum;
578
579 if (signum < 0 || signum >= signalmax)
580 return;
581
582 signals [signum].gotsig = 0;
583
584 for (w = signals [signum].head; w; w = w->next)
585 ev_feed_event (EV_A_ (W)w, EV_SIGNAL);
586}
587
537static void 588static void
538sigcb (EV_P_ struct ev_io *iow, int revents) 589sigcb (EV_P_ struct ev_io *iow, int revents)
539{ 590{
540 WL w;
541 int signum; 591 int signum;
542 592
593#ifdef WIN32
594 recv (sigpipe [0], &revents, 1, MSG_DONTWAIT);
595#else
543 read (sigpipe [0], &revents, 1); 596 read (sigpipe [0], &revents, 1);
597#endif
544 gotsig = 0; 598 gotsig = 0;
545 599
546 for (signum = signalmax; signum--; ) 600 for (signum = signalmax; signum--; )
547 if (signals [signum].gotsig) 601 if (signals [signum].gotsig)
548 { 602 ev_feed_signal_event (EV_A_ signum + 1);
549 signals [signum].gotsig = 0;
550
551 for (w = signals [signum].head; w; w = w->next)
552 event (EV_A_ (W)w, EV_SIGNAL);
553 }
554} 603}
555 604
556static void 605static void
557siginit (EV_P) 606siginit (EV_P)
558{ 607{
570 ev_unref (EV_A); /* child watcher should not keep loop alive */ 619 ev_unref (EV_A); /* child watcher should not keep loop alive */
571} 620}
572 621
573/*****************************************************************************/ 622/*****************************************************************************/
574 623
624static struct ev_child *childs [PID_HASHSIZE];
625
575#ifndef WIN32 626#ifndef WIN32
576 627
577static struct ev_child *childs [PID_HASHSIZE];
578static struct ev_signal childev; 628static struct ev_signal childev;
579 629
580#ifndef WCONTINUED 630#ifndef WCONTINUED
581# define WCONTINUED 0 631# define WCONTINUED 0
582#endif 632#endif
590 if (w->pid == pid || !w->pid) 640 if (w->pid == pid || !w->pid)
591 { 641 {
592 ev_priority (w) = ev_priority (sw); /* need to do it *now* */ 642 ev_priority (w) = ev_priority (sw); /* need to do it *now* */
593 w->rpid = pid; 643 w->rpid = pid;
594 w->rstatus = status; 644 w->rstatus = status;
595 event (EV_A_ (W)w, EV_CHILD); 645 ev_feed_event (EV_A_ (W)w, EV_CHILD);
596 } 646 }
597} 647}
598 648
599static void 649static void
600childcb (EV_P_ struct ev_signal *sw, int revents) 650childcb (EV_P_ struct ev_signal *sw, int revents)
602 int pid, status; 652 int pid, status;
603 653
604 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) 654 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)))
605 { 655 {
606 /* make sure we are called again until all childs have been reaped */ 656 /* make sure we are called again until all childs have been reaped */
607 event (EV_A_ (W)sw, EV_SIGNAL); 657 ev_feed_event (EV_A_ (W)sw, EV_SIGNAL);
608 658
609 child_reap (EV_A_ sw, pid, pid, status); 659 child_reap (EV_A_ sw, pid, pid, status);
610 child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ 660 child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */
611 } 661 }
612} 662}
726#endif 776#endif
727 777
728 for (i = NUMPRI; i--; ) 778 for (i = NUMPRI; i--; )
729 array_free (pending, [i]); 779 array_free (pending, [i]);
730 780
781 /* have to use the microsoft-never-gets-it-right macro */
731 array_free (fdchange, ); 782 array_free_microshit (fdchange);
732 array_free (timer, ); 783 array_free_microshit (timer);
733 array_free (periodic, ); 784 array_free_microshit (periodic);
734 array_free (idle, ); 785 array_free_microshit (idle);
735 array_free (prepare, ); 786 array_free_microshit (prepare);
736 array_free (check, ); 787 array_free_microshit (check);
737 788
738 method = 0; 789 method = 0;
739} 790}
740 791
741static void 792static void
796} 847}
797 848
798#endif 849#endif
799 850
800#if EV_MULTIPLICITY 851#if EV_MULTIPLICITY
801struct ev_loop default_loop_struct;
802static struct ev_loop *default_loop;
803
804struct ev_loop * 852struct ev_loop *
805#else 853#else
806static int default_loop;
807
808int 854int
809#endif 855#endif
810ev_default_loop (int methods) 856ev_default_loop (int methods)
811{ 857{
812 if (sigpipe [0] == sigpipe [1]) 858 if (sigpipe [0] == sigpipe [1])
846{ 892{
847#if EV_MULTIPLICITY 893#if EV_MULTIPLICITY
848 struct ev_loop *loop = default_loop; 894 struct ev_loop *loop = default_loop;
849#endif 895#endif
850 896
897#ifndef WIN32
851 ev_ref (EV_A); /* child watcher */ 898 ev_ref (EV_A); /* child watcher */
852 ev_signal_stop (EV_A_ &childev); 899 ev_signal_stop (EV_A_ &childev);
900#endif
853 901
854 ev_ref (EV_A); /* signal watcher */ 902 ev_ref (EV_A); /* signal watcher */
855 ev_io_stop (EV_A_ &sigev); 903 ev_io_stop (EV_A_ &sigev);
856 904
857 close (sigpipe [0]); sigpipe [0] = 0; 905 close (sigpipe [0]); sigpipe [0] = 0;
870 if (method) 918 if (method)
871 postfork = 1; 919 postfork = 1;
872} 920}
873 921
874/*****************************************************************************/ 922/*****************************************************************************/
923
924static int
925any_pending (EV_P)
926{
927 int pri;
928
929 for (pri = NUMPRI; pri--; )
930 if (pendingcnt [pri])
931 return 1;
932
933 return 0;
934}
875 935
876static void 936static void
877call_pending (EV_P) 937call_pending (EV_P)
878{ 938{
879 int pri; 939 int pri;
908 downheap ((WT *)timers, timercnt, 0); 968 downheap ((WT *)timers, timercnt, 0);
909 } 969 }
910 else 970 else
911 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 971 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
912 972
913 event (EV_A_ (W)w, EV_TIMEOUT); 973 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
914 } 974 }
915} 975}
916 976
917static void 977static void
918periodics_reify (EV_P) 978periodics_reify (EV_P)
922 struct ev_periodic *w = periodics [0]; 982 struct ev_periodic *w = periodics [0];
923 983
924 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 984 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
925 985
926 /* first reschedule or stop timer */ 986 /* first reschedule or stop timer */
987 if (w->reschedule_cb)
988 {
989 ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, rt_now + 0.0001);
990
991 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > rt_now));
992 downheap ((WT *)periodics, periodiccnt, 0);
993 }
927 if (w->interval) 994 else if (w->interval)
928 { 995 {
929 ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; 996 ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval;
930 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now)); 997 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now));
931 downheap ((WT *)periodics, periodiccnt, 0); 998 downheap ((WT *)periodics, periodiccnt, 0);
932 } 999 }
933 else 1000 else
934 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 1001 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
935 1002
936 event (EV_A_ (W)w, EV_PERIODIC); 1003 ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
937 } 1004 }
938} 1005}
939 1006
940static void 1007static void
941periodics_reschedule (EV_P) 1008periodics_reschedule (EV_P)
945 /* adjust periodics after time jump */ 1012 /* adjust periodics after time jump */
946 for (i = 0; i < periodiccnt; ++i) 1013 for (i = 0; i < periodiccnt; ++i)
947 { 1014 {
948 struct ev_periodic *w = periodics [i]; 1015 struct ev_periodic *w = periodics [i];
949 1016
1017 if (w->reschedule_cb)
1018 ((WT)w)->at = w->reschedule_cb (w, rt_now);
950 if (w->interval) 1019 else if (w->interval)
951 {
952 ev_tstamp diff = ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; 1020 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval;
953
954 if (fabs (diff) >= 1e-4)
955 {
956 ev_periodic_stop (EV_A_ w);
957 ev_periodic_start (EV_A_ w);
958
959 i = 0; /* restart loop, inefficient, but time jumps should be rare */
960 }
961 }
962 } 1021 }
1022
1023 /* now rebuild the heap */
1024 for (i = periodiccnt >> 1; i--; )
1025 downheap ((WT *)periodics, periodiccnt, i);
963} 1026}
964 1027
965inline int 1028inline int
966time_update_monotonic (EV_P) 1029time_update_monotonic (EV_P)
967{ 1030{
1063 /* update fd-related kernel structures */ 1126 /* update fd-related kernel structures */
1064 fd_reify (EV_A); 1127 fd_reify (EV_A);
1065 1128
1066 /* calculate blocking time */ 1129 /* calculate blocking time */
1067 1130
1068 /* we only need this for !monotonic clockor timers, but as we basically 1131 /* we only need this for !monotonic clock or timers, but as we basically
1069 always have timers, we just calculate it always */ 1132 always have timers, we just calculate it always */
1070#if EV_USE_MONOTONIC 1133#if EV_USE_MONOTONIC
1071 if (expect_true (have_monotonic)) 1134 if (expect_true (have_monotonic))
1072 time_update_monotonic (EV_A); 1135 time_update_monotonic (EV_A);
1073 else 1136 else
1106 /* queue pending timers and reschedule them */ 1169 /* queue pending timers and reschedule them */
1107 timers_reify (EV_A); /* relative timers called last */ 1170 timers_reify (EV_A); /* relative timers called last */
1108 periodics_reify (EV_A); /* absolute timers called first */ 1171 periodics_reify (EV_A); /* absolute timers called first */
1109 1172
1110 /* queue idle watchers unless io or timers are pending */ 1173 /* queue idle watchers unless io or timers are pending */
1111 if (!pendingcnt) 1174 if (idlecnt && !any_pending (EV_A))
1112 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1175 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
1113 1176
1114 /* queue check watchers, to be executed first */ 1177 /* queue check watchers, to be executed first */
1115 if (checkcnt) 1178 if (checkcnt)
1116 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1179 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1191 return; 1254 return;
1192 1255
1193 assert (("ev_io_start called with negative fd", fd >= 0)); 1256 assert (("ev_io_start called with negative fd", fd >= 0));
1194 1257
1195 ev_start (EV_A_ (W)w, 1); 1258 ev_start (EV_A_ (W)w, 1);
1196 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 1259 array_needsize (ANFD, anfds, anfdmax, fd + 1, anfds_init);
1197 wlist_add ((WL *)&anfds[fd].head, (WL)w); 1260 wlist_add ((WL *)&anfds[fd].head, (WL)w);
1198 1261
1199 fd_change (EV_A_ fd); 1262 fd_change (EV_A_ fd);
1200} 1263}
1201 1264
1221 ((WT)w)->at += mn_now; 1284 ((WT)w)->at += mn_now;
1222 1285
1223 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1286 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1224 1287
1225 ev_start (EV_A_ (W)w, ++timercnt); 1288 ev_start (EV_A_ (W)w, ++timercnt);
1226 array_needsize (timers, timermax, timercnt, ); 1289 array_needsize (struct ev_timer *, timers, timermax, timercnt, (void));
1227 timers [timercnt - 1] = w; 1290 timers [timercnt - 1] = w;
1228 upheap ((WT *)timers, timercnt - 1); 1291 upheap ((WT *)timers, timercnt - 1);
1229 1292
1230 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1293 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1231} 1294}
1271ev_periodic_start (EV_P_ struct ev_periodic *w) 1334ev_periodic_start (EV_P_ struct ev_periodic *w)
1272{ 1335{
1273 if (ev_is_active (w)) 1336 if (ev_is_active (w))
1274 return; 1337 return;
1275 1338
1339 if (w->reschedule_cb)
1340 ((WT)w)->at = w->reschedule_cb (w, rt_now);
1341 else if (w->interval)
1342 {
1276 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); 1343 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
1277
1278 /* this formula differs from the one in periodic_reify because we do not always round up */ 1344 /* this formula differs from the one in periodic_reify because we do not always round up */
1279 if (w->interval)
1280 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; 1345 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval;
1346 }
1281 1347
1282 ev_start (EV_A_ (W)w, ++periodiccnt); 1348 ev_start (EV_A_ (W)w, ++periodiccnt);
1283 array_needsize (periodics, periodicmax, periodiccnt, ); 1349 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void));
1284 periodics [periodiccnt - 1] = w; 1350 periodics [periodiccnt - 1] = w;
1285 upheap ((WT *)periodics, periodiccnt - 1); 1351 upheap ((WT *)periodics, periodiccnt - 1);
1286 1352
1287 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1353 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1288} 1354}
1304 1370
1305 ev_stop (EV_A_ (W)w); 1371 ev_stop (EV_A_ (W)w);
1306} 1372}
1307 1373
1308void 1374void
1375ev_periodic_again (EV_P_ struct ev_periodic *w)
1376{
1377 ev_periodic_stop (EV_A_ w);
1378 ev_periodic_start (EV_A_ w);
1379}
1380
1381void
1309ev_idle_start (EV_P_ struct ev_idle *w) 1382ev_idle_start (EV_P_ struct ev_idle *w)
1310{ 1383{
1311 if (ev_is_active (w)) 1384 if (ev_is_active (w))
1312 return; 1385 return;
1313 1386
1314 ev_start (EV_A_ (W)w, ++idlecnt); 1387 ev_start (EV_A_ (W)w, ++idlecnt);
1315 array_needsize (idles, idlemax, idlecnt, ); 1388 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, (void));
1316 idles [idlecnt - 1] = w; 1389 idles [idlecnt - 1] = w;
1317} 1390}
1318 1391
1319void 1392void
1320ev_idle_stop (EV_P_ struct ev_idle *w) 1393ev_idle_stop (EV_P_ struct ev_idle *w)
1332{ 1405{
1333 if (ev_is_active (w)) 1406 if (ev_is_active (w))
1334 return; 1407 return;
1335 1408
1336 ev_start (EV_A_ (W)w, ++preparecnt); 1409 ev_start (EV_A_ (W)w, ++preparecnt);
1337 array_needsize (prepares, preparemax, preparecnt, ); 1410 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, (void));
1338 prepares [preparecnt - 1] = w; 1411 prepares [preparecnt - 1] = w;
1339} 1412}
1340 1413
1341void 1414void
1342ev_prepare_stop (EV_P_ struct ev_prepare *w) 1415ev_prepare_stop (EV_P_ struct ev_prepare *w)
1354{ 1427{
1355 if (ev_is_active (w)) 1428 if (ev_is_active (w))
1356 return; 1429 return;
1357 1430
1358 ev_start (EV_A_ (W)w, ++checkcnt); 1431 ev_start (EV_A_ (W)w, ++checkcnt);
1359 array_needsize (checks, checkmax, checkcnt, ); 1432 array_needsize (struct ev_check *, checks, checkmax, checkcnt, (void));
1360 checks [checkcnt - 1] = w; 1433 checks [checkcnt - 1] = w;
1361} 1434}
1362 1435
1363void 1436void
1364ev_check_stop (EV_P_ struct ev_check *w) 1437ev_check_stop (EV_P_ struct ev_check *w)
1385 return; 1458 return;
1386 1459
1387 assert (("ev_signal_start called with illegal signal number", w->signum > 0)); 1460 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
1388 1461
1389 ev_start (EV_A_ (W)w, 1); 1462 ev_start (EV_A_ (W)w, 1);
1390 array_needsize (signals, signalmax, w->signum, signals_init); 1463 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init);
1391 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1464 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1392 1465
1393 if (!((WL)w)->next) 1466 if (!((WL)w)->next)
1394 { 1467 {
1395#if WIN32 1468#if WIN32
1478} 1551}
1479 1552
1480void 1553void
1481ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) 1554ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
1482{ 1555{
1483 struct ev_once *once = ev_malloc (sizeof (struct ev_once)); 1556 struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once));
1484 1557
1485 if (!once) 1558 if (!once)
1486 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); 1559 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1487 else 1560 else
1488 { 1561 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines