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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines