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