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

Comparing libev/ev.c (file contents):
Revision 1.73 by root, Tue Nov 6 16:27:10 2007 UTC vs.
Revision 1.98 by root, Sun Nov 11 02:05:20 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
40# ifndef EV_USE_MONOTONIC
35# define EV_USE_MONOTONIC 1 41# define EV_USE_MONOTONIC 1
42# endif
43# ifndef EV_USE_REALTIME
36# define EV_USE_REALTIME 1 44# define EV_USE_REALTIME 1
45# endif
37# endif 46# endif
38 47
39# if HAVE_SELECT && HAVE_SYS_SELECT_H 48# if HAVE_SELECT && HAVE_SYS_SELECT_H && !defined (EV_USE_SELECT)
40# define EV_USE_SELECT 1 49# define EV_USE_SELECT 1
41# endif 50# endif
42 51
43# if HAVE_POLL && HAVE_POLL_H 52# if HAVE_POLL && HAVE_POLL_H && !defined (EV_USE_POLL)
44# define EV_USE_POLL 1 53# define EV_USE_POLL 1
45# endif 54# endif
46 55
47# if HAVE_EPOLL && HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H 56# if HAVE_EPOLL && HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H && !defined (EV_USE_EPOLL)
48# define EV_USE_EPOLL 1 57# define EV_USE_EPOLL 1
49# endif 58# endif
50 59
51# if HAVE_KQUEUE && HAVE_WORKING_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H 60# if HAVE_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H && !defined (EV_USE_KQUEUE)
52# define EV_USE_KQUEUE 1 61# define EV_USE_KQUEUE 1
53# endif 62# endif
54 63
55#endif 64#endif
56 65
126#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 135#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) */ 136#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 */ 137#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 */ 138/*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */
130 139
140#ifdef EV_H
141# include EV_H
142#else
131#include "ev.h" 143# include "ev.h"
144#endif
132 145
133#if __GNUC__ >= 3 146#if __GNUC__ >= 3
134# define expect(expr,value) __builtin_expect ((expr),(value)) 147# define expect(expr,value) __builtin_expect ((expr),(value))
135# define inline inline 148# define inline inline
136#else 149#else
148typedef struct ev_watcher_list *WL; 161typedef struct ev_watcher_list *WL;
149typedef struct ev_watcher_time *WT; 162typedef struct ev_watcher_time *WT;
150 163
151static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 164static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
152 165
166#ifdef WIN32
153#include "ev_win32.c" 167# include "ev_win32.c"
168#endif
154 169
155/*****************************************************************************/ 170/*****************************************************************************/
156 171
157static void (*syserr_cb)(const char *msg); 172static void (*syserr_cb)(const char *msg);
158 173
215 int events; 230 int events;
216} ANPENDING; 231} ANPENDING;
217 232
218#if EV_MULTIPLICITY 233#if EV_MULTIPLICITY
219 234
220struct ev_loop 235 struct ev_loop
221{ 236 {
237 ev_tstamp ev_rt_now;
222# define VAR(name,decl) decl; 238 #define VAR(name,decl) decl;
223# include "ev_vars.h" 239 #include "ev_vars.h"
224};
225# undef VAR 240 #undef VAR
241 };
226# include "ev_wrap.h" 242 #include "ev_wrap.h"
243
244 struct ev_loop default_loop_struct;
245 static struct ev_loop *default_loop;
227 246
228#else 247#else
229 248
249 ev_tstamp ev_rt_now;
230# define VAR(name,decl) static decl; 250 #define VAR(name,decl) static decl;
231# include "ev_vars.h" 251 #include "ev_vars.h"
232# undef VAR 252 #undef VAR
253
254 static int default_loop;
233 255
234#endif 256#endif
235 257
236/*****************************************************************************/ 258/*****************************************************************************/
237 259
238inline ev_tstamp 260ev_tstamp
239ev_time (void) 261ev_time (void)
240{ 262{
241#if EV_USE_REALTIME 263#if EV_USE_REALTIME
242 struct timespec ts; 264 struct timespec ts;
243 clock_gettime (CLOCK_REALTIME, &ts); 265 clock_gettime (CLOCK_REALTIME, &ts);
262#endif 284#endif
263 285
264 return ev_time (); 286 return ev_time ();
265} 287}
266 288
289#if EV_MULTIPLICITY
267ev_tstamp 290ev_tstamp
268ev_now (EV_P) 291ev_now (EV_P)
269{ 292{
270 return rt_now; 293 return ev_rt_now;
271} 294}
295#endif
272 296
273#define array_roundsize(base,n) ((n) | 4 & ~3) 297#define array_roundsize(type,n) ((n) | 4 & ~3)
274 298
275#define array_needsize(base,cur,cnt,init) \ 299#define array_needsize(type,base,cur,cnt,init) \
276 if (expect_false ((cnt) > cur)) \ 300 if (expect_false ((cnt) > cur)) \
277 { \ 301 { \
278 int newcnt = cur; \ 302 int newcnt = cur; \
279 do \ 303 do \
280 { \ 304 { \
281 newcnt = array_roundsize (base, newcnt << 1); \ 305 newcnt = array_roundsize (type, newcnt << 1); \
282 } \ 306 } \
283 while ((cnt) > newcnt); \ 307 while ((cnt) > newcnt); \
284 \ 308 \
285 base = ev_realloc (base, sizeof (*base) * (newcnt)); \ 309 base = (type *)ev_realloc (base, sizeof (type) * (newcnt));\
286 init (base + cur, newcnt - cur); \ 310 init (base + cur, newcnt - cur); \
287 cur = newcnt; \ 311 cur = newcnt; \
288 } 312 }
289 313
290#define array_slim(stem) \ 314#define array_slim(type,stem) \
291 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ 315 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \
292 { \ 316 { \
293 stem ## max = array_roundsize (stem ## cnt >> 1); \ 317 stem ## max = array_roundsize (stem ## cnt >> 1); \
294 base = ev_realloc (base, sizeof (*base) * (stem ## max)); \ 318 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\
295 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ 319 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
296 } 320 }
297 321
298/* microsoft's pseudo-c is quite far from C as the rest of the world and the standard knows it */ 322/* microsoft's pseudo-c is quite far from C as the rest of the world and the standard knows it */
299/* bringing us everlasting joy in form of stupid extra macros that are not required in C */ 323/* bringing us everlasting joy in form of stupid extra macros that are not required in C */
316 340
317 ++base; 341 ++base;
318 } 342 }
319} 343}
320 344
321static void 345void
322event (EV_P_ W w, int events) 346ev_feed_event (EV_P_ void *w, int revents)
323{ 347{
348 W w_ = (W)w;
349
324 if (w->pending) 350 if (w_->pending)
325 { 351 {
326 pendings [ABSPRI (w)][w->pending - 1].events |= events; 352 pendings [ABSPRI (w_)][w_->pending - 1].events |= revents;
327 return; 353 return;
328 } 354 }
329 355
330 w->pending = ++pendingcnt [ABSPRI (w)]; 356 w_->pending = ++pendingcnt [ABSPRI (w_)];
331 array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], (void)); 357 array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], (void));
332 pendings [ABSPRI (w)][w->pending - 1].w = w; 358 pendings [ABSPRI (w_)][w_->pending - 1].w = w_;
333 pendings [ABSPRI (w)][w->pending - 1].events = events; 359 pendings [ABSPRI (w_)][w_->pending - 1].events = revents;
334} 360}
335 361
336static void 362static void
337queue_events (EV_P_ W *events, int eventcnt, int type) 363queue_events (EV_P_ W *events, int eventcnt, int type)
338{ 364{
339 int i; 365 int i;
340 366
341 for (i = 0; i < eventcnt; ++i) 367 for (i = 0; i < eventcnt; ++i)
342 event (EV_A_ events [i], type); 368 ev_feed_event (EV_A_ events [i], type);
343} 369}
344 370
345static void 371inline void
346fd_event (EV_P_ int fd, int events) 372fd_event (EV_P_ int fd, int revents)
347{ 373{
348 ANFD *anfd = anfds + fd; 374 ANFD *anfd = anfds + fd;
349 struct ev_io *w; 375 struct ev_io *w;
350 376
351 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) 377 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
352 { 378 {
353 int ev = w->events & events; 379 int ev = w->events & revents;
354 380
355 if (ev) 381 if (ev)
356 event (EV_A_ (W)w, ev); 382 ev_feed_event (EV_A_ (W)w, ev);
357 } 383 }
384}
385
386void
387ev_feed_fd_event (EV_P_ int fd, int revents)
388{
389 fd_event (EV_A_ fd, revents);
358} 390}
359 391
360/*****************************************************************************/ 392/*****************************************************************************/
361 393
362static void 394static void
391 return; 423 return;
392 424
393 anfds [fd].reify = 1; 425 anfds [fd].reify = 1;
394 426
395 ++fdchangecnt; 427 ++fdchangecnt;
396 array_needsize (fdchanges, fdchangemax, fdchangecnt, (void)); 428 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, (void));
397 fdchanges [fdchangecnt - 1] = fd; 429 fdchanges [fdchangecnt - 1] = fd;
398} 430}
399 431
400static void 432static void
401fd_kill (EV_P_ int fd) 433fd_kill (EV_P_ int fd)
403 struct ev_io *w; 435 struct ev_io *w;
404 436
405 while ((w = (struct ev_io *)anfds [fd].head)) 437 while ((w = (struct ev_io *)anfds [fd].head))
406 { 438 {
407 ev_io_stop (EV_A_ w); 439 ev_io_stop (EV_A_ w);
408 event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); 440 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
409 } 441 }
410} 442}
411 443
412static int 444static int
413fd_valid (int fd) 445fd_valid (int fd)
501 533
502 heap [k] = w; 534 heap [k] = w;
503 ((W)heap [k])->active = k + 1; 535 ((W)heap [k])->active = k + 1;
504} 536}
505 537
538inline void
539adjustheap (WT *heap, int N, int k, ev_tstamp at)
540{
541 ev_tstamp old_at = heap [k]->at;
542 heap [k]->at = at;
543
544 if (old_at < at)
545 downheap (heap, N, k);
546 else
547 upheap (heap, k);
548}
549
506/*****************************************************************************/ 550/*****************************************************************************/
507 551
508typedef struct 552typedef struct
509{ 553{
510 WL head; 554 WL head;
541 585
542 if (!gotsig) 586 if (!gotsig)
543 { 587 {
544 int old_errno = errno; 588 int old_errno = errno;
545 gotsig = 1; 589 gotsig = 1;
590#ifdef WIN32
591 send (sigpipe [1], &signum, 1, MSG_DONTWAIT);
592#else
546 write (sigpipe [1], &signum, 1); 593 write (sigpipe [1], &signum, 1);
594#endif
547 errno = old_errno; 595 errno = old_errno;
548 } 596 }
549} 597}
550 598
599void
600ev_feed_signal_event (EV_P_ int signum)
601{
602 WL w;
603
604#if EV_MULTIPLICITY
605 assert (("feeding signal events is only supported in the default loop", loop == default_loop));
606#endif
607
608 --signum;
609
610 if (signum < 0 || signum >= signalmax)
611 return;
612
613 signals [signum].gotsig = 0;
614
615 for (w = signals [signum].head; w; w = w->next)
616 ev_feed_event (EV_A_ (W)w, EV_SIGNAL);
617}
618
551static void 619static void
552sigcb (EV_P_ struct ev_io *iow, int revents) 620sigcb (EV_P_ struct ev_io *iow, int revents)
553{ 621{
554 WL w;
555 int signum; 622 int signum;
556 623
624#ifdef WIN32
625 recv (sigpipe [0], &revents, 1, MSG_DONTWAIT);
626#else
557 read (sigpipe [0], &revents, 1); 627 read (sigpipe [0], &revents, 1);
628#endif
558 gotsig = 0; 629 gotsig = 0;
559 630
560 for (signum = signalmax; signum--; ) 631 for (signum = signalmax; signum--; )
561 if (signals [signum].gotsig) 632 if (signals [signum].gotsig)
562 { 633 ev_feed_signal_event (EV_A_ signum + 1);
563 signals [signum].gotsig = 0;
564
565 for (w = signals [signum].head; w; w = w->next)
566 event (EV_A_ (W)w, EV_SIGNAL);
567 }
568} 634}
569 635
570static void 636static void
571siginit (EV_P) 637siginit (EV_P)
572{ 638{
605 if (w->pid == pid || !w->pid) 671 if (w->pid == pid || !w->pid)
606 { 672 {
607 ev_priority (w) = ev_priority (sw); /* need to do it *now* */ 673 ev_priority (w) = ev_priority (sw); /* need to do it *now* */
608 w->rpid = pid; 674 w->rpid = pid;
609 w->rstatus = status; 675 w->rstatus = status;
610 event (EV_A_ (W)w, EV_CHILD); 676 ev_feed_event (EV_A_ (W)w, EV_CHILD);
611 } 677 }
612} 678}
613 679
614static void 680static void
615childcb (EV_P_ struct ev_signal *sw, int revents) 681childcb (EV_P_ struct ev_signal *sw, int revents)
617 int pid, status; 683 int pid, status;
618 684
619 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) 685 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)))
620 { 686 {
621 /* make sure we are called again until all childs have been reaped */ 687 /* make sure we are called again until all childs have been reaped */
622 event (EV_A_ (W)sw, EV_SIGNAL); 688 ev_feed_event (EV_A_ (W)sw, EV_SIGNAL);
623 689
624 child_reap (EV_A_ sw, pid, pid, status); 690 child_reap (EV_A_ sw, pid, pid, status);
625 child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ 691 child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */
626 } 692 }
627} 693}
684 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 750 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
685 have_monotonic = 1; 751 have_monotonic = 1;
686 } 752 }
687#endif 753#endif
688 754
689 rt_now = ev_time (); 755 ev_rt_now = ev_time ();
690 mn_now = get_clock (); 756 mn_now = get_clock ();
691 now_floor = mn_now; 757 now_floor = mn_now;
692 rtmn_diff = rt_now - mn_now; 758 rtmn_diff = ev_rt_now - mn_now;
693 759
694 if (methods == EVMETHOD_AUTO) 760 if (methods == EVMETHOD_AUTO)
695 if (!enable_secure () && getenv ("LIBEV_METHODS")) 761 if (!enable_secure () && getenv ("LIBEV_METHODS"))
696 methods = atoi (getenv ("LIBEV_METHODS")); 762 methods = atoi (getenv ("LIBEV_METHODS"));
697 else 763 else
712#endif 778#endif
713#if EV_USE_SELECT 779#if EV_USE_SELECT
714 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 780 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
715#endif 781#endif
716 782
717 ev_watcher_init (&sigev, sigcb); 783 ev_init (&sigev, sigcb);
718 ev_set_priority (&sigev, EV_MAXPRI); 784 ev_set_priority (&sigev, EV_MAXPRI);
719 } 785 }
720} 786}
721 787
722void 788void
744 array_free (pending, [i]); 810 array_free (pending, [i]);
745 811
746 /* have to use the microsoft-never-gets-it-right macro */ 812 /* have to use the microsoft-never-gets-it-right macro */
747 array_free_microshit (fdchange); 813 array_free_microshit (fdchange);
748 array_free_microshit (timer); 814 array_free_microshit (timer);
815#if EV_PERIODICS
749 array_free_microshit (periodic); 816 array_free_microshit (periodic);
817#endif
750 array_free_microshit (idle); 818 array_free_microshit (idle);
751 array_free_microshit (prepare); 819 array_free_microshit (prepare);
752 array_free_microshit (check); 820 array_free_microshit (check);
753 821
754 method = 0; 822 method = 0;
812} 880}
813 881
814#endif 882#endif
815 883
816#if EV_MULTIPLICITY 884#if EV_MULTIPLICITY
817struct ev_loop default_loop_struct;
818static struct ev_loop *default_loop;
819
820struct ev_loop * 885struct ev_loop *
821#else 886#else
822static int default_loop;
823
824int 887int
825#endif 888#endif
826ev_default_loop (int methods) 889ev_default_loop (int methods)
827{ 890{
828 if (sigpipe [0] == sigpipe [1]) 891 if (sigpipe [0] == sigpipe [1])
889 postfork = 1; 952 postfork = 1;
890} 953}
891 954
892/*****************************************************************************/ 955/*****************************************************************************/
893 956
957static int
958any_pending (EV_P)
959{
960 int pri;
961
962 for (pri = NUMPRI; pri--; )
963 if (pendingcnt [pri])
964 return 1;
965
966 return 0;
967}
968
894static void 969static void
895call_pending (EV_P) 970call_pending (EV_P)
896{ 971{
897 int pri; 972 int pri;
898 973
902 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 977 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
903 978
904 if (p->w) 979 if (p->w)
905 { 980 {
906 p->w->pending = 0; 981 p->w->pending = 0;
907 p->w->cb (EV_A_ p->w, p->events); 982 EV_CB_INVOKE (p->w, p->events);
908 } 983 }
909 } 984 }
910} 985}
911 986
912static void 987static void
920 995
921 /* first reschedule or stop timer */ 996 /* first reschedule or stop timer */
922 if (w->repeat) 997 if (w->repeat)
923 { 998 {
924 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); 999 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
1000
925 ((WT)w)->at = mn_now + w->repeat; 1001 ((WT)w)->at += w->repeat;
1002 if (((WT)w)->at < mn_now)
1003 ((WT)w)->at = mn_now;
1004
926 downheap ((WT *)timers, timercnt, 0); 1005 downheap ((WT *)timers, timercnt, 0);
927 } 1006 }
928 else 1007 else
929 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 1008 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
930 1009
931 event (EV_A_ (W)w, EV_TIMEOUT); 1010 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
932 } 1011 }
933} 1012}
934 1013
1014#if EV_PERIODICS
935static void 1015static void
936periodics_reify (EV_P) 1016periodics_reify (EV_P)
937{ 1017{
938 while (periodiccnt && ((WT)periodics [0])->at <= rt_now) 1018 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
939 { 1019 {
940 struct ev_periodic *w = periodics [0]; 1020 struct ev_periodic *w = periodics [0];
941 1021
942 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 1022 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
943 1023
944 /* first reschedule or stop timer */ 1024 /* first reschedule or stop timer */
945 if (w->interval) 1025 if (w->reschedule_cb)
946 { 1026 {
1027 ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001);
1028
1029 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now));
1030 downheap ((WT *)periodics, periodiccnt, 0);
1031 }
1032 else if (w->interval)
1033 {
947 ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; 1034 ((WT)w)->at += floor ((ev_rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval;
948 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now)); 1035 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > ev_rt_now));
949 downheap ((WT *)periodics, periodiccnt, 0); 1036 downheap ((WT *)periodics, periodiccnt, 0);
950 } 1037 }
951 else 1038 else
952 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 1039 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
953 1040
954 event (EV_A_ (W)w, EV_PERIODIC); 1041 ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
955 } 1042 }
956} 1043}
957 1044
958static void 1045static void
959periodics_reschedule (EV_P) 1046periodics_reschedule (EV_P)
963 /* adjust periodics after time jump */ 1050 /* adjust periodics after time jump */
964 for (i = 0; i < periodiccnt; ++i) 1051 for (i = 0; i < periodiccnt; ++i)
965 { 1052 {
966 struct ev_periodic *w = periodics [i]; 1053 struct ev_periodic *w = periodics [i];
967 1054
1055 if (w->reschedule_cb)
1056 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
968 if (w->interval) 1057 else if (w->interval)
969 {
970 ev_tstamp diff = ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; 1058 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
971
972 if (fabs (diff) >= 1e-4)
973 {
974 ev_periodic_stop (EV_A_ w);
975 ev_periodic_start (EV_A_ w);
976
977 i = 0; /* restart loop, inefficient, but time jumps should be rare */
978 }
979 }
980 } 1059 }
1060
1061 /* now rebuild the heap */
1062 for (i = periodiccnt >> 1; i--; )
1063 downheap ((WT *)periodics, periodiccnt, i);
981} 1064}
1065#endif
982 1066
983inline int 1067inline int
984time_update_monotonic (EV_P) 1068time_update_monotonic (EV_P)
985{ 1069{
986 mn_now = get_clock (); 1070 mn_now = get_clock ();
987 1071
988 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) 1072 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
989 { 1073 {
990 rt_now = rtmn_diff + mn_now; 1074 ev_rt_now = rtmn_diff + mn_now;
991 return 0; 1075 return 0;
992 } 1076 }
993 else 1077 else
994 { 1078 {
995 now_floor = mn_now; 1079 now_floor = mn_now;
996 rt_now = ev_time (); 1080 ev_rt_now = ev_time ();
997 return 1; 1081 return 1;
998 } 1082 }
999} 1083}
1000 1084
1001static void 1085static void
1010 { 1094 {
1011 ev_tstamp odiff = rtmn_diff; 1095 ev_tstamp odiff = rtmn_diff;
1012 1096
1013 for (i = 4; --i; ) /* loop a few times, before making important decisions */ 1097 for (i = 4; --i; ) /* loop a few times, before making important decisions */
1014 { 1098 {
1015 rtmn_diff = rt_now - mn_now; 1099 rtmn_diff = ev_rt_now - mn_now;
1016 1100
1017 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) 1101 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)
1018 return; /* all is well */ 1102 return; /* all is well */
1019 1103
1020 rt_now = ev_time (); 1104 ev_rt_now = ev_time ();
1021 mn_now = get_clock (); 1105 mn_now = get_clock ();
1022 now_floor = mn_now; 1106 now_floor = mn_now;
1023 } 1107 }
1024 1108
1109# if EV_PERIODICS
1025 periodics_reschedule (EV_A); 1110 periodics_reschedule (EV_A);
1111# endif
1026 /* no timer adjustment, as the monotonic clock doesn't jump */ 1112 /* no timer adjustment, as the monotonic clock doesn't jump */
1027 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ 1113 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */
1028 } 1114 }
1029 } 1115 }
1030 else 1116 else
1031#endif 1117#endif
1032 { 1118 {
1033 rt_now = ev_time (); 1119 ev_rt_now = ev_time ();
1034 1120
1035 if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) 1121 if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
1036 { 1122 {
1123#if EV_PERIODICS
1037 periodics_reschedule (EV_A); 1124 periodics_reschedule (EV_A);
1125#endif
1038 1126
1039 /* adjust timers. this is easy, as the offset is the same for all */ 1127 /* adjust timers. this is easy, as the offset is the same for all */
1040 for (i = 0; i < timercnt; ++i) 1128 for (i = 0; i < timercnt; ++i)
1041 ((WT)timers [i])->at += rt_now - mn_now; 1129 ((WT)timers [i])->at += ev_rt_now - mn_now;
1042 } 1130 }
1043 1131
1044 mn_now = rt_now; 1132 mn_now = ev_rt_now;
1045 } 1133 }
1046} 1134}
1047 1135
1048void 1136void
1049ev_ref (EV_P) 1137ev_ref (EV_P)
1081 /* update fd-related kernel structures */ 1169 /* update fd-related kernel structures */
1082 fd_reify (EV_A); 1170 fd_reify (EV_A);
1083 1171
1084 /* calculate blocking time */ 1172 /* calculate blocking time */
1085 1173
1086 /* we only need this for !monotonic clockor timers, but as we basically 1174 /* we only need this for !monotonic clock or timers, but as we basically
1087 always have timers, we just calculate it always */ 1175 always have timers, we just calculate it always */
1088#if EV_USE_MONOTONIC 1176#if EV_USE_MONOTONIC
1089 if (expect_true (have_monotonic)) 1177 if (expect_true (have_monotonic))
1090 time_update_monotonic (EV_A); 1178 time_update_monotonic (EV_A);
1091 else 1179 else
1092#endif 1180#endif
1093 { 1181 {
1094 rt_now = ev_time (); 1182 ev_rt_now = ev_time ();
1095 mn_now = rt_now; 1183 mn_now = ev_rt_now;
1096 } 1184 }
1097 1185
1098 if (flags & EVLOOP_NONBLOCK || idlecnt) 1186 if (flags & EVLOOP_NONBLOCK || idlecnt)
1099 block = 0.; 1187 block = 0.;
1100 else 1188 else
1105 { 1193 {
1106 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; 1194 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge;
1107 if (block > to) block = to; 1195 if (block > to) block = to;
1108 } 1196 }
1109 1197
1198#if EV_PERIODICS
1110 if (periodiccnt) 1199 if (periodiccnt)
1111 { 1200 {
1112 ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge; 1201 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge;
1113 if (block > to) block = to; 1202 if (block > to) block = to;
1114 } 1203 }
1204#endif
1115 1205
1116 if (block < 0.) block = 0.; 1206 if (block < 0.) block = 0.;
1117 } 1207 }
1118 1208
1119 method_poll (EV_A_ block); 1209 method_poll (EV_A_ block);
1120 1210
1121 /* update rt_now, do magic */ 1211 /* update ev_rt_now, do magic */
1122 time_update (EV_A); 1212 time_update (EV_A);
1123 1213
1124 /* queue pending timers and reschedule them */ 1214 /* queue pending timers and reschedule them */
1125 timers_reify (EV_A); /* relative timers called last */ 1215 timers_reify (EV_A); /* relative timers called last */
1216#if EV_PERIODICS
1126 periodics_reify (EV_A); /* absolute timers called first */ 1217 periodics_reify (EV_A); /* absolute timers called first */
1218#endif
1127 1219
1128 /* queue idle watchers unless io or timers are pending */ 1220 /* queue idle watchers unless io or timers are pending */
1129 if (!pendingcnt) 1221 if (idlecnt && !any_pending (EV_A))
1130 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1222 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
1131 1223
1132 /* queue check watchers, to be executed first */ 1224 /* queue check watchers, to be executed first */
1133 if (checkcnt) 1225 if (checkcnt)
1134 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1226 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1209 return; 1301 return;
1210 1302
1211 assert (("ev_io_start called with negative fd", fd >= 0)); 1303 assert (("ev_io_start called with negative fd", fd >= 0));
1212 1304
1213 ev_start (EV_A_ (W)w, 1); 1305 ev_start (EV_A_ (W)w, 1);
1214 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 1306 array_needsize (ANFD, anfds, anfdmax, fd + 1, anfds_init);
1215 wlist_add ((WL *)&anfds[fd].head, (WL)w); 1307 wlist_add ((WL *)&anfds[fd].head, (WL)w);
1216 1308
1217 fd_change (EV_A_ fd); 1309 fd_change (EV_A_ fd);
1218} 1310}
1219 1311
1222{ 1314{
1223 ev_clear_pending (EV_A_ (W)w); 1315 ev_clear_pending (EV_A_ (W)w);
1224 if (!ev_is_active (w)) 1316 if (!ev_is_active (w))
1225 return; 1317 return;
1226 1318
1319 assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax));
1320
1227 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 1321 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
1228 ev_stop (EV_A_ (W)w); 1322 ev_stop (EV_A_ (W)w);
1229 1323
1230 fd_change (EV_A_ w->fd); 1324 fd_change (EV_A_ w->fd);
1231} 1325}
1239 ((WT)w)->at += mn_now; 1333 ((WT)w)->at += mn_now;
1240 1334
1241 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1335 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1242 1336
1243 ev_start (EV_A_ (W)w, ++timercnt); 1337 ev_start (EV_A_ (W)w, ++timercnt);
1244 array_needsize (timers, timermax, timercnt, (void)); 1338 array_needsize (struct ev_timer *, timers, timermax, timercnt, (void));
1245 timers [timercnt - 1] = w; 1339 timers [timercnt - 1] = w;
1246 upheap ((WT *)timers, timercnt - 1); 1340 upheap ((WT *)timers, timercnt - 1);
1247 1341
1248 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1342 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1249} 1343}
1261 { 1355 {
1262 timers [((W)w)->active - 1] = timers [timercnt]; 1356 timers [((W)w)->active - 1] = timers [timercnt];
1263 downheap ((WT *)timers, timercnt, ((W)w)->active - 1); 1357 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1264 } 1358 }
1265 1359
1266 ((WT)w)->at = w->repeat; 1360 ((WT)w)->at -= mn_now;
1267 1361
1268 ev_stop (EV_A_ (W)w); 1362 ev_stop (EV_A_ (W)w);
1269} 1363}
1270 1364
1271void 1365void
1272ev_timer_again (EV_P_ struct ev_timer *w) 1366ev_timer_again (EV_P_ struct ev_timer *w)
1273{ 1367{
1274 if (ev_is_active (w)) 1368 if (ev_is_active (w))
1275 { 1369 {
1276 if (w->repeat) 1370 if (w->repeat)
1277 {
1278 ((WT)w)->at = mn_now + w->repeat;
1279 downheap ((WT *)timers, timercnt, ((W)w)->active - 1); 1371 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1, mn_now + w->repeat);
1280 }
1281 else 1372 else
1282 ev_timer_stop (EV_A_ w); 1373 ev_timer_stop (EV_A_ w);
1283 } 1374 }
1284 else if (w->repeat) 1375 else if (w->repeat)
1285 ev_timer_start (EV_A_ w); 1376 ev_timer_start (EV_A_ w);
1286} 1377}
1287 1378
1379#if EV_PERIODICS
1288void 1380void
1289ev_periodic_start (EV_P_ struct ev_periodic *w) 1381ev_periodic_start (EV_P_ struct ev_periodic *w)
1290{ 1382{
1291 if (ev_is_active (w)) 1383 if (ev_is_active (w))
1292 return; 1384 return;
1293 1385
1386 if (w->reschedule_cb)
1387 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1388 else if (w->interval)
1389 {
1294 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); 1390 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
1295
1296 /* this formula differs from the one in periodic_reify because we do not always round up */ 1391 /* this formula differs from the one in periodic_reify because we do not always round up */
1297 if (w->interval)
1298 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; 1392 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1393 }
1299 1394
1300 ev_start (EV_A_ (W)w, ++periodiccnt); 1395 ev_start (EV_A_ (W)w, ++periodiccnt);
1301 array_needsize (periodics, periodicmax, periodiccnt, (void)); 1396 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void));
1302 periodics [periodiccnt - 1] = w; 1397 periodics [periodiccnt - 1] = w;
1303 upheap ((WT *)periodics, periodiccnt - 1); 1398 upheap ((WT *)periodics, periodiccnt - 1);
1304 1399
1305 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1400 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1306} 1401}
1322 1417
1323 ev_stop (EV_A_ (W)w); 1418 ev_stop (EV_A_ (W)w);
1324} 1419}
1325 1420
1326void 1421void
1422ev_periodic_again (EV_P_ struct ev_periodic *w)
1423{
1424 /* TODO: use adjustheap and recalculation */
1425 ev_periodic_stop (EV_A_ w);
1426 ev_periodic_start (EV_A_ w);
1427}
1428#endif
1429
1430void
1327ev_idle_start (EV_P_ struct ev_idle *w) 1431ev_idle_start (EV_P_ struct ev_idle *w)
1328{ 1432{
1329 if (ev_is_active (w)) 1433 if (ev_is_active (w))
1330 return; 1434 return;
1331 1435
1332 ev_start (EV_A_ (W)w, ++idlecnt); 1436 ev_start (EV_A_ (W)w, ++idlecnt);
1333 array_needsize (idles, idlemax, idlecnt, (void)); 1437 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, (void));
1334 idles [idlecnt - 1] = w; 1438 idles [idlecnt - 1] = w;
1335} 1439}
1336 1440
1337void 1441void
1338ev_idle_stop (EV_P_ struct ev_idle *w) 1442ev_idle_stop (EV_P_ struct ev_idle *w)
1350{ 1454{
1351 if (ev_is_active (w)) 1455 if (ev_is_active (w))
1352 return; 1456 return;
1353 1457
1354 ev_start (EV_A_ (W)w, ++preparecnt); 1458 ev_start (EV_A_ (W)w, ++preparecnt);
1355 array_needsize (prepares, preparemax, preparecnt, (void)); 1459 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, (void));
1356 prepares [preparecnt - 1] = w; 1460 prepares [preparecnt - 1] = w;
1357} 1461}
1358 1462
1359void 1463void
1360ev_prepare_stop (EV_P_ struct ev_prepare *w) 1464ev_prepare_stop (EV_P_ struct ev_prepare *w)
1372{ 1476{
1373 if (ev_is_active (w)) 1477 if (ev_is_active (w))
1374 return; 1478 return;
1375 1479
1376 ev_start (EV_A_ (W)w, ++checkcnt); 1480 ev_start (EV_A_ (W)w, ++checkcnt);
1377 array_needsize (checks, checkmax, checkcnt, (void)); 1481 array_needsize (struct ev_check *, checks, checkmax, checkcnt, (void));
1378 checks [checkcnt - 1] = w; 1482 checks [checkcnt - 1] = w;
1379} 1483}
1380 1484
1381void 1485void
1382ev_check_stop (EV_P_ struct ev_check *w) 1486ev_check_stop (EV_P_ struct ev_check *w)
1383{ 1487{
1384 ev_clear_pending (EV_A_ (W)w); 1488 ev_clear_pending (EV_A_ (W)w);
1385 if (ev_is_active (w)) 1489 if (!ev_is_active (w))
1386 return; 1490 return;
1387 1491
1388 checks [((W)w)->active - 1] = checks [--checkcnt]; 1492 checks [((W)w)->active - 1] = checks [--checkcnt];
1389 ev_stop (EV_A_ (W)w); 1493 ev_stop (EV_A_ (W)w);
1390} 1494}
1403 return; 1507 return;
1404 1508
1405 assert (("ev_signal_start called with illegal signal number", w->signum > 0)); 1509 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
1406 1510
1407 ev_start (EV_A_ (W)w, 1); 1511 ev_start (EV_A_ (W)w, 1);
1408 array_needsize (signals, signalmax, w->signum, signals_init); 1512 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init);
1409 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1513 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1410 1514
1411 if (!((WL)w)->next) 1515 if (!((WL)w)->next)
1412 { 1516 {
1413#if WIN32 1517#if WIN32
1451 1555
1452void 1556void
1453ev_child_stop (EV_P_ struct ev_child *w) 1557ev_child_stop (EV_P_ struct ev_child *w)
1454{ 1558{
1455 ev_clear_pending (EV_A_ (W)w); 1559 ev_clear_pending (EV_A_ (W)w);
1456 if (ev_is_active (w)) 1560 if (!ev_is_active (w))
1457 return; 1561 return;
1458 1562
1459 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1563 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1460 ev_stop (EV_A_ (W)w); 1564 ev_stop (EV_A_ (W)w);
1461} 1565}
1496} 1600}
1497 1601
1498void 1602void
1499ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) 1603ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
1500{ 1604{
1501 struct ev_once *once = ev_malloc (sizeof (struct ev_once)); 1605 struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once));
1502 1606
1503 if (!once) 1607 if (!once)
1504 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); 1608 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1505 else 1609 else
1506 { 1610 {
1507 once->cb = cb; 1611 once->cb = cb;
1508 once->arg = arg; 1612 once->arg = arg;
1509 1613
1510 ev_watcher_init (&once->io, once_cb_io); 1614 ev_init (&once->io, once_cb_io);
1511 if (fd >= 0) 1615 if (fd >= 0)
1512 { 1616 {
1513 ev_io_set (&once->io, fd, events); 1617 ev_io_set (&once->io, fd, events);
1514 ev_io_start (EV_A_ &once->io); 1618 ev_io_start (EV_A_ &once->io);
1515 } 1619 }
1516 1620
1517 ev_watcher_init (&once->to, once_cb_to); 1621 ev_init (&once->to, once_cb_to);
1518 if (timeout >= 0.) 1622 if (timeout >= 0.)
1519 { 1623 {
1520 ev_timer_set (&once->to, timeout, 0.); 1624 ev_timer_set (&once->to, timeout, 0.);
1521 ev_timer_start (EV_A_ &once->to); 1625 ev_timer_start (EV_A_ &once->to);
1522 } 1626 }
1523 } 1627 }
1524} 1628}
1525 1629
1630#ifdef __cplusplus
1631}
1632#endif
1633

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines