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

Comparing libev/ev.c (file contents):
Revision 1.54 by root, Sun Nov 4 00:24:16 2007 UTC vs.
Revision 1.66 by root, Sun Nov 4 23:30:53 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
130{ 160{
131 W w; 161 W w;
132 int events; 162 int events;
133} ANPENDING; 163} ANPENDING;
134 164
135#ifdef EV_MULTIPLICITY 165#if EV_MULTIPLICITY
136 166
137struct ev_loop 167struct ev_loop
138{ 168{
139# define VAR(name,decl) decl; 169# define VAR(name,decl) decl;
140# include "ev_vars.h" 170# include "ev_vars.h"
202 base = realloc (base, sizeof (*base) * (newcnt)); \ 232 base = realloc (base, sizeof (*base) * (newcnt)); \
203 init (base + cur, newcnt - cur); \ 233 init (base + cur, newcnt - cur); \
204 cur = newcnt; \ 234 cur = newcnt; \
205 } 235 }
206 236
237#define array_free(stem, idx) \
238 free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
239
207/*****************************************************************************/ 240/*****************************************************************************/
208 241
209static void 242static void
210anfds_init (ANFD *base, int count) 243anfds_init (ANFD *base, int count)
211{ 244{
276 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)
277 events |= w->events; 310 events |= w->events;
278 311
279 anfd->reify = 0; 312 anfd->reify = 0;
280 313
281 if (anfd->events != events)
282 {
283 method_modify (EV_A_ fd, anfd->events, events); 314 method_modify (EV_A_ fd, anfd->events, events);
284 anfd->events = events; 315 anfd->events = events;
285 }
286 } 316 }
287 317
288 fdchangecnt = 0; 318 fdchangecnt = 0;
289} 319}
290 320
327 357
328/* 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 */
329static void 359static void
330fd_enomem (EV_P) 360fd_enomem (EV_P)
331{ 361{
332 int fd = anfdmax; 362 int fd;
333 363
334 while (fd--) 364 for (fd = anfdmax; fd--; )
335 if (anfds [fd].events) 365 if (anfds [fd].events)
336 { 366 {
337 close (fd); 367 close (fd);
338 fd_kill (EV_A_ fd); 368 fd_kill (EV_A_ fd);
339 return; 369 return;
340 } 370 }
341} 371}
342 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
343/*****************************************************************************/ 388/*****************************************************************************/
344 389
345static void 390static void
346upheap (WT *heap, int k) 391upheap (WT *heap, int k)
347{ 392{
348 WT w = heap [k]; 393 WT w = heap [k];
349 394
350 while (k && heap [k >> 1]->at > w->at) 395 while (k && heap [k >> 1]->at > w->at)
351 { 396 {
352 heap [k] = heap [k >> 1]; 397 heap [k] = heap [k >> 1];
353 heap [k]->active = k + 1; 398 ((W)heap [k])->active = k + 1;
354 k >>= 1; 399 k >>= 1;
355 } 400 }
356 401
357 heap [k] = w; 402 heap [k] = w;
358 heap [k]->active = k + 1; 403 ((W)heap [k])->active = k + 1;
359 404
360} 405}
361 406
362static void 407static void
363downheap (WT *heap, int N, int k) 408downheap (WT *heap, int N, int k)
373 418
374 if (w->at <= heap [j]->at) 419 if (w->at <= heap [j]->at)
375 break; 420 break;
376 421
377 heap [k] = heap [j]; 422 heap [k] = heap [j];
378 heap [k]->active = k + 1; 423 ((W)heap [k])->active = k + 1;
379 k = j; 424 k = j;
380 } 425 }
381 426
382 heap [k] = w; 427 heap [k] = w;
383 heap [k]->active = k + 1; 428 ((W)heap [k])->active = k + 1;
384} 429}
385 430
386/*****************************************************************************/ 431/*****************************************************************************/
387 432
388typedef struct 433typedef struct
394static ANSIG *signals; 439static ANSIG *signals;
395static int signalmax; 440static int signalmax;
396 441
397static int sigpipe [2]; 442static int sigpipe [2];
398static sig_atomic_t volatile gotsig; 443static sig_atomic_t volatile gotsig;
444static struct ev_io sigev;
399 445
400static void 446static void
401signals_init (ANSIG *base, int count) 447signals_init (ANSIG *base, int count)
402{ 448{
403 while (count--) 449 while (count--)
461 507
462/*****************************************************************************/ 508/*****************************************************************************/
463 509
464#ifndef WIN32 510#ifndef WIN32
465 511
512static struct ev_child *childs [PID_HASHSIZE];
513static struct ev_signal childev;
514
466#ifndef WCONTINUED 515#ifndef WCONTINUED
467# define WCONTINUED 0 516# define WCONTINUED 0
468#endif 517#endif
469 518
470static void 519static void
473 struct ev_child *w; 522 struct ev_child *w;
474 523
475 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)
476 if (w->pid == pid || !w->pid) 525 if (w->pid == pid || !w->pid)
477 { 526 {
478 w->priority = sw->priority; /* need to do it *now* */ 527 ev_priority (w) = ev_priority (sw); /* need to do it *now* */
479 w->rpid = pid; 528 w->rpid = pid;
480 w->rstatus = status; 529 w->rstatus = status;
481 event (EV_A_ (W)w, EV_CHILD); 530 event (EV_A_ (W)w, EV_CHILD);
482 } 531 }
483} 532}
484 533
485static void 534static void
505# include "ev_kqueue.c" 554# include "ev_kqueue.c"
506#endif 555#endif
507#if EV_USE_EPOLL 556#if EV_USE_EPOLL
508# include "ev_epoll.c" 557# include "ev_epoll.c"
509#endif 558#endif
510#if EV_USEV_POLL 559#if EV_USE_POLL
511# include "ev_poll.c" 560# include "ev_poll.c"
512#endif 561#endif
513#if EV_USE_SELECT 562#if EV_USE_SELECT
514# include "ev_select.c" 563# include "ev_select.c"
515#endif 564#endif
560 rt_now = ev_time (); 609 rt_now = ev_time ();
561 mn_now = get_clock (); 610 mn_now = get_clock ();
562 now_floor = mn_now; 611 now_floor = mn_now;
563 rtmn_diff = rt_now - mn_now; 612 rtmn_diff = rt_now - mn_now;
564 613
565 if (pipe (sigpipe))
566 return 0;
567
568 if (methods == EVMETHOD_AUTO) 614 if (methods == EVMETHOD_AUTO)
569 if (!enable_secure () && getenv ("LIBmethodS")) 615 if (!enable_secure () && getenv ("LIBEV_METHODS"))
570 methods = atoi (getenv ("LIBmethodS")); 616 methods = atoi (getenv ("LIBEV_METHODS"));
571 else 617 else
572 methods = EVMETHOD_ANY; 618 methods = EVMETHOD_ANY;
573 619
574 method = 0; 620 method = 0;
621#if EV_USE_WIN32
622 if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods);
623#endif
575#if EV_USE_KQUEUE 624#if EV_USE_KQUEUE
576 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); 625 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods);
577#endif 626#endif
578#if EV_USE_EPOLL 627#if EV_USE_EPOLL
579 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); 628 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods);
580#endif 629#endif
581#if EV_USEV_POLL 630#if EV_USE_POLL
582 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); 631 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods);
583#endif 632#endif
584#if EV_USE_SELECT 633#if EV_USE_SELECT
585 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 634 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
586#endif 635#endif
636 }
637}
587 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
588 if (method) 741 if (ev_method (EV_A))
589 { 742 {
590 ev_watcher_init (&sigev, sigcb); 743 ev_watcher_init (&sigev, sigcb);
591 ev_set_priority (&sigev, EV_MAXPRI); 744 ev_set_priority (&sigev, EV_MAXPRI);
592 siginit (EV_A); 745 siginit (EV_A);
593 746
596 ev_set_priority (&childev, EV_MAXPRI); 749 ev_set_priority (&childev, EV_MAXPRI);
597 ev_signal_start (EV_A_ &childev); 750 ev_signal_start (EV_A_ &childev);
598 ev_unref (EV_A); /* child watcher should not keep loop alive */ 751 ev_unref (EV_A); /* child watcher should not keep loop alive */
599#endif 752#endif
600 } 753 }
754 else
755 default_loop = 0;
601 } 756 }
602 757
603 return method;
604}
605
606#ifdef EV_MULTIPLICITY
607
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 loop_init (EV_A_ methods);
614
615 return loop; 758 return default_loop;
616} 759}
617 760
618void 761void
619ev_loop_delete (EV_P) 762ev_default_destroy (void)
620{ 763{
621 /*TODO*/
622 free (loop);
623}
624
625#else
626
627int
628ev_init (int methods)
629{
630 loop_init ();
631}
632
633#endif
634
635/*****************************************************************************/
636
637void
638ev_fork_prepare (void)
639{
640 /* nop */
641}
642
643void
644ev_fork_parent (void)
645{
646 /* nop */
647}
648
649void
650ev_fork_child (void)
651{
652 /*TODO*/
653#if !EV_MULTIPLICITY 764#if EV_MULTIPLICITY
654#if EV_USE_EPOLL 765 struct ev_loop *loop = default_loop;
655 if (method == EVMETHOD_EPOLL)
656 epoll_postfork_child (EV_A);
657#endif 766#endif
767
768 ev_ref (EV_A); /* child watcher */
769 ev_signal_stop (EV_A_ &childev);
770
771 ev_ref (EV_A); /* signal watcher */
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);
658 788
659 ev_io_stop (EV_A_ &sigev); 789 ev_io_stop (EV_A_ &sigev);
660 close (sigpipe [0]); 790 close (sigpipe [0]);
661 close (sigpipe [1]); 791 close (sigpipe [1]);
662 pipe (sigpipe); 792 pipe (sigpipe);
793
794 ev_ref (EV_A); /* signal watcher */
663 siginit (EV_A); 795 siginit (EV_A);
664#endif
665} 796}
666 797
667/*****************************************************************************/ 798/*****************************************************************************/
668 799
669static void 800static void
685} 816}
686 817
687static void 818static void
688timers_reify (EV_P) 819timers_reify (EV_P)
689{ 820{
690 while (timercnt && timers [0]->at <= mn_now) 821 while (timercnt && ((WT)timers [0])->at <= mn_now)
691 { 822 {
692 struct ev_timer *w = timers [0]; 823 struct ev_timer *w = timers [0];
824
825 assert (("inactive timer on timer heap detected", ev_is_active (w)));
693 826
694 /* first reschedule or stop timer */ 827 /* first reschedule or stop timer */
695 if (w->repeat) 828 if (w->repeat)
696 { 829 {
697 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); 830 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
698 w->at = mn_now + w->repeat; 831 ((WT)w)->at = mn_now + w->repeat;
699 downheap ((WT *)timers, timercnt, 0); 832 downheap ((WT *)timers, timercnt, 0);
700 } 833 }
701 else 834 else
702 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 835 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
703 836
706} 839}
707 840
708static void 841static void
709periodics_reify (EV_P) 842periodics_reify (EV_P)
710{ 843{
711 while (periodiccnt && periodics [0]->at <= rt_now) 844 while (periodiccnt && ((WT)periodics [0])->at <= rt_now)
712 { 845 {
713 struct ev_periodic *w = periodics [0]; 846 struct ev_periodic *w = periodics [0];
847
848 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
714 849
715 /* first reschedule or stop timer */ 850 /* first reschedule or stop timer */
716 if (w->interval) 851 if (w->interval)
717 { 852 {
718 w->at += floor ((rt_now - w->at) / w->interval + 1.) * w->interval; 853 ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval;
719 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > rt_now)); 854 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now));
720 downheap ((WT *)periodics, periodiccnt, 0); 855 downheap ((WT *)periodics, periodiccnt, 0);
721 } 856 }
722 else 857 else
723 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 858 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
724 859
736 { 871 {
737 struct ev_periodic *w = periodics [i]; 872 struct ev_periodic *w = periodics [i];
738 873
739 if (w->interval) 874 if (w->interval)
740 { 875 {
741 ev_tstamp diff = ceil ((rt_now - w->at) / w->interval) * w->interval; 876 ev_tstamp diff = ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval;
742 877
743 if (fabs (diff) >= 1e-4) 878 if (fabs (diff) >= 1e-4)
744 { 879 {
745 ev_periodic_stop (EV_A_ w); 880 ev_periodic_stop (EV_A_ w);
746 ev_periodic_start (EV_A_ w); 881 ev_periodic_start (EV_A_ w);
807 { 942 {
808 periodics_reschedule (EV_A); 943 periodics_reschedule (EV_A);
809 944
810 /* adjust timers. this is easy, as the offset is the same for all */ 945 /* adjust timers. this is easy, as the offset is the same for all */
811 for (i = 0; i < timercnt; ++i) 946 for (i = 0; i < timercnt; ++i)
812 timers [i]->at += rt_now - mn_now; 947 ((WT)timers [i])->at += rt_now - mn_now;
813 } 948 }
814 949
815 mn_now = rt_now; 950 mn_now = rt_now;
816 } 951 }
817} 952}
868 { 1003 {
869 block = MAX_BLOCKTIME; 1004 block = MAX_BLOCKTIME;
870 1005
871 if (timercnt) 1006 if (timercnt)
872 { 1007 {
873 ev_tstamp to = timers [0]->at - mn_now + method_fudge; 1008 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge;
874 if (block > to) block = to; 1009 if (block > to) block = to;
875 } 1010 }
876 1011
877 if (periodiccnt) 1012 if (periodiccnt)
878 { 1013 {
879 ev_tstamp to = periodics [0]->at - rt_now + method_fudge; 1014 ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge;
880 if (block > to) block = to; 1015 if (block > to) block = to;
881 } 1016 }
882 1017
883 if (block < 0.) block = 0.; 1018 if (block < 0.) block = 0.;
884 } 1019 }
1001ev_timer_start (EV_P_ struct ev_timer *w) 1136ev_timer_start (EV_P_ struct ev_timer *w)
1002{ 1137{
1003 if (ev_is_active (w)) 1138 if (ev_is_active (w))
1004 return; 1139 return;
1005 1140
1006 w->at += mn_now; 1141 ((WT)w)->at += mn_now;
1007 1142
1008 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1143 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1009 1144
1010 ev_start (EV_A_ (W)w, ++timercnt); 1145 ev_start (EV_A_ (W)w, ++timercnt);
1011 array_needsize (timers, timermax, timercnt, ); 1146 array_needsize (timers, timermax, timercnt, );
1012 timers [timercnt - 1] = w; 1147 timers [timercnt - 1] = w;
1013 upheap ((WT *)timers, timercnt - 1); 1148 upheap ((WT *)timers, timercnt - 1);
1149
1150 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1014} 1151}
1015 1152
1016void 1153void
1017ev_timer_stop (EV_P_ struct ev_timer *w) 1154ev_timer_stop (EV_P_ struct ev_timer *w)
1018{ 1155{
1019 ev_clear_pending (EV_A_ (W)w); 1156 ev_clear_pending (EV_A_ (W)w);
1020 if (!ev_is_active (w)) 1157 if (!ev_is_active (w))
1021 return; 1158 return;
1022 1159
1160 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1161
1023 if (w->active < timercnt--) 1162 if (((W)w)->active < timercnt--)
1024 { 1163 {
1025 timers [w->active - 1] = timers [timercnt]; 1164 timers [((W)w)->active - 1] = timers [timercnt];
1026 downheap ((WT *)timers, timercnt, w->active - 1); 1165 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1027 } 1166 }
1028 1167
1029 w->at = w->repeat; 1168 ((WT)w)->at = w->repeat;
1030 1169
1031 ev_stop (EV_A_ (W)w); 1170 ev_stop (EV_A_ (W)w);
1032} 1171}
1033 1172
1034void 1173void
1036{ 1175{
1037 if (ev_is_active (w)) 1176 if (ev_is_active (w))
1038 { 1177 {
1039 if (w->repeat) 1178 if (w->repeat)
1040 { 1179 {
1041 w->at = mn_now + w->repeat; 1180 ((WT)w)->at = mn_now + w->repeat;
1042 downheap ((WT *)timers, timercnt, w->active - 1); 1181 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1043 } 1182 }
1044 else 1183 else
1045 ev_timer_stop (EV_A_ w); 1184 ev_timer_stop (EV_A_ w);
1046 } 1185 }
1047 else if (w->repeat) 1186 else if (w->repeat)
1056 1195
1057 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); 1196 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
1058 1197
1059 /* this formula differs from the one in periodic_reify because we do not always round up */ 1198 /* this formula differs from the one in periodic_reify because we do not always round up */
1060 if (w->interval) 1199 if (w->interval)
1061 w->at += ceil ((rt_now - w->at) / w->interval) * w->interval; 1200 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval;
1062 1201
1063 ev_start (EV_A_ (W)w, ++periodiccnt); 1202 ev_start (EV_A_ (W)w, ++periodiccnt);
1064 array_needsize (periodics, periodicmax, periodiccnt, ); 1203 array_needsize (periodics, periodicmax, periodiccnt, );
1065 periodics [periodiccnt - 1] = w; 1204 periodics [periodiccnt - 1] = w;
1066 upheap ((WT *)periodics, periodiccnt - 1); 1205 upheap ((WT *)periodics, periodiccnt - 1);
1206
1207 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1067} 1208}
1068 1209
1069void 1210void
1070ev_periodic_stop (EV_P_ struct ev_periodic *w) 1211ev_periodic_stop (EV_P_ struct ev_periodic *w)
1071{ 1212{
1072 ev_clear_pending (EV_A_ (W)w); 1213 ev_clear_pending (EV_A_ (W)w);
1073 if (!ev_is_active (w)) 1214 if (!ev_is_active (w))
1074 return; 1215 return;
1075 1216
1217 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1218
1076 if (w->active < periodiccnt--) 1219 if (((W)w)->active < periodiccnt--)
1077 { 1220 {
1078 periodics [w->active - 1] = periodics [periodiccnt]; 1221 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1079 downheap ((WT *)periodics, periodiccnt, w->active - 1); 1222 downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1080 } 1223 }
1081 1224
1225 ev_stop (EV_A_ (W)w);
1226}
1227
1228void
1229ev_idle_start (EV_P_ struct ev_idle *w)
1230{
1231 if (ev_is_active (w))
1232 return;
1233
1234 ev_start (EV_A_ (W)w, ++idlecnt);
1235 array_needsize (idles, idlemax, idlecnt, );
1236 idles [idlecnt - 1] = w;
1237}
1238
1239void
1240ev_idle_stop (EV_P_ struct ev_idle *w)
1241{
1242 ev_clear_pending (EV_A_ (W)w);
1243 if (ev_is_active (w))
1244 return;
1245
1246 idles [((W)w)->active - 1] = idles [--idlecnt];
1247 ev_stop (EV_A_ (W)w);
1248}
1249
1250void
1251ev_prepare_start (EV_P_ struct ev_prepare *w)
1252{
1253 if (ev_is_active (w))
1254 return;
1255
1256 ev_start (EV_A_ (W)w, ++preparecnt);
1257 array_needsize (prepares, preparemax, preparecnt, );
1258 prepares [preparecnt - 1] = w;
1259}
1260
1261void
1262ev_prepare_stop (EV_P_ struct ev_prepare *w)
1263{
1264 ev_clear_pending (EV_A_ (W)w);
1265 if (ev_is_active (w))
1266 return;
1267
1268 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1269 ev_stop (EV_A_ (W)w);
1270}
1271
1272void
1273ev_check_start (EV_P_ struct ev_check *w)
1274{
1275 if (ev_is_active (w))
1276 return;
1277
1278 ev_start (EV_A_ (W)w, ++checkcnt);
1279 array_needsize (checks, checkmax, checkcnt, );
1280 checks [checkcnt - 1] = w;
1281}
1282
1283void
1284ev_check_stop (EV_P_ struct ev_check *w)
1285{
1286 ev_clear_pending (EV_A_ (W)w);
1287 if (ev_is_active (w))
1288 return;
1289
1290 checks [((W)w)->active - 1] = checks [--checkcnt];
1082 ev_stop (EV_A_ (W)w); 1291 ev_stop (EV_A_ (W)w);
1083} 1292}
1084 1293
1085#ifndef SA_RESTART 1294#ifndef SA_RESTART
1086# define SA_RESTART 0 1295# define SA_RESTART 0
1087#endif 1296#endif
1088 1297
1089void 1298void
1090ev_signal_start (EV_P_ struct ev_signal *w) 1299ev_signal_start (EV_P_ struct ev_signal *w)
1091{ 1300{
1301#if EV_MULTIPLICITY
1302 assert (("signal watchers are only supported in the default loop", loop == default_loop));
1303#endif
1092 if (ev_is_active (w)) 1304 if (ev_is_active (w))
1093 return; 1305 return;
1094 1306
1095 assert (("ev_signal_start called with illegal signal number", w->signum > 0)); 1307 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
1096 1308
1097 ev_start (EV_A_ (W)w, 1); 1309 ev_start (EV_A_ (W)w, 1);
1098 array_needsize (signals, signalmax, w->signum, signals_init); 1310 array_needsize (signals, signalmax, w->signum, signals_init);
1099 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1311 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1100 1312
1101 if (!w->next) 1313 if (!((WL)w)->next)
1102 { 1314 {
1103 struct sigaction sa; 1315 struct sigaction sa;
1104 sa.sa_handler = sighandler; 1316 sa.sa_handler = sighandler;
1105 sigfillset (&sa.sa_mask); 1317 sigfillset (&sa.sa_mask);
1106 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ 1318 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
1121 if (!signals [w->signum - 1].head) 1333 if (!signals [w->signum - 1].head)
1122 signal (w->signum, SIG_DFL); 1334 signal (w->signum, SIG_DFL);
1123} 1335}
1124 1336
1125void 1337void
1126ev_idle_start (EV_P_ struct ev_idle *w)
1127{
1128 if (ev_is_active (w))
1129 return;
1130
1131 ev_start (EV_A_ (W)w, ++idlecnt);
1132 array_needsize (idles, idlemax, idlecnt, );
1133 idles [idlecnt - 1] = w;
1134}
1135
1136void
1137ev_idle_stop (EV_P_ struct ev_idle *w)
1138{
1139 ev_clear_pending (EV_A_ (W)w);
1140 if (ev_is_active (w))
1141 return;
1142
1143 idles [w->active - 1] = idles [--idlecnt];
1144 ev_stop (EV_A_ (W)w);
1145}
1146
1147void
1148ev_prepare_start (EV_P_ struct ev_prepare *w)
1149{
1150 if (ev_is_active (w))
1151 return;
1152
1153 ev_start (EV_A_ (W)w, ++preparecnt);
1154 array_needsize (prepares, preparemax, preparecnt, );
1155 prepares [preparecnt - 1] = w;
1156}
1157
1158void
1159ev_prepare_stop (EV_P_ struct ev_prepare *w)
1160{
1161 ev_clear_pending (EV_A_ (W)w);
1162 if (ev_is_active (w))
1163 return;
1164
1165 prepares [w->active - 1] = prepares [--preparecnt];
1166 ev_stop (EV_A_ (W)w);
1167}
1168
1169void
1170ev_check_start (EV_P_ struct ev_check *w)
1171{
1172 if (ev_is_active (w))
1173 return;
1174
1175 ev_start (EV_A_ (W)w, ++checkcnt);
1176 array_needsize (checks, checkmax, checkcnt, );
1177 checks [checkcnt - 1] = w;
1178}
1179
1180void
1181ev_check_stop (EV_P_ struct ev_check *w)
1182{
1183 ev_clear_pending (EV_A_ (W)w);
1184 if (ev_is_active (w))
1185 return;
1186
1187 checks [w->active - 1] = checks [--checkcnt];
1188 ev_stop (EV_A_ (W)w);
1189}
1190
1191void
1192ev_child_start (EV_P_ struct ev_child *w) 1338ev_child_start (EV_P_ struct ev_child *w)
1193{ 1339{
1340#if EV_MULTIPLICITY
1341 assert (("child watchers are only supported in the default loop", loop == default_loop));
1342#endif
1194 if (ev_is_active (w)) 1343 if (ev_is_active (w))
1195 return; 1344 return;
1196 1345
1197 ev_start (EV_A_ (W)w, 1); 1346 ev_start (EV_A_ (W)w, 1);
1198 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1347 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1270 ev_timer_start (EV_A_ &once->to); 1419 ev_timer_start (EV_A_ &once->to);
1271 } 1420 }
1272 } 1421 }
1273} 1422}
1274 1423
1275/*****************************************************************************/
1276
1277#if 0
1278
1279struct ev_io wio;
1280
1281static void
1282sin_cb (struct ev_io *w, int revents)
1283{
1284 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
1285}
1286
1287static void
1288ocb (struct ev_timer *w, int revents)
1289{
1290 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data);
1291 ev_timer_stop (w);
1292 ev_timer_start (w);
1293}
1294
1295static void
1296scb (struct ev_signal *w, int revents)
1297{
1298 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
1299 ev_io_stop (&wio);
1300 ev_io_start (&wio);
1301}
1302
1303static void
1304gcb (struct ev_signal *w, int revents)
1305{
1306 fprintf (stderr, "generic %x\n", revents);
1307
1308}
1309
1310int main (void)
1311{
1312 ev_init (0);
1313
1314 ev_io_init (&wio, sin_cb, 0, EV_READ);
1315 ev_io_start (&wio);
1316
1317 struct ev_timer t[10000];
1318
1319#if 0
1320 int i;
1321 for (i = 0; i < 10000; ++i)
1322 {
1323 struct ev_timer *w = t + i;
1324 ev_watcher_init (w, ocb, i);
1325 ev_timer_init_abs (w, ocb, drand48 (), 0.99775533);
1326 ev_timer_start (w);
1327 if (drand48 () < 0.5)
1328 ev_timer_stop (w);
1329 }
1330#endif
1331
1332 struct ev_timer t1;
1333 ev_timer_init (&t1, ocb, 5, 10);
1334 ev_timer_start (&t1);
1335
1336 struct ev_signal sig;
1337 ev_signal_init (&sig, scb, SIGQUIT);
1338 ev_signal_start (&sig);
1339
1340 struct ev_check cw;
1341 ev_check_init (&cw, gcb);
1342 ev_check_start (&cw);
1343
1344 struct ev_idle iw;
1345 ev_idle_init (&iw, gcb);
1346 ev_idle_start (&iw);
1347
1348 ev_loop (0);
1349
1350 return 0;
1351}
1352
1353#endif
1354
1355
1356
1357

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines