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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines