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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines