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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines