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.65 by root, Sun Nov 4 23:29:48 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
206 base = realloc (base, sizeof (*base) * (newcnt)); \ 232 base = realloc (base, sizeof (*base) * (newcnt)); \
207 init (base + cur, newcnt - cur); \ 233 init (base + cur, newcnt - cur); \
208 cur = newcnt; \ 234 cur = newcnt; \
209 } 235 }
210 236
237#define array_free(stem, idx) \
238 free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
239
211/*****************************************************************************/ 240/*****************************************************************************/
212
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 241
223static void 242static void
224anfds_init (ANFD *base, int count) 243anfds_init (ANFD *base, int count)
225{ 244{
226 while (count--) 245 while (count--)
230 base->reify = 0; 249 base->reify = 0;
231 250
232 ++base; 251 ++base;
233 } 252 }
234} 253}
235
236typedef struct
237{
238 W w;
239 int events;
240} ANPENDING;
241
242static ANPENDING *pendings [NUMPRI];
243static int pendingmax [NUMPRI], pendingcnt [NUMPRI];
244 254
245static void 255static void
246event (EV_P_ W w, int events) 256event (EV_P_ W w, int events)
247{ 257{
248 if (w->pending) 258 if (w->pending)
281 } 291 }
282} 292}
283 293
284/*****************************************************************************/ 294/*****************************************************************************/
285 295
286static int *fdchanges;
287static int fdchangemax, fdchangecnt;
288
289static void 296static void
290fd_reify (EV_P) 297fd_reify (EV_P)
291{ 298{
292 int i; 299 int i;
293 300
302 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) 309 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
303 events |= w->events; 310 events |= w->events;
304 311
305 anfd->reify = 0; 312 anfd->reify = 0;
306 313
307 if (anfd->events != events)
308 {
309 method_modify (EV_A_ fd, anfd->events, events); 314 method_modify (EV_A_ fd, anfd->events, events);
310 anfd->events = events; 315 anfd->events = events;
311 }
312 } 316 }
313 317
314 fdchangecnt = 0; 318 fdchangecnt = 0;
315} 319}
316 320
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 int i;
643
644#if EV_USE_WIN32
645 if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A);
646#endif
647#if EV_USE_KQUEUE
648 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
649#endif
650#if EV_USE_EPOLL
651 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A);
652#endif
653#if EV_USE_POLL
654 if (method == EVMETHOD_POLL ) poll_destroy (EV_A);
655#endif
656#if EV_USE_SELECT
657 if (method == EVMETHOD_SELECT) select_destroy (EV_A);
658#endif
659
660 for (i = NUMPRI; i--; )
661 array_free (pending, [i]);
662
663 array_free (fdchange, );
664 array_free (timer, );
665 array_free (periodic, );
666 array_free (idle, );
667 array_free (prepare, );
668 array_free (check, );
669
670 method = 0;
671 /*TODO*/
672}
673
674void
675loop_fork (EV_P)
676{
677 /*TODO*/
678#if EV_USE_EPOLL
679 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A);
680#endif
681#if EV_USE_KQUEUE
682 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A);
683#endif
684}
685
686#if EV_MULTIPLICITY
687struct ev_loop *
688ev_loop_new (int methods)
689{
690 struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop));
691
692 loop_init (EV_A_ methods);
693
694 if (ev_method (EV_A))
695 return loop;
696
697 return 0;
698}
699
700void
701ev_loop_destroy (EV_P)
702{
703 loop_destroy (EV_A);
704 free (loop);
705}
706
707void
708ev_loop_fork (EV_P)
709{
710 loop_fork (EV_A);
711}
712
713#endif
714
715#if EV_MULTIPLICITY
716struct ev_loop default_loop_struct;
717static struct ev_loop *default_loop;
718
719struct ev_loop *
720#else
721static int default_loop;
722
723int
724#endif
725ev_default_loop (int methods)
726{
727 if (sigpipe [0] == sigpipe [1])
728 if (pipe (sigpipe))
729 return 0;
730
731 if (!default_loop)
732 {
733#if EV_MULTIPLICITY
734 struct ev_loop *loop = default_loop = &default_loop_struct;
735#else
736 default_loop = 1;
737#endif
738
739 loop_init (EV_A_ methods);
740
634 if (method) 741 if (ev_method (EV_A))
635 { 742 {
636 ev_watcher_init (&sigev, sigcb); 743 ev_watcher_init (&sigev, sigcb);
637 ev_set_priority (&sigev, EV_MAXPRI); 744 ev_set_priority (&sigev, EV_MAXPRI);
638 siginit (EV_A); 745 siginit (EV_A);
639 746
640#ifndef WIN32 747#ifndef WIN32
641 ev_signal_init (&childev, childcb, SIGCHLD); 748 ev_signal_init (&childev, childcb, SIGCHLD);
642 ev_set_priority (&childev, EV_MAXPRI); 749 ev_set_priority (&childev, EV_MAXPRI);
643 ev_signal_start (EV_A_ &childev); 750 ev_signal_start (EV_A_ &childev);
751 ev_unref (EV_A); /* child watcher should not keep loop alive */
644#endif 752#endif
645 } 753 }
754 else
755 default_loop = 0;
646 } 756 }
647 757
648 return method; 758 return default_loop;
649} 759}
650 760
651/*****************************************************************************/
652
653void 761void
654ev_fork_prepare (void) 762ev_default_destroy (void)
655{ 763{
656 /* nop */ 764#if EV_MULTIPLICITY
657} 765 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 766#endif
672 767
768 ev_ref (EV_A); /* child watcher */
769 ev_signal_stop (EV_A_ &childev);
770
771 ev_ref (EV_A); /* signal watcher */
673 ev_io_stop (&sigev); 772 ev_io_stop (EV_A_ &sigev);
773
774 close (sigpipe [0]); sigpipe [0] = 0;
775 close (sigpipe [1]); sigpipe [1] = 0;
776
777 loop_destroy (EV_A);
778}
779
780void
781ev_default_fork (void)
782{
783#if EV_MULTIPLICITY
784 struct ev_loop *loop = default_loop;
785#endif
786
787 loop_fork (EV_A);
788
789 ev_io_stop (EV_A_ &sigev);
674 close (sigpipe [0]); 790 close (sigpipe [0]);
675 close (sigpipe [1]); 791 close (sigpipe [1]);
676 pipe (sigpipe); 792 pipe (sigpipe);
793
794 ev_ref (EV_A); /* signal watcher */
677 siginit (); 795 siginit (EV_A);
678} 796}
679 797
680/*****************************************************************************/ 798/*****************************************************************************/
681 799
682static void 800static void
690 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 808 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
691 809
692 if (p->w) 810 if (p->w)
693 { 811 {
694 p->w->pending = 0; 812 p->w->pending = 0;
813
695 p->w->cb (EV_A_ p->w, p->events); 814 ((void (*)(EV_P_ W, int))p->w->cb) (EV_A_ p->w, p->events);
696 } 815 }
697 } 816 }
698} 817}
699 818
700static void 819static void
701timers_reify (EV_P) 820timers_reify (EV_P)
702{ 821{
703 while (timercnt && timers [0]->at <= mn_now) 822 while (timercnt && ((WT)timers [0])->at <= mn_now)
704 { 823 {
705 struct ev_timer *w = timers [0]; 824 struct ev_timer *w = timers [0];
825
826 assert (("inactive timer on timer heap detected", ev_is_active (w)));
706 827
707 /* first reschedule or stop timer */ 828 /* first reschedule or stop timer */
708 if (w->repeat) 829 if (w->repeat)
709 { 830 {
710 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); 831 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
711 w->at = mn_now + w->repeat; 832 ((WT)w)->at = mn_now + w->repeat;
712 downheap ((WT *)timers, timercnt, 0); 833 downheap ((WT *)timers, timercnt, 0);
713 } 834 }
714 else 835 else
715 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 836 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
716 837
717 event ((W)w, EV_TIMEOUT); 838 event (EV_A_ (W)w, EV_TIMEOUT);
718 } 839 }
719} 840}
720 841
721static void 842static void
722periodics_reify (EV_P) 843periodics_reify (EV_P)
723{ 844{
724 while (periodiccnt && periodics [0]->at <= rt_now) 845 while (periodiccnt && ((WT)periodics [0])->at <= rt_now)
725 { 846 {
726 struct ev_periodic *w = periodics [0]; 847 struct ev_periodic *w = periodics [0];
848
849 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
727 850
728 /* first reschedule or stop timer */ 851 /* first reschedule or stop timer */
729 if (w->interval) 852 if (w->interval)
730 { 853 {
731 w->at += floor ((rt_now - w->at) / w->interval + 1.) * w->interval; 854 ((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)); 855 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now));
733 downheap ((WT *)periodics, periodiccnt, 0); 856 downheap ((WT *)periodics, periodiccnt, 0);
734 } 857 }
735 else 858 else
736 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 859 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
737 860
738 event (EV_A_ (W)w, EV_PERIODIC); 861 event (EV_A_ (W)w, EV_PERIODIC);
739 } 862 }
740} 863}
741 864
742static void 865static void
743periodics_reschedule (EV_P_ ev_tstamp diff) 866periodics_reschedule (EV_P)
744{ 867{
745 int i; 868 int i;
746 869
747 /* adjust periodics after time jump */ 870 /* adjust periodics after time jump */
748 for (i = 0; i < periodiccnt; ++i) 871 for (i = 0; i < periodiccnt; ++i)
749 { 872 {
750 struct ev_periodic *w = periodics [i]; 873 struct ev_periodic *w = periodics [i];
751 874
752 if (w->interval) 875 if (w->interval)
753 { 876 {
754 ev_tstamp diff = ceil ((rt_now - w->at) / w->interval) * w->interval; 877 ev_tstamp diff = ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval;
755 878
756 if (fabs (diff) >= 1e-4) 879 if (fabs (diff) >= 1e-4)
757 { 880 {
758 ev_periodic_stop (EV_A_ w); 881 ev_periodic_stop (EV_A_ w);
759 ev_periodic_start (EV_A_ w); 882 ev_periodic_start (EV_A_ w);
769{ 892{
770 mn_now = get_clock (); 893 mn_now = get_clock ();
771 894
772 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) 895 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
773 { 896 {
774 rt_now = mn_now + diff; 897 rt_now = rtmn_diff + mn_now;
775 return 0; 898 return 0;
776 } 899 }
777 else 900 else
778 { 901 {
779 now_floor = mn_now; 902 now_floor = mn_now;
790#if EV_USE_MONOTONIC 913#if EV_USE_MONOTONIC
791 if (expect_true (have_monotonic)) 914 if (expect_true (have_monotonic))
792 { 915 {
793 if (time_update_monotonic (EV_A)) 916 if (time_update_monotonic (EV_A))
794 { 917 {
795 ev_tstamp odiff = diff; 918 ev_tstamp odiff = rtmn_diff;
796 919
797 for (i = 4; --i; ) /* loop a few times, before making important decisions */ 920 for (i = 4; --i; ) /* loop a few times, before making important decisions */
798 { 921 {
799 diff = rt_now - mn_now; 922 rtmn_diff = rt_now - mn_now;
800 923
801 if (fabs (odiff - diff) < MIN_TIMEJUMP) 924 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)
802 return; /* all is well */ 925 return; /* all is well */
803 926
804 rt_now = ev_time (); 927 rt_now = ev_time ();
805 mn_now = get_clock (); 928 mn_now = get_clock ();
806 now_floor = mn_now; 929 now_floor = mn_now;
807 } 930 }
808 931
809 periodics_reschedule (EV_A_ diff - odiff); 932 periodics_reschedule (EV_A);
810 /* no timer adjustment, as the monotonic clock doesn't jump */ 933 /* no timer adjustment, as the monotonic clock doesn't jump */
934 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */
811 } 935 }
812 } 936 }
813 else 937 else
814#endif 938#endif
815 { 939 {
816 rt_now = ev_time (); 940 rt_now = ev_time ();
817 941
818 if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) 942 if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
819 { 943 {
820 periodics_reschedule (EV_A_ rt_now - mn_now); 944 periodics_reschedule (EV_A);
821 945
822 /* adjust timers. this is easy, as the offset is the same for all */ 946 /* adjust timers. this is easy, as the offset is the same for all */
823 for (i = 0; i < timercnt; ++i) 947 for (i = 0; i < timercnt; ++i)
824 timers [i]->at += diff; 948 ((WT)timers [i])->at += rt_now - mn_now;
825 } 949 }
826 950
827 mn_now = rt_now; 951 mn_now = rt_now;
828 } 952 }
829} 953}
880 { 1004 {
881 block = MAX_BLOCKTIME; 1005 block = MAX_BLOCKTIME;
882 1006
883 if (timercnt) 1007 if (timercnt)
884 { 1008 {
885 ev_tstamp to = timers [0]->at - mn_now + method_fudge; 1009 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge;
886 if (block > to) block = to; 1010 if (block > to) block = to;
887 } 1011 }
888 1012
889 if (periodiccnt) 1013 if (periodiccnt)
890 { 1014 {
891 ev_tstamp to = periodics [0]->at - rt_now + method_fudge; 1015 ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge;
892 if (block > to) block = to; 1016 if (block > to) block = to;
893 } 1017 }
894 1018
895 if (block < 0.) block = 0.; 1019 if (block < 0.) block = 0.;
896 } 1020 }
911 /* queue check watchers, to be executed first */ 1035 /* queue check watchers, to be executed first */
912 if (checkcnt) 1036 if (checkcnt)
913 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1037 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
914 1038
915 call_pending (EV_A); 1039 call_pending (EV_A);
916 printf ("activecnt %d\n", activecnt);//D
917 } 1040 }
918 while (activecnt && !loop_done); 1041 while (activecnt && !loop_done);
919 1042
920 if (loop_done != 2) 1043 if (loop_done != 2)
921 loop_done = 0; 1044 loop_done = 0;
1014ev_timer_start (EV_P_ struct ev_timer *w) 1137ev_timer_start (EV_P_ struct ev_timer *w)
1015{ 1138{
1016 if (ev_is_active (w)) 1139 if (ev_is_active (w))
1017 return; 1140 return;
1018 1141
1019 w->at += mn_now; 1142 ((WT)w)->at += mn_now;
1020 1143
1021 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1144 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1022 1145
1023 ev_start (EV_A_ (W)w, ++timercnt); 1146 ev_start (EV_A_ (W)w, ++timercnt);
1024 array_needsize (timers, timermax, timercnt, ); 1147 array_needsize (timers, timermax, timercnt, );
1025 timers [timercnt - 1] = w; 1148 timers [timercnt - 1] = w;
1026 upheap ((WT *)timers, timercnt - 1); 1149 upheap ((WT *)timers, timercnt - 1);
1150
1151 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1027} 1152}
1028 1153
1029void 1154void
1030ev_timer_stop (EV_P_ struct ev_timer *w) 1155ev_timer_stop (EV_P_ struct ev_timer *w)
1031{ 1156{
1032 ev_clear_pending (EV_A_ (W)w); 1157 ev_clear_pending (EV_A_ (W)w);
1033 if (!ev_is_active (w)) 1158 if (!ev_is_active (w))
1034 return; 1159 return;
1035 1160
1161 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1162
1036 if (w->active < timercnt--) 1163 if (((W)w)->active < timercnt--)
1037 { 1164 {
1038 timers [w->active - 1] = timers [timercnt]; 1165 timers [((W)w)->active - 1] = timers [timercnt];
1039 downheap ((WT *)timers, timercnt, w->active - 1); 1166 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1040 } 1167 }
1041 1168
1042 w->at = w->repeat; 1169 ((WT)w)->at = w->repeat;
1043 1170
1044 ev_stop (EV_A_ (W)w); 1171 ev_stop (EV_A_ (W)w);
1045} 1172}
1046 1173
1047void 1174void
1049{ 1176{
1050 if (ev_is_active (w)) 1177 if (ev_is_active (w))
1051 { 1178 {
1052 if (w->repeat) 1179 if (w->repeat)
1053 { 1180 {
1054 w->at = mn_now + w->repeat; 1181 ((WT)w)->at = mn_now + w->repeat;
1055 downheap ((WT *)timers, timercnt, w->active - 1); 1182 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1056 } 1183 }
1057 else 1184 else
1058 ev_timer_stop (EV_A_ w); 1185 ev_timer_stop (EV_A_ w);
1059 } 1186 }
1060 else if (w->repeat) 1187 else if (w->repeat)
1069 1196
1070 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); 1197 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
1071 1198
1072 /* this formula differs from the one in periodic_reify because we do not always round up */ 1199 /* this formula differs from the one in periodic_reify because we do not always round up */
1073 if (w->interval) 1200 if (w->interval)
1074 w->at += ceil ((rt_now - w->at) / w->interval) * w->interval; 1201 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval;
1075 1202
1076 ev_start (EV_A_ (W)w, ++periodiccnt); 1203 ev_start (EV_A_ (W)w, ++periodiccnt);
1077 array_needsize (periodics, periodicmax, periodiccnt, ); 1204 array_needsize (periodics, periodicmax, periodiccnt, );
1078 periodics [periodiccnt - 1] = w; 1205 periodics [periodiccnt - 1] = w;
1079 upheap ((WT *)periodics, periodiccnt - 1); 1206 upheap ((WT *)periodics, periodiccnt - 1);
1207
1208 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1080} 1209}
1081 1210
1082void 1211void
1083ev_periodic_stop (EV_P_ struct ev_periodic *w) 1212ev_periodic_stop (EV_P_ struct ev_periodic *w)
1084{ 1213{
1085 ev_clear_pending (EV_A_ (W)w); 1214 ev_clear_pending (EV_A_ (W)w);
1086 if (!ev_is_active (w)) 1215 if (!ev_is_active (w))
1087 return; 1216 return;
1088 1217
1218 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1219
1089 if (w->active < periodiccnt--) 1220 if (((W)w)->active < periodiccnt--)
1090 { 1221 {
1091 periodics [w->active - 1] = periodics [periodiccnt]; 1222 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1092 downheap ((WT *)periodics, periodiccnt, w->active - 1); 1223 downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1093 } 1224 }
1094 1225
1226 ev_stop (EV_A_ (W)w);
1227}
1228
1229void
1230ev_idle_start (EV_P_ struct ev_idle *w)
1231{
1232 if (ev_is_active (w))
1233 return;
1234
1235 ev_start (EV_A_ (W)w, ++idlecnt);
1236 array_needsize (idles, idlemax, idlecnt, );
1237 idles [idlecnt - 1] = w;
1238}
1239
1240void
1241ev_idle_stop (EV_P_ struct ev_idle *w)
1242{
1243 ev_clear_pending (EV_A_ (W)w);
1244 if (ev_is_active (w))
1245 return;
1246
1247 idles [((W)w)->active - 1] = idles [--idlecnt];
1248 ev_stop (EV_A_ (W)w);
1249}
1250
1251void
1252ev_prepare_start (EV_P_ struct ev_prepare *w)
1253{
1254 if (ev_is_active (w))
1255 return;
1256
1257 ev_start (EV_A_ (W)w, ++preparecnt);
1258 array_needsize (prepares, preparemax, preparecnt, );
1259 prepares [preparecnt - 1] = w;
1260}
1261
1262void
1263ev_prepare_stop (EV_P_ struct ev_prepare *w)
1264{
1265 ev_clear_pending (EV_A_ (W)w);
1266 if (ev_is_active (w))
1267 return;
1268
1269 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1270 ev_stop (EV_A_ (W)w);
1271}
1272
1273void
1274ev_check_start (EV_P_ struct ev_check *w)
1275{
1276 if (ev_is_active (w))
1277 return;
1278
1279 ev_start (EV_A_ (W)w, ++checkcnt);
1280 array_needsize (checks, checkmax, checkcnt, );
1281 checks [checkcnt - 1] = w;
1282}
1283
1284void
1285ev_check_stop (EV_P_ struct ev_check *w)
1286{
1287 ev_clear_pending (EV_A_ (W)w);
1288 if (ev_is_active (w))
1289 return;
1290
1291 checks [((W)w)->active - 1] = checks [--checkcnt];
1095 ev_stop (EV_A_ (W)w); 1292 ev_stop (EV_A_ (W)w);
1096} 1293}
1097 1294
1098#ifndef SA_RESTART 1295#ifndef SA_RESTART
1099# define SA_RESTART 0 1296# define SA_RESTART 0
1100#endif 1297#endif
1101 1298
1102void 1299void
1103ev_signal_start (EV_P_ struct ev_signal *w) 1300ev_signal_start (EV_P_ struct ev_signal *w)
1104{ 1301{
1302#if EV_MULTIPLICITY
1303 assert (("signal watchers are only supported in the default loop", loop == default_loop));
1304#endif
1105 if (ev_is_active (w)) 1305 if (ev_is_active (w))
1106 return; 1306 return;
1107 1307
1108 assert (("ev_signal_start called with illegal signal number", w->signum > 0)); 1308 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
1109 1309
1110 ev_start (EV_A_ (W)w, 1); 1310 ev_start (EV_A_ (W)w, 1);
1111 array_needsize (signals, signalmax, w->signum, signals_init); 1311 array_needsize (signals, signalmax, w->signum, signals_init);
1112 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1312 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1113 1313
1114 if (!w->next) 1314 if (!((WL)w)->next)
1115 { 1315 {
1116 struct sigaction sa; 1316 struct sigaction sa;
1117 sa.sa_handler = sighandler; 1317 sa.sa_handler = sighandler;
1118 sigfillset (&sa.sa_mask); 1318 sigfillset (&sa.sa_mask);
1119 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ 1319 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
1134 if (!signals [w->signum - 1].head) 1334 if (!signals [w->signum - 1].head)
1135 signal (w->signum, SIG_DFL); 1335 signal (w->signum, SIG_DFL);
1136} 1336}
1137 1337
1138void 1338void
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) 1339ev_child_start (EV_P_ struct ev_child *w)
1206{ 1340{
1341#if EV_MULTIPLICITY
1342 assert (("child watchers are only supported in the default loop", loop == default_loop));
1343#endif
1207 if (ev_is_active (w)) 1344 if (ev_is_active (w))
1208 return; 1345 return;
1209 1346
1210 ev_start (EV_A_ (W)w, 1); 1347 ev_start (EV_A_ (W)w, 1);
1211 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1348 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1283 ev_timer_start (EV_A_ &once->to); 1420 ev_timer_start (EV_A_ &once->to);
1284 } 1421 }
1285 } 1422 }
1286} 1423}
1287 1424
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