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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines