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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines