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

Comparing libev/ev.c (file contents):
Revision 1.103 by root, Mon Nov 12 00:31:08 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#endif 125#endif
101 126
102#ifndef EV_USE_POLL 127#ifndef EV_USE_POLL
103# define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */ 128# ifdef _WIN32
129# define EV_USE_POLL 0
130# else
131# define EV_USE_POLL 1
132# endif
104#endif 133#endif
105 134
106#ifndef EV_USE_EPOLL 135#ifndef EV_USE_EPOLL
107# define EV_USE_EPOLL 0 136# define EV_USE_EPOLL 0
108#endif 137#endif
109 138
110#ifndef EV_USE_KQUEUE 139#ifndef EV_USE_KQUEUE
111# define EV_USE_KQUEUE 0 140# define EV_USE_KQUEUE 0
112#endif 141#endif
113 142
114#ifndef EV_USE_REALTIME 143#ifndef EV_USE_PORT
115# define EV_USE_REALTIME 1 144# define EV_USE_PORT 0
116#endif 145#endif
117 146
118/**/ 147/**/
148
149/* darwin simply cannot be helped */
150#ifdef __APPLE__
151# undef EV_USE_POLL
152# undef EV_USE_KQUEUE
153#endif
119 154
120#ifndef CLOCK_MONOTONIC 155#ifndef CLOCK_MONOTONIC
121# undef EV_USE_MONOTONIC 156# undef EV_USE_MONOTONIC
122# define EV_USE_MONOTONIC 0 157# define EV_USE_MONOTONIC 0
123#endif 158#endif
132#endif 167#endif
133 168
134/**/ 169/**/
135 170
136#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) */
137#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) */
138#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 */
139/*#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 */
140 175
141#ifdef EV_H 176#ifdef EV_H
142# include EV_H 177# include EV_H
143#else 178#else
144# include "ev.h" 179# include "ev.h"
145#endif 180#endif
146 181
147#if __GNUC__ >= 3 182#if __GNUC__ >= 3
148# define expect(expr,value) __builtin_expect ((expr),(value)) 183# define expect(expr,value) __builtin_expect ((expr),(value))
149# define inline inline 184# define inline static inline
150#else 185#else
151# define expect(expr,value) (expr) 186# define expect(expr,value) (expr)
152# define inline static 187# define inline static
153#endif 188#endif
154 189
156#define expect_true(expr) expect ((expr) != 0, 1) 191#define expect_true(expr) expect ((expr) != 0, 1)
157 192
158#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) 193#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
159#define ABSPRI(w) ((w)->priority - EV_MINPRI) 194#define ABSPRI(w) ((w)->priority - EV_MINPRI)
160 195
161#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 */
162 198
163typedef struct ev_watcher *W; 199typedef struct ev_watcher *W;
164typedef struct ev_watcher_list *WL; 200typedef struct ev_watcher_list *WL;
165typedef struct ev_watcher_time *WT; 201typedef struct ev_watcher_time *WT;
166 202
246 #include "ev_vars.h" 282 #include "ev_vars.h"
247 #undef VAR 283 #undef VAR
248 }; 284 };
249 #include "ev_wrap.h" 285 #include "ev_wrap.h"
250 286
251 struct ev_loop default_loop_struct; 287 static struct ev_loop default_loop_struct;
252 static struct ev_loop *default_loop; 288 struct ev_loop *ev_default_loop_ptr;
253 289
254#else 290#else
255 291
256 ev_tstamp ev_rt_now; 292 ev_tstamp ev_rt_now;
257 #define VAR(name,decl) static decl; 293 #define VAR(name,decl) static decl;
258 #include "ev_vars.h" 294 #include "ev_vars.h"
259 #undef VAR 295 #undef VAR
260 296
261 static int default_loop; 297 static int ev_default_loop_ptr;
262 298
263#endif 299#endif
264 300
265/*****************************************************************************/ 301/*****************************************************************************/
266 302
299{ 335{
300 return ev_rt_now; 336 return ev_rt_now;
301} 337}
302#endif 338#endif
303 339
304#define array_roundsize(type,n) ((n) | 4 & ~3) 340#define array_roundsize(type,n) (((n) | 4) & ~3)
305 341
306#define array_needsize(type,base,cur,cnt,init) \ 342#define array_needsize(type,base,cur,cnt,init) \
307 if (expect_false ((cnt) > cur)) \ 343 if (expect_false ((cnt) > cur)) \
308 { \ 344 { \
309 int newcnt = cur; \ 345 int newcnt = cur; \
347void 383void
348ev_feed_event (EV_P_ void *w, int revents) 384ev_feed_event (EV_P_ void *w, int revents)
349{ 385{
350 W w_ = (W)w; 386 W w_ = (W)w;
351 387
352 if (w_->pending) 388 if (expect_false (w_->pending))
353 { 389 {
354 pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; 390 pendings [ABSPRI (w_)][w_->pending - 1].events |= revents;
355 return; 391 return;
356 } 392 }
357 393
358 w_->pending = ++pendingcnt [ABSPRI (w_)]; 394 w_->pending = ++pendingcnt [ABSPRI (w_)];
359 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);
360 pendings [ABSPRI (w_)][w_->pending - 1].w = w_; 396 pendings [ABSPRI (w_)][w_->pending - 1].w = w_;
361 pendings [ABSPRI (w_)][w_->pending - 1].events = revents; 397 pendings [ABSPRI (w_)][w_->pending - 1].events = revents;
362} 398}
363 399
364static void 400static void
391 fd_event (EV_A_ fd, revents); 427 fd_event (EV_A_ fd, revents);
392} 428}
393 429
394/*****************************************************************************/ 430/*****************************************************************************/
395 431
396static void 432inline void
397fd_reify (EV_P) 433fd_reify (EV_P)
398{ 434{
399 int i; 435 int i;
400 436
401 for (i = 0; i < fdchangecnt; ++i) 437 for (i = 0; i < fdchangecnt; ++i)
428} 464}
429 465
430static void 466static void
431fd_change (EV_P_ int fd) 467fd_change (EV_P_ int fd)
432{ 468{
433 if (anfds [fd].reify) 469 if (expect_false (anfds [fd].reify))
434 return; 470 return;
435 471
436 anfds [fd].reify = 1; 472 anfds [fd].reify = 1;
437 473
438 ++fdchangecnt; 474 ++fdchangecnt;
439 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, (void)); 475 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2);
440 fdchanges [fdchangecnt - 1] = fd; 476 fdchanges [fdchangecnt - 1] = fd;
441} 477}
442 478
443static void 479static void
444fd_kill (EV_P_ int fd) 480fd_kill (EV_P_ int fd)
450 ev_io_stop (EV_A_ w); 486 ev_io_stop (EV_A_ w);
451 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);
452 } 488 }
453} 489}
454 490
455static int 491inline int
456fd_valid (int fd) 492fd_valid (int fd)
457{ 493{
458#ifdef _WIN32 494#ifdef _WIN32
459 return _get_osfhandle (fd) != -1; 495 return _get_osfhandle (fd) != -1;
460#else 496#else
602ev_feed_signal_event (EV_P_ int signum) 638ev_feed_signal_event (EV_P_ int signum)
603{ 639{
604 WL w; 640 WL w;
605 641
606#if EV_MULTIPLICITY 642#if EV_MULTIPLICITY
607 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));
608#endif 644#endif
609 645
610 --signum; 646 --signum;
611 647
612 if (signum < 0 || signum >= signalmax) 648 if (signum < 0 || signum >= signalmax)
629 for (signum = signalmax; signum--; ) 665 for (signum = signalmax; signum--; )
630 if (signals [signum].gotsig) 666 if (signals [signum].gotsig)
631 ev_feed_signal_event (EV_A_ signum + 1); 667 ev_feed_signal_event (EV_A_ signum + 1);
632} 668}
633 669
634inline void 670static void
635fd_intern (int fd) 671fd_intern (int fd)
636{ 672{
637#ifdef _WIN32 673#ifdef _WIN32
638 int arg = 1; 674 int arg = 1;
639 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); 675 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg);
698 734
699#endif 735#endif
700 736
701/*****************************************************************************/ 737/*****************************************************************************/
702 738
739#if EV_USE_PORT
740# include "ev_port.c"
741#endif
703#if EV_USE_KQUEUE 742#if EV_USE_KQUEUE
704# include "ev_kqueue.c" 743# include "ev_kqueue.c"
705#endif 744#endif
706#if EV_USE_EPOLL 745#if EV_USE_EPOLL
707# include "ev_epoll.c" 746# include "ev_epoll.c"
735 return getuid () != geteuid () 774 return getuid () != geteuid ()
736 || getgid () != getegid (); 775 || getgid () != getegid ();
737#endif 776#endif
738} 777}
739 778
740int 779unsigned int
741ev_method (EV_P) 780ev_method (EV_P)
742{ 781{
743 return method; 782 return method;
744} 783}
745 784
746static void 785static void
747loop_init (EV_P_ int methods) 786loop_init (EV_P_ unsigned int flags)
748{ 787{
749 if (!method) 788 if (!method)
750 { 789 {
751#if EV_USE_MONOTONIC 790#if EV_USE_MONOTONIC
752 { 791 {
759 ev_rt_now = ev_time (); 798 ev_rt_now = ev_time ();
760 mn_now = get_clock (); 799 mn_now = get_clock ();
761 now_floor = mn_now; 800 now_floor = mn_now;
762 rtmn_diff = ev_rt_now - mn_now; 801 rtmn_diff = ev_rt_now - mn_now;
763 802
764 if (methods == EVMETHOD_AUTO) 803 if (!(flags & EVFLAG_NOENV) && !enable_secure () && getenv ("LIBEV_FLAGS"))
765 if (!enable_secure () && getenv ("LIBEV_METHODS"))
766 methods = atoi (getenv ("LIBEV_METHODS")); 804 flags = atoi (getenv ("LIBEV_FLAGS"));
767 else 805
768 methods = EVMETHOD_ANY; 806 if (!(flags & 0x0000ffff))
807 flags |= 0x0000ffff;
769 808
770 method = 0; 809 method = 0;
810#if EV_USE_PORT
811 if (!method && (flags & EVMETHOD_PORT )) method = port_init (EV_A_ flags);
812#endif
771#if EV_USE_KQUEUE 813#if EV_USE_KQUEUE
772 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); 814 if (!method && (flags & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ flags);
773#endif 815#endif
774#if EV_USE_EPOLL 816#if EV_USE_EPOLL
775 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); 817 if (!method && (flags & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ flags);
776#endif 818#endif
777#if EV_USE_POLL 819#if EV_USE_POLL
778 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); 820 if (!method && (flags & EVMETHOD_POLL )) method = poll_init (EV_A_ flags);
779#endif 821#endif
780#if EV_USE_SELECT 822#if EV_USE_SELECT
781 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 823 if (!method && (flags & EVMETHOD_SELECT)) method = select_init (EV_A_ flags);
782#endif 824#endif
783 825
784 ev_init (&sigev, sigcb); 826 ev_init (&sigev, sigcb);
785 ev_set_priority (&sigev, EV_MAXPRI); 827 ev_set_priority (&sigev, EV_MAXPRI);
786 } 828 }
787} 829}
788 830
789void 831static void
790loop_destroy (EV_P) 832loop_destroy (EV_P)
791{ 833{
792 int i; 834 int i;
793 835
836#if EV_USE_PORT
837 if (method == EVMETHOD_PORT ) port_destroy (EV_A);
838#endif
794#if EV_USE_KQUEUE 839#if EV_USE_KQUEUE
795 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); 840 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
796#endif 841#endif
797#if EV_USE_EPOLL 842#if EV_USE_EPOLL
798 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); 843 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A);
806 851
807 for (i = NUMPRI; i--; ) 852 for (i = NUMPRI; i--; )
808 array_free (pending, [i]); 853 array_free (pending, [i]);
809 854
810 /* have to use the microsoft-never-gets-it-right macro */ 855 /* have to use the microsoft-never-gets-it-right macro */
811 array_free (fdchange, EMPTY); 856 array_free (fdchange, EMPTY0);
812 array_free (timer, EMPTY); 857 array_free (timer, EMPTY0);
813#if EV_PERIODICS 858#if EV_PERIODICS
814 array_free (periodic, EMPTY); 859 array_free (periodic, EMPTY0);
815#endif 860#endif
816 array_free (idle, EMPTY); 861 array_free (idle, EMPTY0);
817 array_free (prepare, EMPTY); 862 array_free (prepare, EMPTY0);
818 array_free (check, EMPTY); 863 array_free (check, EMPTY0);
819 864
820 method = 0; 865 method = 0;
821} 866}
822 867
823static void 868static void
824loop_fork (EV_P) 869loop_fork (EV_P)
825{ 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
826#if EV_USE_EPOLL 877#if EV_USE_EPOLL
827 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A); 878 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A);
828#endif
829#if EV_USE_KQUEUE
830 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A);
831#endif 879#endif
832 880
833 if (ev_is_active (&sigev)) 881 if (ev_is_active (&sigev))
834 { 882 {
835 /* default loop */ 883 /* default loop */
848 postfork = 0; 896 postfork = 0;
849} 897}
850 898
851#if EV_MULTIPLICITY 899#if EV_MULTIPLICITY
852struct ev_loop * 900struct ev_loop *
853ev_loop_new (int methods) 901ev_loop_new (unsigned int flags)
854{ 902{
855 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));
856 904
857 memset (loop, 0, sizeof (struct ev_loop)); 905 memset (loop, 0, sizeof (struct ev_loop));
858 906
859 loop_init (EV_A_ methods); 907 loop_init (EV_A_ flags);
860 908
861 if (ev_method (EV_A)) 909 if (ev_method (EV_A))
862 return loop; 910 return loop;
863 911
864 return 0; 912 return 0;
879 927
880#endif 928#endif
881 929
882#if EV_MULTIPLICITY 930#if EV_MULTIPLICITY
883struct ev_loop * 931struct ev_loop *
932ev_default_loop_init (unsigned int flags)
884#else 933#else
885int 934int
935ev_default_loop (unsigned int flags)
886#endif 936#endif
887ev_default_loop (int methods)
888{ 937{
889 if (sigpipe [0] == sigpipe [1]) 938 if (sigpipe [0] == sigpipe [1])
890 if (pipe (sigpipe)) 939 if (pipe (sigpipe))
891 return 0; 940 return 0;
892 941
893 if (!default_loop) 942 if (!ev_default_loop_ptr)
894 { 943 {
895#if EV_MULTIPLICITY 944#if EV_MULTIPLICITY
896 struct ev_loop *loop = default_loop = &default_loop_struct; 945 struct ev_loop *loop = ev_default_loop_ptr = &default_loop_struct;
897#else 946#else
898 default_loop = 1; 947 ev_default_loop_ptr = 1;
899#endif 948#endif
900 949
901 loop_init (EV_A_ methods); 950 loop_init (EV_A_ flags);
902 951
903 if (ev_method (EV_A)) 952 if (ev_method (EV_A))
904 { 953 {
905 siginit (EV_A); 954 siginit (EV_A);
906 955
910 ev_signal_start (EV_A_ &childev); 959 ev_signal_start (EV_A_ &childev);
911 ev_unref (EV_A); /* child watcher should not keep loop alive */ 960 ev_unref (EV_A); /* child watcher should not keep loop alive */
912#endif 961#endif
913 } 962 }
914 else 963 else
915 default_loop = 0; 964 ev_default_loop_ptr = 0;
916 } 965 }
917 966
918 return default_loop; 967 return ev_default_loop_ptr;
919} 968}
920 969
921void 970void
922ev_default_destroy (void) 971ev_default_destroy (void)
923{ 972{
924#if EV_MULTIPLICITY 973#if EV_MULTIPLICITY
925 struct ev_loop *loop = default_loop; 974 struct ev_loop *loop = ev_default_loop_ptr;
926#endif 975#endif
927 976
928#ifndef _WIN32 977#ifndef _WIN32
929 ev_ref (EV_A); /* child watcher */ 978 ev_ref (EV_A); /* child watcher */
930 ev_signal_stop (EV_A_ &childev); 979 ev_signal_stop (EV_A_ &childev);
941 990
942void 991void
943ev_default_fork (void) 992ev_default_fork (void)
944{ 993{
945#if EV_MULTIPLICITY 994#if EV_MULTIPLICITY
946 struct ev_loop *loop = default_loop; 995 struct ev_loop *loop = ev_default_loop_ptr;
947#endif 996#endif
948 997
949 if (method) 998 if (method)
950 postfork = 1; 999 postfork = 1;
951} 1000}
962 return 1; 1011 return 1;
963 1012
964 return 0; 1013 return 0;
965} 1014}
966 1015
967static void 1016inline void
968call_pending (EV_P) 1017call_pending (EV_P)
969{ 1018{
970 int pri; 1019 int pri;
971 1020
972 for (pri = NUMPRI; pri--; ) 1021 for (pri = NUMPRI; pri--; )
973 while (pendingcnt [pri]) 1022 while (pendingcnt [pri])
974 { 1023 {
975 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 1024 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
976 1025
977 if (p->w) 1026 if (expect_true (p->w))
978 { 1027 {
979 p->w->pending = 0; 1028 p->w->pending = 0;
980 EV_CB_INVOKE (p->w, p->events); 1029 EV_CB_INVOKE (p->w, p->events);
981 } 1030 }
982 } 1031 }
983} 1032}
984 1033
985static void 1034inline void
986timers_reify (EV_P) 1035timers_reify (EV_P)
987{ 1036{
988 while (timercnt && ((WT)timers [0])->at <= mn_now) 1037 while (timercnt && ((WT)timers [0])->at <= mn_now)
989 { 1038 {
990 struct ev_timer *w = timers [0]; 1039 struct ev_timer *w = timers [0];
1008 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); 1057 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
1009 } 1058 }
1010} 1059}
1011 1060
1012#if EV_PERIODICS 1061#if EV_PERIODICS
1013static void 1062inline void
1014periodics_reify (EV_P) 1063periodics_reify (EV_P)
1015{ 1064{
1016 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) 1065 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
1017 { 1066 {
1018 struct ev_periodic *w = periodics [0]; 1067 struct ev_periodic *w = periodics [0];
1020 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 1069 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
1021 1070
1022 /* first reschedule or stop timer */ 1071 /* first reschedule or stop timer */
1023 if (w->reschedule_cb) 1072 if (w->reschedule_cb)
1024 { 1073 {
1025 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);
1026
1027 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));
1028 downheap ((WT *)periodics, periodiccnt, 0); 1076 downheap ((WT *)periodics, periodiccnt, 0);
1029 } 1077 }
1030 else if (w->interval) 1078 else if (w->interval)
1031 { 1079 {
1078 ev_rt_now = ev_time (); 1126 ev_rt_now = ev_time ();
1079 return 1; 1127 return 1;
1080 } 1128 }
1081} 1129}
1082 1130
1083static void 1131inline void
1084time_update (EV_P) 1132time_update (EV_P)
1085{ 1133{
1086 int i; 1134 int i;
1087 1135
1088#if EV_USE_MONOTONIC 1136#if EV_USE_MONOTONIC
1149ev_loop (EV_P_ int flags) 1197ev_loop (EV_P_ int flags)
1150{ 1198{
1151 double block; 1199 double block;
1152 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; 1200 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
1153 1201
1154 do 1202 while (activecnt)
1155 { 1203 {
1156 /* queue check watchers (and execute them) */ 1204 /* queue check watchers (and execute them) */
1157 if (expect_false (preparecnt)) 1205 if (expect_false (preparecnt))
1158 { 1206 {
1159 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 1207 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
1199 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;
1200 if (block > to) block = to; 1248 if (block > to) block = to;
1201 } 1249 }
1202#endif 1250#endif
1203 1251
1204 if (block < 0.) block = 0.; 1252 if (expect_false (block < 0.)) block = 0.;
1205 } 1253 }
1206 1254
1207 method_poll (EV_A_ block); 1255 method_poll (EV_A_ block);
1208 1256
1209 /* update ev_rt_now, do magic */ 1257 /* update ev_rt_now, do magic */
1218 /* queue idle watchers unless io or timers are pending */ 1266 /* queue idle watchers unless io or timers are pending */
1219 if (idlecnt && !any_pending (EV_A)) 1267 if (idlecnt && !any_pending (EV_A))
1220 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1268 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
1221 1269
1222 /* queue check watchers, to be executed first */ 1270 /* queue check watchers, to be executed first */
1223 if (checkcnt) 1271 if (expect_false (checkcnt))
1224 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1272 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1225 1273
1226 call_pending (EV_A); 1274 call_pending (EV_A);
1275
1276 if (expect_false (loop_done))
1277 break;
1227 } 1278 }
1228 while (activecnt && !loop_done);
1229 1279
1230 if (loop_done != 2) 1280 if (loop_done != 2)
1231 loop_done = 0; 1281 loop_done = 0;
1232} 1282}
1233 1283
1293void 1343void
1294ev_io_start (EV_P_ struct ev_io *w) 1344ev_io_start (EV_P_ struct ev_io *w)
1295{ 1345{
1296 int fd = w->fd; 1346 int fd = w->fd;
1297 1347
1298 if (ev_is_active (w)) 1348 if (expect_false (ev_is_active (w)))
1299 return; 1349 return;
1300 1350
1301 assert (("ev_io_start called with negative fd", fd >= 0)); 1351 assert (("ev_io_start called with negative fd", fd >= 0));
1302 1352
1303 ev_start (EV_A_ (W)w, 1); 1353 ev_start (EV_A_ (W)w, 1);
1309 1359
1310void 1360void
1311ev_io_stop (EV_P_ struct ev_io *w) 1361ev_io_stop (EV_P_ struct ev_io *w)
1312{ 1362{
1313 ev_clear_pending (EV_A_ (W)w); 1363 ev_clear_pending (EV_A_ (W)w);
1314 if (!ev_is_active (w)) 1364 if (expect_false (!ev_is_active (w)))
1315 return; 1365 return;
1316 1366
1317 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));
1318 1368
1319 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 1369 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
1323} 1373}
1324 1374
1325void 1375void
1326ev_timer_start (EV_P_ struct ev_timer *w) 1376ev_timer_start (EV_P_ struct ev_timer *w)
1327{ 1377{
1328 if (ev_is_active (w)) 1378 if (expect_false (ev_is_active (w)))
1329 return; 1379 return;
1330 1380
1331 ((WT)w)->at += mn_now; 1381 ((WT)w)->at += mn_now;
1332 1382
1333 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.));
1334 1384
1335 ev_start (EV_A_ (W)w, ++timercnt); 1385 ev_start (EV_A_ (W)w, ++timercnt);
1336 array_needsize (struct ev_timer *, timers, timermax, timercnt, (void)); 1386 array_needsize (struct ev_timer *, timers, timermax, timercnt, EMPTY2);
1337 timers [timercnt - 1] = w; 1387 timers [timercnt - 1] = w;
1338 upheap ((WT *)timers, timercnt - 1); 1388 upheap ((WT *)timers, timercnt - 1);
1339 1389
1340 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1390 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1341} 1391}
1342 1392
1343void 1393void
1344ev_timer_stop (EV_P_ struct ev_timer *w) 1394ev_timer_stop (EV_P_ struct ev_timer *w)
1345{ 1395{
1346 ev_clear_pending (EV_A_ (W)w); 1396 ev_clear_pending (EV_A_ (W)w);
1347 if (!ev_is_active (w)) 1397 if (expect_false (!ev_is_active (w)))
1348 return; 1398 return;
1349 1399
1350 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1400 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1351 1401
1352 if (((W)w)->active < timercnt--) 1402 if (expect_true (((W)w)->active < timercnt--))
1353 { 1403 {
1354 timers [((W)w)->active - 1] = timers [timercnt]; 1404 timers [((W)w)->active - 1] = timers [timercnt];
1355 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1); 1405 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1356 } 1406 }
1357 1407
1372 } 1422 }
1373 else 1423 else
1374 ev_timer_stop (EV_A_ w); 1424 ev_timer_stop (EV_A_ w);
1375 } 1425 }
1376 else if (w->repeat) 1426 else if (w->repeat)
1427 {
1428 w->at = w->repeat;
1377 ev_timer_start (EV_A_ w); 1429 ev_timer_start (EV_A_ w);
1430 }
1378} 1431}
1379 1432
1380#if EV_PERIODICS 1433#if EV_PERIODICS
1381void 1434void
1382ev_periodic_start (EV_P_ struct ev_periodic *w) 1435ev_periodic_start (EV_P_ struct ev_periodic *w)
1383{ 1436{
1384 if (ev_is_active (w)) 1437 if (expect_false (ev_is_active (w)))
1385 return; 1438 return;
1386 1439
1387 if (w->reschedule_cb) 1440 if (w->reschedule_cb)
1388 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 1441 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1389 else if (w->interval) 1442 else if (w->interval)
1392 /* 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 */
1393 ((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;
1394 } 1447 }
1395 1448
1396 ev_start (EV_A_ (W)w, ++periodiccnt); 1449 ev_start (EV_A_ (W)w, ++periodiccnt);
1397 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); 1450 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2);
1398 periodics [periodiccnt - 1] = w; 1451 periodics [periodiccnt - 1] = w;
1399 upheap ((WT *)periodics, periodiccnt - 1); 1452 upheap ((WT *)periodics, periodiccnt - 1);
1400 1453
1401 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1454 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1402} 1455}
1403 1456
1404void 1457void
1405ev_periodic_stop (EV_P_ struct ev_periodic *w) 1458ev_periodic_stop (EV_P_ struct ev_periodic *w)
1406{ 1459{
1407 ev_clear_pending (EV_A_ (W)w); 1460 ev_clear_pending (EV_A_ (W)w);
1408 if (!ev_is_active (w)) 1461 if (expect_false (!ev_is_active (w)))
1409 return; 1462 return;
1410 1463
1411 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1464 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1412 1465
1413 if (((W)w)->active < periodiccnt--) 1466 if (expect_true (((W)w)->active < periodiccnt--))
1414 { 1467 {
1415 periodics [((W)w)->active - 1] = periodics [periodiccnt]; 1468 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1416 adjustheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); 1469 adjustheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1417 } 1470 }
1418 1471
1429#endif 1482#endif
1430 1483
1431void 1484void
1432ev_idle_start (EV_P_ struct ev_idle *w) 1485ev_idle_start (EV_P_ struct ev_idle *w)
1433{ 1486{
1434 if (ev_is_active (w)) 1487 if (expect_false (ev_is_active (w)))
1435 return; 1488 return;
1436 1489
1437 ev_start (EV_A_ (W)w, ++idlecnt); 1490 ev_start (EV_A_ (W)w, ++idlecnt);
1438 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, (void)); 1491 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, EMPTY2);
1439 idles [idlecnt - 1] = w; 1492 idles [idlecnt - 1] = w;
1440} 1493}
1441 1494
1442void 1495void
1443ev_idle_stop (EV_P_ struct ev_idle *w) 1496ev_idle_stop (EV_P_ struct ev_idle *w)
1444{ 1497{
1445 ev_clear_pending (EV_A_ (W)w); 1498 ev_clear_pending (EV_A_ (W)w);
1446 if (!ev_is_active (w)) 1499 if (expect_false (!ev_is_active (w)))
1447 return; 1500 return;
1448 1501
1449 idles [((W)w)->active - 1] = idles [--idlecnt]; 1502 idles [((W)w)->active - 1] = idles [--idlecnt];
1450 ev_stop (EV_A_ (W)w); 1503 ev_stop (EV_A_ (W)w);
1451} 1504}
1452 1505
1453void 1506void
1454ev_prepare_start (EV_P_ struct ev_prepare *w) 1507ev_prepare_start (EV_P_ struct ev_prepare *w)
1455{ 1508{
1456 if (ev_is_active (w)) 1509 if (expect_false (ev_is_active (w)))
1457 return; 1510 return;
1458 1511
1459 ev_start (EV_A_ (W)w, ++preparecnt); 1512 ev_start (EV_A_ (W)w, ++preparecnt);
1460 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, (void)); 1513 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, EMPTY2);
1461 prepares [preparecnt - 1] = w; 1514 prepares [preparecnt - 1] = w;
1462} 1515}
1463 1516
1464void 1517void
1465ev_prepare_stop (EV_P_ struct ev_prepare *w) 1518ev_prepare_stop (EV_P_ struct ev_prepare *w)
1466{ 1519{
1467 ev_clear_pending (EV_A_ (W)w); 1520 ev_clear_pending (EV_A_ (W)w);
1468 if (!ev_is_active (w)) 1521 if (expect_false (!ev_is_active (w)))
1469 return; 1522 return;
1470 1523
1471 prepares [((W)w)->active - 1] = prepares [--preparecnt]; 1524 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1472 ev_stop (EV_A_ (W)w); 1525 ev_stop (EV_A_ (W)w);
1473} 1526}
1474 1527
1475void 1528void
1476ev_check_start (EV_P_ struct ev_check *w) 1529ev_check_start (EV_P_ struct ev_check *w)
1477{ 1530{
1478 if (ev_is_active (w)) 1531 if (expect_false (ev_is_active (w)))
1479 return; 1532 return;
1480 1533
1481 ev_start (EV_A_ (W)w, ++checkcnt); 1534 ev_start (EV_A_ (W)w, ++checkcnt);
1482 array_needsize (struct ev_check *, checks, checkmax, checkcnt, (void)); 1535 array_needsize (struct ev_check *, checks, checkmax, checkcnt, EMPTY2);
1483 checks [checkcnt - 1] = w; 1536 checks [checkcnt - 1] = w;
1484} 1537}
1485 1538
1486void 1539void
1487ev_check_stop (EV_P_ struct ev_check *w) 1540ev_check_stop (EV_P_ struct ev_check *w)
1488{ 1541{
1489 ev_clear_pending (EV_A_ (W)w); 1542 ev_clear_pending (EV_A_ (W)w);
1490 if (!ev_is_active (w)) 1543 if (expect_false (!ev_is_active (w)))
1491 return; 1544 return;
1492 1545
1493 checks [((W)w)->active - 1] = checks [--checkcnt]; 1546 checks [((W)w)->active - 1] = checks [--checkcnt];
1494 ev_stop (EV_A_ (W)w); 1547 ev_stop (EV_A_ (W)w);
1495} 1548}
1500 1553
1501void 1554void
1502ev_signal_start (EV_P_ struct ev_signal *w) 1555ev_signal_start (EV_P_ struct ev_signal *w)
1503{ 1556{
1504#if EV_MULTIPLICITY 1557#if EV_MULTIPLICITY
1505 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));
1506#endif 1559#endif
1507 if (ev_is_active (w)) 1560 if (expect_false (ev_is_active (w)))
1508 return; 1561 return;
1509 1562
1510 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));
1511 1564
1512 ev_start (EV_A_ (W)w, 1); 1565 ev_start (EV_A_ (W)w, 1);
1529 1582
1530void 1583void
1531ev_signal_stop (EV_P_ struct ev_signal *w) 1584ev_signal_stop (EV_P_ struct ev_signal *w)
1532{ 1585{
1533 ev_clear_pending (EV_A_ (W)w); 1586 ev_clear_pending (EV_A_ (W)w);
1534 if (!ev_is_active (w)) 1587 if (expect_false (!ev_is_active (w)))
1535 return; 1588 return;
1536 1589
1537 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 1590 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
1538 ev_stop (EV_A_ (W)w); 1591 ev_stop (EV_A_ (W)w);
1539 1592
1543 1596
1544void 1597void
1545ev_child_start (EV_P_ struct ev_child *w) 1598ev_child_start (EV_P_ struct ev_child *w)
1546{ 1599{
1547#if EV_MULTIPLICITY 1600#if EV_MULTIPLICITY
1548 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));
1549#endif 1602#endif
1550 if (ev_is_active (w)) 1603 if (expect_false (ev_is_active (w)))
1551 return; 1604 return;
1552 1605
1553 ev_start (EV_A_ (W)w, 1); 1606 ev_start (EV_A_ (W)w, 1);
1554 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1607 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1555} 1608}
1556 1609
1557void 1610void
1558ev_child_stop (EV_P_ struct ev_child *w) 1611ev_child_stop (EV_P_ struct ev_child *w)
1559{ 1612{
1560 ev_clear_pending (EV_A_ (W)w); 1613 ev_clear_pending (EV_A_ (W)w);
1561 if (!ev_is_active (w)) 1614 if (expect_false (!ev_is_active (w)))
1562 return; 1615 return;
1563 1616
1564 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1617 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1565 ev_stop (EV_A_ (W)w); 1618 ev_stop (EV_A_ (W)w);
1566} 1619}
1603void 1656void
1604ev_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)
1605{ 1658{
1606 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));
1607 1660
1608 if (!once) 1661 if (expect_false (!once))
1662 {
1609 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); 1663 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1610 else 1664 return;
1611 { 1665 }
1666
1612 once->cb = cb; 1667 once->cb = cb;
1613 once->arg = arg; 1668 once->arg = arg;
1614 1669
1615 ev_init (&once->io, once_cb_io); 1670 ev_init (&once->io, once_cb_io);
1616 if (fd >= 0) 1671 if (fd >= 0)
1617 { 1672 {
1618 ev_io_set (&once->io, fd, events); 1673 ev_io_set (&once->io, fd, events);
1619 ev_io_start (EV_A_ &once->io); 1674 ev_io_start (EV_A_ &once->io);
1620 } 1675 }
1621 1676
1622 ev_init (&once->to, once_cb_to); 1677 ev_init (&once->to, once_cb_to);
1623 if (timeout >= 0.) 1678 if (timeout >= 0.)
1624 { 1679 {
1625 ev_timer_set (&once->to, timeout, 0.); 1680 ev_timer_set (&once->to, timeout, 0.);
1626 ev_timer_start (EV_A_ &once->to); 1681 ev_timer_start (EV_A_ &once->to);
1627 }
1628 } 1682 }
1629} 1683}
1630 1684
1631#ifdef __cplusplus 1685#ifdef __cplusplus
1632} 1686}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines