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

Comparing libev/ev.c (file contents):
Revision 1.3 by root, Tue Oct 30 21:45:00 2007 UTC vs.
Revision 1.43 by root, Fri Nov 2 20:21:33 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
44#include <assert.h>
6#include <errno.h> 45#include <errno.h>
46#include <sys/types.h>
47#include <sys/wait.h>
7#include <sys/time.h> 48#include <sys/time.h>
8#include <time.h> 49#include <time.h>
9 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
10#ifdef CLOCK_MONOTONIC 75#ifndef CLOCK_MONOTONIC
76# undef EV_USE_MONOTONIC
11# define HAVE_MONOTONIC 1 77# define EV_USE_MONOTONIC 0
12#endif 78#endif
13 79
14#define HAVE_EPOLL 1 80#ifndef CLOCK_REALTIME
81# undef EV_USE_REALTIME
15#define HAVE_REALTIME 1 82# define EV_USE_REALTIME 0
16#define HAVE_SELECT 0 83#endif
17 84
18#define MAX_BLOCKTIME 60. 85/**/
86
87#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
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 */
19 91
20#include "ev.h" 92#include "ev.h"
21 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
22struct ev_watcher { 108typedef struct ev_watcher *W;
23 EV_WATCHER (ev_watcher);
24};
25
26struct ev_watcher_list { 109typedef struct ev_watcher_list *WL;
27 EV_WATCHER_LIST (ev_watcher_list); 110typedef struct ev_watcher_time *WT;
28};
29 111
112static ev_tstamp now_floor, now, diff; /* monotonic clock */
30ev_tstamp ev_now; 113ev_tstamp ev_now;
31int ev_method; 114int ev_method;
32 115
33static int have_monotonic; /* runtime */ 116static int have_monotonic; /* runtime */
34 117
35static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */ 118static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */
36static void (*method_reify)(void); 119static void (*method_modify)(int fd, int oev, int nev);
37static void (*method_poll)(ev_tstamp timeout); 120static void (*method_poll)(ev_tstamp timeout);
121
122/*****************************************************************************/
38 123
39ev_tstamp 124ev_tstamp
40ev_time (void) 125ev_time (void)
41{ 126{
42#if HAVE_REALTIME 127#if EV_USE_REALTIME
43 struct timespec ts; 128 struct timespec ts;
44 clock_gettime (CLOCK_REALTIME, &ts); 129 clock_gettime (CLOCK_REALTIME, &ts);
45 return ts.tv_sec + ts.tv_nsec * 1e-9; 130 return ts.tv_sec + ts.tv_nsec * 1e-9;
46#else 131#else
47 struct timeval tv; 132 struct timeval tv;
51} 136}
52 137
53static ev_tstamp 138static ev_tstamp
54get_clock (void) 139get_clock (void)
55{ 140{
56#if HAVE_MONOTONIC 141#if EV_USE_MONOTONIC
57 if (have_monotonic) 142 if (expect_true (have_monotonic))
58 { 143 {
59 struct timespec ts; 144 struct timespec ts;
60 clock_gettime (CLOCK_MONOTONIC, &ts); 145 clock_gettime (CLOCK_MONOTONIC, &ts);
61 return ts.tv_sec + ts.tv_nsec * 1e-9; 146 return ts.tv_sec + ts.tv_nsec * 1e-9;
62 } 147 }
63#endif 148#endif
64 149
65 return ev_time (); 150 return ev_time ();
66} 151}
67 152
153#define array_roundsize(base,n) ((n) | 4 & ~3)
154
68#define array_needsize(base,cur,cnt,init) \ 155#define array_needsize(base,cur,cnt,init) \
69 if ((cnt) > cur) \ 156 if (expect_false ((cnt) > cur)) \
70 { \ 157 { \
71 int newcnt = cur ? cur << 1 : 16; \ 158 int newcnt = cur; \
72 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 \
73 base = realloc (base, sizeof (*base) * (newcnt)); \ 165 base = realloc (base, sizeof (*base) * (newcnt)); \
74 init (base + cur, newcnt - cur); \ 166 init (base + cur, newcnt - cur); \
75 cur = newcnt; \ 167 cur = newcnt; \
76 } 168 }
77 169
170/*****************************************************************************/
171
78typedef struct 172typedef struct
79{ 173{
80 struct ev_io *head; 174 struct ev_io *head;
81 unsigned char wev, rev; /* want, received event set */ 175 unsigned char events;
176 unsigned char reify;
82} ANFD; 177} ANFD;
83 178
84static ANFD *anfds; 179static ANFD *anfds;
85static int anfdmax; 180static int anfdmax;
86 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
87static int *fdchanges; 245static int *fdchanges;
88static int fdchangemax, fdchangecnt; 246static int fdchangemax, fdchangecnt;
89 247
90static void 248static void
91anfds_init (ANFD *base, int count) 249fd_reify (void)
92{ 250{
93 while (count--) 251 int i;
94 { 252
95 base->head = 0; 253 for (i = 0; i < fdchangecnt; ++i)
96 base->wev = base->rev = EV_NONE;
97 ++base;
98 } 254 {
99} 255 int fd = fdchanges [i];
100
101typedef struct
102{
103 struct ev_watcher *w;
104 int events;
105} ANPENDING;
106
107static ANPENDING *pendings;
108static int pendingmax, pendingcnt;
109
110static void
111event (struct ev_watcher *w, int events)
112{
113 w->pending = ++pendingcnt;
114 array_needsize (pendings, pendingmax, pendingcnt, );
115 pendings [pendingcnt - 1].w = w;
116 pendings [pendingcnt - 1].events = events;
117}
118
119static void
120fd_event (int fd, int events)
121{
122 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{
123 struct ev_io *w; 292 struct ev_io *w;
124 293
125 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)
126 { 322 {
127 int ev = w->events & events; 323 close (fd);
128 324 fd_kill (fd);
129 if (ev) 325 return;
130 event ((struct ev_watcher *)w, ev);
131 } 326 }
132} 327}
328
329/*****************************************************************************/
133 330
134static struct ev_timer **timers; 331static struct ev_timer **timers;
135static int timermax, timercnt; 332static int timermax, timercnt;
136 333
334static struct ev_periodic **periodics;
335static int periodicmax, periodiccnt;
336
137static void 337static void
138upheap (int k) 338upheap (WT *timers, int k)
139{ 339{
140 struct ev_timer *w = timers [k]; 340 WT w = timers [k];
141 341
142 while (k && timers [k >> 1]->at > w->at) 342 while (k && timers [k >> 1]->at > w->at)
143 { 343 {
144 timers [k] = timers [k >> 1]; 344 timers [k] = timers [k >> 1];
145 timers [k]->active = k + 1; 345 timers [k]->active = k + 1;
150 timers [k]->active = k + 1; 350 timers [k]->active = k + 1;
151 351
152} 352}
153 353
154static void 354static void
155downheap (int k) 355downheap (WT *timers, int N, int k)
156{ 356{
157 struct ev_timer *w = timers [k]; 357 WT w = timers [k];
158 358
159 while (k < (timercnt >> 1)) 359 while (k < (N >> 1))
160 { 360 {
161 int j = k << 1; 361 int j = k << 1;
162 362
163 if (j + 1 < timercnt && timers [j]->at > timers [j + 1]->at) 363 if (j + 1 < N && timers [j]->at > timers [j + 1]->at)
164 ++j; 364 ++j;
165 365
166 if (w->at <= timers [j]->at) 366 if (w->at <= timers [j]->at)
167 break; 367 break;
168 368
173 373
174 timers [k] = w; 374 timers [k] = w;
175 timers [k]->active = k + 1; 375 timers [k]->active = k + 1;
176} 376}
177 377
178static 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;
179static int signalmax, signalcnt; 387static int signalmax;
180 388
389static int sigpipe [2];
390static sig_atomic_t volatile gotsig;
391static struct ev_io sigev;
392
181static void 393static void
182signals_init (struct ev_signal **base, int count) 394signals_init (ANSIG *base, int count)
183{ 395{
184 while (count--) 396 while (count--)
185 *base++ = 0; 397 {
186} 398 base->head = 0;
399 base->gotsig = 0;
187 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
188#if HAVE_EPOLL 487#if EV_USE_EPOLL
189# include "ev_epoll.c" 488# include "ev_epoll.c"
190#endif 489#endif
490#if EV_USE_POLL
491# include "ev_poll.c"
492#endif
191#if HAVE_SELECT 493#if EV_USE_SELECT
192# include "ev_select.c" 494# include "ev_select.c"
193#endif 495#endif
194 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
195int ev_init (int flags) 517int ev_init (int methods)
196{ 518{
519 if (!ev_method)
520 {
197#if HAVE_MONOTONIC 521#if EV_USE_MONOTONIC
198 { 522 {
199 struct timespec ts; 523 struct timespec ts;
200 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 524 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
201 have_monotonic = 1; 525 have_monotonic = 1;
202 } 526 }
203#endif 527#endif
204 528
205 ev_now = ev_time (); 529 ev_now = ev_time ();
530 now = get_clock ();
531 now_floor = now;
532 diff = ev_now - now;
206 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;
207#if HAVE_EPOLL 544#if EV_USE_EPOLL
208 if (epoll_init (flags)) 545 if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods);
209 return ev_method;
210#endif 546#endif
547#if EV_USE_POLL
548 if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods);
549#endif
211#if HAVE_SELECT 550#if EV_USE_SELECT
212 if (select_init (flags)) 551 if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods);
213 return ev_method;
214#endif 552#endif
215 553
216 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
217 return ev_method; 564 return ev_method;
218} 565}
219 566
220void ev_prefork (void) 567/*****************************************************************************/
221{
222}
223 568
569void
570ev_fork_prepare (void)
571{
572 /* nop */
573}
574
575void
224void ev_postfork_parent (void) 576ev_fork_parent (void)
225{ 577{
578 /* nop */
226} 579}
227 580
581void
228void ev_postfork_child (void) 582ev_fork_child (void)
229{ 583{
230#if HAVE_EPOLL 584#if EV_USE_EPOLL
585 if (ev_method == EVMETHOD_EPOLL)
231 epoll_postfork_child (); 586 epoll_postfork_child ();
232#endif 587#endif
233}
234 588
589 ev_io_stop (&sigev);
590 close (sigpipe [0]);
591 close (sigpipe [1]);
592 pipe (sigpipe);
593 siginit ();
594}
595
596/*****************************************************************************/
597
235static void 598static void
236call_pending () 599call_pending (void)
600{
601 int pri;
602
603 for (pri = NUMPRI; pri--; )
604 while (pendingcnt [pri])
605 {
606 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
607
608 if (p->w)
609 {
610 p->w->pending = 0;
611 p->w->cb (p->w, p->events);
612 }
613 }
614}
615
616static void
617timers_reify (void)
618{
619 while (timercnt && timers [0]->at <= now)
620 {
621 struct ev_timer *w = timers [0];
622
623 /* first reschedule or stop timer */
624 if (w->repeat)
625 {
626 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
627 w->at = now + w->repeat;
628 downheap ((WT *)timers, timercnt, 0);
629 }
630 else
631 ev_timer_stop (w); /* nonrepeating: stop timer */
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)
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 */
653
654 event ((W)w, EV_PERIODIC);
655 }
656}
657
658static void
659periodics_reschedule (ev_tstamp diff)
237{ 660{
238 int i; 661 int i;
239 662
663 /* adjust periodics after time jump */
240 for (i = 0; i < pendingcnt; ++i) 664 for (i = 0; i < periodiccnt; ++i)
241 { 665 {
242 ANPENDING *p = pendings + i; 666 struct ev_periodic *w = periodics [i];
243 667
244 if (p->w) 668 if (w->interval)
245 {
246 p->w->pending = 0;
247 p->w->cb (p->w, p->events);
248 } 669 {
249 } 670 ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval;
250 671
251 pendingcnt = 0; 672 if (fabs (diff) >= 1e-4)
252} 673 {
674 ev_periodic_stop (w);
675 ev_periodic_start (w);
253 676
254static void 677 i = 0; /* restart loop, inefficient, but time jumps should be rare */
255timer_reify (void) 678 }
256{
257 while (timercnt && timers [0]->at <= ev_now)
258 {
259 struct ev_timer *w = timers [0];
260
261 /* first reschedule timer */
262 if (w->repeat)
263 { 679 }
264 if (w->is_abs) 680 }
265 w->at += ceil ((ev_now - w->at) / w->repeat + 1.) * w->repeat; 681}
266 else
267 w->at = ev_now + w->repeat;
268 682
269 downheap (0); 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
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 ())
270 } 710 {
711 ev_tstamp odiff = diff;
712
713 for (i = 4; --i; ) /* loop a few times, before making important decisions */
714 {
715 diff = ev_now - now;
716
717 if (fabs (odiff - diff) < MIN_TIMEJUMP)
718 return; /* all is well */
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 ();
271 else 773 else
272 evtimer_stop (w); /* nonrepeating: stop timer */ 774#endif
273 775 {
274 event ((struct ev_watcher *)w, EV_TIMEOUT);
275 }
276}
277
278int ev_loop_done;
279
280int ev_loop (int flags)
281{
282 double block;
283 ev_loop_done = flags & EVLOOP_ONESHOT;
284
285 do
286 {
287 /* update fd-related kernel structures */
288 method_reify (); fdchangecnt = 0;
289
290 /* calculate blocking time */
291 ev_now = ev_time (); 776 ev_now = ev_time ();
777 now = ev_now;
778 }
292 779
293 if (flags & EVLOOP_NONBLOCK) 780 if (flags & EVLOOP_NONBLOCK || idlecnt)
294 block = 0.; 781 block = 0.;
295 else if (!timercnt)
296 block = MAX_BLOCKTIME;
297 else 782 else
298 { 783 {
784 block = MAX_BLOCKTIME;
785
786 if (timercnt)
787 {
299 block = timers [0]->at - ev_now + method_fudge; 788 ev_tstamp to = timers [0]->at - now + method_fudge;
789 if (block > to) block = to;
790 }
791
792 if (periodiccnt)
793 {
794 ev_tstamp to = periodics [0]->at - ev_now + method_fudge;
795 if (block > to) block = to;
796 }
797
300 if (block < 0.) block = 0.; 798 if (block < 0.) block = 0.;
301 else if (block > MAX_BLOCKTIME) block = MAX_BLOCKTIME;
302 } 799 }
303 800
304 method_poll (block); 801 method_poll (block);
305 802
803 /* update ev_now, do magic */
804 time_update ();
805
306 /* put pending timers into pendign queue and reschedule them */ 806 /* queue pending timers and reschedule them */
307 timer_reify (); 807 timers_reify (); /* relative timers called last */
808 periodics_reify (); /* absolute timers called first */
308 809
309 ev_now = ev_time (); 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);
817
310 call_pending (); 818 call_pending ();
311 } 819 }
312 while (!ev_loop_done); 820 while (!ev_loop_done);
313}
314 821
822 if (ev_loop_done != 2)
823 ev_loop_done = 0;
824}
825
826/*****************************************************************************/
827
315static void 828static void
316wlist_add (struct ev_watcher_list **head, struct ev_watcher_list *elem) 829wlist_add (WL *head, WL elem)
317{ 830{
318 elem->next = *head; 831 elem->next = *head;
319 *head = elem; 832 *head = elem;
320} 833}
321 834
322static void 835static void
323wlist_del (struct ev_watcher_list **head, struct ev_watcher_list *elem) 836wlist_del (WL *head, WL elem)
324{ 837{
325 while (*head) 838 while (*head)
326 { 839 {
327 if (*head == elem) 840 if (*head == elem)
328 { 841 {
333 head = &(*head)->next; 846 head = &(*head)->next;
334 } 847 }
335} 848}
336 849
337static void 850static void
338ev_start (struct ev_watcher *w, int active) 851ev_clear_pending (W w)
339{ 852{
853 if (w->pending)
854 {
855 pendings [ABSPRI (w)][w->pending - 1].w = 0;
340 w->pending = 0; 856 w->pending = 0;
857 }
858}
859
860static void
861ev_start (W w, int active)
862{
863 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI;
864 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
865
341 w->active = active; 866 w->active = active;
342} 867}
343 868
344static void 869static void
345ev_stop (struct ev_watcher *w) 870ev_stop (W w)
346{ 871{
347 if (w->pending)
348 pendings [w->pending - 1].w = 0;
349
350 w->active = 0; 872 w->active = 0;
351 /* nop */
352} 873}
353 874
875/*****************************************************************************/
876
354void 877void
355evio_start (struct ev_io *w) 878ev_io_start (struct ev_io *w)
356{ 879{
880 int fd = w->fd;
881
357 if (ev_is_active (w)) 882 if (ev_is_active (w))
358 return; 883 return;
359 884
360 int fd = w->fd; 885 assert (("ev_io_start called with negative fd", fd >= 0));
361 886
362 ev_start ((struct ev_watcher *)w, 1); 887 ev_start ((W)w, 1);
363 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 888 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
364 wlist_add ((struct ev_watcher_list **)&anfds[fd].head, (struct ev_watcher_list *)w); 889 wlist_add ((WL *)&anfds[fd].head, (WL)w);
365 890
366 ++fdchangecnt; 891 fd_change (fd);
367 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
368 fdchanges [fdchangecnt - 1] = fd;
369} 892}
370 893
371void 894void
372evio_stop (struct ev_io *w) 895ev_io_stop (struct ev_io *w)
373{ 896{
897 ev_clear_pending ((W)w);
374 if (!ev_is_active (w)) 898 if (!ev_is_active (w))
375 return; 899 return;
376 900
377 wlist_del ((struct ev_watcher_list **)&anfds[w->fd].head, (struct ev_watcher_list *)w); 901 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
378 ev_stop ((struct ev_watcher *)w); 902 ev_stop ((W)w);
379 903
380 ++fdchangecnt; 904 fd_change (w->fd);
381 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
382 fdchanges [fdchangecnt - 1] = w->fd;
383} 905}
384 906
385void 907void
386evtimer_start (struct ev_timer *w) 908ev_timer_start (struct ev_timer *w)
387{ 909{
388 if (ev_is_active (w)) 910 if (ev_is_active (w))
389 return; 911 return;
390 912
391 if (w->is_abs)
392 {
393 /* this formula differs from the one in timer_reify becuse we do not round up */
394 if (w->repeat)
395 w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat;
396 }
397 else
398 w->at += ev_now; 913 w->at += now;
399 914
400 ev_start ((struct ev_watcher *)w, ++timercnt); 915 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
916
917 ev_start ((W)w, ++timercnt);
401 array_needsize (timers, timermax, timercnt, ); 918 array_needsize (timers, timermax, timercnt, );
402 timers [timercnt - 1] = w; 919 timers [timercnt - 1] = w;
403 upheap (timercnt - 1); 920 upheap ((WT *)timers, timercnt - 1);
404} 921}
405 922
406void 923void
407evtimer_stop (struct ev_timer *w) 924ev_timer_stop (struct ev_timer *w)
408{ 925{
926 ev_clear_pending ((W)w);
409 if (!ev_is_active (w)) 927 if (!ev_is_active (w))
410 return; 928 return;
411 929
412 if (w->active < timercnt--) 930 if (w->active < timercnt--)
413 { 931 {
414 timers [w->active - 1] = timers [timercnt]; 932 timers [w->active - 1] = timers [timercnt];
415 downheap (w->active - 1); 933 downheap ((WT *)timers, timercnt, w->active - 1);
416 } 934 }
417 935
418 ev_stop ((struct ev_watcher *)w); 936 w->at = w->repeat;
419}
420 937
938 ev_stop ((W)w);
939}
940
421void 941void
422evsignal_start (struct ev_signal *w) 942ev_timer_again (struct ev_timer *w)
423{ 943{
424 if (ev_is_active (w)) 944 if (ev_is_active (w))
425 return; 945 {
946 if (w->repeat)
947 {
948 w->at = now + w->repeat;
949 downheap ((WT *)timers, timercnt, w->active - 1);
950 }
951 else
952 ev_timer_stop (w);
953 }
954 else if (w->repeat)
955 ev_timer_start (w);
956}
426 957
427 ev_start ((struct ev_watcher *)w, 1); 958void
959ev_periodic_start (struct ev_periodic *w)
960{
961 if (ev_is_active (w))
962 return;
963
964 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
965
966 /* this formula differs from the one in periodic_reify because we do not always round up */
967 if (w->interval)
968 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
969
970 ev_start ((W)w, ++periodiccnt);
971 array_needsize (periodics, periodicmax, periodiccnt, );
972 periodics [periodiccnt - 1] = w;
973 upheap ((WT *)periodics, periodiccnt - 1);
974}
975
976void
977ev_periodic_stop (struct ev_periodic *w)
978{
979 ev_clear_pending ((W)w);
980 if (!ev_is_active (w))
981 return;
982
983 if (w->active < periodiccnt--)
984 {
985 periodics [w->active - 1] = periodics [periodiccnt];
986 downheap ((WT *)periodics, periodiccnt, w->active - 1);
987 }
988
989 ev_stop ((W)w);
990}
991
992void
993ev_signal_start (struct ev_signal *w)
994{
995 if (ev_is_active (w))
996 return;
997
998 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
999
1000 ev_start ((W)w, 1);
428 array_needsize (signals, signalmax, w->signum, signals_init); 1001 array_needsize (signals, signalmax, w->signum, signals_init);
429 wlist_add ((struct ev_watcher_list **)&signals [w->signum - 1], (struct ev_watcher_list *)w); 1002 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
430}
431 1003
1004 if (!w->next)
1005 {
1006 struct sigaction sa;
1007 sa.sa_handler = sighandler;
1008 sigfillset (&sa.sa_mask);
1009 sa.sa_flags = 0;
1010 sigaction (w->signum, &sa, 0);
1011 }
1012}
1013
432void 1014void
433evsignal_stop (struct ev_signal *w) 1015ev_signal_stop (struct ev_signal *w)
434{ 1016{
1017 ev_clear_pending ((W)w);
435 if (!ev_is_active (w)) 1018 if (!ev_is_active (w))
436 return; 1019 return;
437 1020
438 wlist_del ((struct ev_watcher_list **)&signals [w->signum - 1], (struct ev_watcher_list *)w); 1021 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
1022 ev_stop ((W)w);
1023
1024 if (!signals [w->signum - 1].head)
1025 signal (w->signum, SIG_DFL);
1026}
1027
1028void
1029ev_idle_start (struct ev_idle *w)
1030{
1031 if (ev_is_active (w))
1032 return;
1033
1034 ev_start ((W)w, ++idlecnt);
1035 array_needsize (idles, idlemax, idlecnt, );
1036 idles [idlecnt - 1] = w;
1037}
1038
1039void
1040ev_idle_stop (struct ev_idle *w)
1041{
1042 ev_clear_pending ((W)w);
1043 if (ev_is_active (w))
1044 return;
1045
1046 idles [w->active - 1] = idles [--idlecnt];
1047 ev_stop ((W)w);
1048}
1049
1050void
1051ev_prepare_start (struct ev_prepare *w)
1052{
1053 if (ev_is_active (w))
1054 return;
1055
1056 ev_start ((W)w, ++preparecnt);
1057 array_needsize (prepares, preparemax, preparecnt, );
1058 prepares [preparecnt - 1] = w;
1059}
1060
1061void
1062ev_prepare_stop (struct ev_prepare *w)
1063{
1064 ev_clear_pending ((W)w);
1065 if (ev_is_active (w))
1066 return;
1067
1068 prepares [w->active - 1] = prepares [--preparecnt];
1069 ev_stop ((W)w);
1070}
1071
1072void
1073ev_check_start (struct ev_check *w)
1074{
1075 if (ev_is_active (w))
1076 return;
1077
1078 ev_start ((W)w, ++checkcnt);
1079 array_needsize (checks, checkmax, checkcnt, );
1080 checks [checkcnt - 1] = w;
1081}
1082
1083void
439 ev_stop ((struct ev_watcher *)w); 1084ev_check_stop (struct ev_check *w)
440} 1085{
1086 ev_clear_pending ((W)w);
1087 if (ev_is_active (w))
1088 return;
441 1089
1090 checks [w->active - 1] = checks [--checkcnt];
1091 ev_stop ((W)w);
1092}
1093
1094void
1095ev_child_start (struct ev_child *w)
1096{
1097 if (ev_is_active (w))
1098 return;
1099
1100 ev_start ((W)w, 1);
1101 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1102}
1103
1104void
1105ev_child_stop (struct ev_child *w)
1106{
1107 ev_clear_pending ((W)w);
1108 if (ev_is_active (w))
1109 return;
1110
1111 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1112 ev_stop ((W)w);
1113}
1114
442/*****************************************************************************/ 1115/*****************************************************************************/
1116
1117struct ev_once
1118{
1119 struct ev_io io;
1120 struct ev_timer to;
1121 void (*cb)(int revents, void *arg);
1122 void *arg;
1123};
1124
1125static void
1126once_cb (struct ev_once *once, int revents)
1127{
1128 void (*cb)(int revents, void *arg) = once->cb;
1129 void *arg = once->arg;
1130
1131 ev_io_stop (&once->io);
1132 ev_timer_stop (&once->to);
1133 free (once);
1134
1135 cb (revents, arg);
1136}
1137
1138static void
1139once_cb_io (struct ev_io *w, int revents)
1140{
1141 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents);
1142}
1143
1144static void
1145once_cb_to (struct ev_timer *w, int revents)
1146{
1147 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents);
1148}
1149
1150void
1151ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
1152{
1153 struct ev_once *once = malloc (sizeof (struct ev_once));
1154
1155 if (!once)
1156 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1157 else
1158 {
1159 once->cb = cb;
1160 once->arg = arg;
1161
1162 ev_watcher_init (&once->io, once_cb_io);
1163 if (fd >= 0)
1164 {
1165 ev_io_set (&once->io, fd, events);
1166 ev_io_start (&once->io);
1167 }
1168
1169 ev_watcher_init (&once->to, once_cb_to);
1170 if (timeout >= 0.)
1171 {
1172 ev_timer_set (&once->to, timeout, 0.);
1173 ev_timer_start (&once->to);
1174 }
1175 }
1176}
1177
1178/*****************************************************************************/
1179
443#if 1 1180#if 0
1181
1182struct ev_io wio;
444 1183
445static void 1184static void
446sin_cb (struct ev_io *w, int revents) 1185sin_cb (struct ev_io *w, int revents)
447{ 1186{
448 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); 1187 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
449} 1188}
450 1189
451static void 1190static void
452ocb (struct ev_timer *w, int revents) 1191ocb (struct ev_timer *w, int revents)
453{ 1192{
454 fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); 1193 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data);
1194 ev_timer_stop (w);
1195 ev_timer_start (w);
1196}
1197
1198static void
1199scb (struct ev_signal *w, int revents)
1200{
1201 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
1202 ev_io_stop (&wio);
1203 ev_io_start (&wio);
1204}
1205
1206static void
1207gcb (struct ev_signal *w, int revents)
1208{
1209 fprintf (stderr, "generic %x\n", revents);
1210
455} 1211}
456 1212
457int main (void) 1213int main (void)
458{ 1214{
459 struct ev_io sin;
460
461 ev_init (0); 1215 ev_init (0);
462 1216
463 evw_init (&sin, sin_cb, 55);
464 evio_set (&sin, 0, EV_READ); 1217 ev_io_init (&wio, sin_cb, 0, EV_READ);
465 evio_start (&sin); 1218 ev_io_start (&wio);
466 1219
467 struct ev_timer t[1000]; 1220 struct ev_timer t[10000];
468 1221
1222#if 0
469 int i; 1223 int i;
470 for (i = 0; i < 1000; ++i) 1224 for (i = 0; i < 10000; ++i)
471 { 1225 {
472 struct ev_timer *w = t + i; 1226 struct ev_timer *w = t + i;
473 evw_init (w, ocb, i); 1227 ev_watcher_init (w, ocb, i);
474 evtimer_set_rel (w, drand48 (), 0); 1228 ev_timer_init_abs (w, ocb, drand48 (), 0.99775533);
475 evtimer_start (w); 1229 ev_timer_start (w);
476 if (drand48 () < 0.5) 1230 if (drand48 () < 0.5)
477 evtimer_stop (w); 1231 ev_timer_stop (w);
478 } 1232 }
1233#endif
1234
1235 struct ev_timer t1;
1236 ev_timer_init (&t1, ocb, 5, 10);
1237 ev_timer_start (&t1);
1238
1239 struct ev_signal sig;
1240 ev_signal_init (&sig, scb, SIGQUIT);
1241 ev_signal_start (&sig);
1242
1243 struct ev_check cw;
1244 ev_check_init (&cw, gcb);
1245 ev_check_start (&cw);
1246
1247 struct ev_idle iw;
1248 ev_idle_init (&iw, gcb);
1249 ev_idle_start (&iw);
479 1250
480 ev_loop (0); 1251 ev_loop (0);
481 1252
482 return 0; 1253 return 0;
483} 1254}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines