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

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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines