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

Comparing libev/ev.c (file contents):
Revision 1.85 by root, Sat Nov 10 03:13:50 2007 UTC vs.
Revision 1.110 by root, Mon Nov 12 05:56:49 2007 UTC

26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */ 30 */
31
32#ifdef __cplusplus
33extern "C" {
34#endif
35
31#ifndef EV_STANDALONE 36#ifndef EV_STANDALONE
32# include "config.h" 37# include "config.h"
33 38
34# if HAVE_CLOCK_GETTIME 39# if HAVE_CLOCK_GETTIME
40# ifndef EV_USE_MONOTONIC
35# define EV_USE_MONOTONIC 1 41# define EV_USE_MONOTONIC 1
42# endif
43# ifndef EV_USE_REALTIME
36# define EV_USE_REALTIME 1 44# define EV_USE_REALTIME 1
45# endif
37# endif 46# endif
38 47
39# if HAVE_SELECT && HAVE_SYS_SELECT_H 48# if HAVE_SELECT && HAVE_SYS_SELECT_H && !defined (EV_USE_SELECT)
40# define EV_USE_SELECT 1 49# define EV_USE_SELECT 1
41# endif 50# endif
42 51
43# if HAVE_POLL && HAVE_POLL_H 52# if HAVE_POLL && HAVE_POLL_H && !defined (EV_USE_POLL)
44# define EV_USE_POLL 1 53# define EV_USE_POLL 1
45# endif 54# endif
46 55
47# if HAVE_EPOLL && HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H 56# if HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H && !defined (EV_USE_EPOLL)
48# define EV_USE_EPOLL 1 57# define EV_USE_EPOLL 1
49# endif 58# endif
50 59
51# if HAVE_KQUEUE && HAVE_WORKING_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H 60# if HAVE_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H && !defined (EV_USE_KQUEUE)
52# define EV_USE_KQUEUE 1 61# define EV_USE_KQUEUE 1
53# endif 62# endif
54 63
55#endif 64#endif
56 65
66#include <sys/types.h> 75#include <sys/types.h>
67#include <time.h> 76#include <time.h>
68 77
69#include <signal.h> 78#include <signal.h>
70 79
71#ifndef WIN32 80#ifndef _WIN32
72# include <unistd.h> 81# include <unistd.h>
73# include <sys/time.h> 82# include <sys/time.h>
74# include <sys/wait.h> 83# include <sys/wait.h>
84#else
85# define WIN32_LEAN_AND_MEAN
86# include <windows.h>
87# ifndef EV_SELECT_IS_WINSOCKET
88# define EV_SELECT_IS_WINSOCKET 1
75#endif 89# endif
90#endif
91
76/**/ 92/**/
77 93
78#ifndef EV_USE_MONOTONIC 94#ifndef EV_USE_MONOTONIC
79# define EV_USE_MONOTONIC 1 95# define EV_USE_MONOTONIC 1
80#endif 96#endif
81 97
82#ifndef EV_USE_SELECT 98#ifndef EV_USE_SELECT
83# define EV_USE_SELECT 1 99# define EV_USE_SELECT 1
100# define EV_SELECT_USE_FD_SET 1
84#endif 101#endif
85 102
86#ifndef EV_USE_POLL 103#ifndef EV_USE_POLL
87# define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */ 104# ifdef _WIN32
105# define EV_USE_POLL 0
106# else
107# define EV_USE_POLL 1
108# endif
88#endif 109#endif
89 110
90#ifndef EV_USE_EPOLL 111#ifndef EV_USE_EPOLL
91# define EV_USE_EPOLL 0 112# define EV_USE_EPOLL 0
92#endif 113#endif
93 114
94#ifndef EV_USE_KQUEUE 115#ifndef EV_USE_KQUEUE
95# define EV_USE_KQUEUE 0 116# define EV_USE_KQUEUE 0
96#endif 117#endif
97 118
98#ifndef EV_USE_WIN32
99# ifdef WIN32
100# define EV_USE_WIN32 0 /* it does not exist, use select */
101# undef EV_USE_SELECT
102# define EV_USE_SELECT 1
103# else
104# define EV_USE_WIN32 0
105# endif
106#endif
107
108#ifndef EV_USE_REALTIME 119#ifndef EV_USE_REALTIME
109# define EV_USE_REALTIME 1 120# define EV_USE_REALTIME 1
110#endif 121#endif
111 122
112/**/ 123/**/
124
125/* darwin simply cannot be helped */
126#ifdef __APPLE__
127# undef EV_USE_POLL
128# undef EV_USE_KQUEUE
129#endif
113 130
114#ifndef CLOCK_MONOTONIC 131#ifndef CLOCK_MONOTONIC
115# undef EV_USE_MONOTONIC 132# undef EV_USE_MONOTONIC
116# define EV_USE_MONOTONIC 0 133# define EV_USE_MONOTONIC 0
117#endif 134#endif
118 135
119#ifndef CLOCK_REALTIME 136#ifndef CLOCK_REALTIME
120# undef EV_USE_REALTIME 137# undef EV_USE_REALTIME
121# define EV_USE_REALTIME 0 138# define EV_USE_REALTIME 0
139#endif
140
141#if EV_SELECT_IS_WINSOCKET
142# include <winsock.h>
122#endif 143#endif
123 144
124/**/ 145/**/
125 146
126#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 147#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
146#define expect_true(expr) expect ((expr) != 0, 1) 167#define expect_true(expr) expect ((expr) != 0, 1)
147 168
148#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) 169#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
149#define ABSPRI(w) ((w)->priority - EV_MINPRI) 170#define ABSPRI(w) ((w)->priority - EV_MINPRI)
150 171
172#define EMPTY /* required for microsofts broken pseudo-c compiler */
173
151typedef struct ev_watcher *W; 174typedef struct ev_watcher *W;
152typedef struct ev_watcher_list *WL; 175typedef struct ev_watcher_list *WL;
153typedef struct ev_watcher_time *WT; 176typedef struct ev_watcher_time *WT;
154 177
155static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 178static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
156 179
180#ifdef _WIN32
157#include "ev_win32.c" 181# include "ev_win32.c"
182#endif
158 183
159/*****************************************************************************/ 184/*****************************************************************************/
160 185
161static void (*syserr_cb)(const char *msg); 186static void (*syserr_cb)(const char *msg);
162 187
209typedef struct 234typedef struct
210{ 235{
211 WL head; 236 WL head;
212 unsigned char events; 237 unsigned char events;
213 unsigned char reify; 238 unsigned char reify;
239#if EV_SELECT_IS_WINSOCKET
240 SOCKET handle;
241#endif
214} ANFD; 242} ANFD;
215 243
216typedef struct 244typedef struct
217{ 245{
218 W w; 246 W w;
221 249
222#if EV_MULTIPLICITY 250#if EV_MULTIPLICITY
223 251
224 struct ev_loop 252 struct ev_loop
225 { 253 {
254 ev_tstamp ev_rt_now;
255 #define ev_rt_now ((loop)->ev_rt_now)
226 #define VAR(name,decl) decl; 256 #define VAR(name,decl) decl;
227 #include "ev_vars.h" 257 #include "ev_vars.h"
228 #undef VAR 258 #undef VAR
229 }; 259 };
230 #include "ev_wrap.h" 260 #include "ev_wrap.h"
232 struct ev_loop default_loop_struct; 262 struct ev_loop default_loop_struct;
233 static struct ev_loop *default_loop; 263 static struct ev_loop *default_loop;
234 264
235#else 265#else
236 266
267 ev_tstamp ev_rt_now;
237 #define VAR(name,decl) static decl; 268 #define VAR(name,decl) static decl;
238 #include "ev_vars.h" 269 #include "ev_vars.h"
239 #undef VAR 270 #undef VAR
240 271
241 static int default_loop; 272 static int default_loop;
242 273
243#endif 274#endif
244 275
245/*****************************************************************************/ 276/*****************************************************************************/
246 277
247inline ev_tstamp 278ev_tstamp
248ev_time (void) 279ev_time (void)
249{ 280{
250#if EV_USE_REALTIME 281#if EV_USE_REALTIME
251 struct timespec ts; 282 struct timespec ts;
252 clock_gettime (CLOCK_REALTIME, &ts); 283 clock_gettime (CLOCK_REALTIME, &ts);
279{ 310{
280 return ev_rt_now; 311 return ev_rt_now;
281} 312}
282#endif 313#endif
283 314
284#define array_roundsize(type,n) ((n) | 4 & ~3) 315#define array_roundsize(type,n) (((n) | 4) & ~3)
285 316
286#define array_needsize(type,base,cur,cnt,init) \ 317#define array_needsize(type,base,cur,cnt,init) \
287 if (expect_false ((cnt) > cur)) \ 318 if (expect_false ((cnt) > cur)) \
288 { \ 319 { \
289 int newcnt = cur; \ 320 int newcnt = cur; \
304 stem ## max = array_roundsize (stem ## cnt >> 1); \ 335 stem ## max = array_roundsize (stem ## cnt >> 1); \
305 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ 336 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\
306 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ 337 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
307 } 338 }
308 339
309/* microsoft's pseudo-c is quite far from C as the rest of the world and the standard knows it */
310/* bringing us everlasting joy in form of stupid extra macros that are not required in C */
311#define array_free_microshit(stem) \
312 ev_free (stem ## s); stem ## cnt = stem ## max = 0;
313
314#define array_free(stem, idx) \ 340#define array_free(stem, idx) \
315 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; 341 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
316 342
317/*****************************************************************************/ 343/*****************************************************************************/
318 344
392 int events = 0; 418 int events = 0;
393 419
394 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) 420 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
395 events |= w->events; 421 events |= w->events;
396 422
423#if EV_SELECT_IS_WINSOCKET
424 if (events)
425 {
426 unsigned long argp;
427 anfd->handle = _get_osfhandle (fd);
428 assert (("libev only supports socket fds in this configuration", ioctlsocket (anfd->handle, FIONREAD, &argp) == 0));
429 }
430#endif
431
397 anfd->reify = 0; 432 anfd->reify = 0;
398 433
399 method_modify (EV_A_ fd, anfd->events, events); 434 method_modify (EV_A_ fd, anfd->events, events);
400 anfd->events = events; 435 anfd->events = events;
401 } 436 }
429} 464}
430 465
431static int 466static int
432fd_valid (int fd) 467fd_valid (int fd)
433{ 468{
434#ifdef WIN32 469#ifdef _WIN32
435 return !!win32_get_osfhandle (fd); 470 return _get_osfhandle (fd) != -1;
436#else 471#else
437 return fcntl (fd, F_GETFD) != -1; 472 return fcntl (fd, F_GETFD) != -1;
438#endif 473#endif
439} 474}
440 475
521 heap [k] = w; 556 heap [k] = w;
522 ((W)heap [k])->active = k + 1; 557 ((W)heap [k])->active = k + 1;
523} 558}
524 559
525inline void 560inline void
526adjustheap (WT *heap, int N, int k, ev_tstamp at) 561adjustheap (WT *heap, int N, int k)
527{ 562{
528 ev_tstamp old_at = heap [k]->at; 563 upheap (heap, k);
529 heap [k]->at = at;
530
531 if (old_at < at)
532 downheap (heap, N, k); 564 downheap (heap, N, k);
533 else
534 upheap (heap, k);
535} 565}
536 566
537/*****************************************************************************/ 567/*****************************************************************************/
538 568
539typedef struct 569typedef struct
562} 592}
563 593
564static void 594static void
565sighandler (int signum) 595sighandler (int signum)
566{ 596{
567#if WIN32 597#if _WIN32
568 signal (signum, sighandler); 598 signal (signum, sighandler);
569#endif 599#endif
570 600
571 signals [signum - 1].gotsig = 1; 601 signals [signum - 1].gotsig = 1;
572 602
573 if (!gotsig) 603 if (!gotsig)
574 { 604 {
575 int old_errno = errno; 605 int old_errno = errno;
576 gotsig = 1; 606 gotsig = 1;
577#ifdef WIN32
578 send (sigpipe [1], &signum, 1, MSG_DONTWAIT);
579#else
580 write (sigpipe [1], &signum, 1); 607 write (sigpipe [1], &signum, 1);
581#endif
582 errno = old_errno; 608 errno = old_errno;
583 } 609 }
584} 610}
585 611
586void 612void
606static void 632static void
607sigcb (EV_P_ struct ev_io *iow, int revents) 633sigcb (EV_P_ struct ev_io *iow, int revents)
608{ 634{
609 int signum; 635 int signum;
610 636
611#ifdef WIN32
612 recv (sigpipe [0], &revents, 1, MSG_DONTWAIT);
613#else
614 read (sigpipe [0], &revents, 1); 637 read (sigpipe [0], &revents, 1);
615#endif
616 gotsig = 0; 638 gotsig = 0;
617 639
618 for (signum = signalmax; signum--; ) 640 for (signum = signalmax; signum--; )
619 if (signals [signum].gotsig) 641 if (signals [signum].gotsig)
620 ev_feed_signal_event (EV_A_ signum + 1); 642 ev_feed_signal_event (EV_A_ signum + 1);
621} 643}
622 644
645inline void
646fd_intern (int fd)
647{
648#ifdef _WIN32
649 int arg = 1;
650 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg);
651#else
652 fcntl (fd, F_SETFD, FD_CLOEXEC);
653 fcntl (fd, F_SETFL, O_NONBLOCK);
654#endif
655}
656
623static void 657static void
624siginit (EV_P) 658siginit (EV_P)
625{ 659{
626#ifndef WIN32 660 fd_intern (sigpipe [0]);
627 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); 661 fd_intern (sigpipe [1]);
628 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC);
629
630 /* rather than sort out wether we really need nb, set it */
631 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
632 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
633#endif
634 662
635 ev_io_set (&sigev, sigpipe [0], EV_READ); 663 ev_io_set (&sigev, sigpipe [0], EV_READ);
636 ev_io_start (EV_A_ &sigev); 664 ev_io_start (EV_A_ &sigev);
637 ev_unref (EV_A); /* child watcher should not keep loop alive */ 665 ev_unref (EV_A); /* child watcher should not keep loop alive */
638} 666}
639 667
640/*****************************************************************************/ 668/*****************************************************************************/
641 669
642static struct ev_child *childs [PID_HASHSIZE]; 670static struct ev_child *childs [PID_HASHSIZE];
643 671
644#ifndef WIN32 672#ifndef _WIN32
645 673
646static struct ev_signal childev; 674static struct ev_signal childev;
647 675
648#ifndef WCONTINUED 676#ifndef WCONTINUED
649# define WCONTINUED 0 677# define WCONTINUED 0
710 738
711/* return true if we are running with elevated privileges and should ignore env variables */ 739/* return true if we are running with elevated privileges and should ignore env variables */
712static int 740static int
713enable_secure (void) 741enable_secure (void)
714{ 742{
715#ifdef WIN32 743#ifdef _WIN32
716 return 0; 744 return 0;
717#else 745#else
718 return getuid () != geteuid () 746 return getuid () != geteuid ()
719 || getgid () != getegid (); 747 || getgid () != getegid ();
720#endif 748#endif
725{ 753{
726 return method; 754 return method;
727} 755}
728 756
729static void 757static void
730loop_init (EV_P_ int methods) 758loop_init (EV_P_ unsigned int flags)
731{ 759{
732 if (!method) 760 if (!method)
733 { 761 {
734#if EV_USE_MONOTONIC 762#if EV_USE_MONOTONIC
735 { 763 {
742 ev_rt_now = ev_time (); 770 ev_rt_now = ev_time ();
743 mn_now = get_clock (); 771 mn_now = get_clock ();
744 now_floor = mn_now; 772 now_floor = mn_now;
745 rtmn_diff = ev_rt_now - mn_now; 773 rtmn_diff = ev_rt_now - mn_now;
746 774
747 if (methods == EVMETHOD_AUTO) 775 if (!(flags & EVMETHOD_NOENV) && !enable_secure () && getenv ("LIBEV_FLAGS"))
748 if (!enable_secure () && getenv ("LIBEV_METHODS"))
749 methods = atoi (getenv ("LIBEV_METHODS")); 776 flags = atoi (getenv ("LIBEV_FLAGS"));
750 else 777
751 methods = EVMETHOD_ANY; 778 if (!(flags & 0x0000ffff))
779 flags |= 0x0000ffff;
752 780
753 method = 0; 781 method = 0;
754#if EV_USE_WIN32
755 if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods);
756#endif
757#if EV_USE_KQUEUE 782#if EV_USE_KQUEUE
758 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); 783 if (!method && (flags & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ flags);
759#endif 784#endif
760#if EV_USE_EPOLL 785#if EV_USE_EPOLL
761 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); 786 if (!method && (flags & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ flags);
762#endif 787#endif
763#if EV_USE_POLL 788#if EV_USE_POLL
764 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); 789 if (!method && (flags & EVMETHOD_POLL )) method = poll_init (EV_A_ flags);
765#endif 790#endif
766#if EV_USE_SELECT 791#if EV_USE_SELECT
767 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 792 if (!method && (flags & EVMETHOD_SELECT)) method = select_init (EV_A_ flags);
768#endif 793#endif
769 794
770 ev_init (&sigev, sigcb); 795 ev_init (&sigev, sigcb);
771 ev_set_priority (&sigev, EV_MAXPRI); 796 ev_set_priority (&sigev, EV_MAXPRI);
772 } 797 }
775void 800void
776loop_destroy (EV_P) 801loop_destroy (EV_P)
777{ 802{
778 int i; 803 int i;
779 804
780#if EV_USE_WIN32
781 if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A);
782#endif
783#if EV_USE_KQUEUE 805#if EV_USE_KQUEUE
784 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); 806 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
785#endif 807#endif
786#if EV_USE_EPOLL 808#if EV_USE_EPOLL
787 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); 809 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A);
795 817
796 for (i = NUMPRI; i--; ) 818 for (i = NUMPRI; i--; )
797 array_free (pending, [i]); 819 array_free (pending, [i]);
798 820
799 /* have to use the microsoft-never-gets-it-right macro */ 821 /* have to use the microsoft-never-gets-it-right macro */
800 array_free_microshit (fdchange); 822 array_free (fdchange, EMPTY);
801 array_free_microshit (timer); 823 array_free (timer, EMPTY);
802 array_free_microshit (periodic); 824#if EV_PERIODICS
803 array_free_microshit (idle); 825 array_free (periodic, EMPTY);
804 array_free_microshit (prepare); 826#endif
805 array_free_microshit (check); 827 array_free (idle, EMPTY);
828 array_free (prepare, EMPTY);
829 array_free (check, EMPTY);
806 830
807 method = 0; 831 method = 0;
808} 832}
809 833
810static void 834static void
835 postfork = 0; 859 postfork = 0;
836} 860}
837 861
838#if EV_MULTIPLICITY 862#if EV_MULTIPLICITY
839struct ev_loop * 863struct ev_loop *
840ev_loop_new (int methods) 864ev_loop_new (unsigned int flags)
841{ 865{
842 struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); 866 struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop));
843 867
844 memset (loop, 0, sizeof (struct ev_loop)); 868 memset (loop, 0, sizeof (struct ev_loop));
845 869
846 loop_init (EV_A_ methods); 870 loop_init (EV_A_ flags);
847 871
848 if (ev_method (EV_A)) 872 if (ev_method (EV_A))
849 return loop; 873 return loop;
850 874
851 return 0; 875 return 0;
869#if EV_MULTIPLICITY 893#if EV_MULTIPLICITY
870struct ev_loop * 894struct ev_loop *
871#else 895#else
872int 896int
873#endif 897#endif
874ev_default_loop (int methods) 898ev_default_loop (unsigned int flags)
875{ 899{
876 if (sigpipe [0] == sigpipe [1]) 900 if (sigpipe [0] == sigpipe [1])
877 if (pipe (sigpipe)) 901 if (pipe (sigpipe))
878 return 0; 902 return 0;
879 903
883 struct ev_loop *loop = default_loop = &default_loop_struct; 907 struct ev_loop *loop = default_loop = &default_loop_struct;
884#else 908#else
885 default_loop = 1; 909 default_loop = 1;
886#endif 910#endif
887 911
888 loop_init (EV_A_ methods); 912 loop_init (EV_A_ flags);
889 913
890 if (ev_method (EV_A)) 914 if (ev_method (EV_A))
891 { 915 {
892 siginit (EV_A); 916 siginit (EV_A);
893 917
894#ifndef WIN32 918#ifndef _WIN32
895 ev_signal_init (&childev, childcb, SIGCHLD); 919 ev_signal_init (&childev, childcb, SIGCHLD);
896 ev_set_priority (&childev, EV_MAXPRI); 920 ev_set_priority (&childev, EV_MAXPRI);
897 ev_signal_start (EV_A_ &childev); 921 ev_signal_start (EV_A_ &childev);
898 ev_unref (EV_A); /* child watcher should not keep loop alive */ 922 ev_unref (EV_A); /* child watcher should not keep loop alive */
899#endif 923#endif
910{ 934{
911#if EV_MULTIPLICITY 935#if EV_MULTIPLICITY
912 struct ev_loop *loop = default_loop; 936 struct ev_loop *loop = default_loop;
913#endif 937#endif
914 938
915#ifndef WIN32 939#ifndef _WIN32
916 ev_ref (EV_A); /* child watcher */ 940 ev_ref (EV_A); /* child watcher */
917 ev_signal_stop (EV_A_ &childev); 941 ev_signal_stop (EV_A_ &childev);
918#endif 942#endif
919 943
920 ev_ref (EV_A); /* signal watcher */ 944 ev_ref (EV_A); /* signal watcher */
980 1004
981 /* first reschedule or stop timer */ 1005 /* first reschedule or stop timer */
982 if (w->repeat) 1006 if (w->repeat)
983 { 1007 {
984 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); 1008 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
1009
985 ((WT)w)->at = mn_now + w->repeat; 1010 ((WT)w)->at += w->repeat;
1011 if (((WT)w)->at < mn_now)
1012 ((WT)w)->at = mn_now;
1013
986 downheap ((WT *)timers, timercnt, 0); 1014 downheap ((WT *)timers, timercnt, 0);
987 } 1015 }
988 else 1016 else
989 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 1017 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
990 1018
991 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); 1019 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
992 } 1020 }
993} 1021}
994 1022
1023#if EV_PERIODICS
995static void 1024static void
996periodics_reify (EV_P) 1025periodics_reify (EV_P)
997{ 1026{
998 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) 1027 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
999 { 1028 {
1002 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 1031 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
1003 1032
1004 /* first reschedule or stop timer */ 1033 /* first reschedule or stop timer */
1005 if (w->reschedule_cb) 1034 if (w->reschedule_cb)
1006 { 1035 {
1007 ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001); 1036 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001);
1008
1009 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now)); 1037 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now));
1010 downheap ((WT *)periodics, periodiccnt, 0); 1038 downheap ((WT *)periodics, periodiccnt, 0);
1011 } 1039 }
1012 else if (w->interval) 1040 else if (w->interval)
1013 { 1041 {
1040 1068
1041 /* now rebuild the heap */ 1069 /* now rebuild the heap */
1042 for (i = periodiccnt >> 1; i--; ) 1070 for (i = periodiccnt >> 1; i--; )
1043 downheap ((WT *)periodics, periodiccnt, i); 1071 downheap ((WT *)periodics, periodiccnt, i);
1044} 1072}
1073#endif
1045 1074
1046inline int 1075inline int
1047time_update_monotonic (EV_P) 1076time_update_monotonic (EV_P)
1048{ 1077{
1049 mn_now = get_clock (); 1078 mn_now = get_clock ();
1083 ev_rt_now = ev_time (); 1112 ev_rt_now = ev_time ();
1084 mn_now = get_clock (); 1113 mn_now = get_clock ();
1085 now_floor = mn_now; 1114 now_floor = mn_now;
1086 } 1115 }
1087 1116
1117# if EV_PERIODICS
1088 periodics_reschedule (EV_A); 1118 periodics_reschedule (EV_A);
1119# endif
1089 /* no timer adjustment, as the monotonic clock doesn't jump */ 1120 /* no timer adjustment, as the monotonic clock doesn't jump */
1090 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ 1121 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */
1091 } 1122 }
1092 } 1123 }
1093 else 1124 else
1095 { 1126 {
1096 ev_rt_now = ev_time (); 1127 ev_rt_now = ev_time ();
1097 1128
1098 if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) 1129 if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
1099 { 1130 {
1131#if EV_PERIODICS
1100 periodics_reschedule (EV_A); 1132 periodics_reschedule (EV_A);
1133#endif
1101 1134
1102 /* adjust timers. this is easy, as the offset is the same for all */ 1135 /* adjust timers. this is easy, as the offset is the same for all */
1103 for (i = 0; i < timercnt; ++i) 1136 for (i = 0; i < timercnt; ++i)
1104 ((WT)timers [i])->at += ev_rt_now - mn_now; 1137 ((WT)timers [i])->at += ev_rt_now - mn_now;
1105 } 1138 }
1168 { 1201 {
1169 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; 1202 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge;
1170 if (block > to) block = to; 1203 if (block > to) block = to;
1171 } 1204 }
1172 1205
1206#if EV_PERIODICS
1173 if (periodiccnt) 1207 if (periodiccnt)
1174 { 1208 {
1175 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge; 1209 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge;
1176 if (block > to) block = to; 1210 if (block > to) block = to;
1177 } 1211 }
1212#endif
1178 1213
1179 if (block < 0.) block = 0.; 1214 if (block < 0.) block = 0.;
1180 } 1215 }
1181 1216
1182 method_poll (EV_A_ block); 1217 method_poll (EV_A_ block);
1184 /* update ev_rt_now, do magic */ 1219 /* update ev_rt_now, do magic */
1185 time_update (EV_A); 1220 time_update (EV_A);
1186 1221
1187 /* queue pending timers and reschedule them */ 1222 /* queue pending timers and reschedule them */
1188 timers_reify (EV_A); /* relative timers called last */ 1223 timers_reify (EV_A); /* relative timers called last */
1224#if EV_PERIODICS
1189 periodics_reify (EV_A); /* absolute timers called first */ 1225 periodics_reify (EV_A); /* absolute timers called first */
1226#endif
1190 1227
1191 /* queue idle watchers unless io or timers are pending */ 1228 /* queue idle watchers unless io or timers are pending */
1192 if (idlecnt && !any_pending (EV_A)) 1229 if (idlecnt && !any_pending (EV_A))
1193 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1230 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
1194 1231
1285{ 1322{
1286 ev_clear_pending (EV_A_ (W)w); 1323 ev_clear_pending (EV_A_ (W)w);
1287 if (!ev_is_active (w)) 1324 if (!ev_is_active (w))
1288 return; 1325 return;
1289 1326
1327 assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax));
1328
1290 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 1329 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
1291 ev_stop (EV_A_ (W)w); 1330 ev_stop (EV_A_ (W)w);
1292 1331
1293 fd_change (EV_A_ w->fd); 1332 fd_change (EV_A_ w->fd);
1294} 1333}
1321 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1360 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1322 1361
1323 if (((W)w)->active < timercnt--) 1362 if (((W)w)->active < timercnt--)
1324 { 1363 {
1325 timers [((W)w)->active - 1] = timers [timercnt]; 1364 timers [((W)w)->active - 1] = timers [timercnt];
1326 downheap ((WT *)timers, timercnt, ((W)w)->active - 1); 1365 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1327 } 1366 }
1328 1367
1329 ((WT)w)->at = w->repeat; 1368 ((WT)w)->at -= mn_now;
1330 1369
1331 ev_stop (EV_A_ (W)w); 1370 ev_stop (EV_A_ (W)w);
1332} 1371}
1333 1372
1334void 1373void
1335ev_timer_again (EV_P_ struct ev_timer *w) 1374ev_timer_again (EV_P_ struct ev_timer *w)
1336{ 1375{
1337 if (ev_is_active (w)) 1376 if (ev_is_active (w))
1338 { 1377 {
1339 if (w->repeat) 1378 if (w->repeat)
1379 {
1380 ((WT)w)->at = mn_now + w->repeat;
1340 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1, mn_now + w->repeat); 1381 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1382 }
1341 else 1383 else
1342 ev_timer_stop (EV_A_ w); 1384 ev_timer_stop (EV_A_ w);
1343 } 1385 }
1344 else if (w->repeat) 1386 else if (w->repeat)
1345 ev_timer_start (EV_A_ w); 1387 ev_timer_start (EV_A_ w);
1346} 1388}
1347 1389
1390#if EV_PERIODICS
1348void 1391void
1349ev_periodic_start (EV_P_ struct ev_periodic *w) 1392ev_periodic_start (EV_P_ struct ev_periodic *w)
1350{ 1393{
1351 if (ev_is_active (w)) 1394 if (ev_is_active (w))
1352 return; 1395 return;
1378 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1421 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1379 1422
1380 if (((W)w)->active < periodiccnt--) 1423 if (((W)w)->active < periodiccnt--)
1381 { 1424 {
1382 periodics [((W)w)->active - 1] = periodics [periodiccnt]; 1425 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1383 downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); 1426 adjustheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1384 } 1427 }
1385 1428
1386 ev_stop (EV_A_ (W)w); 1429 ev_stop (EV_A_ (W)w);
1387} 1430}
1388 1431
1391{ 1434{
1392 /* TODO: use adjustheap and recalculation */ 1435 /* TODO: use adjustheap and recalculation */
1393 ev_periodic_stop (EV_A_ w); 1436 ev_periodic_stop (EV_A_ w);
1394 ev_periodic_start (EV_A_ w); 1437 ev_periodic_start (EV_A_ w);
1395} 1438}
1439#endif
1396 1440
1397void 1441void
1398ev_idle_start (EV_P_ struct ev_idle *w) 1442ev_idle_start (EV_P_ struct ev_idle *w)
1399{ 1443{
1400 if (ev_is_active (w)) 1444 if (ev_is_active (w))
1407 1451
1408void 1452void
1409ev_idle_stop (EV_P_ struct ev_idle *w) 1453ev_idle_stop (EV_P_ struct ev_idle *w)
1410{ 1454{
1411 ev_clear_pending (EV_A_ (W)w); 1455 ev_clear_pending (EV_A_ (W)w);
1412 if (ev_is_active (w)) 1456 if (!ev_is_active (w))
1413 return; 1457 return;
1414 1458
1415 idles [((W)w)->active - 1] = idles [--idlecnt]; 1459 idles [((W)w)->active - 1] = idles [--idlecnt];
1416 ev_stop (EV_A_ (W)w); 1460 ev_stop (EV_A_ (W)w);
1417} 1461}
1429 1473
1430void 1474void
1431ev_prepare_stop (EV_P_ struct ev_prepare *w) 1475ev_prepare_stop (EV_P_ struct ev_prepare *w)
1432{ 1476{
1433 ev_clear_pending (EV_A_ (W)w); 1477 ev_clear_pending (EV_A_ (W)w);
1434 if (ev_is_active (w)) 1478 if (!ev_is_active (w))
1435 return; 1479 return;
1436 1480
1437 prepares [((W)w)->active - 1] = prepares [--preparecnt]; 1481 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1438 ev_stop (EV_A_ (W)w); 1482 ev_stop (EV_A_ (W)w);
1439} 1483}
1451 1495
1452void 1496void
1453ev_check_stop (EV_P_ struct ev_check *w) 1497ev_check_stop (EV_P_ struct ev_check *w)
1454{ 1498{
1455 ev_clear_pending (EV_A_ (W)w); 1499 ev_clear_pending (EV_A_ (W)w);
1456 if (ev_is_active (w)) 1500 if (!ev_is_active (w))
1457 return; 1501 return;
1458 1502
1459 checks [((W)w)->active - 1] = checks [--checkcnt]; 1503 checks [((W)w)->active - 1] = checks [--checkcnt];
1460 ev_stop (EV_A_ (W)w); 1504 ev_stop (EV_A_ (W)w);
1461} 1505}
1479 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init); 1523 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init);
1480 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1524 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1481 1525
1482 if (!((WL)w)->next) 1526 if (!((WL)w)->next)
1483 { 1527 {
1484#if WIN32 1528#if _WIN32
1485 signal (w->signum, sighandler); 1529 signal (w->signum, sighandler);
1486#else 1530#else
1487 struct sigaction sa; 1531 struct sigaction sa;
1488 sa.sa_handler = sighandler; 1532 sa.sa_handler = sighandler;
1489 sigfillset (&sa.sa_mask); 1533 sigfillset (&sa.sa_mask);
1522 1566
1523void 1567void
1524ev_child_stop (EV_P_ struct ev_child *w) 1568ev_child_stop (EV_P_ struct ev_child *w)
1525{ 1569{
1526 ev_clear_pending (EV_A_ (W)w); 1570 ev_clear_pending (EV_A_ (W)w);
1527 if (ev_is_active (w)) 1571 if (!ev_is_active (w))
1528 return; 1572 return;
1529 1573
1530 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1574 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1531 ev_stop (EV_A_ (W)w); 1575 ev_stop (EV_A_ (W)w);
1532} 1576}
1592 ev_timer_start (EV_A_ &once->to); 1636 ev_timer_start (EV_A_ &once->to);
1593 } 1637 }
1594 } 1638 }
1595} 1639}
1596 1640
1641#ifdef __cplusplus
1642}
1643#endif
1644

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines