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

Comparing libev/ev.c (file contents):
Revision 1.4 by root, Tue Oct 30 23:10:33 2007 UTC vs.
Revision 1.42 by root, Fri Nov 2 20:05:05 2007 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines