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.50 by root, Sat Nov 3 19:41:55 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#ifndef EV_STANDALONE
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>
37#include <stdio.h> 42#include <stdio.h>
38 43
39#include <assert.h> 44#include <assert.h>
40#include <errno.h> 45#include <errno.h>
41#include <sys/types.h> 46#include <sys/types.h>
47#ifndef WIN32
42#include <sys/wait.h> 48# include <sys/wait.h>
49#endif
43#include <sys/time.h> 50#include <sys/time.h>
44#include <time.h> 51#include <time.h>
45 52
53/**/
54
46#ifndef HAVE_MONOTONIC 55#ifndef EV_USE_MONOTONIC
56# define EV_USE_MONOTONIC 1
57#endif
58
59#ifndef EV_USE_SELECT
60# define EV_USE_SELECT 1
61#endif
62
63#ifndef EV_USE_POLL
64# define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */
65#endif
66
67#ifndef EV_USE_EPOLL
68# define EV_USE_EPOLL 0
69#endif
70
71#ifndef EV_USE_KQUEUE
72# define EV_USE_KQUEUE 0
73#endif
74
75#ifndef EV_USE_REALTIME
76# define EV_USE_REALTIME 1
77#endif
78
79/**/
80
47# ifdef CLOCK_MONOTONIC 81#ifndef CLOCK_MONOTONIC
82# undef EV_USE_MONOTONIC
48# define HAVE_MONOTONIC 1 83# define EV_USE_MONOTONIC 0
49# endif 84#endif
50#endif
51 85
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 86#ifndef CLOCK_REALTIME
61# define HAVE_REALTIME 1 /* posix requirement, but might be slower */ 87# undef EV_USE_REALTIME
88# define EV_USE_REALTIME 0
62#endif 89#endif
90
91/**/
63 92
64#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 93#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
65#define MAX_BLOCKTIME 60. 94#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 */ 95#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */
96/*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */
67 97
68#include "ev.h" 98#include "ev.h"
99
100#if __GNUC__ >= 3
101# define expect(expr,value) __builtin_expect ((expr),(value))
102# define inline inline
103#else
104# define expect(expr,value) (expr)
105# define inline static
106#endif
107
108#define expect_false(expr) expect ((expr) != 0, 0)
109#define expect_true(expr) expect ((expr) != 0, 1)
110
111#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
112#define ABSPRI(w) ((w)->priority - EV_MINPRI)
69 113
70typedef struct ev_watcher *W; 114typedef struct ev_watcher *W;
71typedef struct ev_watcher_list *WL; 115typedef struct ev_watcher_list *WL;
72typedef struct ev_watcher_time *WT; 116typedef struct ev_watcher_time *WT;
73 117
74static ev_tstamp now, diff; /* monotonic clock */ 118static ev_tstamp now_floor, now, diff; /* monotonic clock */
75ev_tstamp ev_now; 119ev_tstamp ev_now;
76int ev_method; 120int ev_method;
77 121
78static int have_monotonic; /* runtime */ 122static int have_monotonic; /* runtime */
79 123
84/*****************************************************************************/ 128/*****************************************************************************/
85 129
86ev_tstamp 130ev_tstamp
87ev_time (void) 131ev_time (void)
88{ 132{
89#if HAVE_REALTIME 133#if EV_USE_REALTIME
90 struct timespec ts; 134 struct timespec ts;
91 clock_gettime (CLOCK_REALTIME, &ts); 135 clock_gettime (CLOCK_REALTIME, &ts);
92 return ts.tv_sec + ts.tv_nsec * 1e-9; 136 return ts.tv_sec + ts.tv_nsec * 1e-9;
93#else 137#else
94 struct timeval tv; 138 struct timeval tv;
98} 142}
99 143
100static ev_tstamp 144static ev_tstamp
101get_clock (void) 145get_clock (void)
102{ 146{
103#if HAVE_MONOTONIC 147#if EV_USE_MONOTONIC
104 if (have_monotonic) 148 if (expect_true (have_monotonic))
105 { 149 {
106 struct timespec ts; 150 struct timespec ts;
107 clock_gettime (CLOCK_MONOTONIC, &ts); 151 clock_gettime (CLOCK_MONOTONIC, &ts);
108 return ts.tv_sec + ts.tv_nsec * 1e-9; 152 return ts.tv_sec + ts.tv_nsec * 1e-9;
109 } 153 }
110#endif 154#endif
111 155
112 return ev_time (); 156 return ev_time ();
113} 157}
114 158
159#define array_roundsize(base,n) ((n) | 4 & ~3)
160
115#define array_needsize(base,cur,cnt,init) \ 161#define array_needsize(base,cur,cnt,init) \
116 if ((cnt) > cur) \ 162 if (expect_false ((cnt) > cur)) \
117 { \ 163 { \
118 int newcnt = cur; \ 164 int newcnt = cur; \
119 do \ 165 do \
120 { \ 166 { \
121 newcnt = (newcnt << 1) | 4 & ~3; \ 167 newcnt = array_roundsize (base, newcnt << 1); \
122 } \ 168 } \
123 while ((cnt) > newcnt); \ 169 while ((cnt) > newcnt); \
124 \ 170 \
125 base = realloc (base, sizeof (*base) * (newcnt)); \ 171 base = realloc (base, sizeof (*base) * (newcnt)); \
126 init (base + cur, newcnt - cur); \ 172 init (base + cur, newcnt - cur); \
129 175
130/*****************************************************************************/ 176/*****************************************************************************/
131 177
132typedef struct 178typedef struct
133{ 179{
134 struct ev_io *head; 180 struct ev_watcher_list *head;
135 int events; 181 unsigned char events;
182 unsigned char reify;
136} ANFD; 183} ANFD;
137 184
138static ANFD *anfds; 185static ANFD *anfds;
139static int anfdmax; 186static int anfdmax;
140 187
143{ 190{
144 while (count--) 191 while (count--)
145 { 192 {
146 base->head = 0; 193 base->head = 0;
147 base->events = EV_NONE; 194 base->events = EV_NONE;
195 base->reify = 0;
196
148 ++base; 197 ++base;
149 } 198 }
150} 199}
151 200
152typedef struct 201typedef struct
153{ 202{
154 W w; 203 W w;
155 int events; 204 int events;
156} ANPENDING; 205} ANPENDING;
157 206
158static ANPENDING *pendings; 207static ANPENDING *pendings [NUMPRI];
159static int pendingmax, pendingcnt; 208static int pendingmax [NUMPRI], pendingcnt [NUMPRI];
160 209
161static void 210static void
162event (W w, int events) 211event (W w, int events)
163{ 212{
164 if (w->active) 213 if (w->pending)
165 { 214 {
166 w->pending = ++pendingcnt;
167 array_needsize (pendings, pendingmax, pendingcnt, );
168 pendings [pendingcnt - 1].w = w;
169 pendings [pendingcnt - 1].events = events; 215 pendings [ABSPRI (w)][w->pending - 1].events |= events;
216 return;
170 } 217 }
218
219 w->pending = ++pendingcnt [ABSPRI (w)];
220 array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], );
221 pendings [ABSPRI (w)][w->pending - 1].w = w;
222 pendings [ABSPRI (w)][w->pending - 1].events = events;
171} 223}
172 224
173static void 225static void
174queue_events (W *events, int eventcnt, int type) 226queue_events (W *events, int eventcnt, int type)
175{ 227{
183fd_event (int fd, int events) 235fd_event (int fd, int events)
184{ 236{
185 ANFD *anfd = anfds + fd; 237 ANFD *anfd = anfds + fd;
186 struct ev_io *w; 238 struct ev_io *w;
187 239
188 for (w = anfd->head; w; w = w->next) 240 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
189 { 241 {
190 int ev = w->events & events; 242 int ev = w->events & events;
191 243
192 if (ev) 244 if (ev)
193 event ((W)w, ev); 245 event ((W)w, ev);
210 ANFD *anfd = anfds + fd; 262 ANFD *anfd = anfds + fd;
211 struct ev_io *w; 263 struct ev_io *w;
212 264
213 int events = 0; 265 int events = 0;
214 266
215 for (w = anfd->head; w; w = w->next) 267 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
216 events |= w->events; 268 events |= w->events;
217 269
218 anfd->events &= ~EV_REIFY; 270 anfd->reify = 0;
219 271
220 if (anfd->events != events) 272 if (anfd->events != events)
221 { 273 {
222 method_modify (fd, anfd->events, events); 274 method_modify (fd, anfd->events, events);
223 anfd->events = events; 275 anfd->events = events;
228} 280}
229 281
230static void 282static void
231fd_change (int fd) 283fd_change (int fd)
232{ 284{
233 if (anfds [fd].events & EV_REIFY) 285 if (anfds [fd].reify || fdchangecnt < 0)
234 return; 286 return;
235 287
236 anfds [fd].events |= EV_REIFY; 288 anfds [fd].reify = 1;
237 289
238 ++fdchangecnt; 290 ++fdchangecnt;
239 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 291 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
240 fdchanges [fdchangecnt - 1] = fd; 292 fdchanges [fdchangecnt - 1] = fd;
241} 293}
242 294
295static void
296fd_kill (int fd)
297{
298 struct ev_io *w;
299
300 while ((w = (struct ev_io *)anfds [fd].head))
301 {
302 ev_io_stop (w);
303 event ((W)w, EV_ERROR | EV_READ | EV_WRITE);
304 }
305}
306
243/* called on EBADF to verify fds */ 307/* called on EBADF to verify fds */
244static void 308static void
245fd_recheck (void) 309fd_ebadf (void)
246{ 310{
247 int fd; 311 int fd;
248 312
249 for (fd = 0; fd < anfdmax; ++fd) 313 for (fd = 0; fd < anfdmax; ++fd)
250 if (anfds [fd].events) 314 if (anfds [fd].events)
251 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 315 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
252 while (anfds [fd].head) 316 fd_kill (fd);
317}
318
319/* called on ENOMEM in select/poll to kill some fds and retry */
320static void
321fd_enomem (void)
322{
323 int fd = anfdmax;
324
325 while (fd--)
326 if (anfds [fd].events)
253 { 327 {
254 event ((W)anfds [fd].head, EV_ERROR); 328 close (fd);
255 ev_io_stop (anfds [fd].head); 329 fd_kill (fd);
330 return;
256 } 331 }
257} 332}
258 333
259/*****************************************************************************/ 334/*****************************************************************************/
260 335
261static struct ev_timer **timers; 336static struct ev_timer **timers;
307 382
308/*****************************************************************************/ 383/*****************************************************************************/
309 384
310typedef struct 385typedef struct
311{ 386{
312 struct ev_signal *head; 387 struct ev_watcher_list *head;
313 sig_atomic_t gotsig; 388 sig_atomic_t volatile gotsig;
314} ANSIG; 389} ANSIG;
315 390
316static ANSIG *signals; 391static ANSIG *signals;
317static int signalmax; 392static int signalmax;
318 393
319static int sigpipe [2]; 394static int sigpipe [2];
320static sig_atomic_t gotsig; 395static sig_atomic_t volatile gotsig;
321static struct ev_io sigev; 396static struct ev_io sigev;
322 397
323static void 398static void
324signals_init (ANSIG *base, int count) 399signals_init (ANSIG *base, int count)
325{ 400{
326 while (count--) 401 while (count--)
327 { 402 {
328 base->head = 0; 403 base->head = 0;
329 base->gotsig = 0; 404 base->gotsig = 0;
405
330 ++base; 406 ++base;
331 } 407 }
332} 408}
333 409
334static void 410static void
336{ 412{
337 signals [signum - 1].gotsig = 1; 413 signals [signum - 1].gotsig = 1;
338 414
339 if (!gotsig) 415 if (!gotsig)
340 { 416 {
417 int old_errno = errno;
341 gotsig = 1; 418 gotsig = 1;
342 write (sigpipe [1], &gotsig, 1); 419 write (sigpipe [1], &signum, 1);
420 errno = old_errno;
343 } 421 }
344} 422}
345 423
346static void 424static void
347sigcb (struct ev_io *iow, int revents) 425sigcb (struct ev_io *iow, int revents)
348{ 426{
349 struct ev_signal *w; 427 struct ev_watcher_list *w;
350 int sig; 428 int signum;
351 429
430 read (sigpipe [0], &revents, 1);
352 gotsig = 0; 431 gotsig = 0;
353 read (sigpipe [0], &revents, 1);
354 432
355 for (sig = signalmax; sig--; ) 433 for (signum = signalmax; signum--; )
356 if (signals [sig].gotsig) 434 if (signals [signum].gotsig)
357 { 435 {
358 signals [sig].gotsig = 0; 436 signals [signum].gotsig = 0;
359 437
360 for (w = signals [sig].head; w; w = w->next) 438 for (w = signals [signum].head; w; w = w->next)
361 event ((W)w, EV_SIGNAL); 439 event ((W)w, EV_SIGNAL);
362 } 440 }
363} 441}
364 442
365static void 443static void
366siginit (void) 444siginit (void)
367{ 445{
446#ifndef WIN32
368 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); 447 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC);
369 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); 448 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC);
370 449
371 /* rather than sort out wether we really need nb, set it */ 450 /* rather than sort out wether we really need nb, set it */
372 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); 451 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
373 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 452 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
453#endif
374 454
375 ev_io_set (&sigev, sigpipe [0], EV_READ); 455 ev_io_set (&sigev, sigpipe [0], EV_READ);
376 ev_io_start (&sigev); 456 ev_io_start (&sigev);
377} 457}
378 458
390/*****************************************************************************/ 470/*****************************************************************************/
391 471
392static struct ev_child *childs [PID_HASHSIZE]; 472static struct ev_child *childs [PID_HASHSIZE];
393static struct ev_signal childev; 473static struct ev_signal childev;
394 474
475#ifndef WIN32
476
395#ifndef WCONTINUED 477#ifndef WCONTINUED
396# define WCONTINUED 0 478# define WCONTINUED 0
397#endif 479#endif
398 480
399static void 481static void
482child_reap (struct ev_signal *sw, int chain, int pid, int status)
483{
484 struct ev_child *w;
485
486 for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next)
487 if (w->pid == pid || !w->pid)
488 {
489 w->priority = sw->priority; /* need to do it *now* */
490 w->rpid = pid;
491 w->rstatus = status;
492 event ((W)w, EV_CHILD);
493 }
494}
495
496static void
400childcb (struct ev_signal *sw, int revents) 497childcb (struct ev_signal *sw, int revents)
401{ 498{
402 struct ev_child *w;
403 int pid, status; 499 int pid, status;
404 500
405 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) 501 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)))
406 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) 502 {
407 if (w->pid == pid || w->pid == -1) 503 /* make sure we are called again until all childs have been reaped */
408 {
409 w->status = status;
410 event ((W)w, EV_CHILD); 504 event ((W)sw, EV_SIGNAL);
411 } 505
506 child_reap (sw, pid, pid, status);
507 child_reap (sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */
508 }
412} 509}
510
511#endif
413 512
414/*****************************************************************************/ 513/*****************************************************************************/
415 514
515#if EV_USE_KQUEUE
516# include "ev_kqueue.c"
517#endif
416#if HAVE_EPOLL 518#if EV_USE_EPOLL
417# include "ev_epoll.c" 519# include "ev_epoll.c"
418#endif 520#endif
521#if EV_USE_POLL
522# include "ev_poll.c"
523#endif
419#if HAVE_SELECT 524#if EV_USE_SELECT
420# include "ev_select.c" 525# include "ev_select.c"
421#endif 526#endif
422 527
423int 528int
424ev_version_major (void) 529ev_version_major (void)
430ev_version_minor (void) 535ev_version_minor (void)
431{ 536{
432 return EV_VERSION_MINOR; 537 return EV_VERSION_MINOR;
433} 538}
434 539
540/* return true if we are running with elevated privileges and should ignore env variables */
541static int
542enable_secure ()
543{
544#ifdef WIN32
545 return 0;
546#else
547 return getuid () != geteuid ()
548 || getgid () != getegid ();
549#endif
550}
551
435int ev_init (int flags) 552int ev_init (int methods)
436{ 553{
437 if (!ev_method) 554 if (!ev_method)
438 { 555 {
439#if HAVE_MONOTONIC 556#if EV_USE_MONOTONIC
440 { 557 {
441 struct timespec ts; 558 struct timespec ts;
442 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 559 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
443 have_monotonic = 1; 560 have_monotonic = 1;
444 } 561 }
445#endif 562#endif
446 563
447 ev_now = ev_time (); 564 ev_now = ev_time ();
448 now = get_clock (); 565 now = get_clock ();
566 now_floor = now;
449 diff = ev_now - now; 567 diff = ev_now - now;
450 568
451 if (pipe (sigpipe)) 569 if (pipe (sigpipe))
452 return 0; 570 return 0;
453 571
572 if (methods == EVMETHOD_AUTO)
573 if (!enable_secure () && getenv ("LIBEV_METHODS"))
574 methods = atoi (getenv ("LIBEV_METHODS"));
575 else
454 ev_method = EVMETHOD_NONE; 576 methods = EVMETHOD_ANY;
577
578 ev_method = 0;
579#if EV_USE_KQUEUE
580 if (!ev_method && (methods & EVMETHOD_KQUEUE)) kqueue_init (methods);
581#endif
455#if HAVE_EPOLL 582#if EV_USE_EPOLL
456 if (ev_method == EVMETHOD_NONE) epoll_init (flags); 583 if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods);
457#endif 584#endif
585#if EV_USE_POLL
586 if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods);
587#endif
458#if HAVE_SELECT 588#if EV_USE_SELECT
459 if (ev_method == EVMETHOD_NONE) select_init (flags); 589 if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods);
460#endif 590#endif
461 591
462 if (ev_method) 592 if (ev_method)
463 { 593 {
464 ev_watcher_init (&sigev, sigcb); 594 ev_watcher_init (&sigev, sigcb);
595 ev_set_priority (&sigev, EV_MAXPRI);
465 siginit (); 596 siginit ();
466 597
598#ifndef WIN32
467 ev_signal_init (&childev, childcb, SIGCHLD); 599 ev_signal_init (&childev, childcb, SIGCHLD);
600 ev_set_priority (&childev, EV_MAXPRI);
468 ev_signal_start (&childev); 601 ev_signal_start (&childev);
602#endif
469 } 603 }
470 } 604 }
471 605
472 return ev_method; 606 return ev_method;
473} 607}
474 608
475/*****************************************************************************/ 609/*****************************************************************************/
476 610
477void 611void
478ev_prefork (void) 612ev_fork_prepare (void)
479{ 613{
480 /* nop */ 614 /* nop */
481} 615}
482 616
483void 617void
484ev_postfork_parent (void) 618ev_fork_parent (void)
485{ 619{
486 /* nop */ 620 /* nop */
487} 621}
488 622
489void 623void
490ev_postfork_child (void) 624ev_fork_child (void)
491{ 625{
492#if HAVE_EPOLL 626#if EV_USE_EPOLL
493 if (ev_method == EVMETHOD_EPOLL) 627 if (ev_method == EVMETHOD_EPOLL)
494 epoll_postfork_child (); 628 epoll_postfork_child ();
495#endif 629#endif
496 630
497 ev_io_stop (&sigev); 631 ev_io_stop (&sigev);
504/*****************************************************************************/ 638/*****************************************************************************/
505 639
506static void 640static void
507call_pending (void) 641call_pending (void)
508{ 642{
643 int pri;
644
645 for (pri = NUMPRI; pri--; )
509 while (pendingcnt) 646 while (pendingcnt [pri])
510 { 647 {
511 ANPENDING *p = pendings + --pendingcnt; 648 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
512 649
513 if (p->w) 650 if (p->w)
514 { 651 {
515 p->w->pending = 0; 652 p->w->pending = 0;
516 p->w->cb (p->w, p->events); 653 p->w->cb (p->w, p->events);
517 } 654 }
518 } 655 }
519} 656}
520 657
521static void 658static void
522timers_reify (void) 659timers_reify (void)
523{ 660{
524 while (timercnt && timers [0]->at <= now) 661 while (timercnt && timers [0]->at <= now)
525 { 662 {
526 struct ev_timer *w = timers [0]; 663 struct ev_timer *w = timers [0];
527
528 event ((W)w, EV_TIMEOUT);
529 664
530 /* first reschedule or stop timer */ 665 /* first reschedule or stop timer */
531 if (w->repeat) 666 if (w->repeat)
532 { 667 {
668 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
533 w->at = now + w->repeat; 669 w->at = now + w->repeat;
534 assert (("timer timeout in the past, negative repeat?", w->at > now));
535 downheap ((WT *)timers, timercnt, 0); 670 downheap ((WT *)timers, timercnt, 0);
536 } 671 }
537 else 672 else
538 ev_timer_stop (w); /* nonrepeating: stop timer */ 673 ev_timer_stop (w); /* nonrepeating: stop timer */
674
675 event ((W)w, EV_TIMEOUT);
539 } 676 }
540} 677}
541 678
542static void 679static void
543periodics_reify (void) 680periodics_reify (void)
548 685
549 /* first reschedule or stop timer */ 686 /* first reschedule or stop timer */
550 if (w->interval) 687 if (w->interval)
551 { 688 {
552 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; 689 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)); 690 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > ev_now));
554 downheap ((WT *)periodics, periodiccnt, 0); 691 downheap ((WT *)periodics, periodiccnt, 0);
555 } 692 }
556 else 693 else
557 ev_periodic_stop (w); /* nonrepeating: stop timer */ 694 ev_periodic_stop (w); /* nonrepeating: stop timer */
558 695
559 event ((W)w, EV_TIMEOUT); 696 event ((W)w, EV_PERIODIC);
560 } 697 }
561} 698}
562 699
563static void 700static void
564periodics_reschedule (ev_tstamp diff) 701periodics_reschedule (ev_tstamp diff)
583 } 720 }
584 } 721 }
585 } 722 }
586} 723}
587 724
725static int
726time_update_monotonic (void)
727{
728 now = get_clock ();
729
730 if (expect_true (now - now_floor < MIN_TIMEJUMP * .5))
731 {
732 ev_now = now + diff;
733 return 0;
734 }
735 else
736 {
737 now_floor = now;
738 ev_now = ev_time ();
739 return 1;
740 }
741}
742
588static void 743static void
589time_update (void) 744time_update (void)
590{ 745{
591 int i; 746 int i;
592 747
593 ev_now = ev_time (); 748#if EV_USE_MONOTONIC
594
595 if (have_monotonic) 749 if (expect_true (have_monotonic))
596 { 750 {
597 ev_tstamp odiff = diff; 751 if (time_update_monotonic ())
598
599 for (i = 4; --i; ) /* loop a few times, before making important decisions */
600 { 752 {
601 now = get_clock (); 753 ev_tstamp odiff = diff;
754
755 for (i = 4; --i; ) /* loop a few times, before making important decisions */
756 {
602 diff = ev_now - now; 757 diff = ev_now - now;
603 758
604 if (fabs (odiff - diff) < MIN_TIMEJUMP) 759 if (fabs (odiff - diff) < MIN_TIMEJUMP)
605 return; /* all is well */ 760 return; /* all is well */
606 761
607 ev_now = ev_time (); 762 ev_now = ev_time ();
763 now = get_clock ();
764 now_floor = now;
765 }
766
767 periodics_reschedule (diff - odiff);
768 /* no timer adjustment, as the monotonic clock doesn't jump */
608 } 769 }
609
610 periodics_reschedule (diff - odiff);
611 /* no timer adjustment, as the monotonic clock doesn't jump */
612 } 770 }
613 else 771 else
772#endif
614 { 773 {
774 ev_now = ev_time ();
775
615 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) 776 if (expect_false (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
616 { 777 {
617 periodics_reschedule (ev_now - now); 778 periodics_reschedule (ev_now - now);
618 779
619 /* adjust timers. this is easy, as the offset is the same for all */ 780 /* adjust timers. this is easy, as the offset is the same for all */
620 for (i = 0; i < timercnt; ++i) 781 for (i = 0; i < timercnt; ++i)
633 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; 794 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
634 795
635 do 796 do
636 { 797 {
637 /* queue check watchers (and execute them) */ 798 /* queue check watchers (and execute them) */
638 if (preparecnt) 799 if (expect_false (preparecnt))
639 { 800 {
640 queue_events ((W *)prepares, preparecnt, EV_PREPARE); 801 queue_events ((W *)prepares, preparecnt, EV_PREPARE);
641 call_pending (); 802 call_pending ();
642 } 803 }
643 804
646 807
647 /* calculate blocking time */ 808 /* calculate blocking time */
648 809
649 /* we only need this for !monotonic clockor timers, but as we basically 810 /* we only need this for !monotonic clockor timers, but as we basically
650 always have timers, we just calculate it always */ 811 always have timers, we just calculate it always */
812#if EV_USE_MONOTONIC
813 if (expect_true (have_monotonic))
814 time_update_monotonic ();
815 else
816#endif
817 {
651 ev_now = ev_time (); 818 ev_now = ev_time ();
819 now = ev_now;
820 }
652 821
653 if (flags & EVLOOP_NONBLOCK || idlecnt) 822 if (flags & EVLOOP_NONBLOCK || idlecnt)
654 block = 0.; 823 block = 0.;
655 else 824 else
656 { 825 {
657 block = MAX_BLOCKTIME; 826 block = MAX_BLOCKTIME;
658 827
659 if (timercnt) 828 if (timercnt)
660 { 829 {
661 ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; 830 ev_tstamp to = timers [0]->at - now + method_fudge;
662 if (block > to) block = to; 831 if (block > to) block = to;
663 } 832 }
664 833
665 if (periodiccnt) 834 if (periodiccnt)
666 { 835 {
719 head = &(*head)->next; 888 head = &(*head)->next;
720 } 889 }
721} 890}
722 891
723static void 892static void
724ev_clear (W w) 893ev_clear_pending (W w)
725{ 894{
726 if (w->pending) 895 if (w->pending)
727 { 896 {
728 pendings [w->pending - 1].w = 0; 897 pendings [ABSPRI (w)][w->pending - 1].w = 0;
729 w->pending = 0; 898 w->pending = 0;
730 } 899 }
731} 900}
732 901
733static void 902static void
734ev_start (W w, int active) 903ev_start (W w, int active)
735{ 904{
905 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI;
906 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
907
736 w->active = active; 908 w->active = active;
737} 909}
738 910
739static void 911static void
740ev_stop (W w) 912ev_stop (W w)
745/*****************************************************************************/ 917/*****************************************************************************/
746 918
747void 919void
748ev_io_start (struct ev_io *w) 920ev_io_start (struct ev_io *w)
749{ 921{
922 int fd = w->fd;
923
750 if (ev_is_active (w)) 924 if (ev_is_active (w))
751 return; 925 return;
752 926
753 int fd = w->fd; 927 assert (("ev_io_start called with negative fd", fd >= 0));
754 928
755 ev_start ((W)w, 1); 929 ev_start ((W)w, 1);
756 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 930 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
757 wlist_add ((WL *)&anfds[fd].head, (WL)w); 931 wlist_add ((WL *)&anfds[fd].head, (WL)w);
758 932
760} 934}
761 935
762void 936void
763ev_io_stop (struct ev_io *w) 937ev_io_stop (struct ev_io *w)
764{ 938{
765 ev_clear ((W)w); 939 ev_clear_pending ((W)w);
766 if (!ev_is_active (w)) 940 if (!ev_is_active (w))
767 return; 941 return;
768 942
769 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 943 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
770 ev_stop ((W)w); 944 ev_stop ((W)w);
778 if (ev_is_active (w)) 952 if (ev_is_active (w))
779 return; 953 return;
780 954
781 w->at += now; 955 w->at += now;
782 956
783 assert (("timer repeat value less than zero not allowed", w->repeat >= 0.)); 957 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
784 958
785 ev_start ((W)w, ++timercnt); 959 ev_start ((W)w, ++timercnt);
786 array_needsize (timers, timermax, timercnt, ); 960 array_needsize (timers, timermax, timercnt, );
787 timers [timercnt - 1] = w; 961 timers [timercnt - 1] = w;
788 upheap ((WT *)timers, timercnt - 1); 962 upheap ((WT *)timers, timercnt - 1);
789} 963}
790 964
791void 965void
792ev_timer_stop (struct ev_timer *w) 966ev_timer_stop (struct ev_timer *w)
793{ 967{
794 ev_clear ((W)w); 968 ev_clear_pending ((W)w);
795 if (!ev_is_active (w)) 969 if (!ev_is_active (w))
796 return; 970 return;
797 971
798 if (w->active < timercnt--) 972 if (w->active < timercnt--)
799 { 973 {
827ev_periodic_start (struct ev_periodic *w) 1001ev_periodic_start (struct ev_periodic *w)
828{ 1002{
829 if (ev_is_active (w)) 1003 if (ev_is_active (w))
830 return; 1004 return;
831 1005
832 assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); 1006 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
833 1007
834 /* this formula differs from the one in periodic_reify because we do not always round up */ 1008 /* this formula differs from the one in periodic_reify because we do not always round up */
835 if (w->interval) 1009 if (w->interval)
836 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; 1010 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
837 1011
842} 1016}
843 1017
844void 1018void
845ev_periodic_stop (struct ev_periodic *w) 1019ev_periodic_stop (struct ev_periodic *w)
846{ 1020{
847 ev_clear ((W)w); 1021 ev_clear_pending ((W)w);
848 if (!ev_is_active (w)) 1022 if (!ev_is_active (w))
849 return; 1023 return;
850 1024
851 if (w->active < periodiccnt--) 1025 if (w->active < periodiccnt--)
852 { 1026 {
855 } 1029 }
856 1030
857 ev_stop ((W)w); 1031 ev_stop ((W)w);
858} 1032}
859 1033
1034#ifndef SA_RESTART
1035# define SA_RESTART 0
1036#endif
1037
860void 1038void
861ev_signal_start (struct ev_signal *w) 1039ev_signal_start (struct ev_signal *w)
862{ 1040{
863 if (ev_is_active (w)) 1041 if (ev_is_active (w))
864 return; 1042 return;
1043
1044 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
865 1045
866 ev_start ((W)w, 1); 1046 ev_start ((W)w, 1);
867 array_needsize (signals, signalmax, w->signum, signals_init); 1047 array_needsize (signals, signalmax, w->signum, signals_init);
868 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1048 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
869 1049
870 if (!w->next) 1050 if (!w->next)
871 { 1051 {
872 struct sigaction sa; 1052 struct sigaction sa;
873 sa.sa_handler = sighandler; 1053 sa.sa_handler = sighandler;
874 sigfillset (&sa.sa_mask); 1054 sigfillset (&sa.sa_mask);
875 sa.sa_flags = 0; 1055 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
876 sigaction (w->signum, &sa, 0); 1056 sigaction (w->signum, &sa, 0);
877 } 1057 }
878} 1058}
879 1059
880void 1060void
881ev_signal_stop (struct ev_signal *w) 1061ev_signal_stop (struct ev_signal *w)
882{ 1062{
883 ev_clear ((W)w); 1063 ev_clear_pending ((W)w);
884 if (!ev_is_active (w)) 1064 if (!ev_is_active (w))
885 return; 1065 return;
886 1066
887 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 1067 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
888 ev_stop ((W)w); 1068 ev_stop ((W)w);
903} 1083}
904 1084
905void 1085void
906ev_idle_stop (struct ev_idle *w) 1086ev_idle_stop (struct ev_idle *w)
907{ 1087{
908 ev_clear ((W)w); 1088 ev_clear_pending ((W)w);
909 if (ev_is_active (w)) 1089 if (ev_is_active (w))
910 return; 1090 return;
911 1091
912 idles [w->active - 1] = idles [--idlecnt]; 1092 idles [w->active - 1] = idles [--idlecnt];
913 ev_stop ((W)w); 1093 ev_stop ((W)w);
925} 1105}
926 1106
927void 1107void
928ev_prepare_stop (struct ev_prepare *w) 1108ev_prepare_stop (struct ev_prepare *w)
929{ 1109{
930 ev_clear ((W)w); 1110 ev_clear_pending ((W)w);
931 if (ev_is_active (w)) 1111 if (ev_is_active (w))
932 return; 1112 return;
933 1113
934 prepares [w->active - 1] = prepares [--preparecnt]; 1114 prepares [w->active - 1] = prepares [--preparecnt];
935 ev_stop ((W)w); 1115 ev_stop ((W)w);
947} 1127}
948 1128
949void 1129void
950ev_check_stop (struct ev_check *w) 1130ev_check_stop (struct ev_check *w)
951{ 1131{
952 ev_clear ((W)w); 1132 ev_clear_pending ((W)w);
953 if (ev_is_active (w)) 1133 if (ev_is_active (w))
954 return; 1134 return;
955 1135
956 checks [w->active - 1] = checks [--checkcnt]; 1136 checks [w->active - 1] = checks [--checkcnt];
957 ev_stop ((W)w); 1137 ev_stop ((W)w);
968} 1148}
969 1149
970void 1150void
971ev_child_stop (struct ev_child *w) 1151ev_child_stop (struct ev_child *w)
972{ 1152{
973 ev_clear ((W)w); 1153 ev_clear_pending ((W)w);
974 if (ev_is_active (w)) 1154 if (ev_is_active (w))
975 return; 1155 return;
976 1156
977 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1157 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
978 ev_stop ((W)w); 1158 ev_stop ((W)w);
1017ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) 1197ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
1018{ 1198{
1019 struct ev_once *once = malloc (sizeof (struct ev_once)); 1199 struct ev_once *once = malloc (sizeof (struct ev_once));
1020 1200
1021 if (!once) 1201 if (!once)
1022 cb (EV_ERROR, arg); 1202 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1023 else 1203 else
1024 { 1204 {
1025 once->cb = cb; 1205 once->cb = cb;
1026 once->arg = arg; 1206 once->arg = arg;
1027 1207
1028 ev_watcher_init (&once->io, once_cb_io); 1208 ev_watcher_init (&once->io, once_cb_io);
1029
1030 if (fd >= 0) 1209 if (fd >= 0)
1031 { 1210 {
1032 ev_io_set (&once->io, fd, events); 1211 ev_io_set (&once->io, fd, events);
1033 ev_io_start (&once->io); 1212 ev_io_start (&once->io);
1034 } 1213 }
1035 1214
1036 ev_watcher_init (&once->to, once_cb_to); 1215 ev_watcher_init (&once->to, once_cb_to);
1037
1038 if (timeout >= 0.) 1216 if (timeout >= 0.)
1039 { 1217 {
1040 ev_timer_set (&once->to, timeout, 0.); 1218 ev_timer_set (&once->to, timeout, 0.);
1041 ev_timer_start (&once->to); 1219 ev_timer_start (&once->to);
1042 } 1220 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines