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

Comparing libev/ev.c (file contents):
Revision 1.22 by root, Wed Oct 31 19:07:43 2007 UTC vs.
Revision 1.43 by root, Fri Nov 2 20:21:33 2007 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines