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

Comparing libev/ev.c (file contents):
Revision 1.53 by root, Sat Nov 3 22:31:11 2007 UTC vs.
Revision 1.64 by root, Sun Nov 4 23:14:11 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
148static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
149
118/*****************************************************************************/ 150/*****************************************************************************/
119 151
120typedef struct 152typedef struct
121{ 153{
122 struct ev_watcher_list *head; 154 struct ev_watcher_list *head;
128{ 160{
129 W w; 161 W w;
130 int events; 162 int events;
131} ANPENDING; 163} ANPENDING;
132 164
133#ifdef EV_MULTIPLICITY 165#if EV_MULTIPLICITY
166
134struct ev_loop 167struct ev_loop
135{ 168{
136# define VAR(name,decl) decl 169# define VAR(name,decl) decl;
137# include "ev_vars.h" 170# include "ev_vars.h"
138}; 171};
172# undef VAR
173# include "ev_wrap.h"
174
139#else 175#else
176
140# define VAR(name,decl) static decl 177# define VAR(name,decl) static decl;
141# include "ev_vars.h" 178# include "ev_vars.h"
142#endif
143#undef VAR 179# undef VAR
180
181#endif
144 182
145/*****************************************************************************/ 183/*****************************************************************************/
146 184
147inline ev_tstamp 185inline ev_tstamp
148ev_time (void) 186ev_time (void)
268 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) 306 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
269 events |= w->events; 307 events |= w->events;
270 308
271 anfd->reify = 0; 309 anfd->reify = 0;
272 310
273 if (anfd->events != events)
274 {
275 method_modify (EV_A_ fd, anfd->events, events); 311 method_modify (EV_A_ fd, anfd->events, events);
276 anfd->events = events; 312 anfd->events = events;
277 }
278 } 313 }
279 314
280 fdchangecnt = 0; 315 fdchangecnt = 0;
281} 316}
282 317
319 354
320/* called on ENOMEM in select/poll to kill some fds and retry */ 355/* called on ENOMEM in select/poll to kill some fds and retry */
321static void 356static void
322fd_enomem (EV_P) 357fd_enomem (EV_P)
323{ 358{
324 int fd = anfdmax; 359 int fd;
325 360
326 while (fd--) 361 for (fd = anfdmax; fd--; )
327 if (anfds [fd].events) 362 if (anfds [fd].events)
328 { 363 {
329 close (fd); 364 close (fd);
330 fd_kill (EV_A_ fd); 365 fd_kill (EV_A_ fd);
331 return; 366 return;
332 } 367 }
333} 368}
334 369
370/* susually called after fork if method needs to re-arm all fds from scratch */
371static void
372fd_rearm_all (EV_P)
373{
374 int fd;
375
376 /* this should be highly optimised to not do anything but set a flag */
377 for (fd = 0; fd < anfdmax; ++fd)
378 if (anfds [fd].events)
379 {
380 anfds [fd].events = 0;
381 fd_change (EV_A_ fd);
382 }
383}
384
335/*****************************************************************************/ 385/*****************************************************************************/
336 386
337static void 387static void
338upheap (WT *timers, int k) 388upheap (WT *heap, int k)
339{ 389{
340 WT w = timers [k]; 390 WT w = heap [k];
341 391
342 while (k && timers [k >> 1]->at > w->at) 392 while (k && heap [k >> 1]->at > w->at)
343 { 393 {
344 timers [k] = timers [k >> 1]; 394 heap [k] = heap [k >> 1];
345 timers [k]->active = k + 1; 395 ((W)heap [k])->active = k + 1;
346 k >>= 1; 396 k >>= 1;
347 } 397 }
348 398
349 timers [k] = w; 399 heap [k] = w;
350 timers [k]->active = k + 1; 400 ((W)heap [k])->active = k + 1;
351 401
352} 402}
353 403
354static void 404static void
355downheap (WT *timers, int N, int k) 405downheap (WT *heap, int N, int k)
356{ 406{
357 WT w = timers [k]; 407 WT w = heap [k];
358 408
359 while (k < (N >> 1)) 409 while (k < (N >> 1))
360 { 410 {
361 int j = k << 1; 411 int j = k << 1;
362 412
363 if (j + 1 < N && timers [j]->at > timers [j + 1]->at) 413 if (j + 1 < N && heap [j]->at > heap [j + 1]->at)
364 ++j; 414 ++j;
365 415
366 if (w->at <= timers [j]->at) 416 if (w->at <= heap [j]->at)
367 break; 417 break;
368 418
369 timers [k] = timers [j]; 419 heap [k] = heap [j];
370 timers [k]->active = k + 1; 420 ((W)heap [k])->active = k + 1;
371 k = j; 421 k = j;
372 } 422 }
373 423
374 timers [k] = w; 424 heap [k] = w;
375 timers [k]->active = k + 1; 425 ((W)heap [k])->active = k + 1;
376} 426}
377 427
378/*****************************************************************************/ 428/*****************************************************************************/
379 429
380typedef struct 430typedef struct
386static ANSIG *signals; 436static ANSIG *signals;
387static int signalmax; 437static int signalmax;
388 438
389static int sigpipe [2]; 439static int sigpipe [2];
390static sig_atomic_t volatile gotsig; 440static sig_atomic_t volatile gotsig;
441static struct ev_io sigev;
391 442
392static void 443static void
393signals_init (ANSIG *base, int count) 444signals_init (ANSIG *base, int count)
394{ 445{
395 while (count--) 446 while (count--)
445 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); 496 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
446 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 497 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
447#endif 498#endif
448 499
449 ev_io_set (&sigev, sigpipe [0], EV_READ); 500 ev_io_set (&sigev, sigpipe [0], EV_READ);
450 ev_io_start (&sigev); 501 ev_io_start (EV_A_ &sigev);
451 ev_unref (EV_A); /* child watcher should not keep loop alive */ 502 ev_unref (EV_A); /* child watcher should not keep loop alive */
452} 503}
453 504
454/*****************************************************************************/ 505/*****************************************************************************/
455 506
456#ifndef WIN32 507#ifndef WIN32
508
509static struct ev_child *childs [PID_HASHSIZE];
510static struct ev_signal childev;
457 511
458#ifndef WCONTINUED 512#ifndef WCONTINUED
459# define WCONTINUED 0 513# define WCONTINUED 0
460#endif 514#endif
461 515
465 struct ev_child *w; 519 struct ev_child *w;
466 520
467 for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) 521 for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next)
468 if (w->pid == pid || !w->pid) 522 if (w->pid == pid || !w->pid)
469 { 523 {
470 w->priority = sw->priority; /* need to do it *now* */ 524 ev_priority (w) = ev_priority (sw); /* need to do it *now* */
471 w->rpid = pid; 525 w->rpid = pid;
472 w->rstatus = status; 526 w->rstatus = status;
473 event (EV_A_ (W)w, EV_CHILD); 527 event (EV_A_ (W)w, EV_CHILD);
474 } 528 }
475} 529}
476 530
477static void 531static void
497# include "ev_kqueue.c" 551# include "ev_kqueue.c"
498#endif 552#endif
499#if EV_USE_EPOLL 553#if EV_USE_EPOLL
500# include "ev_epoll.c" 554# include "ev_epoll.c"
501#endif 555#endif
502#if EV_USEV_POLL 556#if EV_USE_POLL
503# include "ev_poll.c" 557# include "ev_poll.c"
504#endif 558#endif
505#if EV_USE_SELECT 559#if EV_USE_SELECT
506# include "ev_select.c" 560# include "ev_select.c"
507#endif 561#endif
534ev_method (EV_P) 588ev_method (EV_P)
535{ 589{
536 return method; 590 return method;
537} 591}
538 592
539int 593static void
540ev_init (EV_P_ int methods) 594loop_init (EV_P_ int methods)
541{ 595{
542#ifdef EV_MULTIPLICITY
543 memset (loop, 0, sizeof (struct ev_loop));
544#endif
545
546 if (!method) 596 if (!method)
547 { 597 {
548#if EV_USE_MONOTONIC 598#if EV_USE_MONOTONIC
549 { 599 {
550 struct timespec ts; 600 struct timespec ts;
554#endif 604#endif
555 605
556 rt_now = ev_time (); 606 rt_now = ev_time ();
557 mn_now = get_clock (); 607 mn_now = get_clock ();
558 now_floor = mn_now; 608 now_floor = mn_now;
559 diff = rt_now - mn_now; 609 rtmn_diff = rt_now - mn_now;
560
561 if (pipe (sigpipe))
562 return 0;
563 610
564 if (methods == EVMETHOD_AUTO) 611 if (methods == EVMETHOD_AUTO)
565 if (!enable_secure () && getenv ("LIBmethodS")) 612 if (!enable_secure () && getenv ("LIBEV_METHODS"))
566 methods = atoi (getenv ("LIBmethodS")); 613 methods = atoi (getenv ("LIBEV_METHODS"));
567 else 614 else
568 methods = EVMETHOD_ANY; 615 methods = EVMETHOD_ANY;
569 616
570 method = 0; 617 method = 0;
618#if EV_USE_WIN32
619 if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods);
620#endif
571#if EV_USE_KQUEUE 621#if EV_USE_KQUEUE
572 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); 622 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods);
573#endif 623#endif
574#if EV_USE_EPOLL 624#if EV_USE_EPOLL
575 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); 625 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods);
576#endif 626#endif
577#if EV_USEV_POLL 627#if EV_USE_POLL
578 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); 628 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods);
579#endif 629#endif
580#if EV_USE_SELECT 630#if EV_USE_SELECT
581 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 631 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
582#endif 632#endif
633 }
634}
583 635
636void
637loop_destroy (EV_P)
638{
639#if EV_USE_WIN32
640 if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A);
641#endif
642#if EV_USE_KQUEUE
643 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
644#endif
645#if EV_USE_EPOLL
646 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A);
647#endif
648#if EV_USE_POLL
649 if (method == EVMETHOD_POLL ) poll_destroy (EV_A);
650#endif
651#if EV_USE_SELECT
652 if (method == EVMETHOD_SELECT) select_destroy (EV_A);
653#endif
654
655 method = 0;
656 /*TODO*/
657}
658
659void
660loop_fork (EV_P)
661{
662 /*TODO*/
663#if EV_USE_EPOLL
664 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A);
665#endif
666#if EV_USE_KQUEUE
667 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A);
668#endif
669}
670
671#if EV_MULTIPLICITY
672struct ev_loop *
673ev_loop_new (int methods)
674{
675 struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop));
676
677 loop_init (EV_A_ methods);
678
679 if (ev_method (EV_A))
680 return loop;
681
682 return 0;
683}
684
685void
686ev_loop_destroy (EV_P)
687{
688 loop_destroy (EV_A);
689 free (loop);
690}
691
692void
693ev_loop_fork (EV_P)
694{
695 loop_fork (EV_A);
696}
697
698#endif
699
700#if EV_MULTIPLICITY
701struct ev_loop default_loop_struct;
702static struct ev_loop *default_loop;
703
704struct ev_loop *
705#else
706static int default_loop;
707
708int
709#endif
710ev_default_loop (int methods)
711{
712 if (sigpipe [0] == sigpipe [1])
713 if (pipe (sigpipe))
714 return 0;
715
716 if (!default_loop)
717 {
718#if EV_MULTIPLICITY
719 struct ev_loop *loop = default_loop = &default_loop_struct;
720#else
721 default_loop = 1;
722#endif
723
724 loop_init (EV_A_ methods);
725
584 if (method) 726 if (ev_method (EV_A))
585 { 727 {
586 ev_watcher_init (&sigev, sigcb); 728 ev_watcher_init (&sigev, sigcb);
587 ev_set_priority (&sigev, EV_MAXPRI); 729 ev_set_priority (&sigev, EV_MAXPRI);
588 siginit (EV_A); 730 siginit (EV_A);
589 731
592 ev_set_priority (&childev, EV_MAXPRI); 734 ev_set_priority (&childev, EV_MAXPRI);
593 ev_signal_start (EV_A_ &childev); 735 ev_signal_start (EV_A_ &childev);
594 ev_unref (EV_A); /* child watcher should not keep loop alive */ 736 ev_unref (EV_A); /* child watcher should not keep loop alive */
595#endif 737#endif
596 } 738 }
739 else
740 default_loop = 0;
597 } 741 }
598 742
599 return method; 743 return default_loop;
600} 744}
601 745
602/*****************************************************************************/
603
604void 746void
605ev_fork_prepare (void) 747ev_default_destroy (void)
606{ 748{
607 /* nop */ 749#if EV_MULTIPLICITY
608} 750 struct ev_loop *loop = default_loop;
609
610void
611ev_fork_parent (void)
612{
613 /* nop */
614}
615
616void
617ev_fork_child (void)
618{
619#if EV_USE_EPOLL
620 if (method == EVMETHOD_EPOLL)
621 epoll_postfork_child ();
622#endif 751#endif
623 752
753 ev_ref (EV_A); /* child watcher */
754 ev_signal_stop (EV_A_ &childev);
755
756 ev_ref (EV_A); /* signal watcher */
624 ev_io_stop (&sigev); 757 ev_io_stop (EV_A_ &sigev);
758
759 close (sigpipe [0]); sigpipe [0] = 0;
760 close (sigpipe [1]); sigpipe [1] = 0;
761
762 loop_destroy (EV_A);
763}
764
765void
766ev_default_fork (void)
767{
768#if EV_MULTIPLICITY
769 struct ev_loop *loop = default_loop;
770#endif
771
772 loop_fork (EV_A);
773
774 ev_io_stop (EV_A_ &sigev);
625 close (sigpipe [0]); 775 close (sigpipe [0]);
626 close (sigpipe [1]); 776 close (sigpipe [1]);
627 pipe (sigpipe); 777 pipe (sigpipe);
778
779 ev_ref (EV_A); /* signal watcher */
628 siginit (); 780 siginit (EV_A);
629} 781}
630 782
631/*****************************************************************************/ 783/*****************************************************************************/
632 784
633static void 785static void
641 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 793 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
642 794
643 if (p->w) 795 if (p->w)
644 { 796 {
645 p->w->pending = 0; 797 p->w->pending = 0;
798
646 p->w->cb (EV_A_ p->w, p->events); 799 (*(void (**)(EV_P_ W, int))&p->w->cb) (EV_A_ p->w, p->events);
647 } 800 }
648 } 801 }
649} 802}
650 803
651static void 804static void
652timers_reify (EV_P) 805timers_reify (EV_P)
653{ 806{
654 while (timercnt && timers [0]->at <= mn_now) 807 while (timercnt && ((WT)timers [0])->at <= mn_now)
655 { 808 {
656 struct ev_timer *w = timers [0]; 809 struct ev_timer *w = timers [0];
810
811 assert (("inactive timer on timer heap detected", ev_is_active (w)));
657 812
658 /* first reschedule or stop timer */ 813 /* first reschedule or stop timer */
659 if (w->repeat) 814 if (w->repeat)
660 { 815 {
661 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); 816 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
662 w->at = mn_now + w->repeat; 817 ((WT)w)->at = mn_now + w->repeat;
663 downheap ((WT *)timers, timercnt, 0); 818 downheap ((WT *)timers, timercnt, 0);
664 } 819 }
665 else 820 else
666 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 821 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
667 822
668 event ((W)w, EV_TIMEOUT); 823 event (EV_A_ (W)w, EV_TIMEOUT);
669 } 824 }
670} 825}
671 826
672static void 827static void
673periodics_reify (EV_P) 828periodics_reify (EV_P)
674{ 829{
675 while (periodiccnt && periodics [0]->at <= rt_now) 830 while (periodiccnt && ((WT)periodics [0])->at <= rt_now)
676 { 831 {
677 struct ev_periodic *w = periodics [0]; 832 struct ev_periodic *w = periodics [0];
833
834 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
678 835
679 /* first reschedule or stop timer */ 836 /* first reschedule or stop timer */
680 if (w->interval) 837 if (w->interval)
681 { 838 {
682 w->at += floor ((rt_now - w->at) / w->interval + 1.) * w->interval; 839 ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval;
683 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > rt_now)); 840 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now));
684 downheap ((WT *)periodics, periodiccnt, 0); 841 downheap ((WT *)periodics, periodiccnt, 0);
685 } 842 }
686 else 843 else
687 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 844 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
688 845
689 event (EV_A_ (W)w, EV_PERIODIC); 846 event (EV_A_ (W)w, EV_PERIODIC);
690 } 847 }
691} 848}
692 849
693static void 850static void
694periodics_reschedule (EV_P_ ev_tstamp diff) 851periodics_reschedule (EV_P)
695{ 852{
696 int i; 853 int i;
697 854
698 /* adjust periodics after time jump */ 855 /* adjust periodics after time jump */
699 for (i = 0; i < periodiccnt; ++i) 856 for (i = 0; i < periodiccnt; ++i)
700 { 857 {
701 struct ev_periodic *w = periodics [i]; 858 struct ev_periodic *w = periodics [i];
702 859
703 if (w->interval) 860 if (w->interval)
704 { 861 {
705 ev_tstamp diff = ceil ((rt_now - w->at) / w->interval) * w->interval; 862 ev_tstamp diff = ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval;
706 863
707 if (fabs (diff) >= 1e-4) 864 if (fabs (diff) >= 1e-4)
708 { 865 {
709 ev_periodic_stop (EV_A_ w); 866 ev_periodic_stop (EV_A_ w);
710 ev_periodic_start (EV_A_ w); 867 ev_periodic_start (EV_A_ w);
720{ 877{
721 mn_now = get_clock (); 878 mn_now = get_clock ();
722 879
723 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) 880 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
724 { 881 {
725 rt_now = mn_now + diff; 882 rt_now = rtmn_diff + mn_now;
726 return 0; 883 return 0;
727 } 884 }
728 else 885 else
729 { 886 {
730 now_floor = mn_now; 887 now_floor = mn_now;
741#if EV_USE_MONOTONIC 898#if EV_USE_MONOTONIC
742 if (expect_true (have_monotonic)) 899 if (expect_true (have_monotonic))
743 { 900 {
744 if (time_update_monotonic (EV_A)) 901 if (time_update_monotonic (EV_A))
745 { 902 {
746 ev_tstamp odiff = diff; 903 ev_tstamp odiff = rtmn_diff;
747 904
748 for (i = 4; --i; ) /* loop a few times, before making important decisions */ 905 for (i = 4; --i; ) /* loop a few times, before making important decisions */
749 { 906 {
750 diff = rt_now - mn_now; 907 rtmn_diff = rt_now - mn_now;
751 908
752 if (fabs (odiff - diff) < MIN_TIMEJUMP) 909 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)
753 return; /* all is well */ 910 return; /* all is well */
754 911
755 rt_now = ev_time (); 912 rt_now = ev_time ();
756 mn_now = get_clock (); 913 mn_now = get_clock ();
757 now_floor = mn_now; 914 now_floor = mn_now;
758 } 915 }
759 916
760 periodics_reschedule (EV_A_ diff - odiff); 917 periodics_reschedule (EV_A);
761 /* no timer adjustment, as the monotonic clock doesn't jump */ 918 /* no timer adjustment, as the monotonic clock doesn't jump */
919 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */
762 } 920 }
763 } 921 }
764 else 922 else
765#endif 923#endif
766 { 924 {
767 rt_now = ev_time (); 925 rt_now = ev_time ();
768 926
769 if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) 927 if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
770 { 928 {
771 periodics_reschedule (EV_A_ rt_now - mn_now); 929 periodics_reschedule (EV_A);
772 930
773 /* adjust timers. this is easy, as the offset is the same for all */ 931 /* adjust timers. this is easy, as the offset is the same for all */
774 for (i = 0; i < timercnt; ++i) 932 for (i = 0; i < timercnt; ++i)
775 timers [i]->at += diff; 933 ((WT)timers [i])->at += rt_now - mn_now;
776 } 934 }
777 935
778 mn_now = rt_now; 936 mn_now = rt_now;
779 } 937 }
780} 938}
831 { 989 {
832 block = MAX_BLOCKTIME; 990 block = MAX_BLOCKTIME;
833 991
834 if (timercnt) 992 if (timercnt)
835 { 993 {
836 ev_tstamp to = timers [0]->at - mn_now + method_fudge; 994 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge;
837 if (block > to) block = to; 995 if (block > to) block = to;
838 } 996 }
839 997
840 if (periodiccnt) 998 if (periodiccnt)
841 { 999 {
842 ev_tstamp to = periodics [0]->at - rt_now + method_fudge; 1000 ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge;
843 if (block > to) block = to; 1001 if (block > to) block = to;
844 } 1002 }
845 1003
846 if (block < 0.) block = 0.; 1004 if (block < 0.) block = 0.;
847 } 1005 }
964ev_timer_start (EV_P_ struct ev_timer *w) 1122ev_timer_start (EV_P_ struct ev_timer *w)
965{ 1123{
966 if (ev_is_active (w)) 1124 if (ev_is_active (w))
967 return; 1125 return;
968 1126
969 w->at += mn_now; 1127 ((WT)w)->at += mn_now;
970 1128
971 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1129 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
972 1130
973 ev_start (EV_A_ (W)w, ++timercnt); 1131 ev_start (EV_A_ (W)w, ++timercnt);
974 array_needsize (timers, timermax, timercnt, ); 1132 array_needsize (timers, timermax, timercnt, );
975 timers [timercnt - 1] = w; 1133 timers [timercnt - 1] = w;
976 upheap ((WT *)timers, timercnt - 1); 1134 upheap ((WT *)timers, timercnt - 1);
1135
1136 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
977} 1137}
978 1138
979void 1139void
980ev_timer_stop (EV_P_ struct ev_timer *w) 1140ev_timer_stop (EV_P_ struct ev_timer *w)
981{ 1141{
982 ev_clear_pending (EV_A_ (W)w); 1142 ev_clear_pending (EV_A_ (W)w);
983 if (!ev_is_active (w)) 1143 if (!ev_is_active (w))
984 return; 1144 return;
985 1145
1146 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1147
986 if (w->active < timercnt--) 1148 if (((W)w)->active < timercnt--)
987 { 1149 {
988 timers [w->active - 1] = timers [timercnt]; 1150 timers [((W)w)->active - 1] = timers [timercnt];
989 downheap ((WT *)timers, timercnt, w->active - 1); 1151 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
990 } 1152 }
991 1153
992 w->at = w->repeat; 1154 ((WT)w)->at = w->repeat;
993 1155
994 ev_stop (EV_A_ (W)w); 1156 ev_stop (EV_A_ (W)w);
995} 1157}
996 1158
997void 1159void
999{ 1161{
1000 if (ev_is_active (w)) 1162 if (ev_is_active (w))
1001 { 1163 {
1002 if (w->repeat) 1164 if (w->repeat)
1003 { 1165 {
1004 w->at = mn_now + w->repeat; 1166 ((WT)w)->at = mn_now + w->repeat;
1005 downheap ((WT *)timers, timercnt, w->active - 1); 1167 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1006 } 1168 }
1007 else 1169 else
1008 ev_timer_stop (EV_A_ w); 1170 ev_timer_stop (EV_A_ w);
1009 } 1171 }
1010 else if (w->repeat) 1172 else if (w->repeat)
1019 1181
1020 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); 1182 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
1021 1183
1022 /* this formula differs from the one in periodic_reify because we do not always round up */ 1184 /* this formula differs from the one in periodic_reify because we do not always round up */
1023 if (w->interval) 1185 if (w->interval)
1024 w->at += ceil ((rt_now - w->at) / w->interval) * w->interval; 1186 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval;
1025 1187
1026 ev_start (EV_A_ (W)w, ++periodiccnt); 1188 ev_start (EV_A_ (W)w, ++periodiccnt);
1027 array_needsize (periodics, periodicmax, periodiccnt, ); 1189 array_needsize (periodics, periodicmax, periodiccnt, );
1028 periodics [periodiccnt - 1] = w; 1190 periodics [periodiccnt - 1] = w;
1029 upheap ((WT *)periodics, periodiccnt - 1); 1191 upheap ((WT *)periodics, periodiccnt - 1);
1192
1193 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1030} 1194}
1031 1195
1032void 1196void
1033ev_periodic_stop (EV_P_ struct ev_periodic *w) 1197ev_periodic_stop (EV_P_ struct ev_periodic *w)
1034{ 1198{
1035 ev_clear_pending (EV_A_ (W)w); 1199 ev_clear_pending (EV_A_ (W)w);
1036 if (!ev_is_active (w)) 1200 if (!ev_is_active (w))
1037 return; 1201 return;
1038 1202
1203 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1204
1039 if (w->active < periodiccnt--) 1205 if (((W)w)->active < periodiccnt--)
1040 { 1206 {
1041 periodics [w->active - 1] = periodics [periodiccnt]; 1207 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1042 downheap ((WT *)periodics, periodiccnt, w->active - 1); 1208 downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1043 } 1209 }
1044 1210
1211 ev_stop (EV_A_ (W)w);
1212}
1213
1214void
1215ev_idle_start (EV_P_ struct ev_idle *w)
1216{
1217 if (ev_is_active (w))
1218 return;
1219
1220 ev_start (EV_A_ (W)w, ++idlecnt);
1221 array_needsize (idles, idlemax, idlecnt, );
1222 idles [idlecnt - 1] = w;
1223}
1224
1225void
1226ev_idle_stop (EV_P_ struct ev_idle *w)
1227{
1228 ev_clear_pending (EV_A_ (W)w);
1229 if (ev_is_active (w))
1230 return;
1231
1232 idles [((W)w)->active - 1] = idles [--idlecnt];
1233 ev_stop (EV_A_ (W)w);
1234}
1235
1236void
1237ev_prepare_start (EV_P_ struct ev_prepare *w)
1238{
1239 if (ev_is_active (w))
1240 return;
1241
1242 ev_start (EV_A_ (W)w, ++preparecnt);
1243 array_needsize (prepares, preparemax, preparecnt, );
1244 prepares [preparecnt - 1] = w;
1245}
1246
1247void
1248ev_prepare_stop (EV_P_ struct ev_prepare *w)
1249{
1250 ev_clear_pending (EV_A_ (W)w);
1251 if (ev_is_active (w))
1252 return;
1253
1254 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1255 ev_stop (EV_A_ (W)w);
1256}
1257
1258void
1259ev_check_start (EV_P_ struct ev_check *w)
1260{
1261 if (ev_is_active (w))
1262 return;
1263
1264 ev_start (EV_A_ (W)w, ++checkcnt);
1265 array_needsize (checks, checkmax, checkcnt, );
1266 checks [checkcnt - 1] = w;
1267}
1268
1269void
1270ev_check_stop (EV_P_ struct ev_check *w)
1271{
1272 ev_clear_pending (EV_A_ (W)w);
1273 if (ev_is_active (w))
1274 return;
1275
1276 checks [((W)w)->active - 1] = checks [--checkcnt];
1045 ev_stop (EV_A_ (W)w); 1277 ev_stop (EV_A_ (W)w);
1046} 1278}
1047 1279
1048#ifndef SA_RESTART 1280#ifndef SA_RESTART
1049# define SA_RESTART 0 1281# define SA_RESTART 0
1050#endif 1282#endif
1051 1283
1052void 1284void
1053ev_signal_start (EV_P_ struct ev_signal *w) 1285ev_signal_start (EV_P_ struct ev_signal *w)
1054{ 1286{
1287#if EV_MULTIPLICITY
1288 assert (("signal watchers are only supported in the default loop", loop == default_loop));
1289#endif
1055 if (ev_is_active (w)) 1290 if (ev_is_active (w))
1056 return; 1291 return;
1057 1292
1058 assert (("ev_signal_start called with illegal signal number", w->signum > 0)); 1293 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
1059 1294
1060 ev_start (EV_A_ (W)w, 1); 1295 ev_start (EV_A_ (W)w, 1);
1061 array_needsize (signals, signalmax, w->signum, signals_init); 1296 array_needsize (signals, signalmax, w->signum, signals_init);
1062 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1297 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1063 1298
1064 if (!w->next) 1299 if (!((WL)w)->next)
1065 { 1300 {
1066 struct sigaction sa; 1301 struct sigaction sa;
1067 sa.sa_handler = sighandler; 1302 sa.sa_handler = sighandler;
1068 sigfillset (&sa.sa_mask); 1303 sigfillset (&sa.sa_mask);
1069 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ 1304 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
1084 if (!signals [w->signum - 1].head) 1319 if (!signals [w->signum - 1].head)
1085 signal (w->signum, SIG_DFL); 1320 signal (w->signum, SIG_DFL);
1086} 1321}
1087 1322
1088void 1323void
1089ev_idle_start (EV_P_ struct ev_idle *w)
1090{
1091 if (ev_is_active (w))
1092 return;
1093
1094 ev_start (EV_A_ (W)w, ++idlecnt);
1095 array_needsize (idles, idlemax, idlecnt, );
1096 idles [idlecnt - 1] = w;
1097}
1098
1099void
1100ev_idle_stop (EV_P_ struct ev_idle *w)
1101{
1102 ev_clear_pending (EV_A_ (W)w);
1103 if (ev_is_active (w))
1104 return;
1105
1106 idles [w->active - 1] = idles [--idlecnt];
1107 ev_stop (EV_A_ (W)w);
1108}
1109
1110void
1111ev_prepare_start (EV_P_ struct ev_prepare *w)
1112{
1113 if (ev_is_active (w))
1114 return;
1115
1116 ev_start (EV_A_ (W)w, ++preparecnt);
1117 array_needsize (prepares, preparemax, preparecnt, );
1118 prepares [preparecnt - 1] = w;
1119}
1120
1121void
1122ev_prepare_stop (EV_P_ struct ev_prepare *w)
1123{
1124 ev_clear_pending (EV_A_ (W)w);
1125 if (ev_is_active (w))
1126 return;
1127
1128 prepares [w->active - 1] = prepares [--preparecnt];
1129 ev_stop (EV_A_ (W)w);
1130}
1131
1132void
1133ev_check_start (EV_P_ struct ev_check *w)
1134{
1135 if (ev_is_active (w))
1136 return;
1137
1138 ev_start (EV_A_ (W)w, ++checkcnt);
1139 array_needsize (checks, checkmax, checkcnt, );
1140 checks [checkcnt - 1] = w;
1141}
1142
1143void
1144ev_check_stop (EV_P_ struct ev_check *w)
1145{
1146 ev_clear_pending (EV_A_ (W)w);
1147 if (ev_is_active (w))
1148 return;
1149
1150 checks [w->active - 1] = checks [--checkcnt];
1151 ev_stop (EV_A_ (W)w);
1152}
1153
1154void
1155ev_child_start (EV_P_ struct ev_child *w) 1324ev_child_start (EV_P_ struct ev_child *w)
1156{ 1325{
1326#if EV_MULTIPLICITY
1327 assert (("child watchers are only supported in the default loop", loop == default_loop));
1328#endif
1157 if (ev_is_active (w)) 1329 if (ev_is_active (w))
1158 return; 1330 return;
1159 1331
1160 ev_start (EV_A_ (W)w, 1); 1332 ev_start (EV_A_ (W)w, 1);
1161 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1333 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1233 ev_timer_start (EV_A_ &once->to); 1405 ev_timer_start (EV_A_ &once->to);
1234 } 1406 }
1235 } 1407 }
1236} 1408}
1237 1409
1238/*****************************************************************************/
1239
1240#if 0
1241
1242struct ev_io wio;
1243
1244static void
1245sin_cb (struct ev_io *w, int revents)
1246{
1247 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
1248}
1249
1250static void
1251ocb (struct ev_timer *w, int revents)
1252{
1253 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data);
1254 ev_timer_stop (w);
1255 ev_timer_start (w);
1256}
1257
1258static void
1259scb (struct ev_signal *w, int revents)
1260{
1261 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
1262 ev_io_stop (&wio);
1263 ev_io_start (&wio);
1264}
1265
1266static void
1267gcb (struct ev_signal *w, int revents)
1268{
1269 fprintf (stderr, "generic %x\n", revents);
1270
1271}
1272
1273int main (void)
1274{
1275 ev_init (0);
1276
1277 ev_io_init (&wio, sin_cb, 0, EV_READ);
1278 ev_io_start (&wio);
1279
1280 struct ev_timer t[10000];
1281
1282#if 0
1283 int i;
1284 for (i = 0; i < 10000; ++i)
1285 {
1286 struct ev_timer *w = t + i;
1287 ev_watcher_init (w, ocb, i);
1288 ev_timer_init_abs (w, ocb, drand48 (), 0.99775533);
1289 ev_timer_start (w);
1290 if (drand48 () < 0.5)
1291 ev_timer_stop (w);
1292 }
1293#endif
1294
1295 struct ev_timer t1;
1296 ev_timer_init (&t1, ocb, 5, 10);
1297 ev_timer_start (&t1);
1298
1299 struct ev_signal sig;
1300 ev_signal_init (&sig, scb, SIGQUIT);
1301 ev_signal_start (&sig);
1302
1303 struct ev_check cw;
1304 ev_check_init (&cw, gcb);
1305 ev_check_start (&cw);
1306
1307 struct ev_idle iw;
1308 ev_idle_init (&iw, gcb);
1309 ev_idle_start (&iw);
1310
1311 ev_loop (0);
1312
1313 return 0;
1314}
1315
1316#endif
1317
1318
1319
1320

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines