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

Comparing libev/ev.c (file contents):
Revision 1.9 by root, Wed Oct 31 07:24:17 2007 UTC vs.
Revision 1.27 by root, Wed Oct 31 22:16:36 2007 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines