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

Comparing libev/ev.c (file contents):
Revision 1.23 by root, Wed Oct 31 20:10:17 2007 UTC vs.
Revision 1.42 by root, Fri Nov 2 20:05:05 2007 UTC

1/* 1/*
2 * libev event processing core, watcher management
3 *
2 * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> 4 * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de>
3 * All rights reserved. 5 * All rights reserved.
4 * 6 *
5 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are 8 * modification, are permitted provided that the following conditions are
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */ 30 */
31#if EV_USE_CONFIG_H
32# include "config.h"
33#endif
29 34
30#include <math.h> 35#include <math.h>
31#include <stdlib.h> 36#include <stdlib.h>
32#include <unistd.h> 37#include <unistd.h>
33#include <fcntl.h> 38#include <fcntl.h>
41#include <sys/types.h> 46#include <sys/types.h>
42#include <sys/wait.h> 47#include <sys/wait.h>
43#include <sys/time.h> 48#include <sys/time.h>
44#include <time.h> 49#include <time.h>
45 50
51/**/
52
46#ifndef HAVE_MONOTONIC 53#ifndef EV_USE_MONOTONIC
54# define EV_USE_MONOTONIC 1
55#endif
56
57#ifndef EV_USE_SELECT
58# define EV_USE_SELECT 1
59#endif
60
61#ifndef EV_USE_POLL
62# define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */
63#endif
64
65#ifndef EV_USE_EPOLL
66# define EV_USE_EPOLL 0
67#endif
68
69#ifndef EV_USE_REALTIME
70# define EV_USE_REALTIME 1
71#endif
72
73/**/
74
47# ifdef CLOCK_MONOTONIC 75#ifndef CLOCK_MONOTONIC
76# undef EV_USE_MONOTONIC
48# define HAVE_MONOTONIC 1 77# define EV_USE_MONOTONIC 0
49# endif 78#endif
50#endif
51 79
52#ifndef HAVE_SELECT
53# define HAVE_SELECT 1
54#endif
55
56#ifndef HAVE_EPOLL
57# define HAVE_EPOLL 0
58#endif
59
60#ifndef HAVE_REALTIME 80#ifndef CLOCK_REALTIME
61# define HAVE_REALTIME 1 /* posix requirement, but might be slower */ 81# undef EV_USE_REALTIME
82# define EV_USE_REALTIME 0
62#endif 83#endif
84
85/**/
63 86
64#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 87#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
65#define MAX_BLOCKTIME 60. 88#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */
66#define PID_HASHSIZE 16 /* size of pid hahs table, must be power of two */ 89#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */
90/*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */
67 91
68#include "ev.h" 92#include "ev.h"
93
94#if __GNUC__ >= 3
95# define expect(expr,value) __builtin_expect ((expr),(value))
96# define inline inline
97#else
98# define expect(expr,value) (expr)
99# define inline static
100#endif
101
102#define expect_false(expr) expect ((expr) != 0, 0)
103#define expect_true(expr) expect ((expr) != 0, 1)
104
105#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
106#define ABSPRI(w) ((w)->priority - EV_MINPRI)
69 107
70typedef struct ev_watcher *W; 108typedef struct ev_watcher *W;
71typedef struct ev_watcher_list *WL; 109typedef struct ev_watcher_list *WL;
72typedef struct ev_watcher_time *WT; 110typedef struct ev_watcher_time *WT;
73 111
74static ev_tstamp now, diff; /* monotonic clock */ 112static ev_tstamp now_floor, now, diff; /* monotonic clock */
75ev_tstamp ev_now; 113ev_tstamp ev_now;
76int ev_method; 114int ev_method;
77 115
78static int have_monotonic; /* runtime */ 116static int have_monotonic; /* runtime */
79 117
84/*****************************************************************************/ 122/*****************************************************************************/
85 123
86ev_tstamp 124ev_tstamp
87ev_time (void) 125ev_time (void)
88{ 126{
89#if HAVE_REALTIME 127#if EV_USE_REALTIME
90 struct timespec ts; 128 struct timespec ts;
91 clock_gettime (CLOCK_REALTIME, &ts); 129 clock_gettime (CLOCK_REALTIME, &ts);
92 return ts.tv_sec + ts.tv_nsec * 1e-9; 130 return ts.tv_sec + ts.tv_nsec * 1e-9;
93#else 131#else
94 struct timeval tv; 132 struct timeval tv;
98} 136}
99 137
100static ev_tstamp 138static ev_tstamp
101get_clock (void) 139get_clock (void)
102{ 140{
103#if HAVE_MONOTONIC 141#if EV_USE_MONOTONIC
104 if (have_monotonic) 142 if (expect_true (have_monotonic))
105 { 143 {
106 struct timespec ts; 144 struct timespec ts;
107 clock_gettime (CLOCK_MONOTONIC, &ts); 145 clock_gettime (CLOCK_MONOTONIC, &ts);
108 return ts.tv_sec + ts.tv_nsec * 1e-9; 146 return ts.tv_sec + ts.tv_nsec * 1e-9;
109 } 147 }
110#endif 148#endif
111 149
112 return ev_time (); 150 return ev_time ();
113} 151}
114 152
153#define array_roundsize(base,n) ((n) | 4 & ~3)
154
115#define array_needsize(base,cur,cnt,init) \ 155#define array_needsize(base,cur,cnt,init) \
116 if ((cnt) > cur) \ 156 if (expect_false ((cnt) > cur)) \
117 { \ 157 { \
118 int newcnt = cur; \ 158 int newcnt = cur; \
119 do \ 159 do \
120 { \ 160 { \
121 newcnt = (newcnt << 1) | 4 & ~3; \ 161 newcnt = array_roundsize (base, newcnt << 1); \
122 } \ 162 } \
123 while ((cnt) > newcnt); \ 163 while ((cnt) > newcnt); \
124 \ 164 \
125 base = realloc (base, sizeof (*base) * (newcnt)); \ 165 base = realloc (base, sizeof (*base) * (newcnt)); \
126 init (base + cur, newcnt - cur); \ 166 init (base + cur, newcnt - cur); \
130/*****************************************************************************/ 170/*****************************************************************************/
131 171
132typedef struct 172typedef struct
133{ 173{
134 struct ev_io *head; 174 struct ev_io *head;
135 unsigned char wev, rev; /* want, received event set */ 175 unsigned char events;
176 unsigned char reify;
136} ANFD; 177} ANFD;
137 178
138static ANFD *anfds; 179static ANFD *anfds;
139static int anfdmax; 180static int anfdmax;
140 181
141static int *fdchanges;
142static int fdchangemax, fdchangecnt;
143
144static void 182static void
145anfds_init (ANFD *base, int count) 183anfds_init (ANFD *base, int count)
146{ 184{
147 while (count--) 185 while (count--)
148 { 186 {
149 base->head = 0; 187 base->head = 0;
150 base->wev = base->rev = EV_NONE; 188 base->events = EV_NONE;
189 base->reify = 0;
190
151 ++base; 191 ++base;
152 } 192 }
153} 193}
154 194
155typedef struct 195typedef struct
156{ 196{
157 W w; 197 W w;
158 int events; 198 int events;
159} ANPENDING; 199} ANPENDING;
160 200
161static ANPENDING *pendings; 201static ANPENDING *pendings [NUMPRI];
162static int pendingmax, pendingcnt; 202static int pendingmax [NUMPRI], pendingcnt [NUMPRI];
163 203
164static void 204static void
165event (W w, int events) 205event (W w, int events)
166{ 206{
167 if (w->active) 207 if (w->pending)
168 { 208 {
169 w->pending = ++pendingcnt;
170 array_needsize (pendings, pendingmax, pendingcnt, );
171 pendings [pendingcnt - 1].w = w;
172 pendings [pendingcnt - 1].events = events; 209 pendings [ABSPRI (w)][w->pending - 1].events |= events;
210 return;
173 } 211 }
212
213 w->pending = ++pendingcnt [ABSPRI (w)];
214 array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], );
215 pendings [ABSPRI (w)][w->pending - 1].w = w;
216 pendings [ABSPRI (w)][w->pending - 1].events = events;
217}
218
219static void
220queue_events (W *events, int eventcnt, int type)
221{
222 int i;
223
224 for (i = 0; i < eventcnt; ++i)
225 event (events [i], type);
174} 226}
175 227
176static void 228static void
177fd_event (int fd, int events) 229fd_event (int fd, int events)
178{ 230{
186 if (ev) 238 if (ev)
187 event ((W)w, ev); 239 event ((W)w, ev);
188 } 240 }
189} 241}
190 242
243/*****************************************************************************/
244
245static int *fdchanges;
246static int fdchangemax, fdchangecnt;
247
191static void 248static void
192queue_events (W *events, int eventcnt, int type) 249fd_reify (void)
193{ 250{
194 int i; 251 int i;
195 252
196 for (i = 0; i < eventcnt; ++i) 253 for (i = 0; i < fdchangecnt; ++i)
197 event (events [i], type); 254 {
255 int fd = fdchanges [i];
256 ANFD *anfd = anfds + fd;
257 struct ev_io *w;
258
259 int events = 0;
260
261 for (w = anfd->head; w; w = w->next)
262 events |= w->events;
263
264 anfd->reify = 0;
265
266 if (anfd->events != events)
267 {
268 method_modify (fd, anfd->events, events);
269 anfd->events = events;
270 }
271 }
272
273 fdchangecnt = 0;
274}
275
276static void
277fd_change (int fd)
278{
279 if (anfds [fd].reify || fdchangecnt < 0)
280 return;
281
282 anfds [fd].reify = 1;
283
284 ++fdchangecnt;
285 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
286 fdchanges [fdchangecnt - 1] = fd;
287}
288
289static void
290fd_kill (int fd)
291{
292 struct ev_io *w;
293
294 printf ("killing fd %d\n", fd);//D
295 while ((w = anfds [fd].head))
296 {
297 ev_io_stop (w);
298 event ((W)w, EV_ERROR | EV_READ | EV_WRITE);
299 }
198} 300}
199 301
200/* called on EBADF to verify fds */ 302/* called on EBADF to verify fds */
201static void 303static void
202fd_recheck () 304fd_ebadf (void)
203{ 305{
204 int fd; 306 int fd;
205 307
206 for (fd = 0; fd < anfdmax; ++fd) 308 for (fd = 0; fd < anfdmax; ++fd)
207 if (anfds [fd].wev) 309 if (anfds [fd].events)
208 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 310 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
209 while (anfds [fd].head) 311 fd_kill (fd);
210 evio_stop (anfds [fd].head); 312}
313
314/* called on ENOMEM in select/poll to kill some fds and retry */
315static void
316fd_enomem (void)
317{
318 int fd = anfdmax;
319
320 while (fd--)
321 if (anfds [fd].events)
322 {
323 close (fd);
324 fd_kill (fd);
325 return;
326 }
211} 327}
212 328
213/*****************************************************************************/ 329/*****************************************************************************/
214 330
215static struct ev_timer **timers; 331static struct ev_timer **timers;
262/*****************************************************************************/ 378/*****************************************************************************/
263 379
264typedef struct 380typedef struct
265{ 381{
266 struct ev_signal *head; 382 struct ev_signal *head;
267 sig_atomic_t gotsig; 383 sig_atomic_t volatile gotsig;
268} ANSIG; 384} ANSIG;
269 385
270static ANSIG *signals; 386static ANSIG *signals;
271static int signalmax; 387static int signalmax;
272 388
273static int sigpipe [2]; 389static int sigpipe [2];
274static sig_atomic_t gotsig; 390static sig_atomic_t volatile gotsig;
275static struct ev_io sigev; 391static struct ev_io sigev;
276 392
277static void 393static void
278signals_init (ANSIG *base, int count) 394signals_init (ANSIG *base, int count)
279{ 395{
280 while (count--) 396 while (count--)
281 { 397 {
282 base->head = 0; 398 base->head = 0;
283 base->gotsig = 0; 399 base->gotsig = 0;
400
284 ++base; 401 ++base;
285 } 402 }
286} 403}
287 404
288static void 405static void
291 signals [signum - 1].gotsig = 1; 408 signals [signum - 1].gotsig = 1;
292 409
293 if (!gotsig) 410 if (!gotsig)
294 { 411 {
295 gotsig = 1; 412 gotsig = 1;
296 write (sigpipe [1], &gotsig, 1); 413 write (sigpipe [1], &signum, 1);
297 } 414 }
298} 415}
299 416
300static void 417static void
301sigcb (struct ev_io *iow, int revents) 418sigcb (struct ev_io *iow, int revents)
302{ 419{
303 struct ev_signal *w; 420 struct ev_signal *w;
304 int sig; 421 int signum;
305 422
423 read (sigpipe [0], &revents, 1);
306 gotsig = 0; 424 gotsig = 0;
307 read (sigpipe [0], &revents, 1);
308 425
309 for (sig = signalmax; sig--; ) 426 for (signum = signalmax; signum--; )
310 if (signals [sig].gotsig) 427 if (signals [signum].gotsig)
311 { 428 {
312 signals [sig].gotsig = 0; 429 signals [signum].gotsig = 0;
313 430
314 for (w = signals [sig].head; w; w = w->next) 431 for (w = signals [signum].head; w; w = w->next)
315 event ((W)w, EV_SIGNAL); 432 event ((W)w, EV_SIGNAL);
316 } 433 }
317} 434}
318 435
319static void 436static void
324 441
325 /* rather than sort out wether we really need nb, set it */ 442 /* rather than sort out wether we really need nb, set it */
326 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); 443 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
327 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 444 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
328 445
329 evio_set (&sigev, sigpipe [0], EV_READ); 446 ev_io_set (&sigev, sigpipe [0], EV_READ);
330 evio_start (&sigev); 447 ev_io_start (&sigev);
331} 448}
332 449
333/*****************************************************************************/ 450/*****************************************************************************/
334 451
335static struct ev_idle **idles; 452static struct ev_idle **idles;
356 struct ev_child *w; 473 struct ev_child *w;
357 int pid, status; 474 int pid, status;
358 475
359 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) 476 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1)
360 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) 477 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next)
361 if (w->pid == pid || w->pid == -1) 478 if (w->pid == pid || !w->pid)
362 { 479 {
363 w->status = status; 480 w->status = status;
364 event ((W)w, EV_CHILD); 481 event ((W)w, EV_CHILD);
365 } 482 }
366} 483}
367 484
368/*****************************************************************************/ 485/*****************************************************************************/
369 486
370#if HAVE_EPOLL 487#if EV_USE_EPOLL
371# include "ev_epoll.c" 488# include "ev_epoll.c"
372#endif 489#endif
490#if EV_USE_POLL
491# include "ev_poll.c"
492#endif
373#if HAVE_SELECT 493#if EV_USE_SELECT
374# include "ev_select.c" 494# include "ev_select.c"
375#endif 495#endif
376 496
497int
498ev_version_major (void)
499{
500 return EV_VERSION_MAJOR;
501}
502
503int
504ev_version_minor (void)
505{
506 return EV_VERSION_MINOR;
507}
508
509/* return true if we are running with elevated privileges and ignore env variables */
510static int
511enable_secure ()
512{
513 return getuid () != geteuid ()
514 || getgid () != getegid ();
515}
516
377int ev_init (int flags) 517int ev_init (int methods)
378{ 518{
379 if (!ev_method) 519 if (!ev_method)
380 { 520 {
381#if HAVE_MONOTONIC 521#if EV_USE_MONOTONIC
382 { 522 {
383 struct timespec ts; 523 struct timespec ts;
384 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 524 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
385 have_monotonic = 1; 525 have_monotonic = 1;
386 } 526 }
387#endif 527#endif
388 528
389 ev_now = ev_time (); 529 ev_now = ev_time ();
390 now = get_clock (); 530 now = get_clock ();
531 now_floor = now;
391 diff = ev_now - now; 532 diff = ev_now - now;
392 533
393 if (pipe (sigpipe)) 534 if (pipe (sigpipe))
394 return 0; 535 return 0;
395 536
537 if (methods == EVMETHOD_AUTO)
538 if (!enable_secure () && getenv ("LIBEV_METHODS"))
539 methods = atoi (getenv ("LIBEV_METHODS"));
540 else
396 ev_method = EVMETHOD_NONE; 541 methods = EVMETHOD_ANY;
542
543 ev_method = 0;
397#if HAVE_EPOLL 544#if EV_USE_EPOLL
398 if (ev_method == EVMETHOD_NONE) epoll_init (flags); 545 if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods);
399#endif 546#endif
547#if EV_USE_POLL
548 if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods);
549#endif
400#if HAVE_SELECT 550#if EV_USE_SELECT
401 if (ev_method == EVMETHOD_NONE) select_init (flags); 551 if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods);
402#endif 552#endif
403 553
404 if (ev_method) 554 if (ev_method)
405 { 555 {
406 evw_init (&sigev, sigcb); 556 ev_watcher_init (&sigev, sigcb);
407 siginit (); 557 siginit ();
408 558
409 evsignal_init (&childev, childcb, SIGCHLD); 559 ev_signal_init (&childev, childcb, SIGCHLD);
410 evsignal_start (&childev); 560 ev_signal_start (&childev);
411 } 561 }
412 } 562 }
413 563
414 return ev_method; 564 return ev_method;
415} 565}
416 566
417/*****************************************************************************/ 567/*****************************************************************************/
418 568
419void ev_prefork (void) 569void
570ev_fork_prepare (void)
420{ 571{
421 /* nop */ 572 /* nop */
422} 573}
423 574
575void
424void ev_postfork_parent (void) 576ev_fork_parent (void)
425{ 577{
426 /* nop */ 578 /* nop */
427} 579}
428 580
581void
429void ev_postfork_child (void) 582ev_fork_child (void)
430{ 583{
431#if HAVE_EPOLL 584#if EV_USE_EPOLL
432 if (ev_method == EVMETHOD_EPOLL) 585 if (ev_method == EVMETHOD_EPOLL)
433 epoll_postfork_child (); 586 epoll_postfork_child ();
434#endif 587#endif
435 588
436 evio_stop (&sigev); 589 ev_io_stop (&sigev);
437 close (sigpipe [0]); 590 close (sigpipe [0]);
438 close (sigpipe [1]); 591 close (sigpipe [1]);
439 pipe (sigpipe); 592 pipe (sigpipe);
440 siginit (); 593 siginit ();
441} 594}
442 595
443/*****************************************************************************/ 596/*****************************************************************************/
444 597
445static void 598static void
446fd_reify (void) 599call_pending (void)
447{ 600{
448 int i; 601 int pri;
449 602
450 for (i = 0; i < fdchangecnt; ++i) 603 for (pri = NUMPRI; pri--; )
451 { 604 while (pendingcnt [pri])
452 int fd = fdchanges [i];
453 ANFD *anfd = anfds + fd;
454 struct ev_io *w;
455
456 int wev = 0;
457
458 for (w = anfd->head; w; w = w->next)
459 wev |= w->events;
460
461 if (anfd->wev != wev)
462 { 605 {
463 method_modify (fd, anfd->wev, wev);
464 anfd->wev = wev;
465 }
466 }
467
468 fdchangecnt = 0;
469}
470
471static void
472call_pending ()
473{
474 while (pendingcnt)
475 {
476 ANPENDING *p = pendings + --pendingcnt; 606 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
477 607
478 if (p->w) 608 if (p->w)
479 { 609 {
480 p->w->pending = 0; 610 p->w->pending = 0;
481 p->w->cb (p->w, p->events); 611 p->w->cb (p->w, p->events);
482 } 612 }
483 } 613 }
484} 614}
485 615
486static void 616static void
487timers_reify () 617timers_reify (void)
488{ 618{
489 while (timercnt && timers [0]->at <= now) 619 while (timercnt && timers [0]->at <= now)
490 { 620 {
491 struct ev_timer *w = timers [0]; 621 struct ev_timer *w = timers [0];
492
493 event ((W)w, EV_TIMEOUT);
494 622
495 /* first reschedule or stop timer */ 623 /* first reschedule or stop timer */
496 if (w->repeat) 624 if (w->repeat)
497 { 625 {
626 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
498 w->at = now + w->repeat; 627 w->at = now + w->repeat;
499 assert (("timer timeout in the past, negative repeat?", w->at > now));
500 downheap ((WT *)timers, timercnt, 0); 628 downheap ((WT *)timers, timercnt, 0);
501 } 629 }
502 else 630 else
503 evtimer_stop (w); /* nonrepeating: stop timer */ 631 ev_timer_stop (w); /* nonrepeating: stop timer */
504 }
505}
506 632
633 event ((W)w, EV_TIMEOUT);
634 }
635}
636
507static void 637static void
508periodics_reify () 638periodics_reify (void)
509{ 639{
510 while (periodiccnt && periodics [0]->at <= ev_now) 640 while (periodiccnt && periodics [0]->at <= ev_now)
511 { 641 {
512 struct ev_periodic *w = periodics [0]; 642 struct ev_periodic *w = periodics [0];
513 643
514 /* first reschedule or stop timer */ 644 /* first reschedule or stop timer */
515 if (w->interval) 645 if (w->interval)
516 { 646 {
517 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; 647 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval;
518 assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); 648 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > ev_now));
519 downheap ((WT *)periodics, periodiccnt, 0); 649 downheap ((WT *)periodics, periodiccnt, 0);
520 } 650 }
521 else 651 else
522 evperiodic_stop (w); /* nonrepeating: stop timer */ 652 ev_periodic_stop (w); /* nonrepeating: stop timer */
523 653
524 event ((W)w, EV_TIMEOUT); 654 event ((W)w, EV_PERIODIC);
525 } 655 }
526} 656}
527 657
528static void 658static void
529periodics_reschedule (ev_tstamp diff) 659periodics_reschedule (ev_tstamp diff)
539 { 669 {
540 ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; 670 ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval;
541 671
542 if (fabs (diff) >= 1e-4) 672 if (fabs (diff) >= 1e-4)
543 { 673 {
544 evperiodic_stop (w); 674 ev_periodic_stop (w);
545 evperiodic_start (w); 675 ev_periodic_start (w);
546 676
547 i = 0; /* restart loop, inefficient, but time jumps should be rare */ 677 i = 0; /* restart loop, inefficient, but time jumps should be rare */
548 } 678 }
549 } 679 }
550 } 680 }
551} 681}
552 682
553static void 683static int
684time_update_monotonic (void)
685{
686 now = get_clock ();
687
688 if (expect_true (now - now_floor < MIN_TIMEJUMP * .5))
689 {
690 ev_now = now + diff;
691 return 0;
692 }
693 else
694 {
695 now_floor = now;
696 ev_now = ev_time ();
697 return 1;
698 }
699}
700
701static void
554time_update () 702time_update (void)
555{ 703{
556 int i; 704 int i;
557 705
558 ev_now = ev_time (); 706#if EV_USE_MONOTONIC
559
560 if (have_monotonic) 707 if (expect_true (have_monotonic))
561 { 708 {
709 if (time_update_monotonic ())
710 {
562 ev_tstamp odiff = diff; 711 ev_tstamp odiff = diff;
563 712
564 for (i = 4; --i; ) /* loop a few times, before making important decisions */ 713 for (i = 4; --i; ) /* loop a few times, before making important decisions */
565 { 714 {
566 now = get_clock ();
567 diff = ev_now - now; 715 diff = ev_now - now;
568 716
569 if (fabs (odiff - diff) < MIN_TIMEJUMP) 717 if (fabs (odiff - diff) < MIN_TIMEJUMP)
570 return; /* all is well */ 718 return; /* all is well */
571 719
572 ev_now = ev_time (); 720 ev_now = ev_time ();
721 now = get_clock ();
722 now_floor = now;
573 } 723 }
574 724
575 periodics_reschedule (diff - odiff); 725 periodics_reschedule (diff - odiff);
576 /* no timer adjustment, as the monotonic clock doesn't jump */ 726 /* no timer adjustment, as the monotonic clock doesn't jump */
727 }
577 } 728 }
578 else 729 else
730#endif
579 { 731 {
732 ev_now = ev_time ();
733
580 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) 734 if (expect_false (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
581 { 735 {
582 periodics_reschedule (ev_now - now); 736 periodics_reschedule (ev_now - now);
583 737
584 /* adjust timers. this is easy, as the offset is the same for all */ 738 /* adjust timers. this is easy, as the offset is the same for all */
585 for (i = 0; i < timercnt; ++i) 739 for (i = 0; i < timercnt; ++i)
593int ev_loop_done; 747int ev_loop_done;
594 748
595void ev_loop (int flags) 749void ev_loop (int flags)
596{ 750{
597 double block; 751 double block;
598 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; 752 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
599 753
600 do 754 do
601 { 755 {
602 /* queue check watchers (and execute them) */ 756 /* queue check watchers (and execute them) */
603 if (preparecnt) 757 if (expect_false (preparecnt))
604 { 758 {
605 queue_events ((W *)prepares, preparecnt, EV_PREPARE); 759 queue_events ((W *)prepares, preparecnt, EV_PREPARE);
606 call_pending (); 760 call_pending ();
607 } 761 }
608 762
611 765
612 /* calculate blocking time */ 766 /* calculate blocking time */
613 767
614 /* we only need this for !monotonic clockor timers, but as we basically 768 /* we only need this for !monotonic clockor timers, but as we basically
615 always have timers, we just calculate it always */ 769 always have timers, we just calculate it always */
770#if EV_USE_MONOTONIC
771 if (expect_true (have_monotonic))
772 time_update_monotonic ();
773 else
774#endif
775 {
616 ev_now = ev_time (); 776 ev_now = ev_time ();
777 now = ev_now;
778 }
617 779
618 if (flags & EVLOOP_NONBLOCK || idlecnt) 780 if (flags & EVLOOP_NONBLOCK || idlecnt)
619 block = 0.; 781 block = 0.;
620 else 782 else
621 { 783 {
622 block = MAX_BLOCKTIME; 784 block = MAX_BLOCKTIME;
623 785
624 if (timercnt) 786 if (timercnt)
625 { 787 {
626 ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; 788 ev_tstamp to = timers [0]->at - now + method_fudge;
627 if (block > to) block = to; 789 if (block > to) block = to;
628 } 790 }
629 791
630 if (periodiccnt) 792 if (periodiccnt)
631 { 793 {
684 head = &(*head)->next; 846 head = &(*head)->next;
685 } 847 }
686} 848}
687 849
688static void 850static void
689ev_clear (W w) 851ev_clear_pending (W w)
690{ 852{
691 if (w->pending) 853 if (w->pending)
692 { 854 {
693 pendings [w->pending - 1].w = 0; 855 pendings [ABSPRI (w)][w->pending - 1].w = 0;
694 w->pending = 0; 856 w->pending = 0;
695 } 857 }
696} 858}
697 859
698static void 860static void
708} 870}
709 871
710/*****************************************************************************/ 872/*****************************************************************************/
711 873
712void 874void
713evio_start (struct ev_io *w) 875ev_io_start (struct ev_io *w)
714{ 876{
877 int fd = w->fd;
878
715 if (ev_is_active (w)) 879 if (ev_is_active (w))
716 return; 880 return;
717 881
718 int fd = w->fd; 882 assert (("ev_io_start called with negative fd", fd >= 0));
719 883
720 ev_start ((W)w, 1); 884 ev_start ((W)w, 1);
721 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 885 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
722 wlist_add ((WL *)&anfds[fd].head, (WL)w); 886 wlist_add ((WL *)&anfds[fd].head, (WL)w);
723 887
724 ++fdchangecnt; 888 fd_change (fd);
725 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
726 fdchanges [fdchangecnt - 1] = fd;
727} 889}
728 890
729void 891void
730evio_stop (struct ev_io *w) 892ev_io_stop (struct ev_io *w)
731{ 893{
732 ev_clear ((W)w); 894 ev_clear_pending ((W)w);
733 if (!ev_is_active (w)) 895 if (!ev_is_active (w))
734 return; 896 return;
735 897
736 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 898 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
737 ev_stop ((W)w); 899 ev_stop ((W)w);
738 900
739 ++fdchangecnt; 901 fd_change (w->fd);
740 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
741 fdchanges [fdchangecnt - 1] = w->fd;
742} 902}
743 903
744void 904void
745evtimer_start (struct ev_timer *w) 905ev_timer_start (struct ev_timer *w)
746{ 906{
747 if (ev_is_active (w)) 907 if (ev_is_active (w))
748 return; 908 return;
749 909
750 w->at += now; 910 w->at += now;
751 911
752 assert (("timer repeat value less than zero not allowed", w->repeat >= 0.)); 912 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
753 913
754 ev_start ((W)w, ++timercnt); 914 ev_start ((W)w, ++timercnt);
755 array_needsize (timers, timermax, timercnt, ); 915 array_needsize (timers, timermax, timercnt, );
756 timers [timercnt - 1] = w; 916 timers [timercnt - 1] = w;
757 upheap ((WT *)timers, timercnt - 1); 917 upheap ((WT *)timers, timercnt - 1);
758} 918}
759 919
760void 920void
761evtimer_stop (struct ev_timer *w) 921ev_timer_stop (struct ev_timer *w)
762{ 922{
763 ev_clear ((W)w); 923 ev_clear_pending ((W)w);
764 if (!ev_is_active (w)) 924 if (!ev_is_active (w))
765 return; 925 return;
766 926
767 if (w->active < timercnt--) 927 if (w->active < timercnt--)
768 { 928 {
774 934
775 ev_stop ((W)w); 935 ev_stop ((W)w);
776} 936}
777 937
778void 938void
779evtimer_again (struct ev_timer *w) 939ev_timer_again (struct ev_timer *w)
780{ 940{
781 if (ev_is_active (w)) 941 if (ev_is_active (w))
782 { 942 {
783 if (w->repeat) 943 if (w->repeat)
784 { 944 {
785 w->at = now + w->repeat; 945 w->at = now + w->repeat;
786 downheap ((WT *)timers, timercnt, w->active - 1); 946 downheap ((WT *)timers, timercnt, w->active - 1);
787 } 947 }
788 else 948 else
789 evtimer_stop (w); 949 ev_timer_stop (w);
790 } 950 }
791 else if (w->repeat) 951 else if (w->repeat)
792 evtimer_start (w); 952 ev_timer_start (w);
793} 953}
794 954
795void 955void
796evperiodic_start (struct ev_periodic *w) 956ev_periodic_start (struct ev_periodic *w)
797{ 957{
798 if (ev_is_active (w)) 958 if (ev_is_active (w))
799 return; 959 return;
800 960
801 assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); 961 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
802 962
803 /* this formula differs from the one in periodic_reify because we do not always round up */ 963 /* this formula differs from the one in periodic_reify because we do not always round up */
804 if (w->interval) 964 if (w->interval)
805 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; 965 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
806 966
809 periodics [periodiccnt - 1] = w; 969 periodics [periodiccnt - 1] = w;
810 upheap ((WT *)periodics, periodiccnt - 1); 970 upheap ((WT *)periodics, periodiccnt - 1);
811} 971}
812 972
813void 973void
814evperiodic_stop (struct ev_periodic *w) 974ev_periodic_stop (struct ev_periodic *w)
815{ 975{
816 ev_clear ((W)w); 976 ev_clear_pending ((W)w);
817 if (!ev_is_active (w)) 977 if (!ev_is_active (w))
818 return; 978 return;
819 979
820 if (w->active < periodiccnt--) 980 if (w->active < periodiccnt--)
821 { 981 {
825 985
826 ev_stop ((W)w); 986 ev_stop ((W)w);
827} 987}
828 988
829void 989void
830evsignal_start (struct ev_signal *w) 990ev_signal_start (struct ev_signal *w)
831{ 991{
832 if (ev_is_active (w)) 992 if (ev_is_active (w))
833 return; 993 return;
994
995 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
834 996
835 ev_start ((W)w, 1); 997 ev_start ((W)w, 1);
836 array_needsize (signals, signalmax, w->signum, signals_init); 998 array_needsize (signals, signalmax, w->signum, signals_init);
837 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 999 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
838 1000
845 sigaction (w->signum, &sa, 0); 1007 sigaction (w->signum, &sa, 0);
846 } 1008 }
847} 1009}
848 1010
849void 1011void
850evsignal_stop (struct ev_signal *w) 1012ev_signal_stop (struct ev_signal *w)
851{ 1013{
852 ev_clear ((W)w); 1014 ev_clear_pending ((W)w);
853 if (!ev_is_active (w)) 1015 if (!ev_is_active (w))
854 return; 1016 return;
855 1017
856 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 1018 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
857 ev_stop ((W)w); 1019 ev_stop ((W)w);
858 1020
859 if (!signals [w->signum - 1].head) 1021 if (!signals [w->signum - 1].head)
860 signal (w->signum, SIG_DFL); 1022 signal (w->signum, SIG_DFL);
861} 1023}
862 1024
1025void
863void evidle_start (struct ev_idle *w) 1026ev_idle_start (struct ev_idle *w)
864{ 1027{
865 if (ev_is_active (w)) 1028 if (ev_is_active (w))
866 return; 1029 return;
867 1030
868 ev_start ((W)w, ++idlecnt); 1031 ev_start ((W)w, ++idlecnt);
869 array_needsize (idles, idlemax, idlecnt, ); 1032 array_needsize (idles, idlemax, idlecnt, );
870 idles [idlecnt - 1] = w; 1033 idles [idlecnt - 1] = w;
871} 1034}
872 1035
1036void
873void evidle_stop (struct ev_idle *w) 1037ev_idle_stop (struct ev_idle *w)
874{ 1038{
875 ev_clear ((W)w); 1039 ev_clear_pending ((W)w);
876 if (ev_is_active (w)) 1040 if (ev_is_active (w))
877 return; 1041 return;
878 1042
879 idles [w->active - 1] = idles [--idlecnt]; 1043 idles [w->active - 1] = idles [--idlecnt];
880 ev_stop ((W)w); 1044 ev_stop ((W)w);
881} 1045}
882 1046
1047void
883void evprepare_start (struct ev_prepare *w) 1048ev_prepare_start (struct ev_prepare *w)
884{ 1049{
885 if (ev_is_active (w)) 1050 if (ev_is_active (w))
886 return; 1051 return;
887 1052
888 ev_start ((W)w, ++preparecnt); 1053 ev_start ((W)w, ++preparecnt);
889 array_needsize (prepares, preparemax, preparecnt, ); 1054 array_needsize (prepares, preparemax, preparecnt, );
890 prepares [preparecnt - 1] = w; 1055 prepares [preparecnt - 1] = w;
891} 1056}
892 1057
1058void
893void evprepare_stop (struct ev_prepare *w) 1059ev_prepare_stop (struct ev_prepare *w)
894{ 1060{
895 ev_clear ((W)w); 1061 ev_clear_pending ((W)w);
896 if (ev_is_active (w)) 1062 if (ev_is_active (w))
897 return; 1063 return;
898 1064
899 prepares [w->active - 1] = prepares [--preparecnt]; 1065 prepares [w->active - 1] = prepares [--preparecnt];
900 ev_stop ((W)w); 1066 ev_stop ((W)w);
901} 1067}
902 1068
1069void
903void evcheck_start (struct ev_check *w) 1070ev_check_start (struct ev_check *w)
904{ 1071{
905 if (ev_is_active (w)) 1072 if (ev_is_active (w))
906 return; 1073 return;
907 1074
908 ev_start ((W)w, ++checkcnt); 1075 ev_start ((W)w, ++checkcnt);
909 array_needsize (checks, checkmax, checkcnt, ); 1076 array_needsize (checks, checkmax, checkcnt, );
910 checks [checkcnt - 1] = w; 1077 checks [checkcnt - 1] = w;
911} 1078}
912 1079
1080void
913void evcheck_stop (struct ev_check *w) 1081ev_check_stop (struct ev_check *w)
914{ 1082{
915 ev_clear ((W)w); 1083 ev_clear_pending ((W)w);
916 if (ev_is_active (w)) 1084 if (ev_is_active (w))
917 return; 1085 return;
918 1086
919 checks [w->active - 1] = checks [--checkcnt]; 1087 checks [w->active - 1] = checks [--checkcnt];
920 ev_stop ((W)w); 1088 ev_stop ((W)w);
921} 1089}
922 1090
1091void
923void evchild_start (struct ev_child *w) 1092ev_child_start (struct ev_child *w)
924{ 1093{
925 if (ev_is_active (w)) 1094 if (ev_is_active (w))
926 return; 1095 return;
927 1096
928 ev_start ((W)w, 1); 1097 ev_start ((W)w, 1);
929 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1098 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
930} 1099}
931 1100
1101void
932void evchild_stop (struct ev_child *w) 1102ev_child_stop (struct ev_child *w)
933{ 1103{
934 ev_clear ((W)w); 1104 ev_clear_pending ((W)w);
935 if (ev_is_active (w)) 1105 if (ev_is_active (w))
936 return; 1106 return;
937 1107
938 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1108 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
939 ev_stop ((W)w); 1109 ev_stop ((W)w);
953once_cb (struct ev_once *once, int revents) 1123once_cb (struct ev_once *once, int revents)
954{ 1124{
955 void (*cb)(int revents, void *arg) = once->cb; 1125 void (*cb)(int revents, void *arg) = once->cb;
956 void *arg = once->arg; 1126 void *arg = once->arg;
957 1127
958 evio_stop (&once->io); 1128 ev_io_stop (&once->io);
959 evtimer_stop (&once->to); 1129 ev_timer_stop (&once->to);
960 free (once); 1130 free (once);
961 1131
962 cb (revents, arg); 1132 cb (revents, arg);
963} 1133}
964 1134
978ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) 1148ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
979{ 1149{
980 struct ev_once *once = malloc (sizeof (struct ev_once)); 1150 struct ev_once *once = malloc (sizeof (struct ev_once));
981 1151
982 if (!once) 1152 if (!once)
983 cb (EV_ERROR, arg); 1153 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
984 else 1154 else
985 { 1155 {
986 once->cb = cb; 1156 once->cb = cb;
987 once->arg = arg; 1157 once->arg = arg;
988 1158
989 evw_init (&once->io, once_cb_io); 1159 ev_watcher_init (&once->io, once_cb_io);
990
991 if (fd >= 0) 1160 if (fd >= 0)
992 { 1161 {
993 evio_set (&once->io, fd, events); 1162 ev_io_set (&once->io, fd, events);
994 evio_start (&once->io); 1163 ev_io_start (&once->io);
995 } 1164 }
996 1165
997 evw_init (&once->to, once_cb_to); 1166 ev_watcher_init (&once->to, once_cb_to);
998
999 if (timeout >= 0.) 1167 if (timeout >= 0.)
1000 { 1168 {
1001 evtimer_set (&once->to, timeout, 0.); 1169 ev_timer_set (&once->to, timeout, 0.);
1002 evtimer_start (&once->to); 1170 ev_timer_start (&once->to);
1003 } 1171 }
1004 } 1172 }
1005} 1173}
1006 1174
1007/*****************************************************************************/ 1175/*****************************************************************************/
1018 1186
1019static void 1187static void
1020ocb (struct ev_timer *w, int revents) 1188ocb (struct ev_timer *w, int revents)
1021{ 1189{
1022 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); 1190 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data);
1023 evtimer_stop (w); 1191 ev_timer_stop (w);
1024 evtimer_start (w); 1192 ev_timer_start (w);
1025} 1193}
1026 1194
1027static void 1195static void
1028scb (struct ev_signal *w, int revents) 1196scb (struct ev_signal *w, int revents)
1029{ 1197{
1030 fprintf (stderr, "signal %x,%d\n", revents, w->signum); 1198 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
1031 evio_stop (&wio); 1199 ev_io_stop (&wio);
1032 evio_start (&wio); 1200 ev_io_start (&wio);
1033} 1201}
1034 1202
1035static void 1203static void
1036gcb (struct ev_signal *w, int revents) 1204gcb (struct ev_signal *w, int revents)
1037{ 1205{
1041 1209
1042int main (void) 1210int main (void)
1043{ 1211{
1044 ev_init (0); 1212 ev_init (0);
1045 1213
1046 evio_init (&wio, sin_cb, 0, EV_READ); 1214 ev_io_init (&wio, sin_cb, 0, EV_READ);
1047 evio_start (&wio); 1215 ev_io_start (&wio);
1048 1216
1049 struct ev_timer t[10000]; 1217 struct ev_timer t[10000];
1050 1218
1051#if 0 1219#if 0
1052 int i; 1220 int i;
1053 for (i = 0; i < 10000; ++i) 1221 for (i = 0; i < 10000; ++i)
1054 { 1222 {
1055 struct ev_timer *w = t + i; 1223 struct ev_timer *w = t + i;
1056 evw_init (w, ocb, i); 1224 ev_watcher_init (w, ocb, i);
1057 evtimer_init_abs (w, ocb, drand48 (), 0.99775533); 1225 ev_timer_init_abs (w, ocb, drand48 (), 0.99775533);
1058 evtimer_start (w); 1226 ev_timer_start (w);
1059 if (drand48 () < 0.5) 1227 if (drand48 () < 0.5)
1060 evtimer_stop (w); 1228 ev_timer_stop (w);
1061 } 1229 }
1062#endif 1230#endif
1063 1231
1064 struct ev_timer t1; 1232 struct ev_timer t1;
1065 evtimer_init (&t1, ocb, 5, 10); 1233 ev_timer_init (&t1, ocb, 5, 10);
1066 evtimer_start (&t1); 1234 ev_timer_start (&t1);
1067 1235
1068 struct ev_signal sig; 1236 struct ev_signal sig;
1069 evsignal_init (&sig, scb, SIGQUIT); 1237 ev_signal_init (&sig, scb, SIGQUIT);
1070 evsignal_start (&sig); 1238 ev_signal_start (&sig);
1071 1239
1072 struct ev_check cw; 1240 struct ev_check cw;
1073 evcheck_init (&cw, gcb); 1241 ev_check_init (&cw, gcb);
1074 evcheck_start (&cw); 1242 ev_check_start (&cw);
1075 1243
1076 struct ev_idle iw; 1244 struct ev_idle iw;
1077 evidle_init (&iw, gcb); 1245 ev_idle_init (&iw, gcb);
1078 evidle_start (&iw); 1246 ev_idle_start (&iw);
1079 1247
1080 ev_loop (0); 1248 ev_loop (0);
1081 1249
1082 return 0; 1250 return 0;
1083} 1251}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines