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

Comparing libev/ev.c (file contents):
Revision 1.126 by root, Sun Nov 18 01:25:23 2007 UTC vs.
Revision 1.137 by root, Sat Nov 24 08:28:10 2007 UTC

32#ifdef __cplusplus 32#ifdef __cplusplus
33extern "C" { 33extern "C" {
34#endif 34#endif
35 35
36#ifndef EV_STANDALONE 36#ifndef EV_STANDALONE
37# ifdef EV_CONFIG_H
38# include EV_CONFIG_H
39# else
37# include "config.h" 40# include "config.h"
41# endif
38 42
39# if HAVE_CLOCK_GETTIME 43# if HAVE_CLOCK_GETTIME
40# ifndef EV_USE_MONOTONIC 44# ifndef EV_USE_MONOTONIC
41# define EV_USE_MONOTONIC 1 45# define EV_USE_MONOTONIC 1
42# endif 46# endif
50# ifndef EV_USE_REALTIME 54# ifndef EV_USE_REALTIME
51# define EV_USE_REALTIME 0 55# define EV_USE_REALTIME 0
52# endif 56# endif
53# endif 57# endif
54 58
55# if HAVE_SELECT && HAVE_SYS_SELECT_H && !defined (EV_USE_SELECT) 59# ifndef EV_USE_SELECT
60# if HAVE_SELECT && HAVE_SYS_SELECT_H
56# define EV_USE_SELECT 1 61# define EV_USE_SELECT 1
57# else 62# else
58# define EV_USE_SELECT 0 63# define EV_USE_SELECT 0
64# endif
59# endif 65# endif
60 66
61# if HAVE_POLL && HAVE_POLL_H && !defined (EV_USE_POLL) 67# ifndef EV_USE_POLL
68# if HAVE_POLL && HAVE_POLL_H
62# define EV_USE_POLL 1 69# define EV_USE_POLL 1
63# else 70# else
64# define EV_USE_POLL 0 71# define EV_USE_POLL 0
72# endif
65# endif 73# endif
66 74
67# if HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H && !defined (EV_USE_EPOLL) 75# ifndef EV_USE_EPOLL
76# if HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H
68# define EV_USE_EPOLL 1 77# define EV_USE_EPOLL 1
69# else 78# else
70# define EV_USE_EPOLL 0 79# define EV_USE_EPOLL 0
80# endif
71# endif 81# endif
72 82
83# ifndef EV_USE_KQUEUE
73# if HAVE_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H && !defined (EV_USE_KQUEUE) 84# if HAVE_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H
74# define EV_USE_KQUEUE 1 85# define EV_USE_KQUEUE 1
75# else 86# else
76# define EV_USE_KQUEUE 0 87# define EV_USE_KQUEUE 0
88# endif
77# endif 89# endif
78 90
79# if HAVE_PORT_H && HAVE_PORT_CREATE && !defined (EV_USE_PORT) 91# ifndef EV_USE_PORT
92# if HAVE_PORT_H && HAVE_PORT_CREATE
80# define EV_USE_PORT 1 93# define EV_USE_PORT 1
81# else 94# else
82# define EV_USE_PORT 0 95# define EV_USE_PORT 0
96# endif
83# endif 97# endif
84 98
85#endif 99#endif
86 100
87#include <math.h> 101#include <math.h>
144# define EV_USE_PORT 0 158# define EV_USE_PORT 0
145#endif 159#endif
146 160
147/**/ 161/**/
148 162
149/* darwin simply cannot be helped */
150#ifdef __APPLE__
151# undef EV_USE_POLL
152# undef EV_USE_KQUEUE
153#endif
154
155#ifndef CLOCK_MONOTONIC 163#ifndef CLOCK_MONOTONIC
156# undef EV_USE_MONOTONIC 164# undef EV_USE_MONOTONIC
157# define EV_USE_MONOTONIC 0 165# define EV_USE_MONOTONIC 0
158#endif 166#endif
159 167
194#define ABSPRI(w) ((w)->priority - EV_MINPRI) 202#define ABSPRI(w) ((w)->priority - EV_MINPRI)
195 203
196#define EMPTY0 /* required for microsofts broken pseudo-c compiler */ 204#define EMPTY0 /* required for microsofts broken pseudo-c compiler */
197#define EMPTY2(a,b) /* used to suppress some warnings */ 205#define EMPTY2(a,b) /* used to suppress some warnings */
198 206
199typedef struct ev_watcher *W; 207typedef ev_watcher *W;
200typedef struct ev_watcher_list *WL; 208typedef ev_watcher_list *WL;
201typedef struct ev_watcher_time *WT; 209typedef ev_watcher_time *WT;
202 210
203static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 211static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
204 212
205#ifdef _WIN32 213#ifdef _WIN32
206# include "ev_win32.c" 214# include "ev_win32.c"
408 416
409inline void 417inline void
410fd_event (EV_P_ int fd, int revents) 418fd_event (EV_P_ int fd, int revents)
411{ 419{
412 ANFD *anfd = anfds + fd; 420 ANFD *anfd = anfds + fd;
413 struct ev_io *w; 421 ev_io *w;
414 422
415 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) 423 for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next)
416 { 424 {
417 int ev = w->events & revents; 425 int ev = w->events & revents;
418 426
419 if (ev) 427 if (ev)
420 ev_feed_event (EV_A_ (W)w, ev); 428 ev_feed_event (EV_A_ (W)w, ev);
436 444
437 for (i = 0; i < fdchangecnt; ++i) 445 for (i = 0; i < fdchangecnt; ++i)
438 { 446 {
439 int fd = fdchanges [i]; 447 int fd = fdchanges [i];
440 ANFD *anfd = anfds + fd; 448 ANFD *anfd = anfds + fd;
441 struct ev_io *w; 449 ev_io *w;
442 450
443 int events = 0; 451 int events = 0;
444 452
445 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) 453 for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next)
446 events |= w->events; 454 events |= w->events;
447 455
448#if EV_SELECT_IS_WINSOCKET 456#if EV_SELECT_IS_WINSOCKET
449 if (events) 457 if (events)
450 { 458 {
454 } 462 }
455#endif 463#endif
456 464
457 anfd->reify = 0; 465 anfd->reify = 0;
458 466
459 method_modify (EV_A_ fd, anfd->events, events); 467 backend_modify (EV_A_ fd, anfd->events, events);
460 anfd->events = events; 468 anfd->events = events;
461 } 469 }
462 470
463 fdchangecnt = 0; 471 fdchangecnt = 0;
464} 472}
477} 485}
478 486
479static void 487static void
480fd_kill (EV_P_ int fd) 488fd_kill (EV_P_ int fd)
481{ 489{
482 struct ev_io *w; 490 ev_io *w;
483 491
484 while ((w = (struct ev_io *)anfds [fd].head)) 492 while ((w = (ev_io *)anfds [fd].head))
485 { 493 {
486 ev_io_stop (EV_A_ w); 494 ev_io_stop (EV_A_ w);
487 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); 495 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
488 } 496 }
489} 497}
522 fd_kill (EV_A_ fd); 530 fd_kill (EV_A_ fd);
523 return; 531 return;
524 } 532 }
525} 533}
526 534
527/* usually called after fork if method needs to re-arm all fds from scratch */ 535/* usually called after fork if backend needs to re-arm all fds from scratch */
528static void 536static void
529fd_rearm_all (EV_P) 537fd_rearm_all (EV_P)
530{ 538{
531 int fd; 539 int fd;
532 540
600static ANSIG *signals; 608static ANSIG *signals;
601static int signalmax; 609static int signalmax;
602 610
603static int sigpipe [2]; 611static int sigpipe [2];
604static sig_atomic_t volatile gotsig; 612static sig_atomic_t volatile gotsig;
605static struct ev_io sigev; 613static ev_io sigev;
606 614
607static void 615static void
608signals_init (ANSIG *base, int count) 616signals_init (ANSIG *base, int count)
609{ 617{
610 while (count--) 618 while (count--)
653 for (w = signals [signum].head; w; w = w->next) 661 for (w = signals [signum].head; w; w = w->next)
654 ev_feed_event (EV_A_ (W)w, EV_SIGNAL); 662 ev_feed_event (EV_A_ (W)w, EV_SIGNAL);
655} 663}
656 664
657static void 665static void
658sigcb (EV_P_ struct ev_io *iow, int revents) 666sigcb (EV_P_ ev_io *iow, int revents)
659{ 667{
660 int signum; 668 int signum;
661 669
662 read (sigpipe [0], &revents, 1); 670 read (sigpipe [0], &revents, 1);
663 gotsig = 0; 671 gotsig = 0;
690 ev_unref (EV_A); /* child watcher should not keep loop alive */ 698 ev_unref (EV_A); /* child watcher should not keep loop alive */
691} 699}
692 700
693/*****************************************************************************/ 701/*****************************************************************************/
694 702
695static struct ev_child *childs [PID_HASHSIZE]; 703static ev_child *childs [PID_HASHSIZE];
696 704
697#ifndef _WIN32 705#ifndef _WIN32
698 706
699static struct ev_signal childev; 707static ev_signal childev;
700 708
701#ifndef WCONTINUED 709#ifndef WCONTINUED
702# define WCONTINUED 0 710# define WCONTINUED 0
703#endif 711#endif
704 712
705static void 713static void
706child_reap (EV_P_ struct ev_signal *sw, int chain, int pid, int status) 714child_reap (EV_P_ ev_signal *sw, int chain, int pid, int status)
707{ 715{
708 struct ev_child *w; 716 ev_child *w;
709 717
710 for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) 718 for (w = (ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next)
711 if (w->pid == pid || !w->pid) 719 if (w->pid == pid || !w->pid)
712 { 720 {
713 ev_priority (w) = ev_priority (sw); /* need to do it *now* */ 721 ev_priority (w) = ev_priority (sw); /* need to do it *now* */
714 w->rpid = pid; 722 w->rpid = pid;
715 w->rstatus = status; 723 w->rstatus = status;
716 ev_feed_event (EV_A_ (W)w, EV_CHILD); 724 ev_feed_event (EV_A_ (W)w, EV_CHILD);
717 } 725 }
718} 726}
719 727
720static void 728static void
721childcb (EV_P_ struct ev_signal *sw, int revents) 729childcb (EV_P_ ev_signal *sw, int revents)
722{ 730{
723 int pid, status; 731 int pid, status;
724 732
725 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) 733 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)))
726 { 734 {
727 /* make sure we are called again until all childs have been reaped */ 735 /* make sure we are called again until all childs have been reaped */
736 /* we need to do it this way so that the callback gets called before we continue */
728 ev_feed_event (EV_A_ (W)sw, EV_SIGNAL); 737 ev_feed_event (EV_A_ (W)sw, EV_SIGNAL);
729 738
730 child_reap (EV_A_ sw, pid, pid, status); 739 child_reap (EV_A_ sw, pid, pid, status);
731 child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ 740 child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but feed_event catches that */
732 } 741 }
733} 742}
734 743
735#endif 744#endif
736 745
775 || getgid () != getegid (); 784 || getgid () != getegid ();
776#endif 785#endif
777} 786}
778 787
779unsigned int 788unsigned int
780ev_method (EV_P) 789ev_supported_backends (void)
781{ 790{
782 return method; 791 unsigned int flags = 0;
792
793 if (EV_USE_PORT ) flags |= EVBACKEND_PORT;
794 if (EV_USE_KQUEUE) flags |= EVBACKEND_KQUEUE;
795 if (EV_USE_EPOLL ) flags |= EVBACKEND_EPOLL;
796 if (EV_USE_POLL ) flags |= EVBACKEND_POLL;
797 if (EV_USE_SELECT) flags |= EVBACKEND_SELECT;
798
799 return flags;
800}
801
802unsigned int
803ev_recommended_backends (void)
804{
805 unsigned int flags = ev_supported_backends ();
806
807#ifndef __NetBSD__
808 /* kqueue is borked on everything but netbsd apparently */
809 /* it usually doesn't work correctly on anything but sockets and pipes */
810 flags &= ~EVBACKEND_KQUEUE;
811#endif
812#ifdef __APPLE__
813 // flags &= ~EVBACKEND_KQUEUE; for documentation
814 flags &= ~EVBACKEND_POLL;
815#endif
816
817 return flags;
818}
819
820unsigned int
821ev_embeddable_backends (void)
822{
823 return EVBACKEND_EPOLL
824 | EVBACKEND_KQUEUE
825 | EVBACKEND_PORT;
826}
827
828unsigned int
829ev_backend (EV_P)
830{
831 return backend;
783} 832}
784 833
785static void 834static void
786loop_init (EV_P_ unsigned int flags) 835loop_init (EV_P_ unsigned int flags)
787{ 836{
788 if (!method) 837 if (!backend)
789 { 838 {
790#if EV_USE_MONOTONIC 839#if EV_USE_MONOTONIC
791 { 840 {
792 struct timespec ts; 841 struct timespec ts;
793 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 842 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
798 ev_rt_now = ev_time (); 847 ev_rt_now = ev_time ();
799 mn_now = get_clock (); 848 mn_now = get_clock ();
800 now_floor = mn_now; 849 now_floor = mn_now;
801 rtmn_diff = ev_rt_now - mn_now; 850 rtmn_diff = ev_rt_now - mn_now;
802 851
803 if (!(flags & EVFLAG_NOENV) && !enable_secure () && getenv ("LIBEV_FLAGS")) 852 if (!(flags & EVFLAG_NOENV)
853 && !enable_secure ()
854 && getenv ("LIBEV_FLAGS"))
804 flags = atoi (getenv ("LIBEV_FLAGS")); 855 flags = atoi (getenv ("LIBEV_FLAGS"));
805 856
806 if (!(flags & 0x0000ffff)) 857 if (!(flags & 0x0000ffffUL))
807 flags |= 0x0000ffff; 858 flags |= ev_recommended_backends ();
808 859
809 method = 0; 860 backend = 0;
810#if EV_USE_PORT 861#if EV_USE_PORT
811 if (!method && (flags & EVMETHOD_PORT )) method = port_init (EV_A_ flags); 862 if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags);
812#endif 863#endif
813#if EV_USE_KQUEUE 864#if EV_USE_KQUEUE
814 if (!method && (flags & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ flags); 865 if (!backend && (flags & EVBACKEND_KQUEUE)) backend = kqueue_init (EV_A_ flags);
815#endif 866#endif
816#if EV_USE_EPOLL 867#if EV_USE_EPOLL
817 if (!method && (flags & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ flags); 868 if (!backend && (flags & EVBACKEND_EPOLL )) backend = epoll_init (EV_A_ flags);
818#endif 869#endif
819#if EV_USE_POLL 870#if EV_USE_POLL
820 if (!method && (flags & EVMETHOD_POLL )) method = poll_init (EV_A_ flags); 871 if (!backend && (flags & EVBACKEND_POLL )) backend = poll_init (EV_A_ flags);
821#endif 872#endif
822#if EV_USE_SELECT 873#if EV_USE_SELECT
823 if (!method && (flags & EVMETHOD_SELECT)) method = select_init (EV_A_ flags); 874 if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags);
824#endif 875#endif
825 876
826 ev_init (&sigev, sigcb); 877 ev_init (&sigev, sigcb);
827 ev_set_priority (&sigev, EV_MAXPRI); 878 ev_set_priority (&sigev, EV_MAXPRI);
828 } 879 }
832loop_destroy (EV_P) 883loop_destroy (EV_P)
833{ 884{
834 int i; 885 int i;
835 886
836#if EV_USE_PORT 887#if EV_USE_PORT
837 if (method == EVMETHOD_PORT ) port_destroy (EV_A); 888 if (backend == EVBACKEND_PORT ) port_destroy (EV_A);
838#endif 889#endif
839#if EV_USE_KQUEUE 890#if EV_USE_KQUEUE
840 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); 891 if (backend == EVBACKEND_KQUEUE) kqueue_destroy (EV_A);
841#endif 892#endif
842#if EV_USE_EPOLL 893#if EV_USE_EPOLL
843 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); 894 if (backend == EVBACKEND_EPOLL ) epoll_destroy (EV_A);
844#endif 895#endif
845#if EV_USE_POLL 896#if EV_USE_POLL
846 if (method == EVMETHOD_POLL ) poll_destroy (EV_A); 897 if (backend == EVBACKEND_POLL ) poll_destroy (EV_A);
847#endif 898#endif
848#if EV_USE_SELECT 899#if EV_USE_SELECT
849 if (method == EVMETHOD_SELECT) select_destroy (EV_A); 900 if (backend == EVBACKEND_SELECT) select_destroy (EV_A);
850#endif 901#endif
851 902
852 for (i = NUMPRI; i--; ) 903 for (i = NUMPRI; i--; )
853 array_free (pending, [i]); 904 array_free (pending, [i]);
854 905
860#endif 911#endif
861 array_free (idle, EMPTY0); 912 array_free (idle, EMPTY0);
862 array_free (prepare, EMPTY0); 913 array_free (prepare, EMPTY0);
863 array_free (check, EMPTY0); 914 array_free (check, EMPTY0);
864 915
865 method = 0; 916 backend = 0;
866} 917}
867 918
868static void 919static void
869loop_fork (EV_P) 920loop_fork (EV_P)
870{ 921{
871#if EV_USE_PORT 922#if EV_USE_PORT
872 if (method == EVMETHOD_PORT ) port_fork (EV_A); 923 if (backend == EVBACKEND_PORT ) port_fork (EV_A);
873#endif 924#endif
874#if EV_USE_KQUEUE 925#if EV_USE_KQUEUE
875 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A); 926 if (backend == EVBACKEND_KQUEUE) kqueue_fork (EV_A);
876#endif 927#endif
877#if EV_USE_EPOLL 928#if EV_USE_EPOLL
878 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A); 929 if (backend == EVBACKEND_EPOLL ) epoll_fork (EV_A);
879#endif 930#endif
880 931
881 if (ev_is_active (&sigev)) 932 if (ev_is_active (&sigev))
882 { 933 {
883 /* default loop */ 934 /* default loop */
904 955
905 memset (loop, 0, sizeof (struct ev_loop)); 956 memset (loop, 0, sizeof (struct ev_loop));
906 957
907 loop_init (EV_A_ flags); 958 loop_init (EV_A_ flags);
908 959
909 if (ev_method (EV_A)) 960 if (ev_backend (EV_A))
910 return loop; 961 return loop;
911 962
912 return 0; 963 return 0;
913} 964}
914 965
947 ev_default_loop_ptr = 1; 998 ev_default_loop_ptr = 1;
948#endif 999#endif
949 1000
950 loop_init (EV_A_ flags); 1001 loop_init (EV_A_ flags);
951 1002
952 if (ev_method (EV_A)) 1003 if (ev_backend (EV_A))
953 { 1004 {
954 siginit (EV_A); 1005 siginit (EV_A);
955 1006
956#ifndef _WIN32 1007#ifndef _WIN32
957 ev_signal_init (&childev, childcb, SIGCHLD); 1008 ev_signal_init (&childev, childcb, SIGCHLD);
993{ 1044{
994#if EV_MULTIPLICITY 1045#if EV_MULTIPLICITY
995 struct ev_loop *loop = ev_default_loop_ptr; 1046 struct ev_loop *loop = ev_default_loop_ptr;
996#endif 1047#endif
997 1048
998 if (method) 1049 if (backend)
999 postfork = 1; 1050 postfork = 1;
1000} 1051}
1001 1052
1002/*****************************************************************************/ 1053/*****************************************************************************/
1003 1054
1034inline void 1085inline void
1035timers_reify (EV_P) 1086timers_reify (EV_P)
1036{ 1087{
1037 while (timercnt && ((WT)timers [0])->at <= mn_now) 1088 while (timercnt && ((WT)timers [0])->at <= mn_now)
1038 { 1089 {
1039 struct ev_timer *w = timers [0]; 1090 ev_timer *w = timers [0];
1040 1091
1041 assert (("inactive timer on timer heap detected", ev_is_active (w))); 1092 assert (("inactive timer on timer heap detected", ev_is_active (w)));
1042 1093
1043 /* first reschedule or stop timer */ 1094 /* first reschedule or stop timer */
1044 if (w->repeat) 1095 if (w->repeat)
1062inline void 1113inline void
1063periodics_reify (EV_P) 1114periodics_reify (EV_P)
1064{ 1115{
1065 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) 1116 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
1066 { 1117 {
1067 struct ev_periodic *w = periodics [0]; 1118 ev_periodic *w = periodics [0];
1068 1119
1069 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 1120 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
1070 1121
1071 /* first reschedule or stop timer */ 1122 /* first reschedule or stop timer */
1072 if (w->reschedule_cb) 1123 if (w->reschedule_cb)
1094 int i; 1145 int i;
1095 1146
1096 /* adjust periodics after time jump */ 1147 /* adjust periodics after time jump */
1097 for (i = 0; i < periodiccnt; ++i) 1148 for (i = 0; i < periodiccnt; ++i)
1098 { 1149 {
1099 struct ev_periodic *w = periodics [i]; 1150 ev_periodic *w = periodics [i];
1100 1151
1101 if (w->reschedule_cb) 1152 if (w->reschedule_cb)
1102 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 1153 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1103 else if (w->interval) 1154 else if (w->interval)
1104 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; 1155 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1194static int loop_done; 1245static int loop_done;
1195 1246
1196void 1247void
1197ev_loop (EV_P_ int flags) 1248ev_loop (EV_P_ int flags)
1198{ 1249{
1199 double block;
1200 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; 1250 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)
1251 ? EVUNLOOP_ONE
1252 : EVUNLOOP_CANCEL;
1201 1253
1202 while (activecnt) 1254 while (activecnt)
1203 { 1255 {
1204 /* queue check watchers (and execute them) */ 1256 /* queue check watchers (and execute them) */
1205 if (expect_false (preparecnt)) 1257 if (expect_false (preparecnt))
1214 1266
1215 /* update fd-related kernel structures */ 1267 /* update fd-related kernel structures */
1216 fd_reify (EV_A); 1268 fd_reify (EV_A);
1217 1269
1218 /* calculate blocking time */ 1270 /* calculate blocking time */
1271 {
1272 double block;
1219 1273
1220 /* we only need this for !monotonic clock or timers, but as we basically 1274 if (flags & EVLOOP_NONBLOCK || idlecnt)
1221 always have timers, we just calculate it always */ 1275 block = 0.; /* do not block at all */
1276 else
1277 {
1278 /* update time to cancel out callback processing overhead */
1222#if EV_USE_MONOTONIC 1279#if EV_USE_MONOTONIC
1223 if (expect_true (have_monotonic)) 1280 if (expect_true (have_monotonic))
1224 time_update_monotonic (EV_A); 1281 time_update_monotonic (EV_A);
1225 else 1282 else
1226#endif 1283#endif
1227 { 1284 {
1228 ev_rt_now = ev_time (); 1285 ev_rt_now = ev_time ();
1229 mn_now = ev_rt_now; 1286 mn_now = ev_rt_now;
1230 } 1287 }
1231 1288
1232 if (flags & EVLOOP_NONBLOCK || idlecnt)
1233 block = 0.;
1234 else
1235 {
1236 block = MAX_BLOCKTIME; 1289 block = MAX_BLOCKTIME;
1237 1290
1238 if (timercnt) 1291 if (timercnt)
1239 { 1292 {
1240 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; 1293 ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge;
1241 if (block > to) block = to; 1294 if (block > to) block = to;
1242 } 1295 }
1243 1296
1244#if EV_PERIODICS 1297#if EV_PERIODICS
1245 if (periodiccnt) 1298 if (periodiccnt)
1246 { 1299 {
1247 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge; 1300 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + backend_fudge;
1248 if (block > to) block = to; 1301 if (block > to) block = to;
1249 } 1302 }
1250#endif 1303#endif
1251 1304
1252 if (expect_false (block < 0.)) block = 0.; 1305 if (expect_false (block < 0.)) block = 0.;
1253 } 1306 }
1254 1307
1255 method_poll (EV_A_ block); 1308 backend_poll (EV_A_ block);
1309 }
1256 1310
1257 /* update ev_rt_now, do magic */ 1311 /* update ev_rt_now, do magic */
1258 time_update (EV_A); 1312 time_update (EV_A);
1259 1313
1260 /* queue pending timers and reschedule them */ 1314 /* queue pending timers and reschedule them */
1261 timers_reify (EV_A); /* relative timers called last */ 1315 timers_reify (EV_A); /* relative timers called last */
1262#if EV_PERIODICS 1316#if EV_PERIODICS
1263 periodics_reify (EV_A); /* absolute timers called first */ 1317 periodics_reify (EV_A); /* absolute timers called first */
1264#endif 1318#endif
1265 1319
1266 /* queue idle watchers unless io or timers are pending */ 1320 /* queue idle watchers unless other events are pending */
1267 if (idlecnt && !any_pending (EV_A)) 1321 if (idlecnt && !any_pending (EV_A))
1268 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1322 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
1269 1323
1270 /* queue check watchers, to be executed first */ 1324 /* queue check watchers, to be executed first */
1271 if (expect_false (checkcnt)) 1325 if (expect_false (checkcnt))
1275 1329
1276 if (expect_false (loop_done)) 1330 if (expect_false (loop_done))
1277 break; 1331 break;
1278 } 1332 }
1279 1333
1280 if (loop_done != 2) 1334 if (loop_done == EVUNLOOP_ONE)
1281 loop_done = 0; 1335 loop_done = EVUNLOOP_CANCEL;
1282} 1336}
1283 1337
1284void 1338void
1285ev_unloop (EV_P_ int how) 1339ev_unloop (EV_P_ int how)
1286{ 1340{
1339} 1393}
1340 1394
1341/*****************************************************************************/ 1395/*****************************************************************************/
1342 1396
1343void 1397void
1344ev_io_start (EV_P_ struct ev_io *w) 1398ev_io_start (EV_P_ ev_io *w)
1345{ 1399{
1346 int fd = w->fd; 1400 int fd = w->fd;
1347 1401
1348 if (expect_false (ev_is_active (w))) 1402 if (expect_false (ev_is_active (w)))
1349 return; 1403 return;
1356 1410
1357 fd_change (EV_A_ fd); 1411 fd_change (EV_A_ fd);
1358} 1412}
1359 1413
1360void 1414void
1361ev_io_stop (EV_P_ struct ev_io *w) 1415ev_io_stop (EV_P_ ev_io *w)
1362{ 1416{
1363 ev_clear_pending (EV_A_ (W)w); 1417 ev_clear_pending (EV_A_ (W)w);
1364 if (expect_false (!ev_is_active (w))) 1418 if (expect_false (!ev_is_active (w)))
1365 return; 1419 return;
1366 1420
1371 1425
1372 fd_change (EV_A_ w->fd); 1426 fd_change (EV_A_ w->fd);
1373} 1427}
1374 1428
1375void 1429void
1376ev_timer_start (EV_P_ struct ev_timer *w) 1430ev_timer_start (EV_P_ ev_timer *w)
1377{ 1431{
1378 if (expect_false (ev_is_active (w))) 1432 if (expect_false (ev_is_active (w)))
1379 return; 1433 return;
1380 1434
1381 ((WT)w)->at += mn_now; 1435 ((WT)w)->at += mn_now;
1382 1436
1383 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1437 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1384 1438
1385 ev_start (EV_A_ (W)w, ++timercnt); 1439 ev_start (EV_A_ (W)w, ++timercnt);
1386 array_needsize (struct ev_timer *, timers, timermax, timercnt, EMPTY2); 1440 array_needsize (ev_timer *, timers, timermax, timercnt, EMPTY2);
1387 timers [timercnt - 1] = w; 1441 timers [timercnt - 1] = w;
1388 upheap ((WT *)timers, timercnt - 1); 1442 upheap ((WT *)timers, timercnt - 1);
1389 1443
1390 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1444 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1391} 1445}
1392 1446
1393void 1447void
1394ev_timer_stop (EV_P_ struct ev_timer *w) 1448ev_timer_stop (EV_P_ ev_timer *w)
1395{ 1449{
1396 ev_clear_pending (EV_A_ (W)w); 1450 ev_clear_pending (EV_A_ (W)w);
1397 if (expect_false (!ev_is_active (w))) 1451 if (expect_false (!ev_is_active (w)))
1398 return; 1452 return;
1399 1453
1409 1463
1410 ev_stop (EV_A_ (W)w); 1464 ev_stop (EV_A_ (W)w);
1411} 1465}
1412 1466
1413void 1467void
1414ev_timer_again (EV_P_ struct ev_timer *w) 1468ev_timer_again (EV_P_ ev_timer *w)
1415{ 1469{
1416 if (ev_is_active (w)) 1470 if (ev_is_active (w))
1417 { 1471 {
1418 if (w->repeat) 1472 if (w->repeat)
1419 { 1473 {
1430 } 1484 }
1431} 1485}
1432 1486
1433#if EV_PERIODICS 1487#if EV_PERIODICS
1434void 1488void
1435ev_periodic_start (EV_P_ struct ev_periodic *w) 1489ev_periodic_start (EV_P_ ev_periodic *w)
1436{ 1490{
1437 if (expect_false (ev_is_active (w))) 1491 if (expect_false (ev_is_active (w)))
1438 return; 1492 return;
1439 1493
1440 if (w->reschedule_cb) 1494 if (w->reschedule_cb)
1445 /* this formula differs from the one in periodic_reify because we do not always round up */ 1499 /* this formula differs from the one in periodic_reify because we do not always round up */
1446 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; 1500 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1447 } 1501 }
1448 1502
1449 ev_start (EV_A_ (W)w, ++periodiccnt); 1503 ev_start (EV_A_ (W)w, ++periodiccnt);
1450 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); 1504 array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2);
1451 periodics [periodiccnt - 1] = w; 1505 periodics [periodiccnt - 1] = w;
1452 upheap ((WT *)periodics, periodiccnt - 1); 1506 upheap ((WT *)periodics, periodiccnt - 1);
1453 1507
1454 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1508 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1455} 1509}
1456 1510
1457void 1511void
1458ev_periodic_stop (EV_P_ struct ev_periodic *w) 1512ev_periodic_stop (EV_P_ ev_periodic *w)
1459{ 1513{
1460 ev_clear_pending (EV_A_ (W)w); 1514 ev_clear_pending (EV_A_ (W)w);
1461 if (expect_false (!ev_is_active (w))) 1515 if (expect_false (!ev_is_active (w)))
1462 return; 1516 return;
1463 1517
1471 1525
1472 ev_stop (EV_A_ (W)w); 1526 ev_stop (EV_A_ (W)w);
1473} 1527}
1474 1528
1475void 1529void
1476ev_periodic_again (EV_P_ struct ev_periodic *w) 1530ev_periodic_again (EV_P_ ev_periodic *w)
1477{ 1531{
1478 /* TODO: use adjustheap and recalculation */ 1532 /* TODO: use adjustheap and recalculation */
1479 ev_periodic_stop (EV_A_ w); 1533 ev_periodic_stop (EV_A_ w);
1480 ev_periodic_start (EV_A_ w); 1534 ev_periodic_start (EV_A_ w);
1481} 1535}
1482#endif 1536#endif
1483 1537
1484void 1538void
1485ev_idle_start (EV_P_ struct ev_idle *w) 1539ev_idle_start (EV_P_ ev_idle *w)
1486{ 1540{
1487 if (expect_false (ev_is_active (w))) 1541 if (expect_false (ev_is_active (w)))
1488 return; 1542 return;
1489 1543
1490 ev_start (EV_A_ (W)w, ++idlecnt); 1544 ev_start (EV_A_ (W)w, ++idlecnt);
1491 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, EMPTY2); 1545 array_needsize (ev_idle *, idles, idlemax, idlecnt, EMPTY2);
1492 idles [idlecnt - 1] = w; 1546 idles [idlecnt - 1] = w;
1493} 1547}
1494 1548
1495void 1549void
1496ev_idle_stop (EV_P_ struct ev_idle *w) 1550ev_idle_stop (EV_P_ ev_idle *w)
1497{ 1551{
1498 ev_clear_pending (EV_A_ (W)w); 1552 ev_clear_pending (EV_A_ (W)w);
1499 if (expect_false (!ev_is_active (w))) 1553 if (expect_false (!ev_is_active (w)))
1500 return; 1554 return;
1501 1555
1502 idles [((W)w)->active - 1] = idles [--idlecnt]; 1556 idles [((W)w)->active - 1] = idles [--idlecnt];
1503 ev_stop (EV_A_ (W)w); 1557 ev_stop (EV_A_ (W)w);
1504} 1558}
1505 1559
1506void 1560void
1507ev_prepare_start (EV_P_ struct ev_prepare *w) 1561ev_prepare_start (EV_P_ ev_prepare *w)
1508{ 1562{
1509 if (expect_false (ev_is_active (w))) 1563 if (expect_false (ev_is_active (w)))
1510 return; 1564 return;
1511 1565
1512 ev_start (EV_A_ (W)w, ++preparecnt); 1566 ev_start (EV_A_ (W)w, ++preparecnt);
1513 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, EMPTY2); 1567 array_needsize (ev_prepare *, prepares, preparemax, preparecnt, EMPTY2);
1514 prepares [preparecnt - 1] = w; 1568 prepares [preparecnt - 1] = w;
1515} 1569}
1516 1570
1517void 1571void
1518ev_prepare_stop (EV_P_ struct ev_prepare *w) 1572ev_prepare_stop (EV_P_ ev_prepare *w)
1519{ 1573{
1520 ev_clear_pending (EV_A_ (W)w); 1574 ev_clear_pending (EV_A_ (W)w);
1521 if (expect_false (!ev_is_active (w))) 1575 if (expect_false (!ev_is_active (w)))
1522 return; 1576 return;
1523 1577
1524 prepares [((W)w)->active - 1] = prepares [--preparecnt]; 1578 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1525 ev_stop (EV_A_ (W)w); 1579 ev_stop (EV_A_ (W)w);
1526} 1580}
1527 1581
1528void 1582void
1529ev_check_start (EV_P_ struct ev_check *w) 1583ev_check_start (EV_P_ ev_check *w)
1530{ 1584{
1531 if (expect_false (ev_is_active (w))) 1585 if (expect_false (ev_is_active (w)))
1532 return; 1586 return;
1533 1587
1534 ev_start (EV_A_ (W)w, ++checkcnt); 1588 ev_start (EV_A_ (W)w, ++checkcnt);
1535 array_needsize (struct ev_check *, checks, checkmax, checkcnt, EMPTY2); 1589 array_needsize (ev_check *, checks, checkmax, checkcnt, EMPTY2);
1536 checks [checkcnt - 1] = w; 1590 checks [checkcnt - 1] = w;
1537} 1591}
1538 1592
1539void 1593void
1540ev_check_stop (EV_P_ struct ev_check *w) 1594ev_check_stop (EV_P_ ev_check *w)
1541{ 1595{
1542 ev_clear_pending (EV_A_ (W)w); 1596 ev_clear_pending (EV_A_ (W)w);
1543 if (expect_false (!ev_is_active (w))) 1597 if (expect_false (!ev_is_active (w)))
1544 return; 1598 return;
1545 1599
1550#ifndef SA_RESTART 1604#ifndef SA_RESTART
1551# define SA_RESTART 0 1605# define SA_RESTART 0
1552#endif 1606#endif
1553 1607
1554void 1608void
1555ev_signal_start (EV_P_ struct ev_signal *w) 1609ev_signal_start (EV_P_ ev_signal *w)
1556{ 1610{
1557#if EV_MULTIPLICITY 1611#if EV_MULTIPLICITY
1558 assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr)); 1612 assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr));
1559#endif 1613#endif
1560 if (expect_false (ev_is_active (w))) 1614 if (expect_false (ev_is_active (w)))
1579#endif 1633#endif
1580 } 1634 }
1581} 1635}
1582 1636
1583void 1637void
1584ev_signal_stop (EV_P_ struct ev_signal *w) 1638ev_signal_stop (EV_P_ ev_signal *w)
1585{ 1639{
1586 ev_clear_pending (EV_A_ (W)w); 1640 ev_clear_pending (EV_A_ (W)w);
1587 if (expect_false (!ev_is_active (w))) 1641 if (expect_false (!ev_is_active (w)))
1588 return; 1642 return;
1589 1643
1593 if (!signals [w->signum - 1].head) 1647 if (!signals [w->signum - 1].head)
1594 signal (w->signum, SIG_DFL); 1648 signal (w->signum, SIG_DFL);
1595} 1649}
1596 1650
1597void 1651void
1598ev_child_start (EV_P_ struct ev_child *w) 1652ev_child_start (EV_P_ ev_child *w)
1599{ 1653{
1600#if EV_MULTIPLICITY 1654#if EV_MULTIPLICITY
1601 assert (("child watchers are only supported in the default loop", loop == ev_default_loop_ptr)); 1655 assert (("child watchers are only supported in the default loop", loop == ev_default_loop_ptr));
1602#endif 1656#endif
1603 if (expect_false (ev_is_active (w))) 1657 if (expect_false (ev_is_active (w)))
1606 ev_start (EV_A_ (W)w, 1); 1660 ev_start (EV_A_ (W)w, 1);
1607 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1661 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1608} 1662}
1609 1663
1610void 1664void
1611ev_child_stop (EV_P_ struct ev_child *w) 1665ev_child_stop (EV_P_ ev_child *w)
1612{ 1666{
1613 ev_clear_pending (EV_A_ (W)w); 1667 ev_clear_pending (EV_A_ (W)w);
1614 if (expect_false (!ev_is_active (w))) 1668 if (expect_false (!ev_is_active (w)))
1615 return; 1669 return;
1616 1670
1617 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1671 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1618 ev_stop (EV_A_ (W)w); 1672 ev_stop (EV_A_ (W)w);
1619} 1673}
1620 1674
1675#if EV_MULTIPLICITY
1676void
1677ev_embed_loop (EV_P_ ev_embed *w)
1678{
1679 ev_loop (w->loop, EVLOOP_NONBLOCK);
1680}
1681
1682static void
1683embed_cb (EV_P_ ev_io *io, int revents)
1684{
1685 ev_embed *w = (ev_embed *)(((char *)io) - offsetof (ev_embed, io));
1686
1687 if (ev_cb (w))
1688 ev_feed_event (EV_A_ (W)w, EV_EMBED);
1689 else
1690 ev_embed_loop (loop, w);
1691}
1692
1693void
1694ev_embed_start (EV_P_ ev_embed *w)
1695{
1696 if (expect_false (ev_is_active (w)))
1697 return;
1698
1699 {
1700 struct ev_loop *loop = w->loop;
1701 assert (("loop to be embedded is not embeddable", backend & ev_embeddable_backends ()));
1702 ev_io_init (&w->io, embed_cb, backend_fd, EV_READ);
1703 }
1704
1705 ev_set_priority (&w->io, ev_priority (w));
1706 ev_io_start (EV_A_ &w->io);
1707 ev_start (EV_A_ (W)w, 1);
1708}
1709
1710void
1711ev_embed_stop (EV_P_ ev_embed *w)
1712{
1713 ev_clear_pending (EV_A_ (W)w);
1714 if (expect_false (!ev_is_active (w)))
1715 return;
1716
1717 ev_io_stop (EV_A_ &w->io);
1718 ev_stop (EV_A_ (W)w);
1719}
1720#endif
1721
1621/*****************************************************************************/ 1722/*****************************************************************************/
1622 1723
1623struct ev_once 1724struct ev_once
1624{ 1725{
1625 struct ev_io io; 1726 ev_io io;
1626 struct ev_timer to; 1727 ev_timer to;
1627 void (*cb)(int revents, void *arg); 1728 void (*cb)(int revents, void *arg);
1628 void *arg; 1729 void *arg;
1629}; 1730};
1630 1731
1631static void 1732static void
1640 1741
1641 cb (revents, arg); 1742 cb (revents, arg);
1642} 1743}
1643 1744
1644static void 1745static void
1645once_cb_io (EV_P_ struct ev_io *w, int revents) 1746once_cb_io (EV_P_ ev_io *w, int revents)
1646{ 1747{
1647 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); 1748 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents);
1648} 1749}
1649 1750
1650static void 1751static void
1651once_cb_to (EV_P_ struct ev_timer *w, int revents) 1752once_cb_to (EV_P_ ev_timer *w, int revents)
1652{ 1753{
1653 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); 1754 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents);
1654} 1755}
1655 1756
1656void 1757void

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines