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.43 by root, Fri Nov 2 20:21:33 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)
104
105#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
106#define ABSPRI(w) ((w)->priority - EV_MINPRI)
69 107
70typedef struct ev_watcher *W; 108typedef struct ev_watcher *W;
71typedef struct ev_watcher_list *WL; 109typedef struct ev_watcher_list *WL;
72typedef struct ev_watcher_time *WT; 110typedef struct ev_watcher_time *WT;
73 111
74static ev_tstamp now, diff; /* monotonic clock */ 112static ev_tstamp now_floor, now, diff; /* monotonic clock */
75ev_tstamp ev_now; 113ev_tstamp ev_now;
76int ev_method; 114int ev_method;
77 115
78static int have_monotonic; /* runtime */ 116static int have_monotonic; /* runtime */
79 117
84/*****************************************************************************/ 122/*****************************************************************************/
85 123
86ev_tstamp 124ev_tstamp
87ev_time (void) 125ev_time (void)
88{ 126{
89#if HAVE_REALTIME 127#if EV_USE_REALTIME
90 struct timespec ts; 128 struct timespec ts;
91 clock_gettime (CLOCK_REALTIME, &ts); 129 clock_gettime (CLOCK_REALTIME, &ts);
92 return ts.tv_sec + ts.tv_nsec * 1e-9; 130 return ts.tv_sec + ts.tv_nsec * 1e-9;
93#else 131#else
94 struct timeval tv; 132 struct timeval tv;
98} 136}
99 137
100static ev_tstamp 138static ev_tstamp
101get_clock (void) 139get_clock (void)
102{ 140{
103#if HAVE_MONOTONIC 141#if EV_USE_MONOTONIC
104 if (have_monotonic) 142 if (expect_true (have_monotonic))
105 { 143 {
106 struct timespec ts; 144 struct timespec ts;
107 clock_gettime (CLOCK_MONOTONIC, &ts); 145 clock_gettime (CLOCK_MONOTONIC, &ts);
108 return ts.tv_sec + ts.tv_nsec * 1e-9; 146 return ts.tv_sec + ts.tv_nsec * 1e-9;
109 } 147 }
110#endif 148#endif
111 149
112 return ev_time (); 150 return ev_time ();
113} 151}
114 152
153#define array_roundsize(base,n) ((n) | 4 & ~3)
154
115#define array_needsize(base,cur,cnt,init) \ 155#define array_needsize(base,cur,cnt,init) \
116 if ((cnt) > cur) \ 156 if (expect_false ((cnt) > cur)) \
117 { \ 157 { \
118 int newcnt = cur; \ 158 int newcnt = cur; \
119 do \ 159 do \
120 { \ 160 { \
121 newcnt = (newcnt << 1) | 4 & ~3; \ 161 newcnt = array_roundsize (base, newcnt << 1); \
122 } \ 162 } \
123 while ((cnt) > newcnt); \ 163 while ((cnt) > newcnt); \
124 \ 164 \
125 base = realloc (base, sizeof (*base) * (newcnt)); \ 165 base = realloc (base, sizeof (*base) * (newcnt)); \
126 init (base + cur, newcnt - cur); \ 166 init (base + cur, newcnt - cur); \
130/*****************************************************************************/ 170/*****************************************************************************/
131 171
132typedef struct 172typedef struct
133{ 173{
134 struct ev_io *head; 174 struct ev_io *head;
135 int events; 175 unsigned char events;
176 unsigned char reify;
136} ANFD; 177} ANFD;
137 178
138static ANFD *anfds; 179static ANFD *anfds;
139static int anfdmax; 180static int anfdmax;
140 181
143{ 184{
144 while (count--) 185 while (count--)
145 { 186 {
146 base->head = 0; 187 base->head = 0;
147 base->events = EV_NONE; 188 base->events = EV_NONE;
189 base->reify = 0;
190
148 ++base; 191 ++base;
149 } 192 }
150} 193}
151 194
152typedef struct 195typedef struct
153{ 196{
154 W w; 197 W w;
155 int events; 198 int events;
156} ANPENDING; 199} ANPENDING;
157 200
158static ANPENDING *pendings; 201static ANPENDING *pendings [NUMPRI];
159static int pendingmax, pendingcnt; 202static int pendingmax [NUMPRI], pendingcnt [NUMPRI];
160 203
161static void 204static void
162event (W w, int events) 205event (W w, int events)
163{ 206{
164 if (w->active) 207 if (w->pending)
165 { 208 {
166 w->pending = ++pendingcnt;
167 array_needsize (pendings, pendingmax, pendingcnt, );
168 pendings [pendingcnt - 1].w = w;
169 pendings [pendingcnt - 1].events = events; 209 pendings [ABSPRI (w)][w->pending - 1].events |= events;
210 return;
170 } 211 }
212
213 w->pending = ++pendingcnt [ABSPRI (w)];
214 array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], );
215 pendings [ABSPRI (w)][w->pending - 1].w = w;
216 pendings [ABSPRI (w)][w->pending - 1].events = events;
171} 217}
172 218
173static void 219static void
174queue_events (W *events, int eventcnt, int type) 220queue_events (W *events, int eventcnt, int type)
175{ 221{
213 int events = 0; 259 int events = 0;
214 260
215 for (w = anfd->head; w; w = w->next) 261 for (w = anfd->head; w; w = w->next)
216 events |= w->events; 262 events |= w->events;
217 263
218 anfd->events &= ~EV_REIFY; 264 anfd->reify = 0;
219 265
220 if (anfd->events != events) 266 if (anfd->events != events)
221 { 267 {
222 method_modify (fd, anfd->events, events); 268 method_modify (fd, anfd->events, events);
223 anfd->events = events; 269 anfd->events = events;
228} 274}
229 275
230static void 276static void
231fd_change (int fd) 277fd_change (int fd)
232{ 278{
233 if (anfds [fd].events & EV_REIFY) 279 if (anfds [fd].reify || fdchangecnt < 0)
234 return; 280 return;
235 281
236 anfds [fd].events |= EV_REIFY; 282 anfds [fd].reify = 1;
237 283
238 ++fdchangecnt; 284 ++fdchangecnt;
239 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 285 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
240 fdchanges [fdchangecnt - 1] = fd; 286 fdchanges [fdchangecnt - 1] = fd;
241} 287}
242 288
289static void
290fd_kill (int fd)
291{
292 struct ev_io *w;
293
294 printf ("killing fd %d\n", fd);//D
295 while ((w = anfds [fd].head))
296 {
297 ev_io_stop (w);
298 event ((W)w, EV_ERROR | EV_READ | EV_WRITE);
299 }
300}
301
243/* called on EBADF to verify fds */ 302/* called on EBADF to verify fds */
244static void 303static void
245fd_recheck (void) 304fd_ebadf (void)
246{ 305{
247 int fd; 306 int fd;
248 307
249 for (fd = 0; fd < anfdmax; ++fd) 308 for (fd = 0; fd < anfdmax; ++fd)
250 if (anfds [fd].events) 309 if (anfds [fd].events)
251 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 310 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
252 while (anfds [fd].head) 311 fd_kill (fd);
312}
313
314/* called on ENOMEM in select/poll to kill some fds and retry */
315static void
316fd_enomem (void)
317{
318 int fd = anfdmax;
319
320 while (fd--)
321 if (anfds [fd].events)
253 { 322 {
254 event ((W)anfds [fd].head, EV_ERROR); 323 close (fd);
255 ev_io_stop (anfds [fd].head); 324 fd_kill (fd);
325 return;
256 } 326 }
257} 327}
258 328
259/*****************************************************************************/ 329/*****************************************************************************/
260 330
261static struct ev_timer **timers; 331static struct ev_timer **timers;
308/*****************************************************************************/ 378/*****************************************************************************/
309 379
310typedef struct 380typedef struct
311{ 381{
312 struct ev_signal *head; 382 struct ev_signal *head;
313 sig_atomic_t gotsig; 383 sig_atomic_t volatile gotsig;
314} ANSIG; 384} ANSIG;
315 385
316static ANSIG *signals; 386static ANSIG *signals;
317static int signalmax; 387static int signalmax;
318 388
319static int sigpipe [2]; 389static int sigpipe [2];
320static sig_atomic_t gotsig; 390static sig_atomic_t volatile gotsig;
321static struct ev_io sigev; 391static struct ev_io sigev;
322 392
323static void 393static void
324signals_init (ANSIG *base, int count) 394signals_init (ANSIG *base, int count)
325{ 395{
326 while (count--) 396 while (count--)
327 { 397 {
328 base->head = 0; 398 base->head = 0;
329 base->gotsig = 0; 399 base->gotsig = 0;
400
330 ++base; 401 ++base;
331 } 402 }
332} 403}
333 404
334static void 405static void
337 signals [signum - 1].gotsig = 1; 408 signals [signum - 1].gotsig = 1;
338 409
339 if (!gotsig) 410 if (!gotsig)
340 { 411 {
341 gotsig = 1; 412 gotsig = 1;
342 write (sigpipe [1], &gotsig, 1); 413 write (sigpipe [1], &signum, 1);
343 } 414 }
344} 415}
345 416
346static void 417static void
347sigcb (struct ev_io *iow, int revents) 418sigcb (struct ev_io *iow, int revents)
348{ 419{
349 struct ev_signal *w; 420 struct ev_signal *w;
350 int sig; 421 int signum;
351 422
423 read (sigpipe [0], &revents, 1);
352 gotsig = 0; 424 gotsig = 0;
353 read (sigpipe [0], &revents, 1);
354 425
355 for (sig = signalmax; sig--; ) 426 for (signum = signalmax; signum--; )
356 if (signals [sig].gotsig) 427 if (signals [signum].gotsig)
357 { 428 {
358 signals [sig].gotsig = 0; 429 signals [signum].gotsig = 0;
359 430
360 for (w = signals [sig].head; w; w = w->next) 431 for (w = signals [signum].head; w; w = w->next)
361 event ((W)w, EV_SIGNAL); 432 event ((W)w, EV_SIGNAL);
362 } 433 }
363} 434}
364 435
365static void 436static void
402 struct ev_child *w; 473 struct ev_child *w;
403 int pid, status; 474 int pid, status;
404 475
405 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) 476 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1)
406 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) 477 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next)
407 if (w->pid == pid || w->pid == -1) 478 if (w->pid == pid || !w->pid)
408 { 479 {
409 w->status = status; 480 w->status = status;
410 event ((W)w, EV_CHILD); 481 event ((W)w, EV_CHILD);
411 } 482 }
412} 483}
413 484
414/*****************************************************************************/ 485/*****************************************************************************/
415 486
416#if HAVE_EPOLL 487#if EV_USE_EPOLL
417# include "ev_epoll.c" 488# include "ev_epoll.c"
418#endif 489#endif
490#if EV_USE_POLL
491# include "ev_poll.c"
492#endif
419#if HAVE_SELECT 493#if EV_USE_SELECT
420# include "ev_select.c" 494# include "ev_select.c"
421#endif 495#endif
422 496
423int 497int
424ev_version_major (void) 498ev_version_major (void)
430ev_version_minor (void) 504ev_version_minor (void)
431{ 505{
432 return EV_VERSION_MINOR; 506 return EV_VERSION_MINOR;
433} 507}
434 508
509/* return true if we are running with elevated privileges and ignore env variables */
510static int
511enable_secure ()
512{
513 return getuid () != geteuid ()
514 || getgid () != getegid ();
515}
516
435int ev_init (int flags) 517int ev_init (int methods)
436{ 518{
437 if (!ev_method) 519 if (!ev_method)
438 { 520 {
439#if HAVE_MONOTONIC 521#if EV_USE_MONOTONIC
440 { 522 {
441 struct timespec ts; 523 struct timespec ts;
442 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 524 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
443 have_monotonic = 1; 525 have_monotonic = 1;
444 } 526 }
445#endif 527#endif
446 528
447 ev_now = ev_time (); 529 ev_now = ev_time ();
448 now = get_clock (); 530 now = get_clock ();
531 now_floor = now;
449 diff = ev_now - now; 532 diff = ev_now - now;
450 533
451 if (pipe (sigpipe)) 534 if (pipe (sigpipe))
452 return 0; 535 return 0;
453 536
537 if (methods == EVMETHOD_AUTO)
538 if (!enable_secure () && getenv ("LIBEV_METHODS"))
539 methods = atoi (getenv ("LIBEV_METHODS"));
540 else
454 ev_method = EVMETHOD_NONE; 541 methods = EVMETHOD_ANY;
542
543 ev_method = 0;
455#if HAVE_EPOLL 544#if EV_USE_EPOLL
456 if (ev_method == EVMETHOD_NONE) epoll_init (flags); 545 if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods);
457#endif 546#endif
547#if EV_USE_POLL
548 if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods);
549#endif
458#if HAVE_SELECT 550#if EV_USE_SELECT
459 if (ev_method == EVMETHOD_NONE) select_init (flags); 551 if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods);
460#endif 552#endif
461 553
462 if (ev_method) 554 if (ev_method)
463 { 555 {
464 ev_watcher_init (&sigev, sigcb); 556 ev_watcher_init (&sigev, sigcb);
473} 565}
474 566
475/*****************************************************************************/ 567/*****************************************************************************/
476 568
477void 569void
478ev_prefork (void) 570ev_fork_prepare (void)
479{ 571{
480 /* nop */ 572 /* nop */
481} 573}
482 574
483void 575void
484ev_postfork_parent (void) 576ev_fork_parent (void)
485{ 577{
486 /* nop */ 578 /* nop */
487} 579}
488 580
489void 581void
490ev_postfork_child (void) 582ev_fork_child (void)
491{ 583{
492#if HAVE_EPOLL 584#if EV_USE_EPOLL
493 if (ev_method == EVMETHOD_EPOLL) 585 if (ev_method == EVMETHOD_EPOLL)
494 epoll_postfork_child (); 586 epoll_postfork_child ();
495#endif 587#endif
496 588
497 ev_io_stop (&sigev); 589 ev_io_stop (&sigev);
504/*****************************************************************************/ 596/*****************************************************************************/
505 597
506static void 598static void
507call_pending (void) 599call_pending (void)
508{ 600{
601 int pri;
602
603 for (pri = NUMPRI; pri--; )
509 while (pendingcnt) 604 while (pendingcnt [pri])
510 { 605 {
511 ANPENDING *p = pendings + --pendingcnt; 606 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
512 607
513 if (p->w) 608 if (p->w)
514 { 609 {
515 p->w->pending = 0; 610 p->w->pending = 0;
516 p->w->cb (p->w, p->events); 611 p->w->cb (p->w, p->events);
517 } 612 }
518 } 613 }
519} 614}
520 615
521static void 616static void
522timers_reify (void) 617timers_reify (void)
523{ 618{
524 while (timercnt && timers [0]->at <= now) 619 while (timercnt && timers [0]->at <= now)
525 { 620 {
526 struct ev_timer *w = timers [0]; 621 struct ev_timer *w = timers [0];
527
528 event ((W)w, EV_TIMEOUT);
529 622
530 /* first reschedule or stop timer */ 623 /* first reschedule or stop timer */
531 if (w->repeat) 624 if (w->repeat)
532 { 625 {
626 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
533 w->at = now + w->repeat; 627 w->at = now + w->repeat;
534 assert (("timer timeout in the past, negative repeat?", w->at > now));
535 downheap ((WT *)timers, timercnt, 0); 628 downheap ((WT *)timers, timercnt, 0);
536 } 629 }
537 else 630 else
538 ev_timer_stop (w); /* nonrepeating: stop timer */ 631 ev_timer_stop (w); /* nonrepeating: stop timer */
632
633 event ((W)w, EV_TIMEOUT);
539 } 634 }
540} 635}
541 636
542static void 637static void
543periodics_reify (void) 638periodics_reify (void)
548 643
549 /* first reschedule or stop timer */ 644 /* first reschedule or stop timer */
550 if (w->interval) 645 if (w->interval)
551 { 646 {
552 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; 647 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)); 648 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > ev_now));
554 downheap ((WT *)periodics, periodiccnt, 0); 649 downheap ((WT *)periodics, periodiccnt, 0);
555 } 650 }
556 else 651 else
557 ev_periodic_stop (w); /* nonrepeating: stop timer */ 652 ev_periodic_stop (w); /* nonrepeating: stop timer */
558 653
559 event ((W)w, EV_TIMEOUT); 654 event ((W)w, EV_PERIODIC);
560 } 655 }
561} 656}
562 657
563static void 658static void
564periodics_reschedule (ev_tstamp diff) 659periodics_reschedule (ev_tstamp diff)
583 } 678 }
584 } 679 }
585 } 680 }
586} 681}
587 682
683static int
684time_update_monotonic (void)
685{
686 now = get_clock ();
687
688 if (expect_true (now - now_floor < MIN_TIMEJUMP * .5))
689 {
690 ev_now = now + diff;
691 return 0;
692 }
693 else
694 {
695 now_floor = now;
696 ev_now = ev_time ();
697 return 1;
698 }
699}
700
588static void 701static void
589time_update (void) 702time_update (void)
590{ 703{
591 int i; 704 int i;
592 705
593 ev_now = ev_time (); 706#if EV_USE_MONOTONIC
594
595 if (have_monotonic) 707 if (expect_true (have_monotonic))
596 { 708 {
709 if (time_update_monotonic ())
710 {
597 ev_tstamp odiff = diff; 711 ev_tstamp odiff = diff;
598 712
599 for (i = 4; --i; ) /* loop a few times, before making important decisions */ 713 for (i = 4; --i; ) /* loop a few times, before making important decisions */
600 { 714 {
601 now = get_clock ();
602 diff = ev_now - now; 715 diff = ev_now - now;
603 716
604 if (fabs (odiff - diff) < MIN_TIMEJUMP) 717 if (fabs (odiff - diff) < MIN_TIMEJUMP)
605 return; /* all is well */ 718 return; /* all is well */
606 719
607 ev_now = ev_time (); 720 ev_now = ev_time ();
721 now = get_clock ();
722 now_floor = now;
608 } 723 }
609 724
610 periodics_reschedule (diff - odiff); 725 periodics_reschedule (diff - odiff);
611 /* no timer adjustment, as the monotonic clock doesn't jump */ 726 /* no timer adjustment, as the monotonic clock doesn't jump */
727 }
612 } 728 }
613 else 729 else
730#endif
614 { 731 {
732 ev_now = ev_time ();
733
615 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) 734 if (expect_false (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
616 { 735 {
617 periodics_reschedule (ev_now - now); 736 periodics_reschedule (ev_now - now);
618 737
619 /* adjust timers. this is easy, as the offset is the same for all */ 738 /* adjust timers. this is easy, as the offset is the same for all */
620 for (i = 0; i < timercnt; ++i) 739 for (i = 0; i < timercnt; ++i)
633 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; 752 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
634 753
635 do 754 do
636 { 755 {
637 /* queue check watchers (and execute them) */ 756 /* queue check watchers (and execute them) */
638 if (preparecnt) 757 if (expect_false (preparecnt))
639 { 758 {
640 queue_events ((W *)prepares, preparecnt, EV_PREPARE); 759 queue_events ((W *)prepares, preparecnt, EV_PREPARE);
641 call_pending (); 760 call_pending ();
642 } 761 }
643 762
646 765
647 /* calculate blocking time */ 766 /* calculate blocking time */
648 767
649 /* we only need this for !monotonic clockor timers, but as we basically 768 /* we only need this for !monotonic clockor timers, but as we basically
650 always have timers, we just calculate it always */ 769 always have timers, we just calculate it always */
770#if EV_USE_MONOTONIC
771 if (expect_true (have_monotonic))
772 time_update_monotonic ();
773 else
774#endif
775 {
651 ev_now = ev_time (); 776 ev_now = ev_time ();
777 now = ev_now;
778 }
652 779
653 if (flags & EVLOOP_NONBLOCK || idlecnt) 780 if (flags & EVLOOP_NONBLOCK || idlecnt)
654 block = 0.; 781 block = 0.;
655 else 782 else
656 { 783 {
657 block = MAX_BLOCKTIME; 784 block = MAX_BLOCKTIME;
658 785
659 if (timercnt) 786 if (timercnt)
660 { 787 {
661 ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; 788 ev_tstamp to = timers [0]->at - now + method_fudge;
662 if (block > to) block = to; 789 if (block > to) block = to;
663 } 790 }
664 791
665 if (periodiccnt) 792 if (periodiccnt)
666 { 793 {
719 head = &(*head)->next; 846 head = &(*head)->next;
720 } 847 }
721} 848}
722 849
723static void 850static void
724ev_clear (W w) 851ev_clear_pending (W w)
725{ 852{
726 if (w->pending) 853 if (w->pending)
727 { 854 {
728 pendings [w->pending - 1].w = 0; 855 pendings [ABSPRI (w)][w->pending - 1].w = 0;
729 w->pending = 0; 856 w->pending = 0;
730 } 857 }
731} 858}
732 859
733static void 860static void
734ev_start (W w, int active) 861ev_start (W w, int active)
735{ 862{
863 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI;
864 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
865
736 w->active = active; 866 w->active = active;
737} 867}
738 868
739static void 869static void
740ev_stop (W w) 870ev_stop (W w)
745/*****************************************************************************/ 875/*****************************************************************************/
746 876
747void 877void
748ev_io_start (struct ev_io *w) 878ev_io_start (struct ev_io *w)
749{ 879{
880 int fd = w->fd;
881
750 if (ev_is_active (w)) 882 if (ev_is_active (w))
751 return; 883 return;
752 884
753 int fd = w->fd; 885 assert (("ev_io_start called with negative fd", fd >= 0));
754 886
755 ev_start ((W)w, 1); 887 ev_start ((W)w, 1);
756 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 888 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
757 wlist_add ((WL *)&anfds[fd].head, (WL)w); 889 wlist_add ((WL *)&anfds[fd].head, (WL)w);
758 890
760} 892}
761 893
762void 894void
763ev_io_stop (struct ev_io *w) 895ev_io_stop (struct ev_io *w)
764{ 896{
765 ev_clear ((W)w); 897 ev_clear_pending ((W)w);
766 if (!ev_is_active (w)) 898 if (!ev_is_active (w))
767 return; 899 return;
768 900
769 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 901 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
770 ev_stop ((W)w); 902 ev_stop ((W)w);
778 if (ev_is_active (w)) 910 if (ev_is_active (w))
779 return; 911 return;
780 912
781 w->at += now; 913 w->at += now;
782 914
783 assert (("timer repeat value less than zero not allowed", w->repeat >= 0.)); 915 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
784 916
785 ev_start ((W)w, ++timercnt); 917 ev_start ((W)w, ++timercnt);
786 array_needsize (timers, timermax, timercnt, ); 918 array_needsize (timers, timermax, timercnt, );
787 timers [timercnt - 1] = w; 919 timers [timercnt - 1] = w;
788 upheap ((WT *)timers, timercnt - 1); 920 upheap ((WT *)timers, timercnt - 1);
789} 921}
790 922
791void 923void
792ev_timer_stop (struct ev_timer *w) 924ev_timer_stop (struct ev_timer *w)
793{ 925{
794 ev_clear ((W)w); 926 ev_clear_pending ((W)w);
795 if (!ev_is_active (w)) 927 if (!ev_is_active (w))
796 return; 928 return;
797 929
798 if (w->active < timercnt--) 930 if (w->active < timercnt--)
799 { 931 {
827ev_periodic_start (struct ev_periodic *w) 959ev_periodic_start (struct ev_periodic *w)
828{ 960{
829 if (ev_is_active (w)) 961 if (ev_is_active (w))
830 return; 962 return;
831 963
832 assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); 964 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
833 965
834 /* this formula differs from the one in periodic_reify because we do not always round up */ 966 /* this formula differs from the one in periodic_reify because we do not always round up */
835 if (w->interval) 967 if (w->interval)
836 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; 968 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
837 969
842} 974}
843 975
844void 976void
845ev_periodic_stop (struct ev_periodic *w) 977ev_periodic_stop (struct ev_periodic *w)
846{ 978{
847 ev_clear ((W)w); 979 ev_clear_pending ((W)w);
848 if (!ev_is_active (w)) 980 if (!ev_is_active (w))
849 return; 981 return;
850 982
851 if (w->active < periodiccnt--) 983 if (w->active < periodiccnt--)
852 { 984 {
860void 992void
861ev_signal_start (struct ev_signal *w) 993ev_signal_start (struct ev_signal *w)
862{ 994{
863 if (ev_is_active (w)) 995 if (ev_is_active (w))
864 return; 996 return;
997
998 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
865 999
866 ev_start ((W)w, 1); 1000 ev_start ((W)w, 1);
867 array_needsize (signals, signalmax, w->signum, signals_init); 1001 array_needsize (signals, signalmax, w->signum, signals_init);
868 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1002 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
869 1003
878} 1012}
879 1013
880void 1014void
881ev_signal_stop (struct ev_signal *w) 1015ev_signal_stop (struct ev_signal *w)
882{ 1016{
883 ev_clear ((W)w); 1017 ev_clear_pending ((W)w);
884 if (!ev_is_active (w)) 1018 if (!ev_is_active (w))
885 return; 1019 return;
886 1020
887 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 1021 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
888 ev_stop ((W)w); 1022 ev_stop ((W)w);
903} 1037}
904 1038
905void 1039void
906ev_idle_stop (struct ev_idle *w) 1040ev_idle_stop (struct ev_idle *w)
907{ 1041{
908 ev_clear ((W)w); 1042 ev_clear_pending ((W)w);
909 if (ev_is_active (w)) 1043 if (ev_is_active (w))
910 return; 1044 return;
911 1045
912 idles [w->active - 1] = idles [--idlecnt]; 1046 idles [w->active - 1] = idles [--idlecnt];
913 ev_stop ((W)w); 1047 ev_stop ((W)w);
925} 1059}
926 1060
927void 1061void
928ev_prepare_stop (struct ev_prepare *w) 1062ev_prepare_stop (struct ev_prepare *w)
929{ 1063{
930 ev_clear ((W)w); 1064 ev_clear_pending ((W)w);
931 if (ev_is_active (w)) 1065 if (ev_is_active (w))
932 return; 1066 return;
933 1067
934 prepares [w->active - 1] = prepares [--preparecnt]; 1068 prepares [w->active - 1] = prepares [--preparecnt];
935 ev_stop ((W)w); 1069 ev_stop ((W)w);
947} 1081}
948 1082
949void 1083void
950ev_check_stop (struct ev_check *w) 1084ev_check_stop (struct ev_check *w)
951{ 1085{
952 ev_clear ((W)w); 1086 ev_clear_pending ((W)w);
953 if (ev_is_active (w)) 1087 if (ev_is_active (w))
954 return; 1088 return;
955 1089
956 checks [w->active - 1] = checks [--checkcnt]; 1090 checks [w->active - 1] = checks [--checkcnt];
957 ev_stop ((W)w); 1091 ev_stop ((W)w);
968} 1102}
969 1103
970void 1104void
971ev_child_stop (struct ev_child *w) 1105ev_child_stop (struct ev_child *w)
972{ 1106{
973 ev_clear ((W)w); 1107 ev_clear_pending ((W)w);
974 if (ev_is_active (w)) 1108 if (ev_is_active (w))
975 return; 1109 return;
976 1110
977 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1111 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
978 ev_stop ((W)w); 1112 ev_stop ((W)w);
1017ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) 1151ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
1018{ 1152{
1019 struct ev_once *once = malloc (sizeof (struct ev_once)); 1153 struct ev_once *once = malloc (sizeof (struct ev_once));
1020 1154
1021 if (!once) 1155 if (!once)
1022 cb (EV_ERROR, arg); 1156 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1023 else 1157 else
1024 { 1158 {
1025 once->cb = cb; 1159 once->cb = cb;
1026 once->arg = arg; 1160 once->arg = arg;
1027 1161
1028 ev_watcher_init (&once->io, once_cb_io); 1162 ev_watcher_init (&once->io, once_cb_io);
1029
1030 if (fd >= 0) 1163 if (fd >= 0)
1031 { 1164 {
1032 ev_io_set (&once->io, fd, events); 1165 ev_io_set (&once->io, fd, events);
1033 ev_io_start (&once->io); 1166 ev_io_start (&once->io);
1034 } 1167 }
1035 1168
1036 ev_watcher_init (&once->to, once_cb_to); 1169 ev_watcher_init (&once->to, once_cb_to);
1037
1038 if (timeout >= 0.) 1170 if (timeout >= 0.)
1039 { 1171 {
1040 ev_timer_set (&once->to, timeout, 0.); 1172 ev_timer_set (&once->to, timeout, 0.);
1041 ev_timer_start (&once->to); 1173 ev_timer_start (&once->to);
1042 } 1174 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines