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

Comparing libev/ev.c (file contents):
Revision 1.17 by root, Wed Oct 31 14:44:15 2007 UTC vs.
Revision 1.36 by root, Thu Nov 1 13:11:11 2007 UTC

1/* 1/*
2 * libev event processing core, watcher management
3 *
2 * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> 4 * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de>
3 * All rights reserved. 5 * All rights reserved.
4 * 6 *
5 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are 8 * modification, are permitted provided that the following conditions are
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */ 30 */
31#if EV_USE_CONFIG_H
32# include "config.h"
33#endif
29 34
30#include <math.h> 35#include <math.h>
31#include <stdlib.h> 36#include <stdlib.h>
32#include <unistd.h> 37#include <unistd.h>
33#include <fcntl.h> 38#include <fcntl.h>
36 41
37#include <stdio.h> 42#include <stdio.h>
38 43
39#include <assert.h> 44#include <assert.h>
40#include <errno.h> 45#include <errno.h>
46#include <sys/types.h>
47#include <sys/wait.h>
41#include <sys/time.h> 48#include <sys/time.h>
42#include <time.h> 49#include <time.h>
43 50
44#define HAVE_EPOLL 1
45
46#ifndef HAVE_MONOTONIC 51#ifndef EV_USE_MONOTONIC
47# ifdef CLOCK_MONOTONIC 52# ifdef CLOCK_MONOTONIC
48# define HAVE_MONOTONIC 1 53# define EV_USE_MONOTONIC 1
49# endif 54# endif
50#endif 55#endif
51 56
52#ifndef HAVE_SELECT 57#ifndef EV_USE_SELECT
53# define HAVE_SELECT 1 58# define EV_USE_SELECT 1
54#endif 59#endif
55 60
56#ifndef HAVE_EPOLL 61#ifndef EV_USE_EPOLL
57# define HAVE_EPOLL 0 62# define EV_USE_EPOLL 0
58#endif 63#endif
59 64
65#ifndef CLOCK_REALTIME
66# define EV_USE_REALTIME 0
67#endif
60#ifndef HAVE_REALTIME 68#ifndef EV_USE_REALTIME
61# define HAVE_REALTIME 1 /* posix requirement, but might be slower */ 69# define EV_USE_REALTIME 1 /* posix requirement, but might be slower */
62#endif 70#endif
63 71
64#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) */
65#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 */
66 76
67#include "ev.h" 77#include "ev.h"
68 78
69typedef struct ev_watcher *W; 79typedef struct ev_watcher *W;
70typedef struct ev_watcher_list *WL; 80typedef struct ev_watcher_list *WL;
83/*****************************************************************************/ 93/*****************************************************************************/
84 94
85ev_tstamp 95ev_tstamp
86ev_time (void) 96ev_time (void)
87{ 97{
88#if HAVE_REALTIME 98#if EV_USE_REALTIME
89 struct timespec ts; 99 struct timespec ts;
90 clock_gettime (CLOCK_REALTIME, &ts); 100 clock_gettime (CLOCK_REALTIME, &ts);
91 return ts.tv_sec + ts.tv_nsec * 1e-9; 101 return ts.tv_sec + ts.tv_nsec * 1e-9;
92#else 102#else
93 struct timeval tv; 103 struct timeval tv;
97} 107}
98 108
99static ev_tstamp 109static ev_tstamp
100get_clock (void) 110get_clock (void)
101{ 111{
102#if HAVE_MONOTONIC 112#if EV_USE_MONOTONIC
103 if (have_monotonic) 113 if (have_monotonic)
104 { 114 {
105 struct timespec ts; 115 struct timespec ts;
106 clock_gettime (CLOCK_MONOTONIC, &ts); 116 clock_gettime (CLOCK_MONOTONIC, &ts);
107 return ts.tv_sec + ts.tv_nsec * 1e-9; 117 return ts.tv_sec + ts.tv_nsec * 1e-9;
109#endif 119#endif
110 120
111 return ev_time (); 121 return ev_time ();
112} 122}
113 123
124#define array_roundsize(base,n) ((n) | 4 & ~3)
125
114#define array_needsize(base,cur,cnt,init) \ 126#define array_needsize(base,cur,cnt,init) \
115 if ((cnt) > cur) \ 127 if ((cnt) > cur) \
116 { \ 128 { \
117 int newcnt = cur ? cur << 1 : 16; \ 129 int newcnt = cur; \
130 do \
131 { \
132 newcnt = array_roundsize (base, newcnt << 1); \
133 } \
134 while ((cnt) > newcnt); \
135 \
118 base = realloc (base, sizeof (*base) * (newcnt)); \ 136 base = realloc (base, sizeof (*base) * (newcnt)); \
119 init (base + cur, newcnt - cur); \ 137 init (base + cur, newcnt - cur); \
120 cur = newcnt; \ 138 cur = newcnt; \
121 } 139 }
122 140
123/*****************************************************************************/ 141/*****************************************************************************/
124 142
125typedef struct 143typedef struct
126{ 144{
127 struct ev_io *head; 145 struct ev_io *head;
128 unsigned char wev, rev; /* want, received event set */ 146 unsigned char events;
147 unsigned char reify;
129} ANFD; 148} ANFD;
130 149
131static ANFD *anfds; 150static ANFD *anfds;
132static int anfdmax; 151static int anfdmax;
133 152
134static int *fdchanges;
135static int fdchangemax, fdchangecnt;
136
137static void 153static void
138anfds_init (ANFD *base, int count) 154anfds_init (ANFD *base, int count)
139{ 155{
140 while (count--) 156 while (count--)
141 { 157 {
142 base->head = 0; 158 base->head = 0;
143 base->wev = base->rev = EV_NONE; 159 base->events = EV_NONE;
160 base->reify = 0;
161
144 ++base; 162 ++base;
145 } 163 }
146} 164}
147 165
148typedef struct 166typedef struct
155static int pendingmax, pendingcnt; 173static int pendingmax, pendingcnt;
156 174
157static void 175static void
158event (W w, int events) 176event (W w, int events)
159{ 177{
160 if (w->active) 178 if (w->pending)
179 {
180 pendings [w->pending - 1].events |= events;
181 return;
161 { 182 }
183
162 w->pending = ++pendingcnt; 184 w->pending = ++pendingcnt;
163 array_needsize (pendings, pendingmax, pendingcnt, ); 185 array_needsize (pendings, pendingmax, pendingcnt, );
164 pendings [pendingcnt - 1].w = w; 186 pendings [pendingcnt - 1].w = w;
165 pendings [pendingcnt - 1].events = events; 187 pendings [pendingcnt - 1].events = events;
166 } 188}
189
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);
167} 197}
168 198
169static void 199static void
170fd_event (int fd, int events) 200fd_event (int fd, int events)
171{ 201{
179 if (ev) 209 if (ev)
180 event ((W)w, ev); 210 event ((W)w, ev);
181 } 211 }
182} 212}
183 213
214/*****************************************************************************/
215
216static int *fdchanges;
217static int fdchangemax, fdchangecnt;
218
184static void 219static void
185queue_events (W *events, int eventcnt, int type) 220fd_reify (void)
186{ 221{
187 int i; 222 int i;
188 223
189 for (i = 0; i < eventcnt; ++i) 224 for (i = 0; i < fdchangecnt; ++i)
190 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 }
191} 274}
192 275
193/*****************************************************************************/ 276/*****************************************************************************/
194 277
195static struct ev_timer **timers; 278static struct ev_timer **timers;
242/*****************************************************************************/ 325/*****************************************************************************/
243 326
244typedef struct 327typedef struct
245{ 328{
246 struct ev_signal *head; 329 struct ev_signal *head;
247 sig_atomic_t gotsig; 330 sig_atomic_t volatile gotsig;
248} ANSIG; 331} ANSIG;
249 332
250static ANSIG *signals; 333static ANSIG *signals;
251static int signalmax; 334static int signalmax;
252 335
253static int sigpipe [2]; 336static int sigpipe [2];
254static sig_atomic_t gotsig; 337static sig_atomic_t volatile gotsig;
255static struct ev_io sigev; 338static struct ev_io sigev;
256 339
257static void 340static void
258signals_init (ANSIG *base, int count) 341signals_init (ANSIG *base, int count)
259{ 342{
260 while (count--) 343 while (count--)
261 { 344 {
262 base->head = 0; 345 base->head = 0;
263 base->gotsig = 0; 346 base->gotsig = 0;
347
264 ++base; 348 ++base;
265 } 349 }
266} 350}
267 351
268static void 352static void
271 signals [signum - 1].gotsig = 1; 355 signals [signum - 1].gotsig = 1;
272 356
273 if (!gotsig) 357 if (!gotsig)
274 { 358 {
275 gotsig = 1; 359 gotsig = 1;
276 write (sigpipe [1], &gotsig, 1); 360 write (sigpipe [1], &signum, 1);
277 } 361 }
278} 362}
279 363
280static void 364static void
281sigcb (struct ev_io *iow, int revents) 365sigcb (struct ev_io *iow, int revents)
282{ 366{
283 struct ev_signal *w; 367 struct ev_signal *w;
284 int sig; 368 int sig;
285 369
370 read (sigpipe [0], &revents, 1);
286 gotsig = 0; 371 gotsig = 0;
287 read (sigpipe [0], &revents, 1);
288 372
289 for (sig = signalmax; sig--; ) 373 for (sig = signalmax; sig--; )
290 if (signals [sig].gotsig) 374 if (signals [sig].gotsig)
291 { 375 {
292 signals [sig].gotsig = 0; 376 signals [sig].gotsig = 0;
304 388
305 /* rather than sort out wether we really need nb, set it */ 389 /* rather than sort out wether we really need nb, set it */
306 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); 390 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
307 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 391 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
308 392
309 evio_set (&sigev, sigpipe [0], EV_READ); 393 ev_io_set (&sigev, sigpipe [0], EV_READ);
310 evio_start (&sigev); 394 ev_io_start (&sigev);
311} 395}
312 396
313/*****************************************************************************/ 397/*****************************************************************************/
314 398
315static struct ev_idle **idles; 399static struct ev_idle **idles;
316static int idlemax, idlecnt; 400static int idlemax, idlecnt;
317 401
402static struct ev_prepare **prepares;
403static int preparemax, preparecnt;
404
318static struct ev_check **checks; 405static struct ev_check **checks;
319static int checkmax, checkcnt; 406static int checkmax, checkcnt;
320 407
321/*****************************************************************************/ 408/*****************************************************************************/
322 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
323#if HAVE_EPOLL 434#if EV_USE_EPOLL
324# include "ev_epoll.c" 435# include "ev_epoll.c"
325#endif 436#endif
326#if HAVE_SELECT 437#if EV_USE_SELECT
327# include "ev_select.c" 438# include "ev_select.c"
328#endif 439#endif
329 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
330int ev_init (int flags) 453int ev_init (int flags)
331{ 454{
332#if HAVE_MONOTONIC
333 {
334 struct timespec ts;
335 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
336 have_monotonic = 1;
337 }
338#endif
339
340 ev_now = ev_time ();
341 now = get_clock ();
342 diff = ev_now - now;
343
344 if (pipe (sigpipe))
345 return 0;
346
347 ev_method = EVMETHOD_NONE;
348#if HAVE_EPOLL
349 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
350#endif
351#if HAVE_SELECT
352 if (ev_method == EVMETHOD_NONE) select_init (flags);
353#endif
354
355 if (ev_method) 455 if (!ev_method)
456 {
457#if EV_USE_MONOTONIC
356 { 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 {
357 evw_init (&sigev, sigcb); 482 ev_watcher_init (&sigev, sigcb);
358 siginit (); 483 siginit ();
484
485 ev_signal_init (&childev, childcb, SIGCHLD);
486 ev_signal_start (&childev);
487 }
359 } 488 }
360 489
361 return ev_method; 490 return ev_method;
362} 491}
363 492
364/*****************************************************************************/ 493/*****************************************************************************/
365 494
366void ev_prefork (void) 495void
496ev_fork_prepare (void)
367{ 497{
368 /* nop */ 498 /* nop */
369} 499}
370 500
501void
371void ev_postfork_parent (void) 502ev_fork_parent (void)
372{ 503{
373 /* nop */ 504 /* nop */
374} 505}
375 506
507void
376void ev_postfork_child (void) 508ev_fork_child (void)
377{ 509{
378#if HAVE_EPOLL 510#if EV_USE_EPOLL
379 if (ev_method == EVMETHOD_EPOLL) 511 if (ev_method == EVMETHOD_EPOLL)
380 epoll_postfork_child (); 512 epoll_postfork_child ();
381#endif 513#endif
382 514
383 evio_stop (&sigev); 515 ev_io_stop (&sigev);
384 close (sigpipe [0]); 516 close (sigpipe [0]);
385 close (sigpipe [1]); 517 close (sigpipe [1]);
386 pipe (sigpipe); 518 pipe (sigpipe);
387 siginit (); 519 siginit ();
388} 520}
389 521
390/*****************************************************************************/ 522/*****************************************************************************/
391 523
392static void 524static void
393fd_reify (void)
394{
395 int i;
396
397 for (i = 0; i < fdchangecnt; ++i)
398 {
399 int fd = fdchanges [i];
400 ANFD *anfd = anfds + fd;
401 struct ev_io *w;
402
403 int wev = 0;
404
405 for (w = anfd->head; w; w = w->next)
406 wev |= w->events;
407
408 if (anfd->wev != wev)
409 {
410 method_modify (fd, anfd->wev, wev);
411 anfd->wev = wev;
412 }
413 }
414
415 fdchangecnt = 0;
416}
417
418static void
419call_pending () 525call_pending (void)
420{ 526{
421 int i; 527 while (pendingcnt)
422
423 for (i = 0; i < pendingcnt; ++i)
424 { 528 {
425 ANPENDING *p = pendings + i; 529 ANPENDING *p = pendings + --pendingcnt;
426 530
427 if (p->w) 531 if (p->w)
428 { 532 {
429 p->w->pending = 0; 533 p->w->pending = 0;
430 p->w->cb (p->w, p->events); 534 p->w->cb (p->w, p->events);
431 } 535 }
432 } 536 }
433
434 pendingcnt = 0;
435} 537}
436 538
437static void 539static void
438timers_reify () 540timers_reify (void)
439{ 541{
440 while (timercnt && timers [0]->at <= now) 542 while (timercnt && timers [0]->at <= now)
441 { 543 {
442 struct ev_timer *w = timers [0]; 544 struct ev_timer *w = timers [0];
443
444 event ((W)w, EV_TIMEOUT);
445 545
446 /* first reschedule or stop timer */ 546 /* first reschedule or stop timer */
447 if (w->repeat) 547 if (w->repeat)
448 { 548 {
549 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
449 w->at = now + w->repeat; 550 w->at = now + w->repeat;
450 assert (("timer timeout in the past, negative repeat?", w->at > now));
451 downheap ((WT *)timers, timercnt, 0); 551 downheap ((WT *)timers, timercnt, 0);
452 } 552 }
453 else 553 else
454 evtimer_stop (w); /* nonrepeating: stop timer */ 554 ev_timer_stop (w); /* nonrepeating: stop timer */
455 }
456}
457 555
556 event ((W)w, EV_TIMEOUT);
557 }
558}
559
458static void 560static void
459periodics_reify () 561periodics_reify (void)
460{ 562{
461 while (periodiccnt && periodics [0]->at <= ev_now) 563 while (periodiccnt && periodics [0]->at <= ev_now)
462 { 564 {
463 struct ev_periodic *w = periodics [0]; 565 struct ev_periodic *w = periodics [0];
464 566
465 /* first reschedule or stop timer */ 567 /* first reschedule or stop timer */
466 if (w->interval) 568 if (w->interval)
467 { 569 {
468 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;
469 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));
470 downheap ((WT *)periodics, periodiccnt, 0); 572 downheap ((WT *)periodics, periodiccnt, 0);
471 } 573 }
472 else 574 else
473 evperiodic_stop (w); /* nonrepeating: stop timer */ 575 ev_periodic_stop (w); /* nonrepeating: stop timer */
474 576
475 event ((W)w, EV_TIMEOUT); 577 event ((W)w, EV_PERIODIC);
476 } 578 }
477} 579}
478 580
479static void 581static void
480periodics_reschedule (ev_tstamp diff) 582periodics_reschedule (ev_tstamp diff)
490 { 592 {
491 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;
492 594
493 if (fabs (diff) >= 1e-4) 595 if (fabs (diff) >= 1e-4)
494 { 596 {
495 evperiodic_stop (w); 597 ev_periodic_stop (w);
496 evperiodic_start (w); 598 ev_periodic_start (w);
497 599
498 i = 0; /* restart loop, inefficient, but time jumps should be rare */ 600 i = 0; /* restart loop, inefficient, but time jumps should be rare */
499 } 601 }
500 } 602 }
501 } 603 }
502} 604}
503 605
504static void 606static void
505time_update () 607time_update (void)
506{ 608{
507 int i; 609 int i;
508 610
509 ev_now = ev_time (); 611 ev_now = ev_time ();
510 612
544int ev_loop_done; 646int ev_loop_done;
545 647
546void ev_loop (int flags) 648void ev_loop (int flags)
547{ 649{
548 double block; 650 double block;
549 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; 651 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
550
551 if (checkcnt)
552 {
553 queue_events ((W *)checks, checkcnt, EV_CHECK);
554 call_pending ();
555 }
556 652
557 do 653 do
558 { 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
559 /* update fd-related kernel structures */ 662 /* update fd-related kernel structures */
560 fd_reify (); 663 fd_reify ();
561 664
562 /* calculate blocking time */ 665 /* calculate blocking time */
563 666
564 /* 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 */
565 ev_now = ev_time (); 669 ev_now = ev_time ();
566 670
567 if (flags & EVLOOP_NONBLOCK || idlecnt) 671 if (flags & EVLOOP_NONBLOCK || idlecnt)
568 block = 0.; 672 block = 0.;
569 else 673 else
589 693
590 /* update ev_now, do magic */ 694 /* update ev_now, do magic */
591 time_update (); 695 time_update ();
592 696
593 /* queue pending timers and reschedule them */ 697 /* queue pending timers and reschedule them */
698 timers_reify (); /* relative timers called last */
594 periodics_reify (); /* absolute timers first */ 699 periodics_reify (); /* absolute timers called first */
595 timers_reify (); /* relative timers second */
596 700
597 /* queue idle watchers unless io or timers are pending */ 701 /* queue idle watchers unless io or timers are pending */
598 if (!pendingcnt) 702 if (!pendingcnt)
599 queue_events ((W *)idles, idlecnt, EV_IDLE); 703 queue_events ((W *)idles, idlecnt, EV_IDLE);
600 704
601 /* queue check and possibly idle watchers */ 705 /* queue check watchers, to be executed first */
706 if (checkcnt)
602 queue_events ((W *)checks, checkcnt, EV_CHECK); 707 queue_events ((W *)checks, checkcnt, EV_CHECK);
603 708
604 call_pending (); 709 call_pending ();
605 } 710 }
606 while (!ev_loop_done); 711 while (!ev_loop_done);
607 712
632 head = &(*head)->next; 737 head = &(*head)->next;
633 } 738 }
634} 739}
635 740
636static void 741static void
637ev_clear (W w) 742ev_clear_pending (W w)
638{ 743{
639 if (w->pending) 744 if (w->pending)
640 { 745 {
641 pendings [w->pending - 1].w = 0; 746 pendings [w->pending - 1].w = 0;
642 w->pending = 0; 747 w->pending = 0;
656} 761}
657 762
658/*****************************************************************************/ 763/*****************************************************************************/
659 764
660void 765void
661evio_start (struct ev_io *w) 766ev_io_start (struct ev_io *w)
662{ 767{
663 if (ev_is_active (w)) 768 if (ev_is_active (w))
664 return; 769 return;
665 770
666 int fd = w->fd; 771 int fd = w->fd;
772
773 assert (("ev_io_start called with negative fd", fd >= 0));
667 774
668 ev_start ((W)w, 1); 775 ev_start ((W)w, 1);
669 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 776 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
670 wlist_add ((WL *)&anfds[fd].head, (WL)w); 777 wlist_add ((WL *)&anfds[fd].head, (WL)w);
671 778
672 ++fdchangecnt; 779 fd_change (fd);
673 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
674 fdchanges [fdchangecnt - 1] = fd;
675} 780}
676 781
677void 782void
678evio_stop (struct ev_io *w) 783ev_io_stop (struct ev_io *w)
679{ 784{
680 ev_clear ((W)w); 785 ev_clear_pending ((W)w);
681 if (!ev_is_active (w)) 786 if (!ev_is_active (w))
682 return; 787 return;
683 788
684 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 789 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
685 ev_stop ((W)w); 790 ev_stop ((W)w);
686 791
687 ++fdchangecnt; 792 fd_change (w->fd);
688 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
689 fdchanges [fdchangecnt - 1] = w->fd;
690} 793}
691 794
692void 795void
693evtimer_start (struct ev_timer *w) 796ev_timer_start (struct ev_timer *w)
694{ 797{
695 if (ev_is_active (w)) 798 if (ev_is_active (w))
696 return; 799 return;
697 800
698 w->at += now; 801 w->at += now;
699 802
700 assert (("timer repeat value less than zero not allowed", w->repeat >= 0.)); 803 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
701 804
702 ev_start ((W)w, ++timercnt); 805 ev_start ((W)w, ++timercnt);
703 array_needsize (timers, timermax, timercnt, ); 806 array_needsize (timers, timermax, timercnt, );
704 timers [timercnt - 1] = w; 807 timers [timercnt - 1] = w;
705 upheap ((WT *)timers, timercnt - 1); 808 upheap ((WT *)timers, timercnt - 1);
706} 809}
707 810
708void 811void
709evtimer_stop (struct ev_timer *w) 812ev_timer_stop (struct ev_timer *w)
710{ 813{
711 ev_clear ((W)w); 814 ev_clear_pending ((W)w);
712 if (!ev_is_active (w)) 815 if (!ev_is_active (w))
713 return; 816 return;
714 817
715 if (w->active < timercnt--) 818 if (w->active < timercnt--)
716 { 819 {
722 825
723 ev_stop ((W)w); 826 ev_stop ((W)w);
724} 827}
725 828
726void 829void
727evtimer_again (struct ev_timer *w) 830ev_timer_again (struct ev_timer *w)
728{ 831{
729 if (ev_is_active (w)) 832 if (ev_is_active (w))
730 { 833 {
731 if (w->repeat) 834 if (w->repeat)
732 { 835 {
733 w->at = now + w->repeat; 836 w->at = now + w->repeat;
734 downheap ((WT *)timers, timercnt, w->active - 1); 837 downheap ((WT *)timers, timercnt, w->active - 1);
735 } 838 }
736 else 839 else
737 evtimer_stop (w); 840 ev_timer_stop (w);
738 } 841 }
739 else if (w->repeat) 842 else if (w->repeat)
740 evtimer_start (w); 843 ev_timer_start (w);
741} 844}
742 845
743void 846void
744evperiodic_start (struct ev_periodic *w) 847ev_periodic_start (struct ev_periodic *w)
745{ 848{
746 if (ev_is_active (w)) 849 if (ev_is_active (w))
747 return; 850 return;
748 851
749 assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); 852 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
750 853
751 /* 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 */
752 if (w->interval) 855 if (w->interval)
753 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; 856 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
754 857
757 periodics [periodiccnt - 1] = w; 860 periodics [periodiccnt - 1] = w;
758 upheap ((WT *)periodics, periodiccnt - 1); 861 upheap ((WT *)periodics, periodiccnt - 1);
759} 862}
760 863
761void 864void
762evperiodic_stop (struct ev_periodic *w) 865ev_periodic_stop (struct ev_periodic *w)
763{ 866{
764 ev_clear ((W)w); 867 ev_clear_pending ((W)w);
765 if (!ev_is_active (w)) 868 if (!ev_is_active (w))
766 return; 869 return;
767 870
768 if (w->active < periodiccnt--) 871 if (w->active < periodiccnt--)
769 { 872 {
773 876
774 ev_stop ((W)w); 877 ev_stop ((W)w);
775} 878}
776 879
777void 880void
778evsignal_start (struct ev_signal *w) 881ev_signal_start (struct ev_signal *w)
779{ 882{
780 if (ev_is_active (w)) 883 if (ev_is_active (w))
781 return; 884 return;
885
886 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
782 887
783 ev_start ((W)w, 1); 888 ev_start ((W)w, 1);
784 array_needsize (signals, signalmax, w->signum, signals_init); 889 array_needsize (signals, signalmax, w->signum, signals_init);
785 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 890 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
786 891
793 sigaction (w->signum, &sa, 0); 898 sigaction (w->signum, &sa, 0);
794 } 899 }
795} 900}
796 901
797void 902void
798evsignal_stop (struct ev_signal *w) 903ev_signal_stop (struct ev_signal *w)
799{ 904{
800 ev_clear ((W)w); 905 ev_clear_pending ((W)w);
801 if (!ev_is_active (w)) 906 if (!ev_is_active (w))
802 return; 907 return;
803 908
804 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 909 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
805 ev_stop ((W)w); 910 ev_stop ((W)w);
806 911
807 if (!signals [w->signum - 1].head) 912 if (!signals [w->signum - 1].head)
808 signal (w->signum, SIG_DFL); 913 signal (w->signum, SIG_DFL);
809} 914}
810 915
916void
811void evidle_start (struct ev_idle *w) 917ev_idle_start (struct ev_idle *w)
812{ 918{
813 if (ev_is_active (w)) 919 if (ev_is_active (w))
814 return; 920 return;
815 921
816 ev_start ((W)w, ++idlecnt); 922 ev_start ((W)w, ++idlecnt);
817 array_needsize (idles, idlemax, idlecnt, ); 923 array_needsize (idles, idlemax, idlecnt, );
818 idles [idlecnt - 1] = w; 924 idles [idlecnt - 1] = w;
819} 925}
820 926
927void
821void evidle_stop (struct ev_idle *w) 928ev_idle_stop (struct ev_idle *w)
822{ 929{
823 ev_clear ((W)w); 930 ev_clear_pending ((W)w);
824 if (ev_is_active (w)) 931 if (ev_is_active (w))
825 return; 932 return;
826 933
827 idles [w->active - 1] = idles [--idlecnt]; 934 idles [w->active - 1] = idles [--idlecnt];
828 ev_stop ((W)w); 935 ev_stop ((W)w);
829} 936}
830 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
831void evcheck_start (struct ev_check *w) 961ev_check_start (struct ev_check *w)
832{ 962{
833 if (ev_is_active (w)) 963 if (ev_is_active (w))
834 return; 964 return;
835 965
836 ev_start ((W)w, ++checkcnt); 966 ev_start ((W)w, ++checkcnt);
837 array_needsize (checks, checkmax, checkcnt, ); 967 array_needsize (checks, checkmax, checkcnt, );
838 checks [checkcnt - 1] = w; 968 checks [checkcnt - 1] = w;
839} 969}
840 970
971void
841void evcheck_stop (struct ev_check *w) 972ev_check_stop (struct ev_check *w)
842{ 973{
843 ev_clear ((W)w); 974 ev_clear_pending ((W)w);
844 if (ev_is_active (w)) 975 if (ev_is_active (w))
845 return; 976 return;
846 977
847 checks [w->active - 1] = checks [--checkcnt]; 978 checks [w->active - 1] = checks [--checkcnt];
979 ev_stop ((W)w);
980}
981
982void
983ev_child_start (struct ev_child *w)
984{
985 if (ev_is_active (w))
986 return;
987
988 ev_start ((W)w, 1);
989 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
990}
991
992void
993ev_child_stop (struct ev_child *w)
994{
995 ev_clear_pending ((W)w);
996 if (ev_is_active (w))
997 return;
998
999 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
848 ev_stop ((W)w); 1000 ev_stop ((W)w);
849} 1001}
850 1002
851/*****************************************************************************/ 1003/*****************************************************************************/
852 1004
862once_cb (struct ev_once *once, int revents) 1014once_cb (struct ev_once *once, int revents)
863{ 1015{
864 void (*cb)(int revents, void *arg) = once->cb; 1016 void (*cb)(int revents, void *arg) = once->cb;
865 void *arg = once->arg; 1017 void *arg = once->arg;
866 1018
867 evio_stop (&once->io); 1019 ev_io_stop (&once->io);
868 evtimer_stop (&once->to); 1020 ev_timer_stop (&once->to);
869 free (once); 1021 free (once);
870 1022
871 cb (revents, arg); 1023 cb (revents, arg);
872} 1024}
873 1025
887ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) 1039ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
888{ 1040{
889 struct ev_once *once = malloc (sizeof (struct ev_once)); 1041 struct ev_once *once = malloc (sizeof (struct ev_once));
890 1042
891 if (!once) 1043 if (!once)
892 cb (EV_ERROR, arg); 1044 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
893 else 1045 else
894 { 1046 {
895 once->cb = cb; 1047 once->cb = cb;
896 once->arg = arg; 1048 once->arg = arg;
897 1049
898 evw_init (&once->io, once_cb_io); 1050 ev_watcher_init (&once->io, once_cb_io);
899
900 if (fd >= 0) 1051 if (fd >= 0)
901 { 1052 {
902 evio_set (&once->io, fd, events); 1053 ev_io_set (&once->io, fd, events);
903 evio_start (&once->io); 1054 ev_io_start (&once->io);
904 } 1055 }
905 1056
906 evw_init (&once->to, once_cb_to); 1057 ev_watcher_init (&once->to, once_cb_to);
907
908 if (timeout >= 0.) 1058 if (timeout >= 0.)
909 { 1059 {
910 evtimer_set (&once->to, timeout, 0.); 1060 ev_timer_set (&once->to, timeout, 0.);
911 evtimer_start (&once->to); 1061 ev_timer_start (&once->to);
912 } 1062 }
913 } 1063 }
914} 1064}
915 1065
916/*****************************************************************************/ 1066/*****************************************************************************/
927 1077
928static void 1078static void
929ocb (struct ev_timer *w, int revents) 1079ocb (struct ev_timer *w, int revents)
930{ 1080{
931 //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);
932 evtimer_stop (w); 1082 ev_timer_stop (w);
933 evtimer_start (w); 1083 ev_timer_start (w);
934} 1084}
935 1085
936static void 1086static void
937scb (struct ev_signal *w, int revents) 1087scb (struct ev_signal *w, int revents)
938{ 1088{
939 fprintf (stderr, "signal %x,%d\n", revents, w->signum); 1089 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
940 evio_stop (&wio); 1090 ev_io_stop (&wio);
941 evio_start (&wio); 1091 ev_io_start (&wio);
942} 1092}
943 1093
944static void 1094static void
945gcb (struct ev_signal *w, int revents) 1095gcb (struct ev_signal *w, int revents)
946{ 1096{
950 1100
951int main (void) 1101int main (void)
952{ 1102{
953 ev_init (0); 1103 ev_init (0);
954 1104
955 evio_init (&wio, sin_cb, 0, EV_READ); 1105 ev_io_init (&wio, sin_cb, 0, EV_READ);
956 evio_start (&wio); 1106 ev_io_start (&wio);
957 1107
958 struct ev_timer t[10000]; 1108 struct ev_timer t[10000];
959 1109
960#if 0 1110#if 0
961 int i; 1111 int i;
962 for (i = 0; i < 10000; ++i) 1112 for (i = 0; i < 10000; ++i)
963 { 1113 {
964 struct ev_timer *w = t + i; 1114 struct ev_timer *w = t + i;
965 evw_init (w, ocb, i); 1115 ev_watcher_init (w, ocb, i);
966 evtimer_init_abs (w, ocb, drand48 (), 0.99775533); 1116 ev_timer_init_abs (w, ocb, drand48 (), 0.99775533);
967 evtimer_start (w); 1117 ev_timer_start (w);
968 if (drand48 () < 0.5) 1118 if (drand48 () < 0.5)
969 evtimer_stop (w); 1119 ev_timer_stop (w);
970 } 1120 }
971#endif 1121#endif
972 1122
973 struct ev_timer t1; 1123 struct ev_timer t1;
974 evtimer_init (&t1, ocb, 5, 10); 1124 ev_timer_init (&t1, ocb, 5, 10);
975 evtimer_start (&t1); 1125 ev_timer_start (&t1);
976 1126
977 struct ev_signal sig; 1127 struct ev_signal sig;
978 evsignal_init (&sig, scb, SIGQUIT); 1128 ev_signal_init (&sig, scb, SIGQUIT);
979 evsignal_start (&sig); 1129 ev_signal_start (&sig);
980 1130
981 struct ev_check cw; 1131 struct ev_check cw;
982 evcheck_init (&cw, gcb); 1132 ev_check_init (&cw, gcb);
983 evcheck_start (&cw); 1133 ev_check_start (&cw);
984 1134
985 struct ev_idle iw; 1135 struct ev_idle iw;
986 evidle_init (&iw, gcb); 1136 ev_idle_init (&iw, gcb);
987 evidle_start (&iw); 1137 ev_idle_start (&iw);
988 1138
989 ev_loop (0); 1139 ev_loop (0);
990 1140
991 return 0; 1141 return 0;
992} 1142}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines