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

Comparing libev/ev.c (file contents):
Revision 1.8 by root, Wed Oct 31 00:32:33 2007 UTC vs.
Revision 1.28 by root, Thu Nov 1 06:48:49 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{ 204{
103 while (count--) 205 int i;
104 { 206
105 base->head = 0; 207 for (i = 0; i < fdchangecnt; ++i)
106 base->wev = base->rev = EV_NONE;
107 ++base;
108 } 208 {
109} 209 int fd = fdchanges [i];
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; 210 ANFD *anfd = anfds + fd;
133 struct ev_io *w; 211 struct ev_io *w;
134 212
213 int events = 0;
214
135 for (w = anfd->head; w; w = w->next) 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 }
136 { 225 }
137 int ev = w->events & events;
138 226
139 if (ev) 227 fdchangecnt = 0;
140 event ((struct ev_watcher *)w, ev);
141 }
142} 228}
143 229
144/*****************************************************************************/ 230static void
231fd_change (int fd)
232{
233 if (anfds [fd].events & EV_REIFY)
234 return;
145 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 ev_io_stop (anfds [fd].head);
256 }
257}
258
259/*****************************************************************************/
260
146static struct ev_timer **atimers; 261static struct ev_timer **timers;
147static int atimermax, atimercnt; 262static int timermax, timercnt;
148 263
149static struct ev_timer **rtimers; 264static struct ev_periodic **periodics;
150static int rtimermax, rtimercnt; 265static int periodicmax, periodiccnt;
151 266
152static void 267static void
153upheap (struct ev_timer **timers, int k) 268upheap (WT *timers, int k)
154{ 269{
155 struct ev_timer *w = timers [k]; 270 WT w = timers [k];
156 271
157 while (k && timers [k >> 1]->at > w->at) 272 while (k && timers [k >> 1]->at > w->at)
158 { 273 {
159 timers [k] = timers [k >> 1]; 274 timers [k] = timers [k >> 1];
160 timers [k]->active = k + 1; 275 timers [k]->active = k + 1;
165 timers [k]->active = k + 1; 280 timers [k]->active = k + 1;
166 281
167} 282}
168 283
169static void 284static void
170downheap (struct ev_timer **timers, int N, int k) 285downheap (WT *timers, int N, int k)
171{ 286{
172 struct ev_timer *w = timers [k]; 287 WT w = timers [k];
173 288
174 while (k < (N >> 1)) 289 while (k < (N >> 1))
175 { 290 {
176 int j = k << 1; 291 int j = k << 1;
177 292
241 if (signals [sig].gotsig) 356 if (signals [sig].gotsig)
242 { 357 {
243 signals [sig].gotsig = 0; 358 signals [sig].gotsig = 0;
244 359
245 for (w = signals [sig].head; w; w = w->next) 360 for (w = signals [sig].head; w; w = w->next)
246 event ((struct ev_watcher *)w, EV_SIGNAL); 361 event ((W)w, EV_SIGNAL);
247 } 362 }
248} 363}
249 364
250static void 365static void
251siginit (void) 366siginit (void)
255 370
256 /* rather than sort out wether we really need nb, set it */ 371 /* rather than sort out wether we really need nb, set it */
257 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); 372 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
258 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 373 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
259 374
260 evio_set (&sigev, sigpipe [0], EV_READ); 375 ev_io_set (&sigev, sigpipe [0], EV_READ);
261 evio_start (&sigev); 376 ev_io_start (&sigev);
377}
378
379/*****************************************************************************/
380
381static struct ev_idle **idles;
382static int idlemax, idlecnt;
383
384static struct ev_prepare **prepares;
385static int preparemax, preparecnt;
386
387static struct ev_check **checks;
388static int checkmax, checkcnt;
389
390/*****************************************************************************/
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 }
262} 412}
263 413
264/*****************************************************************************/ 414/*****************************************************************************/
265 415
266#if HAVE_EPOLL 416#if HAVE_EPOLL
268#endif 418#endif
269#if HAVE_SELECT 419#if HAVE_SELECT
270# include "ev_select.c" 420# include "ev_select.c"
271#endif 421#endif
272 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
273int ev_init (int flags) 435int ev_init (int flags)
274{ 436{
437 if (!ev_method)
438 {
275#if HAVE_MONOTONIC 439#if HAVE_MONOTONIC
276 { 440 {
277 struct timespec ts; 441 struct timespec ts;
278 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 442 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
279 have_monotonic = 1; 443 have_monotonic = 1;
280 } 444 }
281#endif 445#endif
282 446
283 ev_now = ev_time (); 447 ev_now = ev_time ();
284 now = get_clock (); 448 now = get_clock ();
285 diff = ev_now - now; 449 diff = ev_now - now;
286 450
287 if (pipe (sigpipe)) 451 if (pipe (sigpipe))
288 return 0; 452 return 0;
289 453
290 ev_method = EVMETHOD_NONE; 454 ev_method = EVMETHOD_NONE;
291#if HAVE_EPOLL 455#if HAVE_EPOLL
292 if (ev_method == EVMETHOD_NONE) epoll_init (flags); 456 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
293#endif 457#endif
294#if HAVE_SELECT 458#if HAVE_SELECT
295 if (ev_method == EVMETHOD_NONE) select_init (flags); 459 if (ev_method == EVMETHOD_NONE) select_init (flags);
296#endif 460#endif
297 461
298 if (ev_method) 462 if (ev_method)
299 { 463 {
300 evw_init (&sigev, sigcb, 0); 464 ev_watcher_init (&sigev, sigcb);
301 siginit (); 465 siginit ();
466
467 ev_signal_init (&childev, childcb, SIGCHLD);
468 ev_signal_start (&childev);
469 }
302 } 470 }
303 471
304 return ev_method; 472 return ev_method;
305} 473}
306 474
307/*****************************************************************************/ 475/*****************************************************************************/
308 476
477void
309void ev_prefork (void) 478ev_prefork (void)
310{ 479{
480 /* nop */
311} 481}
312 482
483void
313void ev_postfork_parent (void) 484ev_postfork_parent (void)
314{ 485{
486 /* nop */
315} 487}
316 488
489void
317void ev_postfork_child (void) 490ev_postfork_child (void)
318{ 491{
319#if HAVE_EPOLL 492#if HAVE_EPOLL
320 if (ev_method == EVMETHOD_EPOLL) 493 if (ev_method == EVMETHOD_EPOLL)
321 epoll_postfork_child (); 494 epoll_postfork_child ();
322#endif 495#endif
323 496
324 evio_stop (&sigev); 497 ev_io_stop (&sigev);
325 close (sigpipe [0]); 498 close (sigpipe [0]);
326 close (sigpipe [1]); 499 close (sigpipe [1]);
327 pipe (sigpipe); 500 pipe (sigpipe);
328 siginit (); 501 siginit ();
329} 502}
330 503
331/*****************************************************************************/ 504/*****************************************************************************/
332 505
333static ev_hook hooks [EVHOOK_NUM];
334
335void
336ev_hook_register (int type, ev_hook hook)
337{
338 hooks [type] = hook;
339}
340
341void
342ev_hook_unregister (int type, ev_hook hook)
343{
344 hooks [type] = 0;
345}
346
347static void 506static void
348hook_call (int type)
349{
350 if (hooks [type])
351 hooks [type] ();
352}
353
354static void
355fd_reify (void)
356{
357 int i;
358
359 for (i = 0; i < fdchangecnt; ++i)
360 {
361 int fd = fdchanges [i];
362 ANFD *anfd = anfds + fd;
363 struct ev_io *w;
364
365 int wev = 0;
366
367 for (w = anfd->head; w; w = w->next)
368 wev |= w->events;
369
370 if (anfd->wev != wev)
371 {
372 method_modify (fd, anfd->wev, wev);
373 anfd->wev = wev;
374 }
375 }
376
377 fdchangecnt = 0;
378}
379
380static void
381call_pending () 507call_pending (void)
382{ 508{
383 int i; 509 while (pendingcnt)
384
385 for (i = 0; i < pendingcnt; ++i)
386 { 510 {
387 ANPENDING *p = pendings + i; 511 ANPENDING *p = pendings + --pendingcnt;
388 512
389 if (p->w) 513 if (p->w)
390 { 514 {
391 p->w->pending = 0; 515 p->w->pending = 0;
392 p->w->cb (p->w, p->events); 516 p->w->cb (p->w, p->events);
393 } 517 }
394 } 518 }
395
396 pendingcnt = 0;
397} 519}
398 520
399static void 521static void
400timers_reify (struct ev_timer **timers, int timercnt, ev_tstamp now) 522timers_reify (void)
401{ 523{
402 while (timercnt && timers [0]->at <= now) 524 while (timercnt && timers [0]->at <= now)
403 { 525 {
404 struct ev_timer *w = timers [0]; 526 struct ev_timer *w = timers [0];
527
528 event ((W)w, EV_TIMEOUT);
405 529
406 /* first reschedule or stop timer */ 530 /* first reschedule or stop timer */
407 if (w->repeat) 531 if (w->repeat)
408 { 532 {
409 if (w->is_abs)
410 w->at += floor ((now - w->at) / w->repeat + 1.) * w->repeat;
411 else
412 w->at = now + w->repeat; 533 w->at = now + w->repeat;
413 534 assert (("timer timeout in the past, negative repeat?", w->at > now));
414 assert (w->at > now);
415
416 downheap (timers, timercnt, 0); 535 downheap ((WT *)timers, timercnt, 0);
417 } 536 }
418 else 537 else
419 {
420 evtimer_stop (w); /* nonrepeating: stop timer */ 538 ev_timer_stop (w); /* nonrepeating: stop timer */
421 --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)
422 } 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 ev_periodic_stop (w); /* nonrepeating: stop timer */
423 558
424 event ((struct ev_watcher *)w, EV_TIMEOUT); 559 event ((W)w, EV_TIMEOUT);
425 } 560 }
426} 561}
427 562
428static void 563static void
429time_update () 564periodics_reschedule (ev_tstamp diff)
430{ 565{
431 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 ev_periodic_stop (w);
580 ev_periodic_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
432 ev_now = ev_time (); 593 ev_now = ev_time ();
433 594
434 if (have_monotonic) 595 if (have_monotonic)
435 { 596 {
436 ev_tstamp odiff = diff; 597 ev_tstamp odiff = diff;
437 598
438 /* detecting time jumps is much more difficult */
439 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 */
440 { 600 {
441 now = get_clock (); 601 now = get_clock ();
442 diff = ev_now - now; 602 diff = ev_now - now;
443 603
444 if (fabs (odiff - diff) < MIN_TIMEJUMP) 604 if (fabs (odiff - diff) < MIN_TIMEJUMP)
445 return; /* all is well */ 605 return; /* all is well */
446 606
447 ev_now = ev_time (); 607 ev_now = ev_time ();
448 } 608 }
449 609
450 /* time jump detected, reschedule atimers */ 610 periodics_reschedule (diff - odiff);
451 for (i = 0; i < atimercnt; ++i) 611 /* no timer adjustment, as the monotonic clock doesn't jump */
452 {
453 struct ev_timer *w = atimers [i];
454 w->at += ceil ((ev_now - w->at) / w->repeat + 1.) * w->repeat;
455 }
456 } 612 }
457 else 613 else
458 { 614 {
459 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) 615 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)
460 /* 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 */
461 for (i = 0; i < rtimercnt; ++i) 620 for (i = 0; i < timercnt; ++i)
462 rtimers [i]->at += ev_now - now; 621 timers [i]->at += diff;
622 }
463 623
464 now = ev_now; 624 now = ev_now;
465 } 625 }
466} 626}
467 627
468int ev_loop_done; 628int ev_loop_done;
469 629
470void ev_loop (int flags) 630void ev_loop (int flags)
471{ 631{
472 double block; 632 double block;
473 ev_loop_done = flags & EVLOOP_ONESHOT; 633 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
474 634
475 do 635 do
476 { 636 {
477 hook_call (EVHOOK_PREPOLL); 637 /* queue check watchers (and execute them) */
638 if (preparecnt)
639 {
640 queue_events ((W *)prepares, preparecnt, EV_PREPARE);
641 call_pending ();
642 }
478 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) 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 }
504 method_poll (block); 674 method_poll (block);
505 675
506 /* update ev_now, do magic */ 676 /* update ev_now, do magic */
507 time_update (); 677 time_update ();
508 678
509 hook_call (EVHOOK_POSTPOLL);
510
511 /* put pending timers into pendign queue and reschedule them */ 679 /* queue pending timers and reschedule them */
512 /* absolute timers first */ 680 timers_reify (); /* relative timers called last */
513 timers_reify (atimers, atimercnt, ev_now); 681 periodics_reify (); /* absolute timers called first */
514 /* relative timers second */ 682
515 timers_reify (rtimers, rtimercnt, now); 683 /* queue idle watchers unless io or timers are pending */
684 if (!pendingcnt)
685 queue_events ((W *)idles, idlecnt, EV_IDLE);
686
687 /* queue check watchers, to be executed first */
688 if (checkcnt)
689 queue_events ((W *)checks, checkcnt, EV_CHECK);
516 690
517 call_pending (); 691 call_pending ();
518 } 692 }
519 while (!ev_loop_done); 693 while (!ev_loop_done);
520}
521 694
522/*****************************************************************************/ 695 if (ev_loop_done != 2)
696 ev_loop_done = 0;
697}
523 698
699/*****************************************************************************/
700
524static void 701static void
525wlist_add (struct ev_watcher_list **head, struct ev_watcher_list *elem) 702wlist_add (WL *head, WL elem)
526{ 703{
527 elem->next = *head; 704 elem->next = *head;
528 *head = elem; 705 *head = elem;
529} 706}
530 707
531static void 708static void
532wlist_del (struct ev_watcher_list **head, struct ev_watcher_list *elem) 709wlist_del (WL *head, WL elem)
533{ 710{
534 while (*head) 711 while (*head)
535 { 712 {
536 if (*head == elem) 713 if (*head == elem)
537 { 714 {
542 head = &(*head)->next; 719 head = &(*head)->next;
543 } 720 }
544} 721}
545 722
546static void 723static void
547ev_start (struct ev_watcher *w, int active) 724ev_clear (W w)
548{ 725{
726 if (w->pending)
727 {
728 pendings [w->pending - 1].w = 0;
549 w->pending = 0; 729 w->pending = 0;
730 }
731}
732
733static void
734ev_start (W w, int active)
735{
550 w->active = active; 736 w->active = active;
551} 737}
552 738
553static void 739static void
554ev_stop (struct ev_watcher *w) 740ev_stop (W w)
555{ 741{
556 if (w->pending)
557 pendings [w->pending - 1].w = 0;
558
559 w->active = 0; 742 w->active = 0;
560 /* nop */
561} 743}
562 744
563/*****************************************************************************/ 745/*****************************************************************************/
564 746
565void 747void
566evio_start (struct ev_io *w) 748ev_io_start (struct ev_io *w)
567{ 749{
568 if (ev_is_active (w)) 750 if (ev_is_active (w))
569 return; 751 return;
570 752
571 int fd = w->fd; 753 int fd = w->fd;
572 754
573 ev_start ((struct ev_watcher *)w, 1); 755 ev_start ((W)w, 1);
574 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 756 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
575 wlist_add ((struct ev_watcher_list **)&anfds[fd].head, (struct ev_watcher_list *)w); 757 wlist_add ((WL *)&anfds[fd].head, (WL)w);
576 758
577 ++fdchangecnt; 759 fd_change (fd);
578 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
579 fdchanges [fdchangecnt - 1] = fd;
580} 760}
581 761
582void 762void
583evio_stop (struct ev_io *w) 763ev_io_stop (struct ev_io *w)
584{ 764{
765 ev_clear ((W)w);
585 if (!ev_is_active (w)) 766 if (!ev_is_active (w))
586 return; 767 return;
587 768
588 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);
589 ev_stop ((struct ev_watcher *)w); 770 ev_stop ((W)w);
590 771
591 ++fdchangecnt; 772 fd_change (w->fd);
592 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
593 fdchanges [fdchangecnt - 1] = w->fd;
594} 773}
595 774
596void 775void
597evtimer_start (struct ev_timer *w) 776ev_timer_start (struct ev_timer *w)
598{ 777{
778 if (ev_is_active (w))
779 return;
780
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
792ev_timer_stop (struct ev_timer *w)
793{
794 ev_clear ((W)w);
599 if (ev_is_active (w)) 795 if (!ev_is_active (w))
600 return; 796 return;
601 797
602 if (w->is_abs) 798 if (w->active < timercnt--)
799 {
800 timers [w->active - 1] = timers [timercnt];
801 downheap ((WT *)timers, timercnt, w->active - 1);
603 { 802 }
604 /* 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
810ev_timer_again (struct ev_timer *w)
811{
812 if (ev_is_active (w))
813 {
605 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 ev_timer_stop (w);
821 }
822 else if (w->repeat)
823 ev_timer_start (w);
824}
825
826void
827ev_periodic_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)
606 w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat; 836 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
607 837
608 ev_start ((struct ev_watcher *)w, ++atimercnt); 838 ev_start ((W)w, ++periodiccnt);
609 array_needsize (atimers, atimermax, atimercnt, ); 839 array_needsize (periodics, periodicmax, periodiccnt, );
610 atimers [atimercnt - 1] = w; 840 periodics [periodiccnt - 1] = w;
611 upheap (atimers, atimercnt - 1); 841 upheap ((WT *)periodics, periodiccnt - 1);
612 }
613 else
614 {
615 w->at += now;
616
617 ev_start ((struct ev_watcher *)w, ++rtimercnt);
618 array_needsize (rtimers, rtimermax, rtimercnt, );
619 rtimers [rtimercnt - 1] = w;
620 upheap (rtimers, rtimercnt - 1);
621 }
622
623} 842}
624 843
625void 844void
626evtimer_stop (struct ev_timer *w) 845ev_periodic_stop (struct ev_periodic *w)
627{ 846{
847 ev_clear ((W)w);
628 if (!ev_is_active (w)) 848 if (!ev_is_active (w))
629 return; 849 return;
630 850
631 if (w->is_abs)
632 {
633 if (w->active < atimercnt--) 851 if (w->active < periodiccnt--)
634 {
635 atimers [w->active - 1] = atimers [atimercnt];
636 downheap (atimers, atimercnt, w->active - 1);
637 }
638 } 852 {
639 else 853 periodics [w->active - 1] = periodics [periodiccnt];
854 downheap ((WT *)periodics, periodiccnt, w->active - 1);
640 { 855 }
641 if (w->active < rtimercnt--)
642 {
643 rtimers [w->active - 1] = rtimers [rtimercnt];
644 downheap (rtimers, rtimercnt, w->active - 1);
645 }
646 }
647 856
648 ev_stop ((struct ev_watcher *)w); 857 ev_stop ((W)w);
649} 858}
650 859
651void 860void
652evsignal_start (struct ev_signal *w) 861ev_signal_start (struct ev_signal *w)
653{ 862{
654 if (ev_is_active (w)) 863 if (ev_is_active (w))
655 return; 864 return;
656 865
657 ev_start ((struct ev_watcher *)w, 1); 866 ev_start ((W)w, 1);
658 array_needsize (signals, signalmax, w->signum, signals_init); 867 array_needsize (signals, signalmax, w->signum, signals_init);
659 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);
660 869
661 if (!w->next) 870 if (!w->next)
662 { 871 {
663 struct sigaction sa; 872 struct sigaction sa;
664 sa.sa_handler = sighandler; 873 sa.sa_handler = sighandler;
667 sigaction (w->signum, &sa, 0); 876 sigaction (w->signum, &sa, 0);
668 } 877 }
669} 878}
670 879
671void 880void
672evsignal_stop (struct ev_signal *w) 881ev_signal_stop (struct ev_signal *w)
673{ 882{
883 ev_clear ((W)w);
674 if (!ev_is_active (w)) 884 if (!ev_is_active (w))
675 return; 885 return;
676 886
677 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);
678 ev_stop ((struct ev_watcher *)w); 888 ev_stop ((W)w);
679 889
680 if (!signals [w->signum - 1].head) 890 if (!signals [w->signum - 1].head)
681 signal (w->signum, SIG_DFL); 891 signal (w->signum, SIG_DFL);
682} 892}
683 893
894void
895ev_idle_start (struct ev_idle *w)
896{
897 if (ev_is_active (w))
898 return;
899
900 ev_start ((W)w, ++idlecnt);
901 array_needsize (idles, idlemax, idlecnt, );
902 idles [idlecnt - 1] = w;
903}
904
905void
906ev_idle_stop (struct ev_idle *w)
907{
908 ev_clear ((W)w);
909 if (ev_is_active (w))
910 return;
911
912 idles [w->active - 1] = idles [--idlecnt];
913 ev_stop ((W)w);
914}
915
916void
917ev_prepare_start (struct ev_prepare *w)
918{
919 if (ev_is_active (w))
920 return;
921
922 ev_start ((W)w, ++preparecnt);
923 array_needsize (prepares, preparemax, preparecnt, );
924 prepares [preparecnt - 1] = w;
925}
926
927void
928ev_prepare_stop (struct ev_prepare *w)
929{
930 ev_clear ((W)w);
931 if (ev_is_active (w))
932 return;
933
934 prepares [w->active - 1] = prepares [--preparecnt];
935 ev_stop ((W)w);
936}
937
938void
939ev_check_start (struct ev_check *w)
940{
941 if (ev_is_active (w))
942 return;
943
944 ev_start ((W)w, ++checkcnt);
945 array_needsize (checks, checkmax, checkcnt, );
946 checks [checkcnt - 1] = w;
947}
948
949void
950ev_check_stop (struct ev_check *w)
951{
952 ev_clear ((W)w);
953 if (ev_is_active (w))
954 return;
955
956 checks [w->active - 1] = checks [--checkcnt];
957 ev_stop ((W)w);
958}
959
960void
961ev_child_start (struct ev_child *w)
962{
963 if (ev_is_active (w))
964 return;
965
966 ev_start ((W)w, 1);
967 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
968}
969
970void
971ev_child_stop (struct ev_child *w)
972{
973 ev_clear ((W)w);
974 if (ev_is_active (w))
975 return;
976
977 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
978 ev_stop ((W)w);
979}
980
684/*****************************************************************************/ 981/*****************************************************************************/
982
983struct ev_once
984{
985 struct ev_io io;
986 struct ev_timer to;
987 void (*cb)(int revents, void *arg);
988 void *arg;
989};
990
991static void
992once_cb (struct ev_once *once, int revents)
993{
994 void (*cb)(int revents, void *arg) = once->cb;
995 void *arg = once->arg;
996
997 ev_io_stop (&once->io);
998 ev_timer_stop (&once->to);
999 free (once);
1000
1001 cb (revents, arg);
1002}
1003
1004static void
1005once_cb_io (struct ev_io *w, int revents)
1006{
1007 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents);
1008}
1009
1010static void
1011once_cb_to (struct ev_timer *w, int revents)
1012{
1013 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents);
1014}
1015
1016void
1017ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
1018{
1019 struct ev_once *once = malloc (sizeof (struct ev_once));
1020
1021 if (!once)
1022 cb (EV_ERROR, arg);
1023 else
1024 {
1025 once->cb = cb;
1026 once->arg = arg;
1027
1028 ev_watcher_init (&once->io, once_cb_io);
1029
1030 if (fd >= 0)
1031 {
1032 ev_io_set (&once->io, fd, events);
1033 ev_io_start (&once->io);
1034 }
1035
1036 ev_watcher_init (&once->to, once_cb_to);
1037
1038 if (timeout >= 0.)
1039 {
1040 ev_timer_set (&once->to, timeout, 0.);
1041 ev_timer_start (&once->to);
1042 }
1043 }
1044}
1045
1046/*****************************************************************************/
1047
685#if 1 1048#if 0
1049
1050struct ev_io wio;
686 1051
687static void 1052static void
688sin_cb (struct ev_io *w, int revents) 1053sin_cb (struct ev_io *w, int revents)
689{ 1054{
690 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); 1055 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
692 1057
693static void 1058static void
694ocb (struct ev_timer *w, int revents) 1059ocb (struct ev_timer *w, int revents)
695{ 1060{
696 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); 1061 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data);
697 evtimer_stop (w); 1062 ev_timer_stop (w);
698 evtimer_start (w); 1063 ev_timer_start (w);
699} 1064}
700 1065
701static void 1066static void
702scb (struct ev_signal *w, int revents) 1067scb (struct ev_signal *w, int revents)
703{ 1068{
704 fprintf (stderr, "signal %x,%d\n", revents, w->signum); 1069 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
1070 ev_io_stop (&wio);
1071 ev_io_start (&wio);
1072}
1073
1074static void
1075gcb (struct ev_signal *w, int revents)
1076{
1077 fprintf (stderr, "generic %x\n", revents);
1078
705} 1079}
706 1080
707int main (void) 1081int main (void)
708{ 1082{
709 struct ev_io sin;
710
711 ev_init (0); 1083 ev_init (0);
712 1084
713 evw_init (&sin, sin_cb, 55);
714 evio_set (&sin, 0, EV_READ); 1085 ev_io_init (&wio, sin_cb, 0, EV_READ);
715 evio_start (&sin); 1086 ev_io_start (&wio);
716 1087
717 struct ev_timer t[10000]; 1088 struct ev_timer t[10000];
718 1089
719#if 1 1090#if 0
720 int i; 1091 int i;
721 for (i = 0; i < 10000; ++i) 1092 for (i = 0; i < 10000; ++i)
722 { 1093 {
723 struct ev_timer *w = t + i; 1094 struct ev_timer *w = t + i;
724 evw_init (w, ocb, i); 1095 ev_watcher_init (w, ocb, i);
725 evtimer_set_abs (w, drand48 (), 0.99775533); 1096 ev_timer_init_abs (w, ocb, drand48 (), 0.99775533);
726 evtimer_start (w); 1097 ev_timer_start (w);
727 if (drand48 () < 0.5) 1098 if (drand48 () < 0.5)
728 evtimer_stop (w); 1099 ev_timer_stop (w);
729 } 1100 }
730#endif 1101#endif
731 1102
732 struct ev_timer t1; 1103 struct ev_timer t1;
733 evw_init (&t1, ocb, 0); 1104 ev_timer_init (&t1, ocb, 5, 10);
734 evtimer_set_abs (&t1, 5, 10);
735 evtimer_start (&t1); 1105 ev_timer_start (&t1);
736 1106
737 struct ev_signal sig; 1107 struct ev_signal sig;
738 evw_init (&sig, scb, 65535);
739 evsignal_set (&sig, SIGQUIT); 1108 ev_signal_init (&sig, scb, SIGQUIT);
740 evsignal_start (&sig); 1109 ev_signal_start (&sig);
1110
1111 struct ev_check cw;
1112 ev_check_init (&cw, gcb);
1113 ev_check_start (&cw);
1114
1115 struct ev_idle iw;
1116 ev_idle_init (&iw, gcb);
1117 ev_idle_start (&iw);
741 1118
742 ev_loop (0); 1119 ev_loop (0);
743 1120
744 return 0; 1121 return 0;
745} 1122}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines