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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines