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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines