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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines