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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines