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.46 by root, Sat Nov 3 09:20:12 2007 UTC

1/* 1/*
2 * libev event processing core, watcher management
3 *
2 * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> 4 * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de>
3 * All rights reserved. 5 * All rights reserved.
4 * 6 *
5 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are 8 * modification, are permitted provided that the following conditions are
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */ 30 */
31#if EV_USE_CONFIG_H
32# include "config.h"
33#endif
29 34
30#include <math.h> 35#include <math.h>
31#include <stdlib.h> 36#include <stdlib.h>
32#include <unistd.h> 37#include <unistd.h>
33#include <fcntl.h> 38#include <fcntl.h>
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); \
130/*****************************************************************************/ 176/*****************************************************************************/
131 177
132typedef struct 178typedef struct
133{ 179{
134 struct ev_io *head; 180 struct ev_io *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{
213 int events = 0; 265 int events = 0;
214 266
215 for (w = anfd->head; w; w = w->next) 267 for (w = anfd->head; w; w = 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 printf ("killing fd %d\n", fd);//D
301 while ((w = anfds [fd].head))
302 {
303 ev_io_stop (w);
304 event ((W)w, EV_ERROR | EV_READ | EV_WRITE);
305 }
306}
307
243/* called on EBADF to verify fds */ 308/* called on EBADF to verify fds */
244static void 309static void
245fd_recheck (void) 310fd_ebadf (void)
246{ 311{
247 int fd; 312 int fd;
248 313
249 for (fd = 0; fd < anfdmax; ++fd) 314 for (fd = 0; fd < anfdmax; ++fd)
250 if (anfds [fd].events) 315 if (anfds [fd].events)
251 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 316 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
252 while (anfds [fd].head) 317 fd_kill (fd);
318}
319
320/* called on ENOMEM in select/poll to kill some fds and retry */
321static void
322fd_enomem (void)
323{
324 int fd = anfdmax;
325
326 while (fd--)
327 if (anfds [fd].events)
253 { 328 {
254 event ((W)anfds [fd].head, EV_ERROR); 329 close (fd);
255 ev_io_stop (anfds [fd].head); 330 fd_kill (fd);
331 return;
256 } 332 }
257} 333}
258 334
259/*****************************************************************************/ 335/*****************************************************************************/
260 336
261static struct ev_timer **timers; 337static struct ev_timer **timers;
308/*****************************************************************************/ 384/*****************************************************************************/
309 385
310typedef struct 386typedef struct
311{ 387{
312 struct ev_signal *head; 388 struct ev_signal *head;
313 sig_atomic_t gotsig; 389 sig_atomic_t volatile gotsig;
314} ANSIG; 390} ANSIG;
315 391
316static ANSIG *signals; 392static ANSIG *signals;
317static int signalmax; 393static int signalmax;
318 394
319static int sigpipe [2]; 395static int sigpipe [2];
320static sig_atomic_t gotsig; 396static sig_atomic_t volatile gotsig;
321static struct ev_io sigev; 397static struct ev_io sigev;
322 398
323static void 399static void
324signals_init (ANSIG *base, int count) 400signals_init (ANSIG *base, int count)
325{ 401{
326 while (count--) 402 while (count--)
327 { 403 {
328 base->head = 0; 404 base->head = 0;
329 base->gotsig = 0; 405 base->gotsig = 0;
406
330 ++base; 407 ++base;
331 } 408 }
332} 409}
333 410
334static void 411static void
337 signals [signum - 1].gotsig = 1; 414 signals [signum - 1].gotsig = 1;
338 415
339 if (!gotsig) 416 if (!gotsig)
340 { 417 {
341 gotsig = 1; 418 gotsig = 1;
342 write (sigpipe [1], &gotsig, 1); 419 write (sigpipe [1], &signum, 1);
343 } 420 }
344} 421}
345 422
346static void 423static void
347sigcb (struct ev_io *iow, int revents) 424sigcb (struct ev_io *iow, int revents)
348{ 425{
349 struct ev_signal *w; 426 struct ev_signal *w;
350 int sig; 427 int signum;
351 428
429 read (sigpipe [0], &revents, 1);
352 gotsig = 0; 430 gotsig = 0;
353 read (sigpipe [0], &revents, 1);
354 431
355 for (sig = signalmax; sig--; ) 432 for (signum = signalmax; signum--; )
356 if (signals [sig].gotsig) 433 if (signals [signum].gotsig)
357 { 434 {
358 signals [sig].gotsig = 0; 435 signals [signum].gotsig = 0;
359 436
360 for (w = signals [sig].head; w; w = w->next) 437 for (w = signals [signum].head; w; w = w->next)
361 event ((W)w, EV_SIGNAL); 438 event ((W)w, EV_SIGNAL);
362 } 439 }
363} 440}
364 441
365static void 442static void
366siginit (void) 443siginit (void)
367{ 444{
445#ifndef WIN32
368 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); 446 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC);
369 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); 447 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC);
370 448
371 /* rather than sort out wether we really need nb, set it */ 449 /* rather than sort out wether we really need nb, set it */
372 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); 450 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
373 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 451 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
452#endif
374 453
375 ev_io_set (&sigev, sigpipe [0], EV_READ); 454 ev_io_set (&sigev, sigpipe [0], EV_READ);
376 ev_io_start (&sigev); 455 ev_io_start (&sigev);
377} 456}
378 457
390/*****************************************************************************/ 469/*****************************************************************************/
391 470
392static struct ev_child *childs [PID_HASHSIZE]; 471static struct ev_child *childs [PID_HASHSIZE];
393static struct ev_signal childev; 472static struct ev_signal childev;
394 473
474#ifndef WIN32
475
395#ifndef WCONTINUED 476#ifndef WCONTINUED
396# define WCONTINUED 0 477# define WCONTINUED 0
397#endif 478#endif
398 479
399static void 480static void
402 struct ev_child *w; 483 struct ev_child *w;
403 int pid, status; 484 int pid, status;
404 485
405 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) 486 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1)
406 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) 487 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next)
407 if (w->pid == pid || w->pid == -1) 488 if (w->pid == pid || !w->pid)
408 { 489 {
490 w->rpid = pid;
409 w->status = status; 491 w->rstatus = status;
410 event ((W)w, EV_CHILD); 492 event ((W)w, EV_CHILD);
411 } 493 }
412} 494}
413 495
414/*****************************************************************************/ 496#endif
415 497
498/*****************************************************************************/
499
500#if EV_USE_KQUEUE
501# include "ev_kqueue.c"
502#endif
416#if HAVE_EPOLL 503#if EV_USE_EPOLL
417# include "ev_epoll.c" 504# include "ev_epoll.c"
418#endif 505#endif
506#if EV_USE_POLL
507# include "ev_poll.c"
508#endif
419#if HAVE_SELECT 509#if EV_USE_SELECT
420# include "ev_select.c" 510# include "ev_select.c"
421#endif 511#endif
422 512
423int 513int
424ev_version_major (void) 514ev_version_major (void)
430ev_version_minor (void) 520ev_version_minor (void)
431{ 521{
432 return EV_VERSION_MINOR; 522 return EV_VERSION_MINOR;
433} 523}
434 524
525/* return true if we are running with elevated privileges and ignore env variables */
526static int
527enable_secure ()
528{
529 return getuid () != geteuid ()
530 || getgid () != getegid ();
531}
532
435int ev_init (int flags) 533int ev_init (int methods)
436{ 534{
437 if (!ev_method) 535 if (!ev_method)
438 { 536 {
439#if HAVE_MONOTONIC 537#if EV_USE_MONOTONIC
440 { 538 {
441 struct timespec ts; 539 struct timespec ts;
442 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 540 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
443 have_monotonic = 1; 541 have_monotonic = 1;
444 } 542 }
445#endif 543#endif
446 544
447 ev_now = ev_time (); 545 ev_now = ev_time ();
448 now = get_clock (); 546 now = get_clock ();
547 now_floor = now;
449 diff = ev_now - now; 548 diff = ev_now - now;
450 549
451 if (pipe (sigpipe)) 550 if (pipe (sigpipe))
452 return 0; 551 return 0;
453 552
553 if (methods == EVMETHOD_AUTO)
554 if (!enable_secure () && getenv ("LIBEV_METHODS"))
555 methods = atoi (getenv ("LIBEV_METHODS"));
556 else
454 ev_method = EVMETHOD_NONE; 557 methods = EVMETHOD_ANY;
558
559 ev_method = 0;
560#if EV_USE_KQUEUE
561 if (!ev_method && (methods & EVMETHOD_KQUEUE)) kqueue_init (methods);
562#endif
455#if HAVE_EPOLL 563#if EV_USE_EPOLL
456 if (ev_method == EVMETHOD_NONE) epoll_init (flags); 564 if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods);
457#endif 565#endif
566#if EV_USE_POLL
567 if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods);
568#endif
458#if HAVE_SELECT 569#if EV_USE_SELECT
459 if (ev_method == EVMETHOD_NONE) select_init (flags); 570 if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods);
460#endif 571#endif
461 572
462 if (ev_method) 573 if (ev_method)
463 { 574 {
464 ev_watcher_init (&sigev, sigcb); 575 ev_watcher_init (&sigev, sigcb);
465 siginit (); 576 siginit ();
466 577
578#ifndef WIN32
467 ev_signal_init (&childev, childcb, SIGCHLD); 579 ev_signal_init (&childev, childcb, SIGCHLD);
468 ev_signal_start (&childev); 580 ev_signal_start (&childev);
581#endif
469 } 582 }
470 } 583 }
471 584
472 return ev_method; 585 return ev_method;
473} 586}
474 587
475/*****************************************************************************/ 588/*****************************************************************************/
476 589
477void 590void
478ev_prefork (void) 591ev_fork_prepare (void)
479{ 592{
480 /* nop */ 593 /* nop */
481} 594}
482 595
483void 596void
484ev_postfork_parent (void) 597ev_fork_parent (void)
485{ 598{
486 /* nop */ 599 /* nop */
487} 600}
488 601
489void 602void
490ev_postfork_child (void) 603ev_fork_child (void)
491{ 604{
492#if HAVE_EPOLL 605#if EV_USE_EPOLL
493 if (ev_method == EVMETHOD_EPOLL) 606 if (ev_method == EVMETHOD_EPOLL)
494 epoll_postfork_child (); 607 epoll_postfork_child ();
495#endif 608#endif
496 609
497 ev_io_stop (&sigev); 610 ev_io_stop (&sigev);
504/*****************************************************************************/ 617/*****************************************************************************/
505 618
506static void 619static void
507call_pending (void) 620call_pending (void)
508{ 621{
622 int pri;
623
624 for (pri = NUMPRI; pri--; )
509 while (pendingcnt) 625 while (pendingcnt [pri])
510 { 626 {
511 ANPENDING *p = pendings + --pendingcnt; 627 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
512 628
513 if (p->w) 629 if (p->w)
514 { 630 {
515 p->w->pending = 0; 631 p->w->pending = 0;
516 p->w->cb (p->w, p->events); 632 p->w->cb (p->w, p->events);
517 } 633 }
518 } 634 }
519} 635}
520 636
521static void 637static void
522timers_reify (void) 638timers_reify (void)
523{ 639{
524 while (timercnt && timers [0]->at <= now) 640 while (timercnt && timers [0]->at <= now)
525 { 641 {
526 struct ev_timer *w = timers [0]; 642 struct ev_timer *w = timers [0];
527
528 event ((W)w, EV_TIMEOUT);
529 643
530 /* first reschedule or stop timer */ 644 /* first reschedule or stop timer */
531 if (w->repeat) 645 if (w->repeat)
532 { 646 {
647 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
533 w->at = now + w->repeat; 648 w->at = now + w->repeat;
534 assert (("timer timeout in the past, negative repeat?", w->at > now));
535 downheap ((WT *)timers, timercnt, 0); 649 downheap ((WT *)timers, timercnt, 0);
536 } 650 }
537 else 651 else
538 ev_timer_stop (w); /* nonrepeating: stop timer */ 652 ev_timer_stop (w); /* nonrepeating: stop timer */
653
654 event ((W)w, EV_TIMEOUT);
539 } 655 }
540} 656}
541 657
542static void 658static void
543periodics_reify (void) 659periodics_reify (void)
548 664
549 /* first reschedule or stop timer */ 665 /* first reschedule or stop timer */
550 if (w->interval) 666 if (w->interval)
551 { 667 {
552 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; 668 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)); 669 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > ev_now));
554 downheap ((WT *)periodics, periodiccnt, 0); 670 downheap ((WT *)periodics, periodiccnt, 0);
555 } 671 }
556 else 672 else
557 ev_periodic_stop (w); /* nonrepeating: stop timer */ 673 ev_periodic_stop (w); /* nonrepeating: stop timer */
558 674
559 event ((W)w, EV_TIMEOUT); 675 event ((W)w, EV_PERIODIC);
560 } 676 }
561} 677}
562 678
563static void 679static void
564periodics_reschedule (ev_tstamp diff) 680periodics_reschedule (ev_tstamp diff)
583 } 699 }
584 } 700 }
585 } 701 }
586} 702}
587 703
704static int
705time_update_monotonic (void)
706{
707 now = get_clock ();
708
709 if (expect_true (now - now_floor < MIN_TIMEJUMP * .5))
710 {
711 ev_now = now + diff;
712 return 0;
713 }
714 else
715 {
716 now_floor = now;
717 ev_now = ev_time ();
718 return 1;
719 }
720}
721
588static void 722static void
589time_update (void) 723time_update (void)
590{ 724{
591 int i; 725 int i;
592 726
593 ev_now = ev_time (); 727#if EV_USE_MONOTONIC
594
595 if (have_monotonic) 728 if (expect_true (have_monotonic))
596 { 729 {
730 if (time_update_monotonic ())
731 {
597 ev_tstamp odiff = diff; 732 ev_tstamp odiff = diff;
598 733
599 for (i = 4; --i; ) /* loop a few times, before making important decisions */ 734 for (i = 4; --i; ) /* loop a few times, before making important decisions */
600 { 735 {
601 now = get_clock ();
602 diff = ev_now - now; 736 diff = ev_now - now;
603 737
604 if (fabs (odiff - diff) < MIN_TIMEJUMP) 738 if (fabs (odiff - diff) < MIN_TIMEJUMP)
605 return; /* all is well */ 739 return; /* all is well */
606 740
607 ev_now = ev_time (); 741 ev_now = ev_time ();
742 now = get_clock ();
743 now_floor = now;
608 } 744 }
609 745
610 periodics_reschedule (diff - odiff); 746 periodics_reschedule (diff - odiff);
611 /* no timer adjustment, as the monotonic clock doesn't jump */ 747 /* no timer adjustment, as the monotonic clock doesn't jump */
748 }
612 } 749 }
613 else 750 else
751#endif
614 { 752 {
753 ev_now = ev_time ();
754
615 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) 755 if (expect_false (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
616 { 756 {
617 periodics_reschedule (ev_now - now); 757 periodics_reschedule (ev_now - now);
618 758
619 /* adjust timers. this is easy, as the offset is the same for all */ 759 /* adjust timers. this is easy, as the offset is the same for all */
620 for (i = 0; i < timercnt; ++i) 760 for (i = 0; i < timercnt; ++i)
633 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; 773 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
634 774
635 do 775 do
636 { 776 {
637 /* queue check watchers (and execute them) */ 777 /* queue check watchers (and execute them) */
638 if (preparecnt) 778 if (expect_false (preparecnt))
639 { 779 {
640 queue_events ((W *)prepares, preparecnt, EV_PREPARE); 780 queue_events ((W *)prepares, preparecnt, EV_PREPARE);
641 call_pending (); 781 call_pending ();
642 } 782 }
643 783
646 786
647 /* calculate blocking time */ 787 /* calculate blocking time */
648 788
649 /* we only need this for !monotonic clockor timers, but as we basically 789 /* we only need this for !monotonic clockor timers, but as we basically
650 always have timers, we just calculate it always */ 790 always have timers, we just calculate it always */
791#if EV_USE_MONOTONIC
792 if (expect_true (have_monotonic))
793 time_update_monotonic ();
794 else
795#endif
796 {
651 ev_now = ev_time (); 797 ev_now = ev_time ();
798 now = ev_now;
799 }
652 800
653 if (flags & EVLOOP_NONBLOCK || idlecnt) 801 if (flags & EVLOOP_NONBLOCK || idlecnt)
654 block = 0.; 802 block = 0.;
655 else 803 else
656 { 804 {
657 block = MAX_BLOCKTIME; 805 block = MAX_BLOCKTIME;
658 806
659 if (timercnt) 807 if (timercnt)
660 { 808 {
661 ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; 809 ev_tstamp to = timers [0]->at - now + method_fudge;
662 if (block > to) block = to; 810 if (block > to) block = to;
663 } 811 }
664 812
665 if (periodiccnt) 813 if (periodiccnt)
666 { 814 {
719 head = &(*head)->next; 867 head = &(*head)->next;
720 } 868 }
721} 869}
722 870
723static void 871static void
724ev_clear (W w) 872ev_clear_pending (W w)
725{ 873{
726 if (w->pending) 874 if (w->pending)
727 { 875 {
728 pendings [w->pending - 1].w = 0; 876 pendings [ABSPRI (w)][w->pending - 1].w = 0;
729 w->pending = 0; 877 w->pending = 0;
730 } 878 }
731} 879}
732 880
733static void 881static void
734ev_start (W w, int active) 882ev_start (W w, int active)
735{ 883{
884 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI;
885 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
886
736 w->active = active; 887 w->active = active;
737} 888}
738 889
739static void 890static void
740ev_stop (W w) 891ev_stop (W w)
745/*****************************************************************************/ 896/*****************************************************************************/
746 897
747void 898void
748ev_io_start (struct ev_io *w) 899ev_io_start (struct ev_io *w)
749{ 900{
901 int fd = w->fd;
902
750 if (ev_is_active (w)) 903 if (ev_is_active (w))
751 return; 904 return;
752 905
753 int fd = w->fd; 906 assert (("ev_io_start called with negative fd", fd >= 0));
754 907
755 ev_start ((W)w, 1); 908 ev_start ((W)w, 1);
756 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 909 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
757 wlist_add ((WL *)&anfds[fd].head, (WL)w); 910 wlist_add ((WL *)&anfds[fd].head, (WL)w);
758 911
760} 913}
761 914
762void 915void
763ev_io_stop (struct ev_io *w) 916ev_io_stop (struct ev_io *w)
764{ 917{
765 ev_clear ((W)w); 918 ev_clear_pending ((W)w);
766 if (!ev_is_active (w)) 919 if (!ev_is_active (w))
767 return; 920 return;
768 921
769 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 922 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
770 ev_stop ((W)w); 923 ev_stop ((W)w);
778 if (ev_is_active (w)) 931 if (ev_is_active (w))
779 return; 932 return;
780 933
781 w->at += now; 934 w->at += now;
782 935
783 assert (("timer repeat value less than zero not allowed", w->repeat >= 0.)); 936 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
784 937
785 ev_start ((W)w, ++timercnt); 938 ev_start ((W)w, ++timercnt);
786 array_needsize (timers, timermax, timercnt, ); 939 array_needsize (timers, timermax, timercnt, );
787 timers [timercnt - 1] = w; 940 timers [timercnt - 1] = w;
788 upheap ((WT *)timers, timercnt - 1); 941 upheap ((WT *)timers, timercnt - 1);
789} 942}
790 943
791void 944void
792ev_timer_stop (struct ev_timer *w) 945ev_timer_stop (struct ev_timer *w)
793{ 946{
794 ev_clear ((W)w); 947 ev_clear_pending ((W)w);
795 if (!ev_is_active (w)) 948 if (!ev_is_active (w))
796 return; 949 return;
797 950
798 if (w->active < timercnt--) 951 if (w->active < timercnt--)
799 { 952 {
827ev_periodic_start (struct ev_periodic *w) 980ev_periodic_start (struct ev_periodic *w)
828{ 981{
829 if (ev_is_active (w)) 982 if (ev_is_active (w))
830 return; 983 return;
831 984
832 assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); 985 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
833 986
834 /* this formula differs from the one in periodic_reify because we do not always round up */ 987 /* this formula differs from the one in periodic_reify because we do not always round up */
835 if (w->interval) 988 if (w->interval)
836 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; 989 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
837 990
842} 995}
843 996
844void 997void
845ev_periodic_stop (struct ev_periodic *w) 998ev_periodic_stop (struct ev_periodic *w)
846{ 999{
847 ev_clear ((W)w); 1000 ev_clear_pending ((W)w);
848 if (!ev_is_active (w)) 1001 if (!ev_is_active (w))
849 return; 1002 return;
850 1003
851 if (w->active < periodiccnt--) 1004 if (w->active < periodiccnt--)
852 { 1005 {
860void 1013void
861ev_signal_start (struct ev_signal *w) 1014ev_signal_start (struct ev_signal *w)
862{ 1015{
863 if (ev_is_active (w)) 1016 if (ev_is_active (w))
864 return; 1017 return;
1018
1019 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
865 1020
866 ev_start ((W)w, 1); 1021 ev_start ((W)w, 1);
867 array_needsize (signals, signalmax, w->signum, signals_init); 1022 array_needsize (signals, signalmax, w->signum, signals_init);
868 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1023 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
869 1024
878} 1033}
879 1034
880void 1035void
881ev_signal_stop (struct ev_signal *w) 1036ev_signal_stop (struct ev_signal *w)
882{ 1037{
883 ev_clear ((W)w); 1038 ev_clear_pending ((W)w);
884 if (!ev_is_active (w)) 1039 if (!ev_is_active (w))
885 return; 1040 return;
886 1041
887 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 1042 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
888 ev_stop ((W)w); 1043 ev_stop ((W)w);
903} 1058}
904 1059
905void 1060void
906ev_idle_stop (struct ev_idle *w) 1061ev_idle_stop (struct ev_idle *w)
907{ 1062{
908 ev_clear ((W)w); 1063 ev_clear_pending ((W)w);
909 if (ev_is_active (w)) 1064 if (ev_is_active (w))
910 return; 1065 return;
911 1066
912 idles [w->active - 1] = idles [--idlecnt]; 1067 idles [w->active - 1] = idles [--idlecnt];
913 ev_stop ((W)w); 1068 ev_stop ((W)w);
925} 1080}
926 1081
927void 1082void
928ev_prepare_stop (struct ev_prepare *w) 1083ev_prepare_stop (struct ev_prepare *w)
929{ 1084{
930 ev_clear ((W)w); 1085 ev_clear_pending ((W)w);
931 if (ev_is_active (w)) 1086 if (ev_is_active (w))
932 return; 1087 return;
933 1088
934 prepares [w->active - 1] = prepares [--preparecnt]; 1089 prepares [w->active - 1] = prepares [--preparecnt];
935 ev_stop ((W)w); 1090 ev_stop ((W)w);
947} 1102}
948 1103
949void 1104void
950ev_check_stop (struct ev_check *w) 1105ev_check_stop (struct ev_check *w)
951{ 1106{
952 ev_clear ((W)w); 1107 ev_clear_pending ((W)w);
953 if (ev_is_active (w)) 1108 if (ev_is_active (w))
954 return; 1109 return;
955 1110
956 checks [w->active - 1] = checks [--checkcnt]; 1111 checks [w->active - 1] = checks [--checkcnt];
957 ev_stop ((W)w); 1112 ev_stop ((W)w);
968} 1123}
969 1124
970void 1125void
971ev_child_stop (struct ev_child *w) 1126ev_child_stop (struct ev_child *w)
972{ 1127{
973 ev_clear ((W)w); 1128 ev_clear_pending ((W)w);
974 if (ev_is_active (w)) 1129 if (ev_is_active (w))
975 return; 1130 return;
976 1131
977 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1132 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
978 ev_stop ((W)w); 1133 ev_stop ((W)w);
1017ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) 1172ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
1018{ 1173{
1019 struct ev_once *once = malloc (sizeof (struct ev_once)); 1174 struct ev_once *once = malloc (sizeof (struct ev_once));
1020 1175
1021 if (!once) 1176 if (!once)
1022 cb (EV_ERROR, arg); 1177 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1023 else 1178 else
1024 { 1179 {
1025 once->cb = cb; 1180 once->cb = cb;
1026 once->arg = arg; 1181 once->arg = arg;
1027 1182
1028 ev_watcher_init (&once->io, once_cb_io); 1183 ev_watcher_init (&once->io, once_cb_io);
1029
1030 if (fd >= 0) 1184 if (fd >= 0)
1031 { 1185 {
1032 ev_io_set (&once->io, fd, events); 1186 ev_io_set (&once->io, fd, events);
1033 ev_io_start (&once->io); 1187 ev_io_start (&once->io);
1034 } 1188 }
1035 1189
1036 ev_watcher_init (&once->to, once_cb_to); 1190 ev_watcher_init (&once->to, once_cb_to);
1037
1038 if (timeout >= 0.) 1191 if (timeout >= 0.)
1039 { 1192 {
1040 ev_timer_set (&once->to, timeout, 0.); 1193 ev_timer_set (&once->to, timeout, 0.);
1041 ev_timer_start (&once->to); 1194 ev_timer_start (&once->to);
1042 } 1195 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines