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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines