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.67 by root, Mon Nov 5 16:42:15 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
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 int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 148static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
149
150#if WIN32
151/* note: the comment below could not be substantiated, but what would I care */
152/* MSDN says this is required to handle SIGFPE */
153volatile double SIGFPE_REQ = 0.0f;
154#endif
119 155
120/*****************************************************************************/ 156/*****************************************************************************/
121 157
122typedef struct 158typedef struct
123{ 159{
202 base = realloc (base, sizeof (*base) * (newcnt)); \ 238 base = realloc (base, sizeof (*base) * (newcnt)); \
203 init (base + cur, newcnt - cur); \ 239 init (base + cur, newcnt - cur); \
204 cur = newcnt; \ 240 cur = newcnt; \
205 } 241 }
206 242
243#define array_slim(stem) \
244 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \
245 { \
246 stem ## max = array_roundsize (stem ## cnt >> 1); \
247 base = realloc (base, sizeof (*base) * (stem ## max)); \
248 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
249 }
250
251#define array_free(stem, idx) \
252 free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
253
207/*****************************************************************************/ 254/*****************************************************************************/
208 255
209static void 256static void
210anfds_init (ANFD *base, int count) 257anfds_init (ANFD *base, int count)
211{ 258{
276 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) 323 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
277 events |= w->events; 324 events |= w->events;
278 325
279 anfd->reify = 0; 326 anfd->reify = 0;
280 327
281 if (anfd->events != events)
282 {
283 method_modify (EV_A_ fd, anfd->events, events); 328 method_modify (EV_A_ fd, anfd->events, events);
284 anfd->events = events; 329 anfd->events = events;
285 }
286 } 330 }
287 331
288 fdchangecnt = 0; 332 fdchangecnt = 0;
289} 333}
290 334
327 371
328/* called on ENOMEM in select/poll to kill some fds and retry */ 372/* called on ENOMEM in select/poll to kill some fds and retry */
329static void 373static void
330fd_enomem (EV_P) 374fd_enomem (EV_P)
331{ 375{
332 int fd = anfdmax; 376 int fd;
333 377
334 while (fd--) 378 for (fd = anfdmax; fd--; )
335 if (anfds [fd].events) 379 if (anfds [fd].events)
336 { 380 {
337 close (fd); 381 close (fd);
338 fd_kill (EV_A_ fd); 382 fd_kill (EV_A_ fd);
339 return; 383 return;
340 } 384 }
341} 385}
342 386
387/* susually called after fork if method needs to re-arm all fds from scratch */
388static void
389fd_rearm_all (EV_P)
390{
391 int fd;
392
393 /* this should be highly optimised to not do anything but set a flag */
394 for (fd = 0; fd < anfdmax; ++fd)
395 if (anfds [fd].events)
396 {
397 anfds [fd].events = 0;
398 fd_change (EV_A_ fd);
399 }
400}
401
343/*****************************************************************************/ 402/*****************************************************************************/
344 403
345static void 404static void
346upheap (WT *heap, int k) 405upheap (WT *heap, int k)
347{ 406{
348 WT w = heap [k]; 407 WT w = heap [k];
349 408
350 while (k && heap [k >> 1]->at > w->at) 409 while (k && heap [k >> 1]->at > w->at)
351 { 410 {
352 heap [k] = heap [k >> 1]; 411 heap [k] = heap [k >> 1];
353 heap [k]->active = k + 1; 412 ((W)heap [k])->active = k + 1;
354 k >>= 1; 413 k >>= 1;
355 } 414 }
356 415
357 heap [k] = w; 416 heap [k] = w;
358 heap [k]->active = k + 1; 417 ((W)heap [k])->active = k + 1;
359 418
360} 419}
361 420
362static void 421static void
363downheap (WT *heap, int N, int k) 422downheap (WT *heap, int N, int k)
373 432
374 if (w->at <= heap [j]->at) 433 if (w->at <= heap [j]->at)
375 break; 434 break;
376 435
377 heap [k] = heap [j]; 436 heap [k] = heap [j];
378 heap [k]->active = k + 1; 437 ((W)heap [k])->active = k + 1;
379 k = j; 438 k = j;
380 } 439 }
381 440
382 heap [k] = w; 441 heap [k] = w;
383 heap [k]->active = k + 1; 442 ((W)heap [k])->active = k + 1;
384} 443}
385 444
386/*****************************************************************************/ 445/*****************************************************************************/
387 446
388typedef struct 447typedef struct
394static ANSIG *signals; 453static ANSIG *signals;
395static int signalmax; 454static int signalmax;
396 455
397static int sigpipe [2]; 456static int sigpipe [2];
398static sig_atomic_t volatile gotsig; 457static sig_atomic_t volatile gotsig;
458static struct ev_io sigev;
399 459
400static void 460static void
401signals_init (ANSIG *base, int count) 461signals_init (ANSIG *base, int count)
402{ 462{
403 while (count--) 463 while (count--)
410} 470}
411 471
412static void 472static void
413sighandler (int signum) 473sighandler (int signum)
414{ 474{
475#if WIN32
476 signal (signum, sighandler);
477#endif
478
415 signals [signum - 1].gotsig = 1; 479 signals [signum - 1].gotsig = 1;
416 480
417 if (!gotsig) 481 if (!gotsig)
418 { 482 {
419 int old_errno = errno; 483 int old_errno = errno;
461 525
462/*****************************************************************************/ 526/*****************************************************************************/
463 527
464#ifndef WIN32 528#ifndef WIN32
465 529
530static struct ev_child *childs [PID_HASHSIZE];
531static struct ev_signal childev;
532
466#ifndef WCONTINUED 533#ifndef WCONTINUED
467# define WCONTINUED 0 534# define WCONTINUED 0
468#endif 535#endif
469 536
470static void 537static void
473 struct ev_child *w; 540 struct ev_child *w;
474 541
475 for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) 542 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) 543 if (w->pid == pid || !w->pid)
477 { 544 {
478 w->priority = sw->priority; /* need to do it *now* */ 545 ev_priority (w) = ev_priority (sw); /* need to do it *now* */
479 w->rpid = pid; 546 w->rpid = pid;
480 w->rstatus = status; 547 w->rstatus = status;
481 event (EV_A_ (W)w, EV_CHILD); 548 event (EV_A_ (W)w, EV_CHILD);
482 } 549 }
483} 550}
484 551
485static void 552static void
505# include "ev_kqueue.c" 572# include "ev_kqueue.c"
506#endif 573#endif
507#if EV_USE_EPOLL 574#if EV_USE_EPOLL
508# include "ev_epoll.c" 575# include "ev_epoll.c"
509#endif 576#endif
510#if EV_USEV_POLL 577#if EV_USE_POLL
511# include "ev_poll.c" 578# include "ev_poll.c"
512#endif 579#endif
513#if EV_USE_SELECT 580#if EV_USE_SELECT
514# include "ev_select.c" 581# include "ev_select.c"
515#endif 582#endif
542ev_method (EV_P) 609ev_method (EV_P)
543{ 610{
544 return method; 611 return method;
545} 612}
546 613
547inline int 614static void
548loop_init (EV_P_ int methods) 615loop_init (EV_P_ int methods)
549{ 616{
550 if (!method) 617 if (!method)
551 { 618 {
552#if EV_USE_MONOTONIC 619#if EV_USE_MONOTONIC
560 rt_now = ev_time (); 627 rt_now = ev_time ();
561 mn_now = get_clock (); 628 mn_now = get_clock ();
562 now_floor = mn_now; 629 now_floor = mn_now;
563 rtmn_diff = rt_now - mn_now; 630 rtmn_diff = rt_now - mn_now;
564 631
565 if (pipe (sigpipe))
566 return 0;
567
568 if (methods == EVMETHOD_AUTO) 632 if (methods == EVMETHOD_AUTO)
569 if (!enable_secure () && getenv ("LIBmethodS")) 633 if (!enable_secure () && getenv ("LIBEV_METHODS"))
570 methods = atoi (getenv ("LIBmethodS")); 634 methods = atoi (getenv ("LIBEV_METHODS"));
571 else 635 else
572 methods = EVMETHOD_ANY; 636 methods = EVMETHOD_ANY;
573 637
574 method = 0; 638 method = 0;
639#if EV_USE_WIN32
640 if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods);
641#endif
575#if EV_USE_KQUEUE 642#if EV_USE_KQUEUE
576 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); 643 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods);
577#endif 644#endif
578#if EV_USE_EPOLL 645#if EV_USE_EPOLL
579 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); 646 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods);
580#endif 647#endif
581#if EV_USEV_POLL 648#if EV_USE_POLL
582 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); 649 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods);
583#endif 650#endif
584#if EV_USE_SELECT 651#if EV_USE_SELECT
585 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 652 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
586#endif 653#endif
654 }
655}
587 656
657void
658loop_destroy (EV_P)
659{
660 int i;
661
662#if EV_USE_WIN32
663 if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A);
664#endif
665#if EV_USE_KQUEUE
666 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
667#endif
668#if EV_USE_EPOLL
669 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A);
670#endif
671#if EV_USE_POLL
672 if (method == EVMETHOD_POLL ) poll_destroy (EV_A);
673#endif
674#if EV_USE_SELECT
675 if (method == EVMETHOD_SELECT) select_destroy (EV_A);
676#endif
677
678 for (i = NUMPRI; i--; )
679 array_free (pending, [i]);
680
681 array_free (fdchange, );
682 array_free (timer, );
683 array_free (periodic, );
684 array_free (idle, );
685 array_free (prepare, );
686 array_free (check, );
687
688 method = 0;
689 /*TODO*/
690}
691
692void
693loop_fork (EV_P)
694{
695 /*TODO*/
696#if EV_USE_EPOLL
697 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A);
698#endif
699#if EV_USE_KQUEUE
700 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A);
701#endif
702}
703
704#if EV_MULTIPLICITY
705struct ev_loop *
706ev_loop_new (int methods)
707{
708 struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop));
709
710 loop_init (EV_A_ methods);
711
712 if (ev_method (EV_A))
713 return loop;
714
715 return 0;
716}
717
718void
719ev_loop_destroy (EV_P)
720{
721 loop_destroy (EV_A);
722 free (loop);
723}
724
725void
726ev_loop_fork (EV_P)
727{
728 loop_fork (EV_A);
729}
730
731#endif
732
733#if EV_MULTIPLICITY
734struct ev_loop default_loop_struct;
735static struct ev_loop *default_loop;
736
737struct ev_loop *
738#else
739static int default_loop;
740
741int
742#endif
743ev_default_loop (int methods)
744{
745 if (sigpipe [0] == sigpipe [1])
746 if (pipe (sigpipe))
747 return 0;
748
749 if (!default_loop)
750 {
751#if EV_MULTIPLICITY
752 struct ev_loop *loop = default_loop = &default_loop_struct;
753#else
754 default_loop = 1;
755#endif
756
757 loop_init (EV_A_ methods);
758
588 if (method) 759 if (ev_method (EV_A))
589 { 760 {
590 ev_watcher_init (&sigev, sigcb); 761 ev_watcher_init (&sigev, sigcb);
591 ev_set_priority (&sigev, EV_MAXPRI); 762 ev_set_priority (&sigev, EV_MAXPRI);
592 siginit (EV_A); 763 siginit (EV_A);
593 764
596 ev_set_priority (&childev, EV_MAXPRI); 767 ev_set_priority (&childev, EV_MAXPRI);
597 ev_signal_start (EV_A_ &childev); 768 ev_signal_start (EV_A_ &childev);
598 ev_unref (EV_A); /* child watcher should not keep loop alive */ 769 ev_unref (EV_A); /* child watcher should not keep loop alive */
599#endif 770#endif
600 } 771 }
772 else
773 default_loop = 0;
601 } 774 }
602 775
603 return method; 776 return default_loop;
604} 777}
605 778
779void
780ev_default_destroy (void)
781{
606#if EV_MULTIPLICITY 782#if EV_MULTIPLICITY
607 783 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 784#endif
637 785
638/*****************************************************************************/ 786 ev_ref (EV_A); /* child watcher */
787 ev_signal_stop (EV_A_ &childev);
639 788
640void 789 ev_ref (EV_A); /* signal watcher */
641ev_fork_prepare (void) 790 ev_io_stop (EV_A_ &sigev);
642{
643 /* nop */
644}
645 791
646void 792 close (sigpipe [0]); sigpipe [0] = 0;
647ev_fork_parent (void) 793 close (sigpipe [1]); sigpipe [1] = 0;
648{
649 /* nop */
650}
651 794
795 loop_destroy (EV_A);
796}
797
652void 798void
653ev_fork_child (void) 799ev_default_fork (void)
654{ 800{
655 /*TODO*/
656#if !EV_MULTIPLICITY 801#if EV_MULTIPLICITY
657#if EV_USE_EPOLL 802 struct ev_loop *loop = default_loop;
658 if (method == EVMETHOD_EPOLL)
659 epoll_postfork_child (EV_A);
660#endif 803#endif
804
805 loop_fork (EV_A);
661 806
662 ev_io_stop (EV_A_ &sigev); 807 ev_io_stop (EV_A_ &sigev);
663 close (sigpipe [0]); 808 close (sigpipe [0]);
664 close (sigpipe [1]); 809 close (sigpipe [1]);
665 pipe (sigpipe); 810 pipe (sigpipe);
811
812 ev_ref (EV_A); /* signal watcher */
666 siginit (EV_A); 813 siginit (EV_A);
667#endif
668} 814}
669 815
670/*****************************************************************************/ 816/*****************************************************************************/
671 817
672static void 818static void
688} 834}
689 835
690static void 836static void
691timers_reify (EV_P) 837timers_reify (EV_P)
692{ 838{
693 while (timercnt && timers [0]->at <= mn_now) 839 while (timercnt && ((WT)timers [0])->at <= mn_now)
694 { 840 {
695 struct ev_timer *w = timers [0]; 841 struct ev_timer *w = timers [0];
842
843 assert (("inactive timer on timer heap detected", ev_is_active (w)));
696 844
697 /* first reschedule or stop timer */ 845 /* first reschedule or stop timer */
698 if (w->repeat) 846 if (w->repeat)
699 { 847 {
700 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); 848 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
701 w->at = mn_now + w->repeat; 849 ((WT)w)->at = mn_now + w->repeat;
702 downheap ((WT *)timers, timercnt, 0); 850 downheap ((WT *)timers, timercnt, 0);
703 } 851 }
704 else 852 else
705 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 853 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
706 854
709} 857}
710 858
711static void 859static void
712periodics_reify (EV_P) 860periodics_reify (EV_P)
713{ 861{
714 while (periodiccnt && periodics [0]->at <= rt_now) 862 while (periodiccnt && ((WT)periodics [0])->at <= rt_now)
715 { 863 {
716 struct ev_periodic *w = periodics [0]; 864 struct ev_periodic *w = periodics [0];
865
866 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
717 867
718 /* first reschedule or stop timer */ 868 /* first reschedule or stop timer */
719 if (w->interval) 869 if (w->interval)
720 { 870 {
721 w->at += floor ((rt_now - w->at) / w->interval + 1.) * w->interval; 871 ((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)); 872 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now));
723 downheap ((WT *)periodics, periodiccnt, 0); 873 downheap ((WT *)periodics, periodiccnt, 0);
724 } 874 }
725 else 875 else
726 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 876 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
727 877
739 { 889 {
740 struct ev_periodic *w = periodics [i]; 890 struct ev_periodic *w = periodics [i];
741 891
742 if (w->interval) 892 if (w->interval)
743 { 893 {
744 ev_tstamp diff = ceil ((rt_now - w->at) / w->interval) * w->interval; 894 ev_tstamp diff = ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval;
745 895
746 if (fabs (diff) >= 1e-4) 896 if (fabs (diff) >= 1e-4)
747 { 897 {
748 ev_periodic_stop (EV_A_ w); 898 ev_periodic_stop (EV_A_ w);
749 ev_periodic_start (EV_A_ w); 899 ev_periodic_start (EV_A_ w);
810 { 960 {
811 periodics_reschedule (EV_A); 961 periodics_reschedule (EV_A);
812 962
813 /* adjust timers. this is easy, as the offset is the same for all */ 963 /* adjust timers. this is easy, as the offset is the same for all */
814 for (i = 0; i < timercnt; ++i) 964 for (i = 0; i < timercnt; ++i)
815 timers [i]->at += rt_now - mn_now; 965 ((WT)timers [i])->at += rt_now - mn_now;
816 } 966 }
817 967
818 mn_now = rt_now; 968 mn_now = rt_now;
819 } 969 }
820} 970}
871 { 1021 {
872 block = MAX_BLOCKTIME; 1022 block = MAX_BLOCKTIME;
873 1023
874 if (timercnt) 1024 if (timercnt)
875 { 1025 {
876 ev_tstamp to = timers [0]->at - mn_now + method_fudge; 1026 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge;
877 if (block > to) block = to; 1027 if (block > to) block = to;
878 } 1028 }
879 1029
880 if (periodiccnt) 1030 if (periodiccnt)
881 { 1031 {
882 ev_tstamp to = periodics [0]->at - rt_now + method_fudge; 1032 ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge;
883 if (block > to) block = to; 1033 if (block > to) block = to;
884 } 1034 }
885 1035
886 if (block < 0.) block = 0.; 1036 if (block < 0.) block = 0.;
887 } 1037 }
1004ev_timer_start (EV_P_ struct ev_timer *w) 1154ev_timer_start (EV_P_ struct ev_timer *w)
1005{ 1155{
1006 if (ev_is_active (w)) 1156 if (ev_is_active (w))
1007 return; 1157 return;
1008 1158
1009 w->at += mn_now; 1159 ((WT)w)->at += mn_now;
1010 1160
1011 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1161 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1012 1162
1013 ev_start (EV_A_ (W)w, ++timercnt); 1163 ev_start (EV_A_ (W)w, ++timercnt);
1014 array_needsize (timers, timermax, timercnt, ); 1164 array_needsize (timers, timermax, timercnt, );
1015 timers [timercnt - 1] = w; 1165 timers [timercnt - 1] = w;
1016 upheap ((WT *)timers, timercnt - 1); 1166 upheap ((WT *)timers, timercnt - 1);
1167
1168 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1017} 1169}
1018 1170
1019void 1171void
1020ev_timer_stop (EV_P_ struct ev_timer *w) 1172ev_timer_stop (EV_P_ struct ev_timer *w)
1021{ 1173{
1022 ev_clear_pending (EV_A_ (W)w); 1174 ev_clear_pending (EV_A_ (W)w);
1023 if (!ev_is_active (w)) 1175 if (!ev_is_active (w))
1024 return; 1176 return;
1025 1177
1178 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1179
1026 if (w->active < timercnt--) 1180 if (((W)w)->active < timercnt--)
1027 { 1181 {
1028 timers [w->active - 1] = timers [timercnt]; 1182 timers [((W)w)->active - 1] = timers [timercnt];
1029 downheap ((WT *)timers, timercnt, w->active - 1); 1183 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1030 } 1184 }
1031 1185
1032 w->at = w->repeat; 1186 ((WT)w)->at = w->repeat;
1033 1187
1034 ev_stop (EV_A_ (W)w); 1188 ev_stop (EV_A_ (W)w);
1035} 1189}
1036 1190
1037void 1191void
1039{ 1193{
1040 if (ev_is_active (w)) 1194 if (ev_is_active (w))
1041 { 1195 {
1042 if (w->repeat) 1196 if (w->repeat)
1043 { 1197 {
1044 w->at = mn_now + w->repeat; 1198 ((WT)w)->at = mn_now + w->repeat;
1045 downheap ((WT *)timers, timercnt, w->active - 1); 1199 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1046 } 1200 }
1047 else 1201 else
1048 ev_timer_stop (EV_A_ w); 1202 ev_timer_stop (EV_A_ w);
1049 } 1203 }
1050 else if (w->repeat) 1204 else if (w->repeat)
1059 1213
1060 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); 1214 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
1061 1215
1062 /* this formula differs from the one in periodic_reify because we do not always round up */ 1216 /* this formula differs from the one in periodic_reify because we do not always round up */
1063 if (w->interval) 1217 if (w->interval)
1064 w->at += ceil ((rt_now - w->at) / w->interval) * w->interval; 1218 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval;
1065 1219
1066 ev_start (EV_A_ (W)w, ++periodiccnt); 1220 ev_start (EV_A_ (W)w, ++periodiccnt);
1067 array_needsize (periodics, periodicmax, periodiccnt, ); 1221 array_needsize (periodics, periodicmax, periodiccnt, );
1068 periodics [periodiccnt - 1] = w; 1222 periodics [periodiccnt - 1] = w;
1069 upheap ((WT *)periodics, periodiccnt - 1); 1223 upheap ((WT *)periodics, periodiccnt - 1);
1224
1225 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1070} 1226}
1071 1227
1072void 1228void
1073ev_periodic_stop (EV_P_ struct ev_periodic *w) 1229ev_periodic_stop (EV_P_ struct ev_periodic *w)
1074{ 1230{
1075 ev_clear_pending (EV_A_ (W)w); 1231 ev_clear_pending (EV_A_ (W)w);
1076 if (!ev_is_active (w)) 1232 if (!ev_is_active (w))
1077 return; 1233 return;
1078 1234
1235 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1236
1079 if (w->active < periodiccnt--) 1237 if (((W)w)->active < periodiccnt--)
1080 { 1238 {
1081 periodics [w->active - 1] = periodics [periodiccnt]; 1239 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1082 downheap ((WT *)periodics, periodiccnt, w->active - 1); 1240 downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1083 } 1241 }
1084 1242
1243 ev_stop (EV_A_ (W)w);
1244}
1245
1246void
1247ev_idle_start (EV_P_ struct ev_idle *w)
1248{
1249 if (ev_is_active (w))
1250 return;
1251
1252 ev_start (EV_A_ (W)w, ++idlecnt);
1253 array_needsize (idles, idlemax, idlecnt, );
1254 idles [idlecnt - 1] = w;
1255}
1256
1257void
1258ev_idle_stop (EV_P_ struct ev_idle *w)
1259{
1260 ev_clear_pending (EV_A_ (W)w);
1261 if (ev_is_active (w))
1262 return;
1263
1264 idles [((W)w)->active - 1] = idles [--idlecnt];
1265 ev_stop (EV_A_ (W)w);
1266}
1267
1268void
1269ev_prepare_start (EV_P_ struct ev_prepare *w)
1270{
1271 if (ev_is_active (w))
1272 return;
1273
1274 ev_start (EV_A_ (W)w, ++preparecnt);
1275 array_needsize (prepares, preparemax, preparecnt, );
1276 prepares [preparecnt - 1] = w;
1277}
1278
1279void
1280ev_prepare_stop (EV_P_ struct ev_prepare *w)
1281{
1282 ev_clear_pending (EV_A_ (W)w);
1283 if (ev_is_active (w))
1284 return;
1285
1286 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1287 ev_stop (EV_A_ (W)w);
1288}
1289
1290void
1291ev_check_start (EV_P_ struct ev_check *w)
1292{
1293 if (ev_is_active (w))
1294 return;
1295
1296 ev_start (EV_A_ (W)w, ++checkcnt);
1297 array_needsize (checks, checkmax, checkcnt, );
1298 checks [checkcnt - 1] = w;
1299}
1300
1301void
1302ev_check_stop (EV_P_ struct ev_check *w)
1303{
1304 ev_clear_pending (EV_A_ (W)w);
1305 if (ev_is_active (w))
1306 return;
1307
1308 checks [((W)w)->active - 1] = checks [--checkcnt];
1085 ev_stop (EV_A_ (W)w); 1309 ev_stop (EV_A_ (W)w);
1086} 1310}
1087 1311
1088#ifndef SA_RESTART 1312#ifndef SA_RESTART
1089# define SA_RESTART 0 1313# define SA_RESTART 0
1090#endif 1314#endif
1091 1315
1092void 1316void
1093ev_signal_start (EV_P_ struct ev_signal *w) 1317ev_signal_start (EV_P_ struct ev_signal *w)
1094{ 1318{
1319#if EV_MULTIPLICITY
1320 assert (("signal watchers are only supported in the default loop", loop == default_loop));
1321#endif
1095 if (ev_is_active (w)) 1322 if (ev_is_active (w))
1096 return; 1323 return;
1097 1324
1098 assert (("ev_signal_start called with illegal signal number", w->signum > 0)); 1325 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
1099 1326
1100 ev_start (EV_A_ (W)w, 1); 1327 ev_start (EV_A_ (W)w, 1);
1101 array_needsize (signals, signalmax, w->signum, signals_init); 1328 array_needsize (signals, signalmax, w->signum, signals_init);
1102 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1329 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1103 1330
1104 if (!w->next) 1331 if (!((WL)w)->next)
1105 { 1332 {
1333#if WIN32
1334 signal (w->signum, sighandler);
1335#else
1106 struct sigaction sa; 1336 struct sigaction sa;
1107 sa.sa_handler = sighandler; 1337 sa.sa_handler = sighandler;
1108 sigfillset (&sa.sa_mask); 1338 sigfillset (&sa.sa_mask);
1109 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ 1339 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
1110 sigaction (w->signum, &sa, 0); 1340 sigaction (w->signum, &sa, 0);
1341#endif
1111 } 1342 }
1112} 1343}
1113 1344
1114void 1345void
1115ev_signal_stop (EV_P_ struct ev_signal *w) 1346ev_signal_stop (EV_P_ struct ev_signal *w)
1124 if (!signals [w->signum - 1].head) 1355 if (!signals [w->signum - 1].head)
1125 signal (w->signum, SIG_DFL); 1356 signal (w->signum, SIG_DFL);
1126} 1357}
1127 1358
1128void 1359void
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) 1360ev_child_start (EV_P_ struct ev_child *w)
1196{ 1361{
1362#if EV_MULTIPLICITY
1363 assert (("child watchers are only supported in the default loop", loop == default_loop));
1364#endif
1197 if (ev_is_active (w)) 1365 if (ev_is_active (w))
1198 return; 1366 return;
1199 1367
1200 ev_start (EV_A_ (W)w, 1); 1368 ev_start (EV_A_ (W)w, 1);
1201 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1369 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1273 ev_timer_start (EV_A_ &once->to); 1441 ev_timer_start (EV_A_ &once->to);
1274 } 1442 }
1275 } 1443 }
1276} 1444}
1277 1445
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