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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines