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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines