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

Comparing libev/ev.c (file contents):
Revision 1.19 by root, Wed Oct 31 17:55:55 2007 UTC vs.
Revision 1.41 by root, Fri Nov 2 16:54:34 2007 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines