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

Comparing libev/ev.c (file contents):
Revision 1.55 by root, Sun Nov 4 00:39:24 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
276 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)
277 events |= w->events; 307 events |= w->events;
278 308
279 anfd->reify = 0; 309 anfd->reify = 0;
280 310
281 if (anfd->events != events)
282 {
283 method_modify (EV_A_ fd, anfd->events, events); 311 method_modify (EV_A_ fd, anfd->events, events);
284 anfd->events = events; 312 anfd->events = events;
285 }
286 } 313 }
287 314
288 fdchangecnt = 0; 315 fdchangecnt = 0;
289} 316}
290 317
327 354
328/* 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 */
329static void 356static void
330fd_enomem (EV_P) 357fd_enomem (EV_P)
331{ 358{
332 int fd = anfdmax; 359 int fd;
333 360
334 while (fd--) 361 for (fd = anfdmax; fd--; )
335 if (anfds [fd].events) 362 if (anfds [fd].events)
336 { 363 {
337 close (fd); 364 close (fd);
338 fd_kill (EV_A_ fd); 365 fd_kill (EV_A_ fd);
339 return; 366 return;
340 } 367 }
341} 368}
342 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
343/*****************************************************************************/ 385/*****************************************************************************/
344 386
345static void 387static void
346upheap (WT *heap, int k) 388upheap (WT *heap, int k)
347{ 389{
348 WT w = heap [k]; 390 WT w = heap [k];
349 391
350 while (k && heap [k >> 1]->at > w->at) 392 while (k && heap [k >> 1]->at > w->at)
351 { 393 {
352 heap [k] = heap [k >> 1]; 394 heap [k] = heap [k >> 1];
353 heap [k]->active = k + 1; 395 ((W)heap [k])->active = k + 1;
354 k >>= 1; 396 k >>= 1;
355 } 397 }
356 398
357 heap [k] = w; 399 heap [k] = w;
358 heap [k]->active = k + 1; 400 ((W)heap [k])->active = k + 1;
359 401
360} 402}
361 403
362static void 404static void
363downheap (WT *heap, int N, int k) 405downheap (WT *heap, int N, int k)
373 415
374 if (w->at <= heap [j]->at) 416 if (w->at <= heap [j]->at)
375 break; 417 break;
376 418
377 heap [k] = heap [j]; 419 heap [k] = heap [j];
378 heap [k]->active = k + 1; 420 ((W)heap [k])->active = k + 1;
379 k = j; 421 k = j;
380 } 422 }
381 423
382 heap [k] = w; 424 heap [k] = w;
383 heap [k]->active = k + 1; 425 ((W)heap [k])->active = k + 1;
384} 426}
385 427
386/*****************************************************************************/ 428/*****************************************************************************/
387 429
388typedef struct 430typedef struct
394static ANSIG *signals; 436static ANSIG *signals;
395static int signalmax; 437static int signalmax;
396 438
397static int sigpipe [2]; 439static int sigpipe [2];
398static sig_atomic_t volatile gotsig; 440static sig_atomic_t volatile gotsig;
441static struct ev_io sigev;
399 442
400static void 443static void
401signals_init (ANSIG *base, int count) 444signals_init (ANSIG *base, int count)
402{ 445{
403 while (count--) 446 while (count--)
461 504
462/*****************************************************************************/ 505/*****************************************************************************/
463 506
464#ifndef WIN32 507#ifndef WIN32
465 508
509static struct ev_child *childs [PID_HASHSIZE];
510static struct ev_signal childev;
511
466#ifndef WCONTINUED 512#ifndef WCONTINUED
467# define WCONTINUED 0 513# define WCONTINUED 0
468#endif 514#endif
469 515
470static void 516static void
473 struct ev_child *w; 519 struct ev_child *w;
474 520
475 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)
476 if (w->pid == pid || !w->pid) 522 if (w->pid == pid || !w->pid)
477 { 523 {
478 w->priority = sw->priority; /* need to do it *now* */ 524 ev_priority (w) = ev_priority (sw); /* need to do it *now* */
479 w->rpid = pid; 525 w->rpid = pid;
480 w->rstatus = status; 526 w->rstatus = status;
481 event (EV_A_ (W)w, EV_CHILD); 527 event (EV_A_ (W)w, EV_CHILD);
482 } 528 }
483} 529}
484 530
485static void 531static void
505# include "ev_kqueue.c" 551# include "ev_kqueue.c"
506#endif 552#endif
507#if EV_USE_EPOLL 553#if EV_USE_EPOLL
508# include "ev_epoll.c" 554# include "ev_epoll.c"
509#endif 555#endif
510#if EV_USEV_POLL 556#if EV_USE_POLL
511# include "ev_poll.c" 557# include "ev_poll.c"
512#endif 558#endif
513#if EV_USE_SELECT 559#if EV_USE_SELECT
514# include "ev_select.c" 560# include "ev_select.c"
515#endif 561#endif
542ev_method (EV_P) 588ev_method (EV_P)
543{ 589{
544 return method; 590 return method;
545} 591}
546 592
547inline int 593static void
548loop_init (EV_P_ int methods) 594loop_init (EV_P_ int methods)
549{ 595{
550 if (!method) 596 if (!method)
551 { 597 {
552#if EV_USE_MONOTONIC 598#if EV_USE_MONOTONIC
560 rt_now = ev_time (); 606 rt_now = ev_time ();
561 mn_now = get_clock (); 607 mn_now = get_clock ();
562 now_floor = mn_now; 608 now_floor = mn_now;
563 rtmn_diff = rt_now - mn_now; 609 rtmn_diff = rt_now - mn_now;
564 610
565 if (pipe (sigpipe))
566 return 0;
567
568 if (methods == EVMETHOD_AUTO) 611 if (methods == EVMETHOD_AUTO)
569 if (!enable_secure () && getenv ("LIBmethodS")) 612 if (!enable_secure () && getenv ("LIBEV_METHODS"))
570 methods = atoi (getenv ("LIBmethodS")); 613 methods = atoi (getenv ("LIBEV_METHODS"));
571 else 614 else
572 methods = EVMETHOD_ANY; 615 methods = EVMETHOD_ANY;
573 616
574 method = 0; 617 method = 0;
618#if EV_USE_WIN32
619 if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods);
620#endif
575#if EV_USE_KQUEUE 621#if EV_USE_KQUEUE
576 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); 622 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods);
577#endif 623#endif
578#if EV_USE_EPOLL 624#if EV_USE_EPOLL
579 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); 625 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods);
580#endif 626#endif
581#if EV_USEV_POLL 627#if EV_USE_POLL
582 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); 628 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods);
583#endif 629#endif
584#if EV_USE_SELECT 630#if EV_USE_SELECT
585 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 631 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
586#endif 632#endif
633 }
634}
587 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
588 if (method) 726 if (ev_method (EV_A))
589 { 727 {
590 ev_watcher_init (&sigev, sigcb); 728 ev_watcher_init (&sigev, sigcb);
591 ev_set_priority (&sigev, EV_MAXPRI); 729 ev_set_priority (&sigev, EV_MAXPRI);
592 siginit (EV_A); 730 siginit (EV_A);
593 731
596 ev_set_priority (&childev, EV_MAXPRI); 734 ev_set_priority (&childev, EV_MAXPRI);
597 ev_signal_start (EV_A_ &childev); 735 ev_signal_start (EV_A_ &childev);
598 ev_unref (EV_A); /* child watcher should not keep loop alive */ 736 ev_unref (EV_A); /* child watcher should not keep loop alive */
599#endif 737#endif
600 } 738 }
739 else
740 default_loop = 0;
601 } 741 }
602 742
603 return method; 743 return default_loop;
604} 744}
605 745
746void
747ev_default_destroy (void)
748{
606#if EV_MULTIPLICITY 749#if EV_MULTIPLICITY
607 750 struct ev_loop *loop = default_loop;
608struct ev_loop *
609ev_loop_new (int methods)
610{
611 struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop));
612
613 if (loop_init (EV_A_ methods))
614 return loop;
615
616 ev_loop_delete (loop);
617
618 return 0;
619}
620
621void
622ev_loop_delete (EV_P)
623{
624 /*TODO*/
625 free (loop);
626}
627
628#else
629
630int
631ev_init (int methods)
632{
633 return loop_init (methods);
634}
635
636#endif 751#endif
637 752
638/*****************************************************************************/ 753 ev_ref (EV_A); /* child watcher */
754 ev_signal_stop (EV_A_ &childev);
639 755
640void 756 ev_ref (EV_A); /* signal watcher */
641ev_fork_prepare (void) 757 ev_io_stop (EV_A_ &sigev);
642{
643 /* nop */
644}
645 758
646void 759 close (sigpipe [0]); sigpipe [0] = 0;
647ev_fork_parent (void) 760 close (sigpipe [1]); sigpipe [1] = 0;
648{
649 /* nop */
650}
651 761
762 loop_destroy (EV_A);
763}
764
652void 765void
653ev_fork_child (void) 766ev_default_fork (void)
654{ 767{
655 /*TODO*/
656#if !EV_MULTIPLICITY 768#if EV_MULTIPLICITY
657#if EV_USE_EPOLL 769 struct ev_loop *loop = default_loop;
658 if (method == EVMETHOD_EPOLL)
659 epoll_postfork_child (EV_A);
660#endif 770#endif
771
772 loop_fork (EV_A);
661 773
662 ev_io_stop (EV_A_ &sigev); 774 ev_io_stop (EV_A_ &sigev);
663 close (sigpipe [0]); 775 close (sigpipe [0]);
664 close (sigpipe [1]); 776 close (sigpipe [1]);
665 pipe (sigpipe); 777 pipe (sigpipe);
778
779 ev_ref (EV_A); /* signal watcher */
666 siginit (EV_A); 780 siginit (EV_A);
667#endif
668} 781}
669 782
670/*****************************************************************************/ 783/*****************************************************************************/
671 784
672static void 785static void
680 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 793 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
681 794
682 if (p->w) 795 if (p->w)
683 { 796 {
684 p->w->pending = 0; 797 p->w->pending = 0;
798
685 p->w->cb (EV_A_ p->w, p->events); 799 (*(void (**)(EV_P_ W, int))&p->w->cb) (EV_A_ p->w, p->events);
686 } 800 }
687 } 801 }
688} 802}
689 803
690static void 804static void
691timers_reify (EV_P) 805timers_reify (EV_P)
692{ 806{
693 while (timercnt && timers [0]->at <= mn_now) 807 while (timercnt && ((WT)timers [0])->at <= mn_now)
694 { 808 {
695 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)));
696 812
697 /* first reschedule or stop timer */ 813 /* first reschedule or stop timer */
698 if (w->repeat) 814 if (w->repeat)
699 { 815 {
700 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.));
701 w->at = mn_now + w->repeat; 817 ((WT)w)->at = mn_now + w->repeat;
702 downheap ((WT *)timers, timercnt, 0); 818 downheap ((WT *)timers, timercnt, 0);
703 } 819 }
704 else 820 else
705 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 821 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
706 822
709} 825}
710 826
711static void 827static void
712periodics_reify (EV_P) 828periodics_reify (EV_P)
713{ 829{
714 while (periodiccnt && periodics [0]->at <= rt_now) 830 while (periodiccnt && ((WT)periodics [0])->at <= rt_now)
715 { 831 {
716 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)));
717 835
718 /* first reschedule or stop timer */ 836 /* first reschedule or stop timer */
719 if (w->interval) 837 if (w->interval)
720 { 838 {
721 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;
722 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));
723 downheap ((WT *)periodics, periodiccnt, 0); 841 downheap ((WT *)periodics, periodiccnt, 0);
724 } 842 }
725 else 843 else
726 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 844 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
727 845
739 { 857 {
740 struct ev_periodic *w = periodics [i]; 858 struct ev_periodic *w = periodics [i];
741 859
742 if (w->interval) 860 if (w->interval)
743 { 861 {
744 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;
745 863
746 if (fabs (diff) >= 1e-4) 864 if (fabs (diff) >= 1e-4)
747 { 865 {
748 ev_periodic_stop (EV_A_ w); 866 ev_periodic_stop (EV_A_ w);
749 ev_periodic_start (EV_A_ w); 867 ev_periodic_start (EV_A_ w);
810 { 928 {
811 periodics_reschedule (EV_A); 929 periodics_reschedule (EV_A);
812 930
813 /* 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 */
814 for (i = 0; i < timercnt; ++i) 932 for (i = 0; i < timercnt; ++i)
815 timers [i]->at += rt_now - mn_now; 933 ((WT)timers [i])->at += rt_now - mn_now;
816 } 934 }
817 935
818 mn_now = rt_now; 936 mn_now = rt_now;
819 } 937 }
820} 938}
871 { 989 {
872 block = MAX_BLOCKTIME; 990 block = MAX_BLOCKTIME;
873 991
874 if (timercnt) 992 if (timercnt)
875 { 993 {
876 ev_tstamp to = timers [0]->at - mn_now + method_fudge; 994 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge;
877 if (block > to) block = to; 995 if (block > to) block = to;
878 } 996 }
879 997
880 if (periodiccnt) 998 if (periodiccnt)
881 { 999 {
882 ev_tstamp to = periodics [0]->at - rt_now + method_fudge; 1000 ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge;
883 if (block > to) block = to; 1001 if (block > to) block = to;
884 } 1002 }
885 1003
886 if (block < 0.) block = 0.; 1004 if (block < 0.) block = 0.;
887 } 1005 }
1004ev_timer_start (EV_P_ struct ev_timer *w) 1122ev_timer_start (EV_P_ struct ev_timer *w)
1005{ 1123{
1006 if (ev_is_active (w)) 1124 if (ev_is_active (w))
1007 return; 1125 return;
1008 1126
1009 w->at += mn_now; 1127 ((WT)w)->at += mn_now;
1010 1128
1011 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.));
1012 1130
1013 ev_start (EV_A_ (W)w, ++timercnt); 1131 ev_start (EV_A_ (W)w, ++timercnt);
1014 array_needsize (timers, timermax, timercnt, ); 1132 array_needsize (timers, timermax, timercnt, );
1015 timers [timercnt - 1] = w; 1133 timers [timercnt - 1] = w;
1016 upheap ((WT *)timers, timercnt - 1); 1134 upheap ((WT *)timers, timercnt - 1);
1135
1136 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1017} 1137}
1018 1138
1019void 1139void
1020ev_timer_stop (EV_P_ struct ev_timer *w) 1140ev_timer_stop (EV_P_ struct ev_timer *w)
1021{ 1141{
1022 ev_clear_pending (EV_A_ (W)w); 1142 ev_clear_pending (EV_A_ (W)w);
1023 if (!ev_is_active (w)) 1143 if (!ev_is_active (w))
1024 return; 1144 return;
1025 1145
1146 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1147
1026 if (w->active < timercnt--) 1148 if (((W)w)->active < timercnt--)
1027 { 1149 {
1028 timers [w->active - 1] = timers [timercnt]; 1150 timers [((W)w)->active - 1] = timers [timercnt];
1029 downheap ((WT *)timers, timercnt, w->active - 1); 1151 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1030 } 1152 }
1031 1153
1032 w->at = w->repeat; 1154 ((WT)w)->at = w->repeat;
1033 1155
1034 ev_stop (EV_A_ (W)w); 1156 ev_stop (EV_A_ (W)w);
1035} 1157}
1036 1158
1037void 1159void
1039{ 1161{
1040 if (ev_is_active (w)) 1162 if (ev_is_active (w))
1041 { 1163 {
1042 if (w->repeat) 1164 if (w->repeat)
1043 { 1165 {
1044 w->at = mn_now + w->repeat; 1166 ((WT)w)->at = mn_now + w->repeat;
1045 downheap ((WT *)timers, timercnt, w->active - 1); 1167 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1046 } 1168 }
1047 else 1169 else
1048 ev_timer_stop (EV_A_ w); 1170 ev_timer_stop (EV_A_ w);
1049 } 1171 }
1050 else if (w->repeat) 1172 else if (w->repeat)
1059 1181
1060 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.));
1061 1183
1062 /* 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 */
1063 if (w->interval) 1185 if (w->interval)
1064 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;
1065 1187
1066 ev_start (EV_A_ (W)w, ++periodiccnt); 1188 ev_start (EV_A_ (W)w, ++periodiccnt);
1067 array_needsize (periodics, periodicmax, periodiccnt, ); 1189 array_needsize (periodics, periodicmax, periodiccnt, );
1068 periodics [periodiccnt - 1] = w; 1190 periodics [periodiccnt - 1] = w;
1069 upheap ((WT *)periodics, periodiccnt - 1); 1191 upheap ((WT *)periodics, periodiccnt - 1);
1192
1193 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1070} 1194}
1071 1195
1072void 1196void
1073ev_periodic_stop (EV_P_ struct ev_periodic *w) 1197ev_periodic_stop (EV_P_ struct ev_periodic *w)
1074{ 1198{
1075 ev_clear_pending (EV_A_ (W)w); 1199 ev_clear_pending (EV_A_ (W)w);
1076 if (!ev_is_active (w)) 1200 if (!ev_is_active (w))
1077 return; 1201 return;
1078 1202
1203 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1204
1079 if (w->active < periodiccnt--) 1205 if (((W)w)->active < periodiccnt--)
1080 { 1206 {
1081 periodics [w->active - 1] = periodics [periodiccnt]; 1207 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1082 downheap ((WT *)periodics, periodiccnt, w->active - 1); 1208 downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1083 } 1209 }
1084 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];
1085 ev_stop (EV_A_ (W)w); 1277 ev_stop (EV_A_ (W)w);
1086} 1278}
1087 1279
1088#ifndef SA_RESTART 1280#ifndef SA_RESTART
1089# define SA_RESTART 0 1281# define SA_RESTART 0
1090#endif 1282#endif
1091 1283
1092void 1284void
1093ev_signal_start (EV_P_ struct ev_signal *w) 1285ev_signal_start (EV_P_ struct ev_signal *w)
1094{ 1286{
1287#if EV_MULTIPLICITY
1288 assert (("signal watchers are only supported in the default loop", loop == default_loop));
1289#endif
1095 if (ev_is_active (w)) 1290 if (ev_is_active (w))
1096 return; 1291 return;
1097 1292
1098 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));
1099 1294
1100 ev_start (EV_A_ (W)w, 1); 1295 ev_start (EV_A_ (W)w, 1);
1101 array_needsize (signals, signalmax, w->signum, signals_init); 1296 array_needsize (signals, signalmax, w->signum, signals_init);
1102 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1297 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1103 1298
1104 if (!w->next) 1299 if (!((WL)w)->next)
1105 { 1300 {
1106 struct sigaction sa; 1301 struct sigaction sa;
1107 sa.sa_handler = sighandler; 1302 sa.sa_handler = sighandler;
1108 sigfillset (&sa.sa_mask); 1303 sigfillset (&sa.sa_mask);
1109 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 */
1124 if (!signals [w->signum - 1].head) 1319 if (!signals [w->signum - 1].head)
1125 signal (w->signum, SIG_DFL); 1320 signal (w->signum, SIG_DFL);
1126} 1321}
1127 1322
1128void 1323void
1129ev_idle_start (EV_P_ struct ev_idle *w)
1130{
1131 if (ev_is_active (w))
1132 return;
1133
1134 ev_start (EV_A_ (W)w, ++idlecnt);
1135 array_needsize (idles, idlemax, idlecnt, );
1136 idles [idlecnt - 1] = w;
1137}
1138
1139void
1140ev_idle_stop (EV_P_ struct ev_idle *w)
1141{
1142 ev_clear_pending (EV_A_ (W)w);
1143 if (ev_is_active (w))
1144 return;
1145
1146 idles [w->active - 1] = idles [--idlecnt];
1147 ev_stop (EV_A_ (W)w);
1148}
1149
1150void
1151ev_prepare_start (EV_P_ struct ev_prepare *w)
1152{
1153 if (ev_is_active (w))
1154 return;
1155
1156 ev_start (EV_A_ (W)w, ++preparecnt);
1157 array_needsize (prepares, preparemax, preparecnt, );
1158 prepares [preparecnt - 1] = w;
1159}
1160
1161void
1162ev_prepare_stop (EV_P_ struct ev_prepare *w)
1163{
1164 ev_clear_pending (EV_A_ (W)w);
1165 if (ev_is_active (w))
1166 return;
1167
1168 prepares [w->active - 1] = prepares [--preparecnt];
1169 ev_stop (EV_A_ (W)w);
1170}
1171
1172void
1173ev_check_start (EV_P_ struct ev_check *w)
1174{
1175 if (ev_is_active (w))
1176 return;
1177
1178 ev_start (EV_A_ (W)w, ++checkcnt);
1179 array_needsize (checks, checkmax, checkcnt, );
1180 checks [checkcnt - 1] = w;
1181}
1182
1183void
1184ev_check_stop (EV_P_ struct ev_check *w)
1185{
1186 ev_clear_pending (EV_A_ (W)w);
1187 if (ev_is_active (w))
1188 return;
1189
1190 checks [w->active - 1] = checks [--checkcnt];
1191 ev_stop (EV_A_ (W)w);
1192}
1193
1194void
1195ev_child_start (EV_P_ struct ev_child *w) 1324ev_child_start (EV_P_ struct ev_child *w)
1196{ 1325{
1326#if EV_MULTIPLICITY
1327 assert (("child watchers are only supported in the default loop", loop == default_loop));
1328#endif
1197 if (ev_is_active (w)) 1329 if (ev_is_active (w))
1198 return; 1330 return;
1199 1331
1200 ev_start (EV_A_ (W)w, 1); 1332 ev_start (EV_A_ (W)w, 1);
1201 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1333 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1273 ev_timer_start (EV_A_ &once->to); 1405 ev_timer_start (EV_A_ &once->to);
1274 } 1406 }
1275 } 1407 }
1276} 1408}
1277 1409
1278/*****************************************************************************/
1279
1280#if 0
1281
1282struct ev_io wio;
1283
1284static void
1285sin_cb (struct ev_io *w, int revents)
1286{
1287 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
1288}
1289
1290static void
1291ocb (struct ev_timer *w, int revents)
1292{
1293 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data);
1294 ev_timer_stop (w);
1295 ev_timer_start (w);
1296}
1297
1298static void
1299scb (struct ev_signal *w, int revents)
1300{
1301 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
1302 ev_io_stop (&wio);
1303 ev_io_start (&wio);
1304}
1305
1306static void
1307gcb (struct ev_signal *w, int revents)
1308{
1309 fprintf (stderr, "generic %x\n", revents);
1310
1311}
1312
1313int main (void)
1314{
1315 ev_init (0);
1316
1317 ev_io_init (&wio, sin_cb, 0, EV_READ);
1318 ev_io_start (&wio);
1319
1320 struct ev_timer t[10000];
1321
1322#if 0
1323 int i;
1324 for (i = 0; i < 10000; ++i)
1325 {
1326 struct ev_timer *w = t + i;
1327 ev_watcher_init (w, ocb, i);
1328 ev_timer_init_abs (w, ocb, drand48 (), 0.99775533);
1329 ev_timer_start (w);
1330 if (drand48 () < 0.5)
1331 ev_timer_stop (w);
1332 }
1333#endif
1334
1335 struct ev_timer t1;
1336 ev_timer_init (&t1, ocb, 5, 10);
1337 ev_timer_start (&t1);
1338
1339 struct ev_signal sig;
1340 ev_signal_init (&sig, scb, SIGQUIT);
1341 ev_signal_start (&sig);
1342
1343 struct ev_check cw;
1344 ev_check_init (&cw, gcb);
1345 ev_check_start (&cw);
1346
1347 struct ev_idle iw;
1348 ev_idle_init (&iw, gcb);
1349 ev_idle_start (&iw);
1350
1351 ev_loop (0);
1352
1353 return 0;
1354}
1355
1356#endif
1357
1358
1359
1360

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines