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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines