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

Comparing libev/ev.c (file contents):
Revision 1.51 by root, Sat Nov 3 21:58:51 2007 UTC vs.
Revision 1.63 by root, Sun Nov 4 22:03:17 2007 UTC

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#ifndef EV_STANDALONE 31#ifndef EV_STANDALONE
32# include "config.h" 32# include "config.h"
33
34# if HAVE_CLOCK_GETTIME
35# define EV_USE_MONOTONIC 1
36# define EV_USE_REALTIME 1
37# endif
38
39# if HAVE_SELECT && HAVE_SYS_SELECT_H
40# define EV_USE_SELECT 1
41# endif
42
43# if HAVE_POLL && HAVE_POLL_H
44# define EV_USE_POLL 1
45# endif
46
47# if HAVE_EPOLL && HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H
48# define EV_USE_EPOLL 1
49# endif
50
51# if HAVE_KQUEUE && HAVE_WORKING_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H
52# define EV_USE_KQUEUE 1
53# endif
54
33#endif 55#endif
34 56
35#include <math.h> 57#include <math.h>
36#include <stdlib.h> 58#include <stdlib.h>
37#include <unistd.h> 59#include <unistd.h>
58 80
59#ifndef EV_USE_SELECT 81#ifndef EV_USE_SELECT
60# define EV_USE_SELECT 1 82# define EV_USE_SELECT 1
61#endif 83#endif
62 84
63#ifndef EV_USEV_POLL 85#ifndef EV_USE_POLL
64# define EV_USEV_POLL 0 /* poll is usually slower than select, and not as well tested */ 86# define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */
65#endif 87#endif
66 88
67#ifndef EV_USE_EPOLL 89#ifndef EV_USE_EPOLL
68# define EV_USE_EPOLL 0 90# define EV_USE_EPOLL 0
69#endif 91#endif
70 92
71#ifndef EV_USE_KQUEUE 93#ifndef EV_USE_KQUEUE
72# define EV_USE_KQUEUE 0 94# define EV_USE_KQUEUE 0
95#endif
96
97#ifndef EV_USE_WIN32
98# ifdef WIN32
99# define EV_USE_WIN32 1
100# else
101# define EV_USE_WIN32 0
102# endif
73#endif 103#endif
74 104
75#ifndef EV_USE_REALTIME 105#ifndef EV_USE_REALTIME
76# define EV_USE_REALTIME 1 106# define EV_USE_REALTIME 1
77#endif 107#endif
113 143
114typedef struct ev_watcher *W; 144typedef struct ev_watcher *W;
115typedef struct ev_watcher_list *WL; 145typedef struct ev_watcher_list *WL;
116typedef struct ev_watcher_time *WT; 146typedef struct ev_watcher_time *WT;
117 147
118static ev_tstamp now_floor, mn_now, diff; /* monotonic clock */ 148static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
119static ev_tstamp rt_now;
120static int method;
121 149
122static int have_monotonic; /* runtime */ 150/*****************************************************************************/
123 151
124static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */ 152typedef struct
125static void (*method_modify)(EV_P_ int fd, int oev, int nev); 153{
126static void (*method_poll)(EV_P_ ev_tstamp timeout); 154 struct ev_watcher_list *head;
155 unsigned char events;
156 unsigned char reify;
157} ANFD;
127 158
128static int activecnt; /* number of active events */ 159typedef struct
160{
161 W w;
162 int events;
163} ANPENDING;
129 164
130#if EV_USE_SELECT 165#if EV_MULTIPLICITY
131static unsigned char *vec_ri, *vec_ro, *vec_wi, *vec_wo;
132static int vec_max;
133#endif
134 166
135#if EV_USEV_POLL 167struct ev_loop
136static struct pollfd *polls; 168{
137static int pollmax, pollcnt; 169# define VAR(name,decl) decl;
138static int *pollidxs; /* maps fds into structure indices */ 170# include "ev_vars.h"
139static int pollidxmax; 171};
140#endif 172# undef VAR
173# include "ev_wrap.h"
141 174
142#if EV_USE_EPOLL 175#else
143static int epoll_fd = -1;
144 176
145static struct epoll_event *events; 177# define VAR(name,decl) static decl;
146static int eventmax; 178# include "ev_vars.h"
147#endif 179# undef VAR
148 180
149#if EV_USE_KQUEUE
150static int kqueue_fd;
151static struct kevent *kqueue_changes;
152static int kqueue_changemax, kqueue_changecnt;
153static struct kevent *kqueue_events;
154static int kqueue_eventmax;
155#endif 181#endif
156 182
157/*****************************************************************************/ 183/*****************************************************************************/
158 184
159inline ev_tstamp 185inline ev_tstamp
208 cur = newcnt; \ 234 cur = newcnt; \
209 } 235 }
210 236
211/*****************************************************************************/ 237/*****************************************************************************/
212 238
213typedef struct
214{
215 struct ev_watcher_list *head;
216 unsigned char events;
217 unsigned char reify;
218} ANFD;
219
220static ANFD *anfds;
221static int anfdmax;
222
223static void 239static void
224anfds_init (ANFD *base, int count) 240anfds_init (ANFD *base, int count)
225{ 241{
226 while (count--) 242 while (count--)
227 { 243 {
230 base->reify = 0; 246 base->reify = 0;
231 247
232 ++base; 248 ++base;
233 } 249 }
234} 250}
235
236typedef struct
237{
238 W w;
239 int events;
240} ANPENDING;
241
242static ANPENDING *pendings [NUMPRI];
243static int pendingmax [NUMPRI], pendingcnt [NUMPRI];
244 251
245static void 252static void
246event (EV_P_ W w, int events) 253event (EV_P_ W w, int events)
247{ 254{
248 if (w->pending) 255 if (w->pending)
280 event (EV_A_ (W)w, ev); 287 event (EV_A_ (W)w, ev);
281 } 288 }
282} 289}
283 290
284/*****************************************************************************/ 291/*****************************************************************************/
285
286static int *fdchanges;
287static int fdchangemax, fdchangecnt;
288 292
289static void 293static void
290fd_reify (EV_P) 294fd_reify (EV_P)
291{ 295{
292 int i; 296 int i;
353 357
354/* called on ENOMEM in select/poll to kill some fds and retry */ 358/* called on ENOMEM in select/poll to kill some fds and retry */
355static void 359static void
356fd_enomem (EV_P) 360fd_enomem (EV_P)
357{ 361{
358 int fd = anfdmax; 362 int fd;
359 363
360 while (fd--) 364 for (fd = anfdmax; fd--; )
361 if (anfds [fd].events) 365 if (anfds [fd].events)
362 { 366 {
363 close (fd); 367 close (fd);
364 fd_kill (EV_A_ fd); 368 fd_kill (EV_A_ fd);
365 return; 369 return;
366 } 370 }
367} 371}
368 372
373/* susually called after fork if method needs to re-arm all fds from scratch */
374static void
375fd_rearm_all (EV_P)
376{
377 int fd;
378
379 /* this should be highly optimised to not do anything but set a flag */
380 for (fd = 0; fd < anfdmax; ++fd)
381 if (anfds [fd].events)
382 {
383 anfds [fd].events = 0;
384 fd_change (EV_A_ fd);
385 }
386}
387
369/*****************************************************************************/ 388/*****************************************************************************/
370 389
371static struct ev_timer **timers;
372static int timermax, timercnt;
373
374static struct ev_periodic **periodics;
375static int periodicmax, periodiccnt;
376
377static void 390static void
378upheap (WT *timers, int k) 391upheap (WT *heap, int k)
379{ 392{
380 WT w = timers [k]; 393 WT w = heap [k];
381 394
382 while (k && timers [k >> 1]->at > w->at) 395 while (k && heap [k >> 1]->at > w->at)
383 { 396 {
384 timers [k] = timers [k >> 1]; 397 heap [k] = heap [k >> 1];
385 timers [k]->active = k + 1; 398 ((W)heap [k])->active = k + 1;
386 k >>= 1; 399 k >>= 1;
387 } 400 }
388 401
389 timers [k] = w; 402 heap [k] = w;
390 timers [k]->active = k + 1; 403 ((W)heap [k])->active = k + 1;
391 404
392} 405}
393 406
394static void 407static void
395downheap (WT *timers, int N, int k) 408downheap (WT *heap, int N, int k)
396{ 409{
397 WT w = timers [k]; 410 WT w = heap [k];
398 411
399 while (k < (N >> 1)) 412 while (k < (N >> 1))
400 { 413 {
401 int j = k << 1; 414 int j = k << 1;
402 415
403 if (j + 1 < N && timers [j]->at > timers [j + 1]->at) 416 if (j + 1 < N && heap [j]->at > heap [j + 1]->at)
404 ++j; 417 ++j;
405 418
406 if (w->at <= timers [j]->at) 419 if (w->at <= heap [j]->at)
407 break; 420 break;
408 421
409 timers [k] = timers [j]; 422 heap [k] = heap [j];
410 timers [k]->active = k + 1; 423 ((W)heap [k])->active = k + 1;
411 k = j; 424 k = j;
412 } 425 }
413 426
414 timers [k] = w; 427 heap [k] = w;
415 timers [k]->active = k + 1; 428 ((W)heap [k])->active = k + 1;
416} 429}
417 430
418/*****************************************************************************/ 431/*****************************************************************************/
419 432
420typedef struct 433typedef struct
486 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); 499 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
487 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 500 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
488#endif 501#endif
489 502
490 ev_io_set (&sigev, sigpipe [0], EV_READ); 503 ev_io_set (&sigev, sigpipe [0], EV_READ);
491 ev_io_start (&sigev); 504 ev_io_start (EV_A_ &sigev);
505 ev_unref (EV_A); /* child watcher should not keep loop alive */
492} 506}
493 507
494/*****************************************************************************/ 508/*****************************************************************************/
495 509
496static struct ev_idle **idles; 510#ifndef WIN32
497static int idlemax, idlecnt;
498
499static struct ev_prepare **prepares;
500static int preparemax, preparecnt;
501
502static struct ev_check **checks;
503static int checkmax, checkcnt;
504
505/*****************************************************************************/
506 511
507static struct ev_child *childs [PID_HASHSIZE]; 512static struct ev_child *childs [PID_HASHSIZE];
508static struct ev_signal childev; 513static struct ev_signal childev;
509
510#ifndef WIN32
511 514
512#ifndef WCONTINUED 515#ifndef WCONTINUED
513# define WCONTINUED 0 516# define WCONTINUED 0
514#endif 517#endif
515 518
519 struct ev_child *w; 522 struct ev_child *w;
520 523
521 for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) 524 for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next)
522 if (w->pid == pid || !w->pid) 525 if (w->pid == pid || !w->pid)
523 { 526 {
524 w->priority = sw->priority; /* need to do it *now* */ 527 ev_priority (w) = ev_priority (sw); /* need to do it *now* */
525 w->rpid = pid; 528 w->rpid = pid;
526 w->rstatus = status; 529 w->rstatus = status;
527 event (EV_A_ (W)w, EV_CHILD); 530 event (EV_A_ (W)w, EV_CHILD);
528 } 531 }
529} 532}
530 533
531static void 534static void
551# include "ev_kqueue.c" 554# include "ev_kqueue.c"
552#endif 555#endif
553#if EV_USE_EPOLL 556#if EV_USE_EPOLL
554# include "ev_epoll.c" 557# include "ev_epoll.c"
555#endif 558#endif
556#if EV_USEV_POLL 559#if EV_USE_POLL
557# include "ev_poll.c" 560# include "ev_poll.c"
558#endif 561#endif
559#if EV_USE_SELECT 562#if EV_USE_SELECT
560# include "ev_select.c" 563# include "ev_select.c"
561#endif 564#endif
588ev_method (EV_P) 591ev_method (EV_P)
589{ 592{
590 return method; 593 return method;
591} 594}
592 595
593int 596static void
594ev_init (EV_P_ int methods) 597loop_init (EV_P_ int methods)
595{ 598{
596 if (!method) 599 if (!method)
597 { 600 {
598#if EV_USE_MONOTONIC 601#if EV_USE_MONOTONIC
599 { 602 {
604#endif 607#endif
605 608
606 rt_now = ev_time (); 609 rt_now = ev_time ();
607 mn_now = get_clock (); 610 mn_now = get_clock ();
608 now_floor = mn_now; 611 now_floor = mn_now;
609 diff = rt_now - mn_now; 612 rtmn_diff = rt_now - mn_now;
610
611 if (pipe (sigpipe))
612 return 0;
613 613
614 if (methods == EVMETHOD_AUTO) 614 if (methods == EVMETHOD_AUTO)
615 if (!enable_secure () && getenv ("LIBmethodS")) 615 if (!enable_secure () && getenv ("LIBEV_METHODS"))
616 methods = atoi (getenv ("LIBmethodS")); 616 methods = atoi (getenv ("LIBEV_METHODS"));
617 else 617 else
618 methods = EVMETHOD_ANY; 618 methods = EVMETHOD_ANY;
619 619
620 method = 0; 620 method = 0;
621#if EV_USE_WIN32
622 if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods);
623#endif
621#if EV_USE_KQUEUE 624#if EV_USE_KQUEUE
622 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); 625 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods);
623#endif 626#endif
624#if EV_USE_EPOLL 627#if EV_USE_EPOLL
625 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); 628 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods);
626#endif 629#endif
627#if EV_USEV_POLL 630#if EV_USE_POLL
628 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); 631 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods);
629#endif 632#endif
630#if EV_USE_SELECT 633#if EV_USE_SELECT
631 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 634 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
632#endif 635#endif
636 }
637}
633 638
639void
640loop_destroy (EV_P)
641{
642#if EV_USE_WIN32
643 if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A);
644#endif
645#if EV_USE_KQUEUE
646 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
647#endif
648#if EV_USE_EPOLL
649 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A);
650#endif
651#if EV_USE_POLL
652 if (method == EVMETHOD_POLL ) poll_destroy (EV_A);
653#endif
654#if EV_USE_SELECT
655 if (method == EVMETHOD_SELECT) select_destroy (EV_A);
656#endif
657
658 method = 0;
659 /*TODO*/
660}
661
662void
663loop_fork (EV_P)
664{
665 /*TODO*/
666#if EV_USE_EPOLL
667 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A);
668#endif
669#if EV_USE_KQUEUE
670 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A);
671#endif
672}
673
674#if EV_MULTIPLICITY
675struct ev_loop *
676ev_loop_new (int methods)
677{
678 struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop));
679
680 loop_init (EV_A_ methods);
681
682 if (ev_method (EV_A))
683 return loop;
684
685 return 0;
686}
687
688void
689ev_loop_destroy (EV_P)
690{
691 loop_destroy (EV_A);
692 free (loop);
693}
694
695void
696ev_loop_fork (EV_P)
697{
698 loop_fork (EV_A);
699}
700
701#endif
702
703#if EV_MULTIPLICITY
704struct ev_loop default_loop_struct;
705static struct ev_loop *default_loop;
706
707struct ev_loop *
708#else
709static int default_loop;
710
711int
712#endif
713ev_default_loop (int methods)
714{
715 if (sigpipe [0] == sigpipe [1])
716 if (pipe (sigpipe))
717 return 0;
718
719 if (!default_loop)
720 {
721#if EV_MULTIPLICITY
722 struct ev_loop *loop = default_loop = &default_loop_struct;
723#else
724 default_loop = 1;
725#endif
726
727 loop_init (EV_A_ methods);
728
634 if (method) 729 if (ev_method (EV_A))
635 { 730 {
636 ev_watcher_init (&sigev, sigcb); 731 ev_watcher_init (&sigev, sigcb);
637 ev_set_priority (&sigev, EV_MAXPRI); 732 ev_set_priority (&sigev, EV_MAXPRI);
638 siginit (EV_A); 733 siginit (EV_A);
639 734
640#ifndef WIN32 735#ifndef WIN32
641 ev_signal_init (&childev, childcb, SIGCHLD); 736 ev_signal_init (&childev, childcb, SIGCHLD);
642 ev_set_priority (&childev, EV_MAXPRI); 737 ev_set_priority (&childev, EV_MAXPRI);
643 ev_signal_start (EV_A_ &childev); 738 ev_signal_start (EV_A_ &childev);
739 ev_unref (EV_A); /* child watcher should not keep loop alive */
644#endif 740#endif
645 } 741 }
742 else
743 default_loop = 0;
646 } 744 }
647 745
648 return method; 746 return default_loop;
649} 747}
650 748
651/*****************************************************************************/
652
653void 749void
654ev_fork_prepare (void) 750ev_default_destroy (void)
655{ 751{
656 /* nop */ 752#if EV_MULTIPLICITY
657} 753 struct ev_loop *loop = default_loop;
658
659void
660ev_fork_parent (void)
661{
662 /* nop */
663}
664
665void
666ev_fork_child (void)
667{
668#if EV_USE_EPOLL
669 if (method == EVMETHOD_EPOLL)
670 epoll_postfork_child ();
671#endif 754#endif
672 755
756 ev_ref (EV_A); /* child watcher */
757 ev_signal_stop (EV_A_ &childev);
758
759 ev_ref (EV_A); /* signal watcher */
673 ev_io_stop (&sigev); 760 ev_io_stop (EV_A_ &sigev);
761
762 close (sigpipe [0]); sigpipe [0] = 0;
763 close (sigpipe [1]); sigpipe [1] = 0;
764
765 loop_destroy (EV_A);
766}
767
768void
769ev_default_fork (void)
770{
771#if EV_MULTIPLICITY
772 struct ev_loop *loop = default_loop;
773#endif
774
775 loop_fork (EV_A);
776
777 ev_io_stop (EV_A_ &sigev);
674 close (sigpipe [0]); 778 close (sigpipe [0]);
675 close (sigpipe [1]); 779 close (sigpipe [1]);
676 pipe (sigpipe); 780 pipe (sigpipe);
781
782 ev_ref (EV_A); /* signal watcher */
677 siginit (); 783 siginit (EV_A);
678} 784}
679 785
680/*****************************************************************************/ 786/*****************************************************************************/
681 787
682static void 788static void
690 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 796 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
691 797
692 if (p->w) 798 if (p->w)
693 { 799 {
694 p->w->pending = 0; 800 p->w->pending = 0;
801
695 p->w->cb (EV_A_ p->w, p->events); 802 (*(void (**)(EV_P_ W, int))&p->w->cb) (EV_A_ p->w, p->events);
696 } 803 }
697 } 804 }
698} 805}
699 806
700static void 807static void
701timers_reify (EV_P) 808timers_reify (EV_P)
702{ 809{
703 while (timercnt && timers [0]->at <= mn_now) 810 while (timercnt && ((WT)timers [0])->at <= mn_now)
704 { 811 {
705 struct ev_timer *w = timers [0]; 812 struct ev_timer *w = timers [0];
813
814 assert (("inactive timer on timer heap detected", ev_is_active (w)));
706 815
707 /* first reschedule or stop timer */ 816 /* first reschedule or stop timer */
708 if (w->repeat) 817 if (w->repeat)
709 { 818 {
710 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); 819 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
711 w->at = mn_now + w->repeat; 820 ((WT)w)->at = mn_now + w->repeat;
712 downheap ((WT *)timers, timercnt, 0); 821 downheap ((WT *)timers, timercnt, 0);
713 } 822 }
714 else 823 else
715 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 824 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
716 825
717 event ((W)w, EV_TIMEOUT); 826 event (EV_A_ (W)w, EV_TIMEOUT);
718 } 827 }
719} 828}
720 829
721static void 830static void
722periodics_reify (EV_P) 831periodics_reify (EV_P)
723{ 832{
724 while (periodiccnt && periodics [0]->at <= rt_now) 833 while (periodiccnt && ((WT)periodics [0])->at <= rt_now)
725 { 834 {
726 struct ev_periodic *w = periodics [0]; 835 struct ev_periodic *w = periodics [0];
836
837 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
727 838
728 /* first reschedule or stop timer */ 839 /* first reschedule or stop timer */
729 if (w->interval) 840 if (w->interval)
730 { 841 {
731 w->at += floor ((rt_now - w->at) / w->interval + 1.) * w->interval; 842 ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval;
732 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > rt_now)); 843 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now));
733 downheap ((WT *)periodics, periodiccnt, 0); 844 downheap ((WT *)periodics, periodiccnt, 0);
734 } 845 }
735 else 846 else
736 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 847 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
737 848
738 event (EV_A_ (W)w, EV_PERIODIC); 849 event (EV_A_ (W)w, EV_PERIODIC);
739 } 850 }
740} 851}
741 852
742static void 853static void
743periodics_reschedule (EV_P_ ev_tstamp diff) 854periodics_reschedule (EV_P)
744{ 855{
745 int i; 856 int i;
746 857
747 /* adjust periodics after time jump */ 858 /* adjust periodics after time jump */
748 for (i = 0; i < periodiccnt; ++i) 859 for (i = 0; i < periodiccnt; ++i)
749 { 860 {
750 struct ev_periodic *w = periodics [i]; 861 struct ev_periodic *w = periodics [i];
751 862
752 if (w->interval) 863 if (w->interval)
753 { 864 {
754 ev_tstamp diff = ceil ((rt_now - w->at) / w->interval) * w->interval; 865 ev_tstamp diff = ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval;
755 866
756 if (fabs (diff) >= 1e-4) 867 if (fabs (diff) >= 1e-4)
757 { 868 {
758 ev_periodic_stop (EV_A_ w); 869 ev_periodic_stop (EV_A_ w);
759 ev_periodic_start (EV_A_ w); 870 ev_periodic_start (EV_A_ w);
769{ 880{
770 mn_now = get_clock (); 881 mn_now = get_clock ();
771 882
772 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) 883 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
773 { 884 {
774 rt_now = mn_now + diff; 885 rt_now = rtmn_diff + mn_now;
775 return 0; 886 return 0;
776 } 887 }
777 else 888 else
778 { 889 {
779 now_floor = mn_now; 890 now_floor = mn_now;
790#if EV_USE_MONOTONIC 901#if EV_USE_MONOTONIC
791 if (expect_true (have_monotonic)) 902 if (expect_true (have_monotonic))
792 { 903 {
793 if (time_update_monotonic (EV_A)) 904 if (time_update_monotonic (EV_A))
794 { 905 {
795 ev_tstamp odiff = diff; 906 ev_tstamp odiff = rtmn_diff;
796 907
797 for (i = 4; --i; ) /* loop a few times, before making important decisions */ 908 for (i = 4; --i; ) /* loop a few times, before making important decisions */
798 { 909 {
799 diff = rt_now - mn_now; 910 rtmn_diff = rt_now - mn_now;
800 911
801 if (fabs (odiff - diff) < MIN_TIMEJUMP) 912 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)
802 return; /* all is well */ 913 return; /* all is well */
803 914
804 rt_now = ev_time (); 915 rt_now = ev_time ();
805 mn_now = get_clock (); 916 mn_now = get_clock ();
806 now_floor = mn_now; 917 now_floor = mn_now;
807 } 918 }
808 919
809 periodics_reschedule (EV_A_ diff - odiff); 920 periodics_reschedule (EV_A);
810 /* no timer adjustment, as the monotonic clock doesn't jump */ 921 /* no timer adjustment, as the monotonic clock doesn't jump */
922 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */
811 } 923 }
812 } 924 }
813 else 925 else
814#endif 926#endif
815 { 927 {
816 rt_now = ev_time (); 928 rt_now = ev_time ();
817 929
818 if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) 930 if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
819 { 931 {
820 periodics_reschedule (EV_A_ rt_now - mn_now); 932 periodics_reschedule (EV_A);
821 933
822 /* adjust timers. this is easy, as the offset is the same for all */ 934 /* adjust timers. this is easy, as the offset is the same for all */
823 for (i = 0; i < timercnt; ++i) 935 for (i = 0; i < timercnt; ++i)
824 timers [i]->at += diff; 936 ((WT)timers [i])->at += rt_now - mn_now;
825 } 937 }
826 938
827 mn_now = rt_now; 939 mn_now = rt_now;
828 } 940 }
829} 941}
880 { 992 {
881 block = MAX_BLOCKTIME; 993 block = MAX_BLOCKTIME;
882 994
883 if (timercnt) 995 if (timercnt)
884 { 996 {
885 ev_tstamp to = timers [0]->at - mn_now + method_fudge; 997 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge;
886 if (block > to) block = to; 998 if (block > to) block = to;
887 } 999 }
888 1000
889 if (periodiccnt) 1001 if (periodiccnt)
890 { 1002 {
891 ev_tstamp to = periodics [0]->at - rt_now + method_fudge; 1003 ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge;
892 if (block > to) block = to; 1004 if (block > to) block = to;
893 } 1005 }
894 1006
895 if (block < 0.) block = 0.; 1007 if (block < 0.) block = 0.;
896 } 1008 }
911 /* queue check watchers, to be executed first */ 1023 /* queue check watchers, to be executed first */
912 if (checkcnt) 1024 if (checkcnt)
913 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1025 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
914 1026
915 call_pending (EV_A); 1027 call_pending (EV_A);
916 printf ("activecnt %d\n", activecnt);//D
917 } 1028 }
918 while (activecnt && !loop_done); 1029 while (activecnt && !loop_done);
919 1030
920 if (loop_done != 2) 1031 if (loop_done != 2)
921 loop_done = 0; 1032 loop_done = 0;
1014ev_timer_start (EV_P_ struct ev_timer *w) 1125ev_timer_start (EV_P_ struct ev_timer *w)
1015{ 1126{
1016 if (ev_is_active (w)) 1127 if (ev_is_active (w))
1017 return; 1128 return;
1018 1129
1019 w->at += mn_now; 1130 ((WT)w)->at += mn_now;
1020 1131
1021 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1132 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1022 1133
1023 ev_start (EV_A_ (W)w, ++timercnt); 1134 ev_start (EV_A_ (W)w, ++timercnt);
1024 array_needsize (timers, timermax, timercnt, ); 1135 array_needsize (timers, timermax, timercnt, );
1025 timers [timercnt - 1] = w; 1136 timers [timercnt - 1] = w;
1026 upheap ((WT *)timers, timercnt - 1); 1137 upheap ((WT *)timers, timercnt - 1);
1138
1139 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1027} 1140}
1028 1141
1029void 1142void
1030ev_timer_stop (EV_P_ struct ev_timer *w) 1143ev_timer_stop (EV_P_ struct ev_timer *w)
1031{ 1144{
1032 ev_clear_pending (EV_A_ (W)w); 1145 ev_clear_pending (EV_A_ (W)w);
1033 if (!ev_is_active (w)) 1146 if (!ev_is_active (w))
1034 return; 1147 return;
1035 1148
1149 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1150
1036 if (w->active < timercnt--) 1151 if (((W)w)->active < timercnt--)
1037 { 1152 {
1038 timers [w->active - 1] = timers [timercnt]; 1153 timers [((W)w)->active - 1] = timers [timercnt];
1039 downheap ((WT *)timers, timercnt, w->active - 1); 1154 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1040 } 1155 }
1041 1156
1042 w->at = w->repeat; 1157 ((WT)w)->at = w->repeat;
1043 1158
1044 ev_stop (EV_A_ (W)w); 1159 ev_stop (EV_A_ (W)w);
1045} 1160}
1046 1161
1047void 1162void
1049{ 1164{
1050 if (ev_is_active (w)) 1165 if (ev_is_active (w))
1051 { 1166 {
1052 if (w->repeat) 1167 if (w->repeat)
1053 { 1168 {
1054 w->at = mn_now + w->repeat; 1169 ((WT)w)->at = mn_now + w->repeat;
1055 downheap ((WT *)timers, timercnt, w->active - 1); 1170 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1056 } 1171 }
1057 else 1172 else
1058 ev_timer_stop (EV_A_ w); 1173 ev_timer_stop (EV_A_ w);
1059 } 1174 }
1060 else if (w->repeat) 1175 else if (w->repeat)
1069 1184
1070 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); 1185 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
1071 1186
1072 /* this formula differs from the one in periodic_reify because we do not always round up */ 1187 /* this formula differs from the one in periodic_reify because we do not always round up */
1073 if (w->interval) 1188 if (w->interval)
1074 w->at += ceil ((rt_now - w->at) / w->interval) * w->interval; 1189 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval;
1075 1190
1076 ev_start (EV_A_ (W)w, ++periodiccnt); 1191 ev_start (EV_A_ (W)w, ++periodiccnt);
1077 array_needsize (periodics, periodicmax, periodiccnt, ); 1192 array_needsize (periodics, periodicmax, periodiccnt, );
1078 periodics [periodiccnt - 1] = w; 1193 periodics [periodiccnt - 1] = w;
1079 upheap ((WT *)periodics, periodiccnt - 1); 1194 upheap ((WT *)periodics, periodiccnt - 1);
1195
1196 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1080} 1197}
1081 1198
1082void 1199void
1083ev_periodic_stop (EV_P_ struct ev_periodic *w) 1200ev_periodic_stop (EV_P_ struct ev_periodic *w)
1084{ 1201{
1085 ev_clear_pending (EV_A_ (W)w); 1202 ev_clear_pending (EV_A_ (W)w);
1086 if (!ev_is_active (w)) 1203 if (!ev_is_active (w))
1087 return; 1204 return;
1088 1205
1206 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1207
1089 if (w->active < periodiccnt--) 1208 if (((W)w)->active < periodiccnt--)
1090 { 1209 {
1091 periodics [w->active - 1] = periodics [periodiccnt]; 1210 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1092 downheap ((WT *)periodics, periodiccnt, w->active - 1); 1211 downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1093 } 1212 }
1094 1213
1214 ev_stop (EV_A_ (W)w);
1215}
1216
1217void
1218ev_idle_start (EV_P_ struct ev_idle *w)
1219{
1220 if (ev_is_active (w))
1221 return;
1222
1223 ev_start (EV_A_ (W)w, ++idlecnt);
1224 array_needsize (idles, idlemax, idlecnt, );
1225 idles [idlecnt - 1] = w;
1226}
1227
1228void
1229ev_idle_stop (EV_P_ struct ev_idle *w)
1230{
1231 ev_clear_pending (EV_A_ (W)w);
1232 if (ev_is_active (w))
1233 return;
1234
1235 idles [((W)w)->active - 1] = idles [--idlecnt];
1236 ev_stop (EV_A_ (W)w);
1237}
1238
1239void
1240ev_prepare_start (EV_P_ struct ev_prepare *w)
1241{
1242 if (ev_is_active (w))
1243 return;
1244
1245 ev_start (EV_A_ (W)w, ++preparecnt);
1246 array_needsize (prepares, preparemax, preparecnt, );
1247 prepares [preparecnt - 1] = w;
1248}
1249
1250void
1251ev_prepare_stop (EV_P_ struct ev_prepare *w)
1252{
1253 ev_clear_pending (EV_A_ (W)w);
1254 if (ev_is_active (w))
1255 return;
1256
1257 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1258 ev_stop (EV_A_ (W)w);
1259}
1260
1261void
1262ev_check_start (EV_P_ struct ev_check *w)
1263{
1264 if (ev_is_active (w))
1265 return;
1266
1267 ev_start (EV_A_ (W)w, ++checkcnt);
1268 array_needsize (checks, checkmax, checkcnt, );
1269 checks [checkcnt - 1] = w;
1270}
1271
1272void
1273ev_check_stop (EV_P_ struct ev_check *w)
1274{
1275 ev_clear_pending (EV_A_ (W)w);
1276 if (ev_is_active (w))
1277 return;
1278
1279 checks [((W)w)->active - 1] = checks [--checkcnt];
1095 ev_stop (EV_A_ (W)w); 1280 ev_stop (EV_A_ (W)w);
1096} 1281}
1097 1282
1098#ifndef SA_RESTART 1283#ifndef SA_RESTART
1099# define SA_RESTART 0 1284# define SA_RESTART 0
1100#endif 1285#endif
1101 1286
1102void 1287void
1103ev_signal_start (EV_P_ struct ev_signal *w) 1288ev_signal_start (EV_P_ struct ev_signal *w)
1104{ 1289{
1290#if EV_MULTIPLICITY
1291 assert (("signal watchers are only supported in the default loop", loop == default_loop));
1292#endif
1105 if (ev_is_active (w)) 1293 if (ev_is_active (w))
1106 return; 1294 return;
1107 1295
1108 assert (("ev_signal_start called with illegal signal number", w->signum > 0)); 1296 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
1109 1297
1110 ev_start (EV_A_ (W)w, 1); 1298 ev_start (EV_A_ (W)w, 1);
1111 array_needsize (signals, signalmax, w->signum, signals_init); 1299 array_needsize (signals, signalmax, w->signum, signals_init);
1112 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1300 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1113 1301
1114 if (!w->next) 1302 if (!((WL)w)->next)
1115 { 1303 {
1116 struct sigaction sa; 1304 struct sigaction sa;
1117 sa.sa_handler = sighandler; 1305 sa.sa_handler = sighandler;
1118 sigfillset (&sa.sa_mask); 1306 sigfillset (&sa.sa_mask);
1119 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ 1307 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
1134 if (!signals [w->signum - 1].head) 1322 if (!signals [w->signum - 1].head)
1135 signal (w->signum, SIG_DFL); 1323 signal (w->signum, SIG_DFL);
1136} 1324}
1137 1325
1138void 1326void
1139ev_idle_start (EV_P_ struct ev_idle *w)
1140{
1141 if (ev_is_active (w))
1142 return;
1143
1144 ev_start (EV_A_ (W)w, ++idlecnt);
1145 array_needsize (idles, idlemax, idlecnt, );
1146 idles [idlecnt - 1] = w;
1147}
1148
1149void
1150ev_idle_stop (EV_P_ struct ev_idle *w)
1151{
1152 ev_clear_pending (EV_A_ (W)w);
1153 if (ev_is_active (w))
1154 return;
1155
1156 idles [w->active - 1] = idles [--idlecnt];
1157 ev_stop (EV_A_ (W)w);
1158}
1159
1160void
1161ev_prepare_start (EV_P_ struct ev_prepare *w)
1162{
1163 if (ev_is_active (w))
1164 return;
1165
1166 ev_start (EV_A_ (W)w, ++preparecnt);
1167 array_needsize (prepares, preparemax, preparecnt, );
1168 prepares [preparecnt - 1] = w;
1169}
1170
1171void
1172ev_prepare_stop (EV_P_ struct ev_prepare *w)
1173{
1174 ev_clear_pending (EV_A_ (W)w);
1175 if (ev_is_active (w))
1176 return;
1177
1178 prepares [w->active - 1] = prepares [--preparecnt];
1179 ev_stop (EV_A_ (W)w);
1180}
1181
1182void
1183ev_check_start (EV_P_ struct ev_check *w)
1184{
1185 if (ev_is_active (w))
1186 return;
1187
1188 ev_start (EV_A_ (W)w, ++checkcnt);
1189 array_needsize (checks, checkmax, checkcnt, );
1190 checks [checkcnt - 1] = w;
1191}
1192
1193void
1194ev_check_stop (EV_P_ struct ev_check *w)
1195{
1196 ev_clear_pending (EV_A_ (W)w);
1197 if (ev_is_active (w))
1198 return;
1199
1200 checks [w->active - 1] = checks [--checkcnt];
1201 ev_stop (EV_A_ (W)w);
1202}
1203
1204void
1205ev_child_start (EV_P_ struct ev_child *w) 1327ev_child_start (EV_P_ struct ev_child *w)
1206{ 1328{
1329#if EV_MULTIPLICITY
1330 assert (("child watchers are only supported in the default loop", loop == default_loop));
1331#endif
1207 if (ev_is_active (w)) 1332 if (ev_is_active (w))
1208 return; 1333 return;
1209 1334
1210 ev_start (EV_A_ (W)w, 1); 1335 ev_start (EV_A_ (W)w, 1);
1211 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1336 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1283 ev_timer_start (EV_A_ &once->to); 1408 ev_timer_start (EV_A_ &once->to);
1284 } 1409 }
1285 } 1410 }
1286} 1411}
1287 1412
1288/*****************************************************************************/
1289
1290#if 0
1291
1292struct ev_io wio;
1293
1294static void
1295sin_cb (struct ev_io *w, int revents)
1296{
1297 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
1298}
1299
1300static void
1301ocb (struct ev_timer *w, int revents)
1302{
1303 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data);
1304 ev_timer_stop (w);
1305 ev_timer_start (w);
1306}
1307
1308static void
1309scb (struct ev_signal *w, int revents)
1310{
1311 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
1312 ev_io_stop (&wio);
1313 ev_io_start (&wio);
1314}
1315
1316static void
1317gcb (struct ev_signal *w, int revents)
1318{
1319 fprintf (stderr, "generic %x\n", revents);
1320
1321}
1322
1323int main (void)
1324{
1325 ev_init (0);
1326
1327 ev_io_init (&wio, sin_cb, 0, EV_READ);
1328 ev_io_start (&wio);
1329
1330 struct ev_timer t[10000];
1331
1332#if 0
1333 int i;
1334 for (i = 0; i < 10000; ++i)
1335 {
1336 struct ev_timer *w = t + i;
1337 ev_watcher_init (w, ocb, i);
1338 ev_timer_init_abs (w, ocb, drand48 (), 0.99775533);
1339 ev_timer_start (w);
1340 if (drand48 () < 0.5)
1341 ev_timer_stop (w);
1342 }
1343#endif
1344
1345 struct ev_timer t1;
1346 ev_timer_init (&t1, ocb, 5, 10);
1347 ev_timer_start (&t1);
1348
1349 struct ev_signal sig;
1350 ev_signal_init (&sig, scb, SIGQUIT);
1351 ev_signal_start (&sig);
1352
1353 struct ev_check cw;
1354 ev_check_init (&cw, gcb);
1355 ev_check_start (&cw);
1356
1357 struct ev_idle iw;
1358 ev_idle_init (&iw, gcb);
1359 ev_idle_start (&iw);
1360
1361 ev_loop (0);
1362
1363 return 0;
1364}
1365
1366#endif
1367
1368
1369
1370

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines