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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines