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

Comparing libev/ev.c (file contents):
Revision 1.28 by root, Thu Nov 1 06:48:49 2007 UTC vs.
Revision 1.41 by root, Fri Nov 2 16:54:34 2007 UTC

1/* 1/*
2 * libev event processing core, watcher management
3 *
2 * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> 4 * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de>
3 * All rights reserved. 5 * All rights reserved.
4 * 6 *
5 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are 8 * modification, are permitted provided that the following conditions are
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (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
27 * 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.
28 */ 30 */
31#if EV_USE_CONFIG_H
32# include "config.h"
33#endif
29 34
30#include <math.h> 35#include <math.h>
31#include <stdlib.h> 36#include <stdlib.h>
32#include <unistd.h> 37#include <unistd.h>
33#include <fcntl.h> 38#include <fcntl.h>
41#include <sys/types.h> 46#include <sys/types.h>
42#include <sys/wait.h> 47#include <sys/wait.h>
43#include <sys/time.h> 48#include <sys/time.h>
44#include <time.h> 49#include <time.h>
45 50
51/**/
52
46#ifndef HAVE_MONOTONIC 53#ifndef EV_USE_MONOTONIC
54# define EV_USE_MONOTONIC 1
55#endif
56
57#ifndef EV_USE_SELECT
58# define EV_USE_SELECT 1
59#endif
60
61#ifndef EV_USE_POLL
62# define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */
63#endif
64
65#ifndef EV_USE_EPOLL
66# define EV_USE_EPOLL 0
67#endif
68
69#ifndef EV_USE_REALTIME
70# define EV_USE_REALTIME 1
71#endif
72
73/**/
74
47# ifdef CLOCK_MONOTONIC 75#ifndef CLOCK_MONOTONIC
76# undef EV_USE_MONOTONIC
48# define HAVE_MONOTONIC 1 77# define EV_USE_MONOTONIC 0
49# endif 78#endif
50#endif
51 79
52#ifndef HAVE_SELECT
53# define HAVE_SELECT 1
54#endif
55
56#ifndef HAVE_EPOLL
57# define HAVE_EPOLL 0
58#endif
59
60#ifndef HAVE_REALTIME 80#ifndef CLOCK_REALTIME
61# define HAVE_REALTIME 1 /* posix requirement, but might be slower */ 81# undef EV_USE_REALTIME
82# define EV_USE_REALTIME 0
62#endif 83#endif
84
85/**/
63 86
64#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 87#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
65#define MAX_BLOCKTIME 60. 88#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */
66#define PID_HASHSIZE 16 /* size of pid hahs table, must be power of two */ 89#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */
90/*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */
67 91
68#include "ev.h" 92#include "ev.h"
93
94#if __GNUC__ >= 3
95# define expect(expr,value) __builtin_expect ((expr),(value))
96# define inline inline
97#else
98# define expect(expr,value) (expr)
99# define inline static
100#endif
101
102#define expect_false(expr) expect ((expr) != 0, 0)
103#define expect_true(expr) expect ((expr) != 0, 1)
69 104
70typedef struct ev_watcher *W; 105typedef struct ev_watcher *W;
71typedef struct ev_watcher_list *WL; 106typedef struct ev_watcher_list *WL;
72typedef struct ev_watcher_time *WT; 107typedef struct ev_watcher_time *WT;
73 108
74static ev_tstamp now, diff; /* monotonic clock */ 109static ev_tstamp now_floor, now, diff; /* monotonic clock */
75ev_tstamp ev_now; 110ev_tstamp ev_now;
76int ev_method; 111int ev_method;
77 112
78static int have_monotonic; /* runtime */ 113static int have_monotonic; /* runtime */
79 114
84/*****************************************************************************/ 119/*****************************************************************************/
85 120
86ev_tstamp 121ev_tstamp
87ev_time (void) 122ev_time (void)
88{ 123{
89#if HAVE_REALTIME 124#if EV_USE_REALTIME
90 struct timespec ts; 125 struct timespec ts;
91 clock_gettime (CLOCK_REALTIME, &ts); 126 clock_gettime (CLOCK_REALTIME, &ts);
92 return ts.tv_sec + ts.tv_nsec * 1e-9; 127 return ts.tv_sec + ts.tv_nsec * 1e-9;
93#else 128#else
94 struct timeval tv; 129 struct timeval tv;
98} 133}
99 134
100static ev_tstamp 135static ev_tstamp
101get_clock (void) 136get_clock (void)
102{ 137{
103#if HAVE_MONOTONIC 138#if EV_USE_MONOTONIC
104 if (have_monotonic) 139 if (expect_true (have_monotonic))
105 { 140 {
106 struct timespec ts; 141 struct timespec ts;
107 clock_gettime (CLOCK_MONOTONIC, &ts); 142 clock_gettime (CLOCK_MONOTONIC, &ts);
108 return ts.tv_sec + ts.tv_nsec * 1e-9; 143 return ts.tv_sec + ts.tv_nsec * 1e-9;
109 } 144 }
110#endif 145#endif
111 146
112 return ev_time (); 147 return ev_time ();
113} 148}
114 149
150#define array_roundsize(base,n) ((n) | 4 & ~3)
151
115#define array_needsize(base,cur,cnt,init) \ 152#define array_needsize(base,cur,cnt,init) \
116 if ((cnt) > cur) \ 153 if (expect_false ((cnt) > cur)) \
117 { \ 154 { \
118 int newcnt = cur; \ 155 int newcnt = cur; \
119 do \ 156 do \
120 { \ 157 { \
121 newcnt = (newcnt << 1) | 4 & ~3; \ 158 newcnt = array_roundsize (base, newcnt << 1); \
122 } \ 159 } \
123 while ((cnt) > newcnt); \ 160 while ((cnt) > newcnt); \
124 \ 161 \
125 base = realloc (base, sizeof (*base) * (newcnt)); \ 162 base = realloc (base, sizeof (*base) * (newcnt)); \
126 init (base + cur, newcnt - cur); \ 163 init (base + cur, newcnt - cur); \
130/*****************************************************************************/ 167/*****************************************************************************/
131 168
132typedef struct 169typedef struct
133{ 170{
134 struct ev_io *head; 171 struct ev_io *head;
135 int events; 172 unsigned char events;
173 unsigned char reify;
136} ANFD; 174} ANFD;
137 175
138static ANFD *anfds; 176static ANFD *anfds;
139static int anfdmax; 177static int anfdmax;
140 178
143{ 181{
144 while (count--) 182 while (count--)
145 { 183 {
146 base->head = 0; 184 base->head = 0;
147 base->events = EV_NONE; 185 base->events = EV_NONE;
186 base->reify = 0;
187
148 ++base; 188 ++base;
149 } 189 }
150} 190}
151 191
152typedef struct 192typedef struct
159static int pendingmax, pendingcnt; 199static int pendingmax, pendingcnt;
160 200
161static void 201static void
162event (W w, int events) 202event (W w, int events)
163{ 203{
164 if (w->active) 204 if (w->pending)
205 {
206 pendings [w->pending - 1].events |= events;
207 return;
165 { 208 }
209
166 w->pending = ++pendingcnt; 210 w->pending = ++pendingcnt;
167 array_needsize (pendings, pendingmax, pendingcnt, ); 211 array_needsize (pendings, pendingmax, pendingcnt, );
168 pendings [pendingcnt - 1].w = w; 212 pendings [pendingcnt - 1].w = w;
169 pendings [pendingcnt - 1].events = events; 213 pendings [pendingcnt - 1].events = events;
170 }
171} 214}
172 215
173static void 216static void
174queue_events (W *events, int eventcnt, int type) 217queue_events (W *events, int eventcnt, int type)
175{ 218{
213 int events = 0; 256 int events = 0;
214 257
215 for (w = anfd->head; w; w = w->next) 258 for (w = anfd->head; w; w = w->next)
216 events |= w->events; 259 events |= w->events;
217 260
218 anfd->events &= ~EV_REIFY; 261 anfd->reify = 0;
219 262
220 if (anfd->events != events) 263 if (anfd->events != events)
221 { 264 {
222 method_modify (fd, anfd->events, events); 265 method_modify (fd, anfd->events, events);
223 anfd->events = events; 266 anfd->events = events;
228} 271}
229 272
230static void 273static void
231fd_change (int fd) 274fd_change (int fd)
232{ 275{
233 if (anfds [fd].events & EV_REIFY) 276 if (anfds [fd].reify || fdchangecnt < 0)
234 return; 277 return;
235 278
236 anfds [fd].events |= EV_REIFY; 279 anfds [fd].reify = 1;
237 280
238 ++fdchangecnt; 281 ++fdchangecnt;
239 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 282 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
240 fdchanges [fdchangecnt - 1] = fd; 283 fdchanges [fdchangecnt - 1] = fd;
241} 284}
242 285
286static void
287fd_kill (int fd)
288{
289 struct ev_io *w;
290
291 printf ("killing fd %d\n", fd);//D
292 while ((w = anfds [fd].head))
293 {
294 ev_io_stop (w);
295 event ((W)w, EV_ERROR | EV_READ | EV_WRITE);
296 }
297}
298
243/* called on EBADF to verify fds */ 299/* called on EBADF to verify fds */
244static void 300static void
245fd_recheck (void) 301fd_ebadf (void)
246{ 302{
247 int fd; 303 int fd;
248 304
249 for (fd = 0; fd < anfdmax; ++fd) 305 for (fd = 0; fd < anfdmax; ++fd)
250 if (anfds [fd].events) 306 if (anfds [fd].events)
251 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 307 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
252 while (anfds [fd].head) 308 fd_kill (fd);
309}
310
311/* called on ENOMEM in select/poll to kill some fds and retry */
312static void
313fd_enomem (void)
314{
315 int fd = anfdmax;
316
317 while (fd--)
318 if (anfds [fd].events)
253 { 319 {
254 event ((W)anfds [fd].head, EV_ERROR); 320 close (fd);
255 ev_io_stop (anfds [fd].head); 321 fd_kill (fd);
322 return;
256 } 323 }
257} 324}
258 325
259/*****************************************************************************/ 326/*****************************************************************************/
260 327
261static struct ev_timer **timers; 328static struct ev_timer **timers;
308/*****************************************************************************/ 375/*****************************************************************************/
309 376
310typedef struct 377typedef struct
311{ 378{
312 struct ev_signal *head; 379 struct ev_signal *head;
313 sig_atomic_t gotsig; 380 sig_atomic_t volatile gotsig;
314} ANSIG; 381} ANSIG;
315 382
316static ANSIG *signals; 383static ANSIG *signals;
317static int signalmax; 384static int signalmax;
318 385
319static int sigpipe [2]; 386static int sigpipe [2];
320static sig_atomic_t gotsig; 387static sig_atomic_t volatile gotsig;
321static struct ev_io sigev; 388static struct ev_io sigev;
322 389
323static void 390static void
324signals_init (ANSIG *base, int count) 391signals_init (ANSIG *base, int count)
325{ 392{
326 while (count--) 393 while (count--)
327 { 394 {
328 base->head = 0; 395 base->head = 0;
329 base->gotsig = 0; 396 base->gotsig = 0;
397
330 ++base; 398 ++base;
331 } 399 }
332} 400}
333 401
334static void 402static void
337 signals [signum - 1].gotsig = 1; 405 signals [signum - 1].gotsig = 1;
338 406
339 if (!gotsig) 407 if (!gotsig)
340 { 408 {
341 gotsig = 1; 409 gotsig = 1;
342 write (sigpipe [1], &gotsig, 1); 410 write (sigpipe [1], &signum, 1);
343 } 411 }
344} 412}
345 413
346static void 414static void
347sigcb (struct ev_io *iow, int revents) 415sigcb (struct ev_io *iow, int revents)
348{ 416{
349 struct ev_signal *w; 417 struct ev_signal *w;
350 int sig; 418 int signum;
351 419
420 read (sigpipe [0], &revents, 1);
352 gotsig = 0; 421 gotsig = 0;
353 read (sigpipe [0], &revents, 1);
354 422
355 for (sig = signalmax; sig--; ) 423 for (signum = signalmax; signum--; )
356 if (signals [sig].gotsig) 424 if (signals [signum].gotsig)
357 { 425 {
358 signals [sig].gotsig = 0; 426 signals [signum].gotsig = 0;
359 427
360 for (w = signals [sig].head; w; w = w->next) 428 for (w = signals [signum].head; w; w = w->next)
361 event ((W)w, EV_SIGNAL); 429 event ((W)w, EV_SIGNAL);
362 } 430 }
363} 431}
364 432
365static void 433static void
402 struct ev_child *w; 470 struct ev_child *w;
403 int pid, status; 471 int pid, status;
404 472
405 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) 473 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1)
406 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) 474 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next)
407 if (w->pid == pid || w->pid == -1) 475 if (w->pid == pid || !w->pid)
408 { 476 {
409 w->status = status; 477 w->status = status;
410 event ((W)w, EV_CHILD); 478 event ((W)w, EV_CHILD);
411 } 479 }
412} 480}
413 481
414/*****************************************************************************/ 482/*****************************************************************************/
415 483
416#if HAVE_EPOLL 484#if EV_USE_EPOLL
417# include "ev_epoll.c" 485# include "ev_epoll.c"
418#endif 486#endif
487#if EV_USE_POLL
488# include "ev_poll.c"
489#endif
419#if HAVE_SELECT 490#if EV_USE_SELECT
420# include "ev_select.c" 491# include "ev_select.c"
421#endif 492#endif
422 493
423int 494int
424ev_version_major (void) 495ev_version_major (void)
430ev_version_minor (void) 501ev_version_minor (void)
431{ 502{
432 return EV_VERSION_MINOR; 503 return EV_VERSION_MINOR;
433} 504}
434 505
506/* return true if we are running with elevated privileges and ignore env variables */
507static int
508enable_secure ()
509{
510 return getuid () != geteuid ()
511 || getgid () != getegid ();
512}
513
435int ev_init (int flags) 514int ev_init (int methods)
436{ 515{
437 if (!ev_method) 516 if (!ev_method)
438 { 517 {
439#if HAVE_MONOTONIC 518#if EV_USE_MONOTONIC
440 { 519 {
441 struct timespec ts; 520 struct timespec ts;
442 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 521 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
443 have_monotonic = 1; 522 have_monotonic = 1;
444 } 523 }
445#endif 524#endif
446 525
447 ev_now = ev_time (); 526 ev_now = ev_time ();
448 now = get_clock (); 527 now = get_clock ();
528 now_floor = now;
449 diff = ev_now - now; 529 diff = ev_now - now;
450 530
451 if (pipe (sigpipe)) 531 if (pipe (sigpipe))
452 return 0; 532 return 0;
453 533
534 if (methods == EVMETHOD_AUTO)
535 if (!enable_secure () && getenv ("LIBEV_METHODS"))
536 methods = atoi (getenv ("LIBEV_METHODS"));
537 else
454 ev_method = EVMETHOD_NONE; 538 methods = EVMETHOD_ANY;
539
540 ev_method = 0;
455#if HAVE_EPOLL 541#if EV_USE_EPOLL
456 if (ev_method == EVMETHOD_NONE) epoll_init (flags); 542 if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods);
457#endif 543#endif
544#if EV_USE_POLL
545 if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods);
546#endif
458#if HAVE_SELECT 547#if EV_USE_SELECT
459 if (ev_method == EVMETHOD_NONE) select_init (flags); 548 if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods);
460#endif 549#endif
461 550
462 if (ev_method) 551 if (ev_method)
463 { 552 {
464 ev_watcher_init (&sigev, sigcb); 553 ev_watcher_init (&sigev, sigcb);
473} 562}
474 563
475/*****************************************************************************/ 564/*****************************************************************************/
476 565
477void 566void
478ev_prefork (void) 567ev_fork_prepare (void)
479{ 568{
480 /* nop */ 569 /* nop */
481} 570}
482 571
483void 572void
484ev_postfork_parent (void) 573ev_fork_parent (void)
485{ 574{
486 /* nop */ 575 /* nop */
487} 576}
488 577
489void 578void
490ev_postfork_child (void) 579ev_fork_child (void)
491{ 580{
492#if HAVE_EPOLL 581#if EV_USE_EPOLL
493 if (ev_method == EVMETHOD_EPOLL) 582 if (ev_method == EVMETHOD_EPOLL)
494 epoll_postfork_child (); 583 epoll_postfork_child ();
495#endif 584#endif
496 585
497 ev_io_stop (&sigev); 586 ev_io_stop (&sigev);
523{ 612{
524 while (timercnt && timers [0]->at <= now) 613 while (timercnt && timers [0]->at <= now)
525 { 614 {
526 struct ev_timer *w = timers [0]; 615 struct ev_timer *w = timers [0];
527 616
528 event ((W)w, EV_TIMEOUT);
529
530 /* first reschedule or stop timer */ 617 /* first reschedule or stop timer */
531 if (w->repeat) 618 if (w->repeat)
532 { 619 {
620 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
533 w->at = now + w->repeat; 621 w->at = now + w->repeat;
534 assert (("timer timeout in the past, negative repeat?", w->at > now));
535 downheap ((WT *)timers, timercnt, 0); 622 downheap ((WT *)timers, timercnt, 0);
536 } 623 }
537 else 624 else
538 ev_timer_stop (w); /* nonrepeating: stop timer */ 625 ev_timer_stop (w); /* nonrepeating: stop timer */
626
627 event ((W)w, EV_TIMEOUT);
539 } 628 }
540} 629}
541 630
542static void 631static void
543periodics_reify (void) 632periodics_reify (void)
548 637
549 /* first reschedule or stop timer */ 638 /* first reschedule or stop timer */
550 if (w->interval) 639 if (w->interval)
551 { 640 {
552 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; 641 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval;
553 assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); 642 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > ev_now));
554 downheap ((WT *)periodics, periodiccnt, 0); 643 downheap ((WT *)periodics, periodiccnt, 0);
555 } 644 }
556 else 645 else
557 ev_periodic_stop (w); /* nonrepeating: stop timer */ 646 ev_periodic_stop (w); /* nonrepeating: stop timer */
558 647
559 event ((W)w, EV_TIMEOUT); 648 event ((W)w, EV_PERIODIC);
560 } 649 }
561} 650}
562 651
563static void 652static void
564periodics_reschedule (ev_tstamp diff) 653periodics_reschedule (ev_tstamp diff)
583 } 672 }
584 } 673 }
585 } 674 }
586} 675}
587 676
677static int
678time_update_monotonic (void)
679{
680 now = get_clock ();
681
682 if (expect_true (now - now_floor < MIN_TIMEJUMP * .5))
683 {
684 ev_now = now + diff;
685 return 0;
686 }
687 else
688 {
689 now_floor = now;
690 ev_now = ev_time ();
691 return 1;
692 }
693}
694
588static void 695static void
589time_update (void) 696time_update (void)
590{ 697{
591 int i; 698 int i;
592 699
593 ev_now = ev_time (); 700#if EV_USE_MONOTONIC
594
595 if (have_monotonic) 701 if (expect_true (have_monotonic))
596 { 702 {
703 if (time_update_monotonic ())
704 {
597 ev_tstamp odiff = diff; 705 ev_tstamp odiff = diff;
598 706
599 for (i = 4; --i; ) /* loop a few times, before making important decisions */ 707 for (i = 4; --i; ) /* loop a few times, before making important decisions */
600 { 708 {
601 now = get_clock ();
602 diff = ev_now - now; 709 diff = ev_now - now;
603 710
604 if (fabs (odiff - diff) < MIN_TIMEJUMP) 711 if (fabs (odiff - diff) < MIN_TIMEJUMP)
605 return; /* all is well */ 712 return; /* all is well */
606 713
607 ev_now = ev_time (); 714 ev_now = ev_time ();
715 now = get_clock ();
716 now_floor = now;
608 } 717 }
609 718
610 periodics_reschedule (diff - odiff); 719 periodics_reschedule (diff - odiff);
611 /* no timer adjustment, as the monotonic clock doesn't jump */ 720 /* no timer adjustment, as the monotonic clock doesn't jump */
721 }
612 } 722 }
613 else 723 else
724#endif
614 { 725 {
726 ev_now = ev_time ();
727
615 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) 728 if (expect_false (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
616 { 729 {
617 periodics_reschedule (ev_now - now); 730 periodics_reschedule (ev_now - now);
618 731
619 /* adjust timers. this is easy, as the offset is the same for all */ 732 /* adjust timers. this is easy, as the offset is the same for all */
620 for (i = 0; i < timercnt; ++i) 733 for (i = 0; i < timercnt; ++i)
633 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; 746 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
634 747
635 do 748 do
636 { 749 {
637 /* queue check watchers (and execute them) */ 750 /* queue check watchers (and execute them) */
638 if (preparecnt) 751 if (expect_false (preparecnt))
639 { 752 {
640 queue_events ((W *)prepares, preparecnt, EV_PREPARE); 753 queue_events ((W *)prepares, preparecnt, EV_PREPARE);
641 call_pending (); 754 call_pending ();
642 } 755 }
643 756
646 759
647 /* calculate blocking time */ 760 /* calculate blocking time */
648 761
649 /* we only need this for !monotonic clockor timers, but as we basically 762 /* we only need this for !monotonic clockor timers, but as we basically
650 always have timers, we just calculate it always */ 763 always have timers, we just calculate it always */
764#if EV_USE_MONOTONIC
765 if (expect_true (have_monotonic))
766 time_update_monotonic ();
767 else
768#endif
769 {
651 ev_now = ev_time (); 770 ev_now = ev_time ();
771 now = ev_now;
772 }
652 773
653 if (flags & EVLOOP_NONBLOCK || idlecnt) 774 if (flags & EVLOOP_NONBLOCK || idlecnt)
654 block = 0.; 775 block = 0.;
655 else 776 else
656 { 777 {
657 block = MAX_BLOCKTIME; 778 block = MAX_BLOCKTIME;
658 779
659 if (timercnt) 780 if (timercnt)
660 { 781 {
661 ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; 782 ev_tstamp to = timers [0]->at - now + method_fudge;
662 if (block > to) block = to; 783 if (block > to) block = to;
663 } 784 }
664 785
665 if (periodiccnt) 786 if (periodiccnt)
666 { 787 {
719 head = &(*head)->next; 840 head = &(*head)->next;
720 } 841 }
721} 842}
722 843
723static void 844static void
724ev_clear (W w) 845ev_clear_pending (W w)
725{ 846{
726 if (w->pending) 847 if (w->pending)
727 { 848 {
728 pendings [w->pending - 1].w = 0; 849 pendings [w->pending - 1].w = 0;
729 w->pending = 0; 850 w->pending = 0;
745/*****************************************************************************/ 866/*****************************************************************************/
746 867
747void 868void
748ev_io_start (struct ev_io *w) 869ev_io_start (struct ev_io *w)
749{ 870{
871 int fd = w->fd;
872
750 if (ev_is_active (w)) 873 if (ev_is_active (w))
751 return; 874 return;
752 875
753 int fd = w->fd; 876 assert (("ev_io_start called with negative fd", fd >= 0));
754 877
755 ev_start ((W)w, 1); 878 ev_start ((W)w, 1);
756 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 879 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
757 wlist_add ((WL *)&anfds[fd].head, (WL)w); 880 wlist_add ((WL *)&anfds[fd].head, (WL)w);
758 881
760} 883}
761 884
762void 885void
763ev_io_stop (struct ev_io *w) 886ev_io_stop (struct ev_io *w)
764{ 887{
765 ev_clear ((W)w); 888 ev_clear_pending ((W)w);
766 if (!ev_is_active (w)) 889 if (!ev_is_active (w))
767 return; 890 return;
768 891
769 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 892 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
770 ev_stop ((W)w); 893 ev_stop ((W)w);
778 if (ev_is_active (w)) 901 if (ev_is_active (w))
779 return; 902 return;
780 903
781 w->at += now; 904 w->at += now;
782 905
783 assert (("timer repeat value less than zero not allowed", w->repeat >= 0.)); 906 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
784 907
785 ev_start ((W)w, ++timercnt); 908 ev_start ((W)w, ++timercnt);
786 array_needsize (timers, timermax, timercnt, ); 909 array_needsize (timers, timermax, timercnt, );
787 timers [timercnt - 1] = w; 910 timers [timercnt - 1] = w;
788 upheap ((WT *)timers, timercnt - 1); 911 upheap ((WT *)timers, timercnt - 1);
789} 912}
790 913
791void 914void
792ev_timer_stop (struct ev_timer *w) 915ev_timer_stop (struct ev_timer *w)
793{ 916{
794 ev_clear ((W)w); 917 ev_clear_pending ((W)w);
795 if (!ev_is_active (w)) 918 if (!ev_is_active (w))
796 return; 919 return;
797 920
798 if (w->active < timercnt--) 921 if (w->active < timercnt--)
799 { 922 {
827ev_periodic_start (struct ev_periodic *w) 950ev_periodic_start (struct ev_periodic *w)
828{ 951{
829 if (ev_is_active (w)) 952 if (ev_is_active (w))
830 return; 953 return;
831 954
832 assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); 955 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
833 956
834 /* this formula differs from the one in periodic_reify because we do not always round up */ 957 /* this formula differs from the one in periodic_reify because we do not always round up */
835 if (w->interval) 958 if (w->interval)
836 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; 959 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
837 960
842} 965}
843 966
844void 967void
845ev_periodic_stop (struct ev_periodic *w) 968ev_periodic_stop (struct ev_periodic *w)
846{ 969{
847 ev_clear ((W)w); 970 ev_clear_pending ((W)w);
848 if (!ev_is_active (w)) 971 if (!ev_is_active (w))
849 return; 972 return;
850 973
851 if (w->active < periodiccnt--) 974 if (w->active < periodiccnt--)
852 { 975 {
860void 983void
861ev_signal_start (struct ev_signal *w) 984ev_signal_start (struct ev_signal *w)
862{ 985{
863 if (ev_is_active (w)) 986 if (ev_is_active (w))
864 return; 987 return;
988
989 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
865 990
866 ev_start ((W)w, 1); 991 ev_start ((W)w, 1);
867 array_needsize (signals, signalmax, w->signum, signals_init); 992 array_needsize (signals, signalmax, w->signum, signals_init);
868 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 993 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
869 994
878} 1003}
879 1004
880void 1005void
881ev_signal_stop (struct ev_signal *w) 1006ev_signal_stop (struct ev_signal *w)
882{ 1007{
883 ev_clear ((W)w); 1008 ev_clear_pending ((W)w);
884 if (!ev_is_active (w)) 1009 if (!ev_is_active (w))
885 return; 1010 return;
886 1011
887 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 1012 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
888 ev_stop ((W)w); 1013 ev_stop ((W)w);
903} 1028}
904 1029
905void 1030void
906ev_idle_stop (struct ev_idle *w) 1031ev_idle_stop (struct ev_idle *w)
907{ 1032{
908 ev_clear ((W)w); 1033 ev_clear_pending ((W)w);
909 if (ev_is_active (w)) 1034 if (ev_is_active (w))
910 return; 1035 return;
911 1036
912 idles [w->active - 1] = idles [--idlecnt]; 1037 idles [w->active - 1] = idles [--idlecnt];
913 ev_stop ((W)w); 1038 ev_stop ((W)w);
925} 1050}
926 1051
927void 1052void
928ev_prepare_stop (struct ev_prepare *w) 1053ev_prepare_stop (struct ev_prepare *w)
929{ 1054{
930 ev_clear ((W)w); 1055 ev_clear_pending ((W)w);
931 if (ev_is_active (w)) 1056 if (ev_is_active (w))
932 return; 1057 return;
933 1058
934 prepares [w->active - 1] = prepares [--preparecnt]; 1059 prepares [w->active - 1] = prepares [--preparecnt];
935 ev_stop ((W)w); 1060 ev_stop ((W)w);
947} 1072}
948 1073
949void 1074void
950ev_check_stop (struct ev_check *w) 1075ev_check_stop (struct ev_check *w)
951{ 1076{
952 ev_clear ((W)w); 1077 ev_clear_pending ((W)w);
953 if (ev_is_active (w)) 1078 if (ev_is_active (w))
954 return; 1079 return;
955 1080
956 checks [w->active - 1] = checks [--checkcnt]; 1081 checks [w->active - 1] = checks [--checkcnt];
957 ev_stop ((W)w); 1082 ev_stop ((W)w);
968} 1093}
969 1094
970void 1095void
971ev_child_stop (struct ev_child *w) 1096ev_child_stop (struct ev_child *w)
972{ 1097{
973 ev_clear ((W)w); 1098 ev_clear_pending ((W)w);
974 if (ev_is_active (w)) 1099 if (ev_is_active (w))
975 return; 1100 return;
976 1101
977 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1102 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
978 ev_stop ((W)w); 1103 ev_stop ((W)w);
1017ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) 1142ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
1018{ 1143{
1019 struct ev_once *once = malloc (sizeof (struct ev_once)); 1144 struct ev_once *once = malloc (sizeof (struct ev_once));
1020 1145
1021 if (!once) 1146 if (!once)
1022 cb (EV_ERROR, arg); 1147 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1023 else 1148 else
1024 { 1149 {
1025 once->cb = cb; 1150 once->cb = cb;
1026 once->arg = arg; 1151 once->arg = arg;
1027 1152
1028 ev_watcher_init (&once->io, once_cb_io); 1153 ev_watcher_init (&once->io, once_cb_io);
1029
1030 if (fd >= 0) 1154 if (fd >= 0)
1031 { 1155 {
1032 ev_io_set (&once->io, fd, events); 1156 ev_io_set (&once->io, fd, events);
1033 ev_io_start (&once->io); 1157 ev_io_start (&once->io);
1034 } 1158 }
1035 1159
1036 ev_watcher_init (&once->to, once_cb_to); 1160 ev_watcher_init (&once->to, once_cb_to);
1037
1038 if (timeout >= 0.) 1161 if (timeout >= 0.)
1039 { 1162 {
1040 ev_timer_set (&once->to, timeout, 0.); 1163 ev_timer_set (&once->to, timeout, 0.);
1041 ev_timer_start (&once->to); 1164 ev_timer_start (&once->to);
1042 } 1165 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines