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

Comparing libev/ev.c (file contents):
Revision 1.106 by root, Mon Nov 12 01:07:50 2007 UTC vs.
Revision 1.126 by root, Sun Nov 18 01:25:23 2007 UTC

41# define EV_USE_MONOTONIC 1 41# define EV_USE_MONOTONIC 1
42# endif 42# endif
43# ifndef EV_USE_REALTIME 43# ifndef EV_USE_REALTIME
44# define EV_USE_REALTIME 1 44# define EV_USE_REALTIME 1
45# endif 45# endif
46# else
47# ifndef EV_USE_MONOTONIC
48# define EV_USE_MONOTONIC 0
49# endif
50# ifndef EV_USE_REALTIME
51# define EV_USE_REALTIME 0
52# endif
46# endif 53# endif
47 54
48# if HAVE_SELECT && HAVE_SYS_SELECT_H && !defined (EV_USE_SELECT) 55# if HAVE_SELECT && HAVE_SYS_SELECT_H && !defined (EV_USE_SELECT)
49# define EV_USE_SELECT 1 56# define EV_USE_SELECT 1
57# else
58# define EV_USE_SELECT 0
50# endif 59# endif
51 60
52# if HAVE_POLL && HAVE_POLL_H && !defined (EV_USE_POLL) 61# if HAVE_POLL && HAVE_POLL_H && !defined (EV_USE_POLL)
53# define EV_USE_POLL 1 62# define EV_USE_POLL 1
63# else
64# define EV_USE_POLL 0
54# endif 65# endif
55 66
56# if HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H && !defined (EV_USE_EPOLL) 67# if HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H && !defined (EV_USE_EPOLL)
57# define EV_USE_EPOLL 1 68# define EV_USE_EPOLL 1
69# else
70# define EV_USE_EPOLL 0
58# endif 71# endif
59 72
60# if HAVE_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H && !defined (EV_USE_KQUEUE) 73# if HAVE_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H && !defined (EV_USE_KQUEUE)
61# define EV_USE_KQUEUE 1 74# define EV_USE_KQUEUE 1
75# else
76# define EV_USE_KQUEUE 0
77# endif
78
79# if HAVE_PORT_H && HAVE_PORT_CREATE && !defined (EV_USE_PORT)
80# define EV_USE_PORT 1
81# else
82# define EV_USE_PORT 0
62# endif 83# endif
63 84
64#endif 85#endif
65 86
66#include <math.h> 87#include <math.h>
90#endif 111#endif
91 112
92/**/ 113/**/
93 114
94#ifndef EV_USE_MONOTONIC 115#ifndef EV_USE_MONOTONIC
95# define EV_USE_MONOTONIC 1 116# define EV_USE_MONOTONIC 0
117#endif
118
119#ifndef EV_USE_REALTIME
120# define EV_USE_REALTIME 0
96#endif 121#endif
97 122
98#ifndef EV_USE_SELECT 123#ifndef EV_USE_SELECT
99# define EV_USE_SELECT 1 124# define EV_USE_SELECT 1
100# define EV_SELECT_USE_FD_SET 1
101#endif 125#endif
102 126
103#ifndef EV_USE_POLL 127#ifndef EV_USE_POLL
104# ifdef _WIN32 128# ifdef _WIN32
105# define EV_USE_POLL 0 129# define EV_USE_POLL 0
114 138
115#ifndef EV_USE_KQUEUE 139#ifndef EV_USE_KQUEUE
116# define EV_USE_KQUEUE 0 140# define EV_USE_KQUEUE 0
117#endif 141#endif
118 142
119#ifndef EV_USE_REALTIME 143#ifndef EV_USE_PORT
120# define EV_USE_REALTIME 1 144# define EV_USE_PORT 0
121#endif 145#endif
122 146
123/**/ 147/**/
124 148
125/* darwin simply cnanot be helped */ 149/* darwin simply cannot be helped */
126#ifdef __APPLE__ 150#ifdef __APPLE__
127# undef EV_USE_POLL 151# undef EV_USE_POLL
128# undef EV_USE_KQUEUE 152# undef EV_USE_KQUEUE
129#endif 153#endif
130 154
143#endif 167#endif
144 168
145/**/ 169/**/
146 170
147#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 171#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
148#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */ 172#define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */
149#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ 173#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */
150/*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ 174/*#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */
151 175
152#ifdef EV_H 176#ifdef EV_H
153# include EV_H 177# include EV_H
154#else 178#else
155# include "ev.h" 179# include "ev.h"
156#endif 180#endif
157 181
158#if __GNUC__ >= 3 182#if __GNUC__ >= 3
159# define expect(expr,value) __builtin_expect ((expr),(value)) 183# define expect(expr,value) __builtin_expect ((expr),(value))
160# define inline inline 184# define inline static inline
161#else 185#else
162# define expect(expr,value) (expr) 186# define expect(expr,value) (expr)
163# define inline static 187# define inline static
164#endif 188#endif
165 189
167#define expect_true(expr) expect ((expr) != 0, 1) 191#define expect_true(expr) expect ((expr) != 0, 1)
168 192
169#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) 193#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
170#define ABSPRI(w) ((w)->priority - EV_MINPRI) 194#define ABSPRI(w) ((w)->priority - EV_MINPRI)
171 195
172#define EMPTY /* required for microsofts broken pseudo-c compiler */ 196#define EMPTY0 /* required for microsofts broken pseudo-c compiler */
197#define EMPTY2(a,b) /* used to suppress some warnings */
173 198
174typedef struct ev_watcher *W; 199typedef struct ev_watcher *W;
175typedef struct ev_watcher_list *WL; 200typedef struct ev_watcher_list *WL;
176typedef struct ev_watcher_time *WT; 201typedef struct ev_watcher_time *WT;
177 202
257 #include "ev_vars.h" 282 #include "ev_vars.h"
258 #undef VAR 283 #undef VAR
259 }; 284 };
260 #include "ev_wrap.h" 285 #include "ev_wrap.h"
261 286
262 struct ev_loop default_loop_struct; 287 static struct ev_loop default_loop_struct;
263 static struct ev_loop *default_loop; 288 struct ev_loop *ev_default_loop_ptr;
264 289
265#else 290#else
266 291
267 ev_tstamp ev_rt_now; 292 ev_tstamp ev_rt_now;
268 #define VAR(name,decl) static decl; 293 #define VAR(name,decl) static decl;
269 #include "ev_vars.h" 294 #include "ev_vars.h"
270 #undef VAR 295 #undef VAR
271 296
272 static int default_loop; 297 static int ev_default_loop_ptr;
273 298
274#endif 299#endif
275 300
276/*****************************************************************************/ 301/*****************************************************************************/
277 302
310{ 335{
311 return ev_rt_now; 336 return ev_rt_now;
312} 337}
313#endif 338#endif
314 339
315#define array_roundsize(type,n) ((n) | 4 & ~3) 340#define array_roundsize(type,n) (((n) | 4) & ~3)
316 341
317#define array_needsize(type,base,cur,cnt,init) \ 342#define array_needsize(type,base,cur,cnt,init) \
318 if (expect_false ((cnt) > cur)) \ 343 if (expect_false ((cnt) > cur)) \
319 { \ 344 { \
320 int newcnt = cur; \ 345 int newcnt = cur; \
358void 383void
359ev_feed_event (EV_P_ void *w, int revents) 384ev_feed_event (EV_P_ void *w, int revents)
360{ 385{
361 W w_ = (W)w; 386 W w_ = (W)w;
362 387
363 if (w_->pending) 388 if (expect_false (w_->pending))
364 { 389 {
365 pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; 390 pendings [ABSPRI (w_)][w_->pending - 1].events |= revents;
366 return; 391 return;
367 } 392 }
368 393
369 w_->pending = ++pendingcnt [ABSPRI (w_)]; 394 w_->pending = ++pendingcnt [ABSPRI (w_)];
370 array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], (void)); 395 array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], EMPTY2);
371 pendings [ABSPRI (w_)][w_->pending - 1].w = w_; 396 pendings [ABSPRI (w_)][w_->pending - 1].w = w_;
372 pendings [ABSPRI (w_)][w_->pending - 1].events = revents; 397 pendings [ABSPRI (w_)][w_->pending - 1].events = revents;
373} 398}
374 399
375static void 400static void
402 fd_event (EV_A_ fd, revents); 427 fd_event (EV_A_ fd, revents);
403} 428}
404 429
405/*****************************************************************************/ 430/*****************************************************************************/
406 431
407static void 432inline void
408fd_reify (EV_P) 433fd_reify (EV_P)
409{ 434{
410 int i; 435 int i;
411 436
412 for (i = 0; i < fdchangecnt; ++i) 437 for (i = 0; i < fdchangecnt; ++i)
439} 464}
440 465
441static void 466static void
442fd_change (EV_P_ int fd) 467fd_change (EV_P_ int fd)
443{ 468{
444 if (anfds [fd].reify) 469 if (expect_false (anfds [fd].reify))
445 return; 470 return;
446 471
447 anfds [fd].reify = 1; 472 anfds [fd].reify = 1;
448 473
449 ++fdchangecnt; 474 ++fdchangecnt;
450 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, (void)); 475 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2);
451 fdchanges [fdchangecnt - 1] = fd; 476 fdchanges [fdchangecnt - 1] = fd;
452} 477}
453 478
454static void 479static void
455fd_kill (EV_P_ int fd) 480fd_kill (EV_P_ int fd)
461 ev_io_stop (EV_A_ w); 486 ev_io_stop (EV_A_ w);
462 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); 487 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
463 } 488 }
464} 489}
465 490
466static int 491inline int
467fd_valid (int fd) 492fd_valid (int fd)
468{ 493{
469#ifdef _WIN32 494#ifdef _WIN32
470 return _get_osfhandle (fd) != -1; 495 return _get_osfhandle (fd) != -1;
471#else 496#else
613ev_feed_signal_event (EV_P_ int signum) 638ev_feed_signal_event (EV_P_ int signum)
614{ 639{
615 WL w; 640 WL w;
616 641
617#if EV_MULTIPLICITY 642#if EV_MULTIPLICITY
618 assert (("feeding signal events is only supported in the default loop", loop == default_loop)); 643 assert (("feeding signal events is only supported in the default loop", loop == ev_default_loop_ptr));
619#endif 644#endif
620 645
621 --signum; 646 --signum;
622 647
623 if (signum < 0 || signum >= signalmax) 648 if (signum < 0 || signum >= signalmax)
640 for (signum = signalmax; signum--; ) 665 for (signum = signalmax; signum--; )
641 if (signals [signum].gotsig) 666 if (signals [signum].gotsig)
642 ev_feed_signal_event (EV_A_ signum + 1); 667 ev_feed_signal_event (EV_A_ signum + 1);
643} 668}
644 669
645inline void 670static void
646fd_intern (int fd) 671fd_intern (int fd)
647{ 672{
648#ifdef _WIN32 673#ifdef _WIN32
649 int arg = 1; 674 int arg = 1;
650 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); 675 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg);
709 734
710#endif 735#endif
711 736
712/*****************************************************************************/ 737/*****************************************************************************/
713 738
739#if EV_USE_PORT
740# include "ev_port.c"
741#endif
714#if EV_USE_KQUEUE 742#if EV_USE_KQUEUE
715# include "ev_kqueue.c" 743# include "ev_kqueue.c"
716#endif 744#endif
717#if EV_USE_EPOLL 745#if EV_USE_EPOLL
718# include "ev_epoll.c" 746# include "ev_epoll.c"
746 return getuid () != geteuid () 774 return getuid () != geteuid ()
747 || getgid () != getegid (); 775 || getgid () != getegid ();
748#endif 776#endif
749} 777}
750 778
751int 779unsigned int
752ev_method (EV_P) 780ev_method (EV_P)
753{ 781{
754 return method; 782 return method;
755} 783}
756 784
757static void 785static void
758loop_init (EV_P_ int methods) 786loop_init (EV_P_ unsigned int flags)
759{ 787{
760 if (!method) 788 if (!method)
761 { 789 {
762#if EV_USE_MONOTONIC 790#if EV_USE_MONOTONIC
763 { 791 {
770 ev_rt_now = ev_time (); 798 ev_rt_now = ev_time ();
771 mn_now = get_clock (); 799 mn_now = get_clock ();
772 now_floor = mn_now; 800 now_floor = mn_now;
773 rtmn_diff = ev_rt_now - mn_now; 801 rtmn_diff = ev_rt_now - mn_now;
774 802
775 if (methods == EVMETHOD_AUTO) 803 if (!(flags & EVFLAG_NOENV) && !enable_secure () && getenv ("LIBEV_FLAGS"))
776 if (!enable_secure () && getenv ("LIBEV_METHODS"))
777 methods = atoi (getenv ("LIBEV_METHODS")); 804 flags = atoi (getenv ("LIBEV_FLAGS"));
778 else 805
779 methods = EVMETHOD_ANY; 806 if (!(flags & 0x0000ffff))
807 flags |= 0x0000ffff;
780 808
781 method = 0; 809 method = 0;
810#if EV_USE_PORT
811 if (!method && (flags & EVMETHOD_PORT )) method = port_init (EV_A_ flags);
812#endif
782#if EV_USE_KQUEUE 813#if EV_USE_KQUEUE
783 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); 814 if (!method && (flags & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ flags);
784#endif 815#endif
785#if EV_USE_EPOLL 816#if EV_USE_EPOLL
786 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); 817 if (!method && (flags & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ flags);
787#endif 818#endif
788#if EV_USE_POLL 819#if EV_USE_POLL
789 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); 820 if (!method && (flags & EVMETHOD_POLL )) method = poll_init (EV_A_ flags);
790#endif 821#endif
791#if EV_USE_SELECT 822#if EV_USE_SELECT
792 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 823 if (!method && (flags & EVMETHOD_SELECT)) method = select_init (EV_A_ flags);
793#endif 824#endif
794 825
795 ev_init (&sigev, sigcb); 826 ev_init (&sigev, sigcb);
796 ev_set_priority (&sigev, EV_MAXPRI); 827 ev_set_priority (&sigev, EV_MAXPRI);
797 } 828 }
798} 829}
799 830
800void 831static void
801loop_destroy (EV_P) 832loop_destroy (EV_P)
802{ 833{
803 int i; 834 int i;
804 835
836#if EV_USE_PORT
837 if (method == EVMETHOD_PORT ) port_destroy (EV_A);
838#endif
805#if EV_USE_KQUEUE 839#if EV_USE_KQUEUE
806 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); 840 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
807#endif 841#endif
808#if EV_USE_EPOLL 842#if EV_USE_EPOLL
809 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); 843 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A);
817 851
818 for (i = NUMPRI; i--; ) 852 for (i = NUMPRI; i--; )
819 array_free (pending, [i]); 853 array_free (pending, [i]);
820 854
821 /* have to use the microsoft-never-gets-it-right macro */ 855 /* have to use the microsoft-never-gets-it-right macro */
822 array_free (fdchange, EMPTY); 856 array_free (fdchange, EMPTY0);
823 array_free (timer, EMPTY); 857 array_free (timer, EMPTY0);
824#if EV_PERIODICS 858#if EV_PERIODICS
825 array_free (periodic, EMPTY); 859 array_free (periodic, EMPTY0);
826#endif 860#endif
827 array_free (idle, EMPTY); 861 array_free (idle, EMPTY0);
828 array_free (prepare, EMPTY); 862 array_free (prepare, EMPTY0);
829 array_free (check, EMPTY); 863 array_free (check, EMPTY0);
830 864
831 method = 0; 865 method = 0;
832} 866}
833 867
834static void 868static void
835loop_fork (EV_P) 869loop_fork (EV_P)
836{ 870{
871#if EV_USE_PORT
872 if (method == EVMETHOD_PORT ) port_fork (EV_A);
873#endif
874#if EV_USE_KQUEUE
875 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A);
876#endif
837#if EV_USE_EPOLL 877#if EV_USE_EPOLL
838 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A); 878 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A);
839#endif
840#if EV_USE_KQUEUE
841 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A);
842#endif 879#endif
843 880
844 if (ev_is_active (&sigev)) 881 if (ev_is_active (&sigev))
845 { 882 {
846 /* default loop */ 883 /* default loop */
859 postfork = 0; 896 postfork = 0;
860} 897}
861 898
862#if EV_MULTIPLICITY 899#if EV_MULTIPLICITY
863struct ev_loop * 900struct ev_loop *
864ev_loop_new (int methods) 901ev_loop_new (unsigned int flags)
865{ 902{
866 struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); 903 struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop));
867 904
868 memset (loop, 0, sizeof (struct ev_loop)); 905 memset (loop, 0, sizeof (struct ev_loop));
869 906
870 loop_init (EV_A_ methods); 907 loop_init (EV_A_ flags);
871 908
872 if (ev_method (EV_A)) 909 if (ev_method (EV_A))
873 return loop; 910 return loop;
874 911
875 return 0; 912 return 0;
890 927
891#endif 928#endif
892 929
893#if EV_MULTIPLICITY 930#if EV_MULTIPLICITY
894struct ev_loop * 931struct ev_loop *
932ev_default_loop_init (unsigned int flags)
895#else 933#else
896int 934int
935ev_default_loop (unsigned int flags)
897#endif 936#endif
898ev_default_loop (int methods)
899{ 937{
900 if (sigpipe [0] == sigpipe [1]) 938 if (sigpipe [0] == sigpipe [1])
901 if (pipe (sigpipe)) 939 if (pipe (sigpipe))
902 return 0; 940 return 0;
903 941
904 if (!default_loop) 942 if (!ev_default_loop_ptr)
905 { 943 {
906#if EV_MULTIPLICITY 944#if EV_MULTIPLICITY
907 struct ev_loop *loop = default_loop = &default_loop_struct; 945 struct ev_loop *loop = ev_default_loop_ptr = &default_loop_struct;
908#else 946#else
909 default_loop = 1; 947 ev_default_loop_ptr = 1;
910#endif 948#endif
911 949
912 loop_init (EV_A_ methods); 950 loop_init (EV_A_ flags);
913 951
914 if (ev_method (EV_A)) 952 if (ev_method (EV_A))
915 { 953 {
916 siginit (EV_A); 954 siginit (EV_A);
917 955
921 ev_signal_start (EV_A_ &childev); 959 ev_signal_start (EV_A_ &childev);
922 ev_unref (EV_A); /* child watcher should not keep loop alive */ 960 ev_unref (EV_A); /* child watcher should not keep loop alive */
923#endif 961#endif
924 } 962 }
925 else 963 else
926 default_loop = 0; 964 ev_default_loop_ptr = 0;
927 } 965 }
928 966
929 return default_loop; 967 return ev_default_loop_ptr;
930} 968}
931 969
932void 970void
933ev_default_destroy (void) 971ev_default_destroy (void)
934{ 972{
935#if EV_MULTIPLICITY 973#if EV_MULTIPLICITY
936 struct ev_loop *loop = default_loop; 974 struct ev_loop *loop = ev_default_loop_ptr;
937#endif 975#endif
938 976
939#ifndef _WIN32 977#ifndef _WIN32
940 ev_ref (EV_A); /* child watcher */ 978 ev_ref (EV_A); /* child watcher */
941 ev_signal_stop (EV_A_ &childev); 979 ev_signal_stop (EV_A_ &childev);
952 990
953void 991void
954ev_default_fork (void) 992ev_default_fork (void)
955{ 993{
956#if EV_MULTIPLICITY 994#if EV_MULTIPLICITY
957 struct ev_loop *loop = default_loop; 995 struct ev_loop *loop = ev_default_loop_ptr;
958#endif 996#endif
959 997
960 if (method) 998 if (method)
961 postfork = 1; 999 postfork = 1;
962} 1000}
973 return 1; 1011 return 1;
974 1012
975 return 0; 1013 return 0;
976} 1014}
977 1015
978static void 1016inline void
979call_pending (EV_P) 1017call_pending (EV_P)
980{ 1018{
981 int pri; 1019 int pri;
982 1020
983 for (pri = NUMPRI; pri--; ) 1021 for (pri = NUMPRI; pri--; )
984 while (pendingcnt [pri]) 1022 while (pendingcnt [pri])
985 { 1023 {
986 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 1024 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
987 1025
988 if (p->w) 1026 if (expect_true (p->w))
989 { 1027 {
990 p->w->pending = 0; 1028 p->w->pending = 0;
991 EV_CB_INVOKE (p->w, p->events); 1029 EV_CB_INVOKE (p->w, p->events);
992 } 1030 }
993 } 1031 }
994} 1032}
995 1033
996static void 1034inline void
997timers_reify (EV_P) 1035timers_reify (EV_P)
998{ 1036{
999 while (timercnt && ((WT)timers [0])->at <= mn_now) 1037 while (timercnt && ((WT)timers [0])->at <= mn_now)
1000 { 1038 {
1001 struct ev_timer *w = timers [0]; 1039 struct ev_timer *w = timers [0];
1019 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); 1057 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
1020 } 1058 }
1021} 1059}
1022 1060
1023#if EV_PERIODICS 1061#if EV_PERIODICS
1024static void 1062inline void
1025periodics_reify (EV_P) 1063periodics_reify (EV_P)
1026{ 1064{
1027 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) 1065 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
1028 { 1066 {
1029 struct ev_periodic *w = periodics [0]; 1067 struct ev_periodic *w = periodics [0];
1031 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 1069 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
1032 1070
1033 /* first reschedule or stop timer */ 1071 /* first reschedule or stop timer */
1034 if (w->reschedule_cb) 1072 if (w->reschedule_cb)
1035 { 1073 {
1036 ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001); 1074 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001);
1037
1038 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now)); 1075 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now));
1039 downheap ((WT *)periodics, periodiccnt, 0); 1076 downheap ((WT *)periodics, periodiccnt, 0);
1040 } 1077 }
1041 else if (w->interval) 1078 else if (w->interval)
1042 { 1079 {
1089 ev_rt_now = ev_time (); 1126 ev_rt_now = ev_time ();
1090 return 1; 1127 return 1;
1091 } 1128 }
1092} 1129}
1093 1130
1094static void 1131inline void
1095time_update (EV_P) 1132time_update (EV_P)
1096{ 1133{
1097 int i; 1134 int i;
1098 1135
1099#if EV_USE_MONOTONIC 1136#if EV_USE_MONOTONIC
1160ev_loop (EV_P_ int flags) 1197ev_loop (EV_P_ int flags)
1161{ 1198{
1162 double block; 1199 double block;
1163 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; 1200 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
1164 1201
1165 do 1202 while (activecnt)
1166 { 1203 {
1167 /* queue check watchers (and execute them) */ 1204 /* queue check watchers (and execute them) */
1168 if (expect_false (preparecnt)) 1205 if (expect_false (preparecnt))
1169 { 1206 {
1170 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 1207 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
1210 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge; 1247 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge;
1211 if (block > to) block = to; 1248 if (block > to) block = to;
1212 } 1249 }
1213#endif 1250#endif
1214 1251
1215 if (block < 0.) block = 0.; 1252 if (expect_false (block < 0.)) block = 0.;
1216 } 1253 }
1217 1254
1218 method_poll (EV_A_ block); 1255 method_poll (EV_A_ block);
1219 1256
1220 /* update ev_rt_now, do magic */ 1257 /* update ev_rt_now, do magic */
1229 /* queue idle watchers unless io or timers are pending */ 1266 /* queue idle watchers unless io or timers are pending */
1230 if (idlecnt && !any_pending (EV_A)) 1267 if (idlecnt && !any_pending (EV_A))
1231 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1268 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
1232 1269
1233 /* queue check watchers, to be executed first */ 1270 /* queue check watchers, to be executed first */
1234 if (checkcnt) 1271 if (expect_false (checkcnt))
1235 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1272 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1236 1273
1237 call_pending (EV_A); 1274 call_pending (EV_A);
1275
1276 if (expect_false (loop_done))
1277 break;
1238 } 1278 }
1239 while (activecnt && !loop_done);
1240 1279
1241 if (loop_done != 2) 1280 if (loop_done != 2)
1242 loop_done = 0; 1281 loop_done = 0;
1243} 1282}
1244 1283
1304void 1343void
1305ev_io_start (EV_P_ struct ev_io *w) 1344ev_io_start (EV_P_ struct ev_io *w)
1306{ 1345{
1307 int fd = w->fd; 1346 int fd = w->fd;
1308 1347
1309 if (ev_is_active (w)) 1348 if (expect_false (ev_is_active (w)))
1310 return; 1349 return;
1311 1350
1312 assert (("ev_io_start called with negative fd", fd >= 0)); 1351 assert (("ev_io_start called with negative fd", fd >= 0));
1313 1352
1314 ev_start (EV_A_ (W)w, 1); 1353 ev_start (EV_A_ (W)w, 1);
1320 1359
1321void 1360void
1322ev_io_stop (EV_P_ struct ev_io *w) 1361ev_io_stop (EV_P_ struct ev_io *w)
1323{ 1362{
1324 ev_clear_pending (EV_A_ (W)w); 1363 ev_clear_pending (EV_A_ (W)w);
1325 if (!ev_is_active (w)) 1364 if (expect_false (!ev_is_active (w)))
1326 return; 1365 return;
1327 1366
1328 assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax)); 1367 assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax));
1329 1368
1330 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 1369 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
1334} 1373}
1335 1374
1336void 1375void
1337ev_timer_start (EV_P_ struct ev_timer *w) 1376ev_timer_start (EV_P_ struct ev_timer *w)
1338{ 1377{
1339 if (ev_is_active (w)) 1378 if (expect_false (ev_is_active (w)))
1340 return; 1379 return;
1341 1380
1342 ((WT)w)->at += mn_now; 1381 ((WT)w)->at += mn_now;
1343 1382
1344 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1383 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1345 1384
1346 ev_start (EV_A_ (W)w, ++timercnt); 1385 ev_start (EV_A_ (W)w, ++timercnt);
1347 array_needsize (struct ev_timer *, timers, timermax, timercnt, (void)); 1386 array_needsize (struct ev_timer *, timers, timermax, timercnt, EMPTY2);
1348 timers [timercnt - 1] = w; 1387 timers [timercnt - 1] = w;
1349 upheap ((WT *)timers, timercnt - 1); 1388 upheap ((WT *)timers, timercnt - 1);
1350 1389
1351 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1390 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1352} 1391}
1353 1392
1354void 1393void
1355ev_timer_stop (EV_P_ struct ev_timer *w) 1394ev_timer_stop (EV_P_ struct ev_timer *w)
1356{ 1395{
1357 ev_clear_pending (EV_A_ (W)w); 1396 ev_clear_pending (EV_A_ (W)w);
1358 if (!ev_is_active (w)) 1397 if (expect_false (!ev_is_active (w)))
1359 return; 1398 return;
1360 1399
1361 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1400 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1362 1401
1363 if (((W)w)->active < timercnt--) 1402 if (expect_true (((W)w)->active < timercnt--))
1364 { 1403 {
1365 timers [((W)w)->active - 1] = timers [timercnt]; 1404 timers [((W)w)->active - 1] = timers [timercnt];
1366 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1); 1405 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1367 } 1406 }
1368 1407
1383 } 1422 }
1384 else 1423 else
1385 ev_timer_stop (EV_A_ w); 1424 ev_timer_stop (EV_A_ w);
1386 } 1425 }
1387 else if (w->repeat) 1426 else if (w->repeat)
1427 {
1428 w->at = w->repeat;
1388 ev_timer_start (EV_A_ w); 1429 ev_timer_start (EV_A_ w);
1430 }
1389} 1431}
1390 1432
1391#if EV_PERIODICS 1433#if EV_PERIODICS
1392void 1434void
1393ev_periodic_start (EV_P_ struct ev_periodic *w) 1435ev_periodic_start (EV_P_ struct ev_periodic *w)
1394{ 1436{
1395 if (ev_is_active (w)) 1437 if (expect_false (ev_is_active (w)))
1396 return; 1438 return;
1397 1439
1398 if (w->reschedule_cb) 1440 if (w->reschedule_cb)
1399 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 1441 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1400 else if (w->interval) 1442 else if (w->interval)
1403 /* this formula differs from the one in periodic_reify because we do not always round up */ 1445 /* this formula differs from the one in periodic_reify because we do not always round up */
1404 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; 1446 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1405 } 1447 }
1406 1448
1407 ev_start (EV_A_ (W)w, ++periodiccnt); 1449 ev_start (EV_A_ (W)w, ++periodiccnt);
1408 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); 1450 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2);
1409 periodics [periodiccnt - 1] = w; 1451 periodics [periodiccnt - 1] = w;
1410 upheap ((WT *)periodics, periodiccnt - 1); 1452 upheap ((WT *)periodics, periodiccnt - 1);
1411 1453
1412 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1454 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1413} 1455}
1414 1456
1415void 1457void
1416ev_periodic_stop (EV_P_ struct ev_periodic *w) 1458ev_periodic_stop (EV_P_ struct ev_periodic *w)
1417{ 1459{
1418 ev_clear_pending (EV_A_ (W)w); 1460 ev_clear_pending (EV_A_ (W)w);
1419 if (!ev_is_active (w)) 1461 if (expect_false (!ev_is_active (w)))
1420 return; 1462 return;
1421 1463
1422 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1464 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1423 1465
1424 if (((W)w)->active < periodiccnt--) 1466 if (expect_true (((W)w)->active < periodiccnt--))
1425 { 1467 {
1426 periodics [((W)w)->active - 1] = periodics [periodiccnt]; 1468 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1427 adjustheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); 1469 adjustheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1428 } 1470 }
1429 1471
1440#endif 1482#endif
1441 1483
1442void 1484void
1443ev_idle_start (EV_P_ struct ev_idle *w) 1485ev_idle_start (EV_P_ struct ev_idle *w)
1444{ 1486{
1445 if (ev_is_active (w)) 1487 if (expect_false (ev_is_active (w)))
1446 return; 1488 return;
1447 1489
1448 ev_start (EV_A_ (W)w, ++idlecnt); 1490 ev_start (EV_A_ (W)w, ++idlecnt);
1449 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, (void)); 1491 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, EMPTY2);
1450 idles [idlecnt - 1] = w; 1492 idles [idlecnt - 1] = w;
1451} 1493}
1452 1494
1453void 1495void
1454ev_idle_stop (EV_P_ struct ev_idle *w) 1496ev_idle_stop (EV_P_ struct ev_idle *w)
1455{ 1497{
1456 ev_clear_pending (EV_A_ (W)w); 1498 ev_clear_pending (EV_A_ (W)w);
1457 if (!ev_is_active (w)) 1499 if (expect_false (!ev_is_active (w)))
1458 return; 1500 return;
1459 1501
1460 idles [((W)w)->active - 1] = idles [--idlecnt]; 1502 idles [((W)w)->active - 1] = idles [--idlecnt];
1461 ev_stop (EV_A_ (W)w); 1503 ev_stop (EV_A_ (W)w);
1462} 1504}
1463 1505
1464void 1506void
1465ev_prepare_start (EV_P_ struct ev_prepare *w) 1507ev_prepare_start (EV_P_ struct ev_prepare *w)
1466{ 1508{
1467 if (ev_is_active (w)) 1509 if (expect_false (ev_is_active (w)))
1468 return; 1510 return;
1469 1511
1470 ev_start (EV_A_ (W)w, ++preparecnt); 1512 ev_start (EV_A_ (W)w, ++preparecnt);
1471 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, (void)); 1513 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, EMPTY2);
1472 prepares [preparecnt - 1] = w; 1514 prepares [preparecnt - 1] = w;
1473} 1515}
1474 1516
1475void 1517void
1476ev_prepare_stop (EV_P_ struct ev_prepare *w) 1518ev_prepare_stop (EV_P_ struct ev_prepare *w)
1477{ 1519{
1478 ev_clear_pending (EV_A_ (W)w); 1520 ev_clear_pending (EV_A_ (W)w);
1479 if (!ev_is_active (w)) 1521 if (expect_false (!ev_is_active (w)))
1480 return; 1522 return;
1481 1523
1482 prepares [((W)w)->active - 1] = prepares [--preparecnt]; 1524 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1483 ev_stop (EV_A_ (W)w); 1525 ev_stop (EV_A_ (W)w);
1484} 1526}
1485 1527
1486void 1528void
1487ev_check_start (EV_P_ struct ev_check *w) 1529ev_check_start (EV_P_ struct ev_check *w)
1488{ 1530{
1489 if (ev_is_active (w)) 1531 if (expect_false (ev_is_active (w)))
1490 return; 1532 return;
1491 1533
1492 ev_start (EV_A_ (W)w, ++checkcnt); 1534 ev_start (EV_A_ (W)w, ++checkcnt);
1493 array_needsize (struct ev_check *, checks, checkmax, checkcnt, (void)); 1535 array_needsize (struct ev_check *, checks, checkmax, checkcnt, EMPTY2);
1494 checks [checkcnt - 1] = w; 1536 checks [checkcnt - 1] = w;
1495} 1537}
1496 1538
1497void 1539void
1498ev_check_stop (EV_P_ struct ev_check *w) 1540ev_check_stop (EV_P_ struct ev_check *w)
1499{ 1541{
1500 ev_clear_pending (EV_A_ (W)w); 1542 ev_clear_pending (EV_A_ (W)w);
1501 if (!ev_is_active (w)) 1543 if (expect_false (!ev_is_active (w)))
1502 return; 1544 return;
1503 1545
1504 checks [((W)w)->active - 1] = checks [--checkcnt]; 1546 checks [((W)w)->active - 1] = checks [--checkcnt];
1505 ev_stop (EV_A_ (W)w); 1547 ev_stop (EV_A_ (W)w);
1506} 1548}
1511 1553
1512void 1554void
1513ev_signal_start (EV_P_ struct ev_signal *w) 1555ev_signal_start (EV_P_ struct ev_signal *w)
1514{ 1556{
1515#if EV_MULTIPLICITY 1557#if EV_MULTIPLICITY
1516 assert (("signal watchers are only supported in the default loop", loop == default_loop)); 1558 assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr));
1517#endif 1559#endif
1518 if (ev_is_active (w)) 1560 if (expect_false (ev_is_active (w)))
1519 return; 1561 return;
1520 1562
1521 assert (("ev_signal_start called with illegal signal number", w->signum > 0)); 1563 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
1522 1564
1523 ev_start (EV_A_ (W)w, 1); 1565 ev_start (EV_A_ (W)w, 1);
1540 1582
1541void 1583void
1542ev_signal_stop (EV_P_ struct ev_signal *w) 1584ev_signal_stop (EV_P_ struct ev_signal *w)
1543{ 1585{
1544 ev_clear_pending (EV_A_ (W)w); 1586 ev_clear_pending (EV_A_ (W)w);
1545 if (!ev_is_active (w)) 1587 if (expect_false (!ev_is_active (w)))
1546 return; 1588 return;
1547 1589
1548 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 1590 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
1549 ev_stop (EV_A_ (W)w); 1591 ev_stop (EV_A_ (W)w);
1550 1592
1554 1596
1555void 1597void
1556ev_child_start (EV_P_ struct ev_child *w) 1598ev_child_start (EV_P_ struct ev_child *w)
1557{ 1599{
1558#if EV_MULTIPLICITY 1600#if EV_MULTIPLICITY
1559 assert (("child watchers are only supported in the default loop", loop == default_loop)); 1601 assert (("child watchers are only supported in the default loop", loop == ev_default_loop_ptr));
1560#endif 1602#endif
1561 if (ev_is_active (w)) 1603 if (expect_false (ev_is_active (w)))
1562 return; 1604 return;
1563 1605
1564 ev_start (EV_A_ (W)w, 1); 1606 ev_start (EV_A_ (W)w, 1);
1565 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1607 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1566} 1608}
1567 1609
1568void 1610void
1569ev_child_stop (EV_P_ struct ev_child *w) 1611ev_child_stop (EV_P_ struct ev_child *w)
1570{ 1612{
1571 ev_clear_pending (EV_A_ (W)w); 1613 ev_clear_pending (EV_A_ (W)w);
1572 if (!ev_is_active (w)) 1614 if (expect_false (!ev_is_active (w)))
1573 return; 1615 return;
1574 1616
1575 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1617 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1576 ev_stop (EV_A_ (W)w); 1618 ev_stop (EV_A_ (W)w);
1577} 1619}
1614void 1656void
1615ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) 1657ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
1616{ 1658{
1617 struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once)); 1659 struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once));
1618 1660
1619 if (!once) 1661 if (expect_false (!once))
1662 {
1620 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); 1663 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1621 else 1664 return;
1622 { 1665 }
1666
1623 once->cb = cb; 1667 once->cb = cb;
1624 once->arg = arg; 1668 once->arg = arg;
1625 1669
1626 ev_init (&once->io, once_cb_io); 1670 ev_init (&once->io, once_cb_io);
1627 if (fd >= 0) 1671 if (fd >= 0)
1628 { 1672 {
1629 ev_io_set (&once->io, fd, events); 1673 ev_io_set (&once->io, fd, events);
1630 ev_io_start (EV_A_ &once->io); 1674 ev_io_start (EV_A_ &once->io);
1631 } 1675 }
1632 1676
1633 ev_init (&once->to, once_cb_to); 1677 ev_init (&once->to, once_cb_to);
1634 if (timeout >= 0.) 1678 if (timeout >= 0.)
1635 { 1679 {
1636 ev_timer_set (&once->to, timeout, 0.); 1680 ev_timer_set (&once->to, timeout, 0.);
1637 ev_timer_start (EV_A_ &once->to); 1681 ev_timer_start (EV_A_ &once->to);
1638 }
1639 } 1682 }
1640} 1683}
1641 1684
1642#ifdef __cplusplus 1685#ifdef __cplusplus
1643} 1686}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines