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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines