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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines