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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines