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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines