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

Comparing libev/ev.c (file contents):
Revision 1.7 by root, Wed Oct 31 00:24:16 2007 UTC vs.
Revision 1.32 by root, Thu Nov 1 09:21:51 2007 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines