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.21 by root, Wed Oct 31 18:37:38 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>
32#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 62#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
33#define MAX_BLOCKTIME 60. 63#define MAX_BLOCKTIME 60.
34 64
35#include "ev.h" 65#include "ev.h"
36 66
37struct ev_watcher {
38 EV_WATCHER (ev_watcher);
39};
40
41struct ev_watcher_list {
42 EV_WATCHER_LIST (ev_watcher_list);
43};
44
45typedef struct ev_watcher *W; 67typedef struct ev_watcher *W;
46typedef struct ev_watcher_list *WL; 68typedef struct ev_watcher_list *WL;
69typedef struct ev_watcher_time *WT;
47 70
48static ev_tstamp now, diff; /* monotonic clock */ 71static ev_tstamp now, diff; /* monotonic clock */
49ev_tstamp ev_now; 72ev_tstamp ev_now;
50int ev_method; 73int ev_method;
51 74
88 111
89#define array_needsize(base,cur,cnt,init) \ 112#define array_needsize(base,cur,cnt,init) \
90 if ((cnt) > cur) \ 113 if ((cnt) > cur) \
91 { \ 114 { \
92 int newcnt = cur ? cur << 1 : 16; \ 115 int newcnt = cur ? cur << 1 : 16; \
93 fprintf (stderr, "resize(" # base ") from %d to %d\n", cur, newcnt);\
94 base = realloc (base, sizeof (*base) * (newcnt)); \ 116 base = realloc (base, sizeof (*base) * (newcnt)); \
95 init (base + cur, newcnt - cur); \ 117 init (base + cur, newcnt - cur); \
96 cur = newcnt; \ 118 cur = newcnt; \
97 } 119 }
98 120
131static int pendingmax, pendingcnt; 153static int pendingmax, pendingcnt;
132 154
133static void 155static void
134event (W w, int events) 156event (W w, int events)
135{ 157{
158 if (w->active)
159 {
136 w->pending = ++pendingcnt; 160 w->pending = ++pendingcnt;
137 array_needsize (pendings, pendingmax, pendingcnt, ); 161 array_needsize (pendings, pendingmax, pendingcnt, );
138 pendings [pendingcnt - 1].w = w; 162 pendings [pendingcnt - 1].w = w;
139 pendings [pendingcnt - 1].events = events; 163 pendings [pendingcnt - 1].events = events;
164 }
140} 165}
141 166
142static void 167static void
143fd_event (int fd, int events) 168fd_event (int fd, int events)
144{ 169{
161 186
162 for (i = 0; i < eventcnt; ++i) 187 for (i = 0; i < eventcnt; ++i)
163 event (events [i], type); 188 event (events [i], type);
164} 189}
165 190
166/*****************************************************************************/ 191/* called on EBADF to verify fds */
192static void
193fd_recheck ()
194{
195 int fd;
167 196
197 for (fd = 0; fd < anfdmax; ++fd)
198 if (anfds [fd].wev)
199 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
200 while (anfds [fd].head)
201 evio_stop (anfds [fd].head);
202}
203
204/*****************************************************************************/
205
168static struct ev_timer **atimers; 206static struct ev_timer **timers;
169static int atimermax, atimercnt; 207static int timermax, timercnt;
170 208
171static struct ev_timer **rtimers; 209static struct ev_periodic **periodics;
172static int rtimermax, rtimercnt; 210static int periodicmax, periodiccnt;
173 211
174static void 212static void
175upheap (struct ev_timer **timers, int k) 213upheap (WT *timers, int k)
176{ 214{
177 struct ev_timer *w = timers [k]; 215 WT w = timers [k];
178 216
179 while (k && timers [k >> 1]->at > w->at) 217 while (k && timers [k >> 1]->at > w->at)
180 { 218 {
181 timers [k] = timers [k >> 1]; 219 timers [k] = timers [k >> 1];
182 timers [k]->active = k + 1; 220 timers [k]->active = k + 1;
187 timers [k]->active = k + 1; 225 timers [k]->active = k + 1;
188 226
189} 227}
190 228
191static void 229static void
192downheap (struct ev_timer **timers, int N, int k) 230downheap (WT *timers, int N, int k)
193{ 231{
194 struct ev_timer *w = timers [k]; 232 WT w = timers [k];
195 233
196 while (k < (N >> 1)) 234 while (k < (N >> 1))
197 { 235 {
198 int j = k << 1; 236 int j = k << 1;
199 237
285 323
286/*****************************************************************************/ 324/*****************************************************************************/
287 325
288static struct ev_idle **idles; 326static struct ev_idle **idles;
289static int idlemax, idlecnt; 327static int idlemax, idlecnt;
328
329static struct ev_prepare **prepares;
330static int preparemax, preparecnt;
290 331
291static struct ev_check **checks; 332static struct ev_check **checks;
292static int checkmax, checkcnt; 333static int checkmax, checkcnt;
293 334
294/*****************************************************************************/ 335/*****************************************************************************/
325 if (ev_method == EVMETHOD_NONE) select_init (flags); 366 if (ev_method == EVMETHOD_NONE) select_init (flags);
326#endif 367#endif
327 368
328 if (ev_method) 369 if (ev_method)
329 { 370 {
330 evw_init (&sigev, sigcb, 0); 371 evw_init (&sigev, sigcb);
331 siginit (); 372 siginit ();
332 } 373 }
333 374
334 return ev_method; 375 return ev_method;
335} 376}
336 377
337/*****************************************************************************/ 378/*****************************************************************************/
338 379
339void ev_prefork (void) 380void ev_prefork (void)
340{ 381{
382 /* nop */
341} 383}
342 384
343void ev_postfork_parent (void) 385void ev_postfork_parent (void)
344{ 386{
387 /* nop */
345} 388}
346 389
347void ev_postfork_child (void) 390void ev_postfork_child (void)
348{ 391{
349#if HAVE_EPOLL 392#if HAVE_EPOLL
387} 430}
388 431
389static void 432static void
390call_pending () 433call_pending ()
391{ 434{
392 int i; 435 while (pendingcnt)
393
394 for (i = 0; i < pendingcnt; ++i)
395 { 436 {
396 ANPENDING *p = pendings + i; 437 ANPENDING *p = pendings + --pendingcnt;
397 438
398 if (p->w) 439 if (p->w)
399 { 440 {
400 p->w->pending = 0; 441 p->w->pending = 0;
401 p->w->cb (p->w, p->events); 442 p->w->cb (p->w, p->events);
402 } 443 }
403 } 444 }
404
405 pendingcnt = 0;
406} 445}
407 446
408static void 447static void
409timers_reify (struct ev_timer **timers, int timercnt, ev_tstamp now) 448timers_reify ()
410{ 449{
411 while (timercnt && timers [0]->at <= now) 450 while (timercnt && timers [0]->at <= now)
412 { 451 {
413 struct ev_timer *w = timers [0]; 452 struct ev_timer *w = timers [0];
453
454 event ((W)w, EV_TIMEOUT);
414 455
415 /* first reschedule or stop timer */ 456 /* first reschedule or stop timer */
416 if (w->repeat) 457 if (w->repeat)
417 { 458 {
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; 459 w->at = now + w->repeat;
422 460 assert (("timer timeout in the past, negative repeat?", w->at > now));
423 assert (w->at > now);
424
425 downheap (timers, timercnt, 0); 461 downheap ((WT *)timers, timercnt, 0);
426 } 462 }
427 else 463 else
428 {
429 evtimer_stop (w); /* nonrepeating: stop timer */ 464 evtimer_stop (w); /* nonrepeating: stop timer */
430 --timercnt; /* maybe pass by reference instead? */ 465 }
466}
467
468static void
469periodics_reify ()
470{
471 while (periodiccnt && periodics [0]->at <= ev_now)
472 {
473 struct ev_periodic *w = periodics [0];
474
475 /* first reschedule or stop timer */
476 if (w->interval)
431 } 477 {
478 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval;
479 assert (("periodic timeout in the past, negative interval?", w->at > ev_now));
480 downheap ((WT *)periodics, periodiccnt, 0);
481 }
482 else
483 evperiodic_stop (w); /* nonrepeating: stop timer */
432 484
433 event ((W)w, EV_TIMEOUT); 485 event ((W)w, EV_TIMEOUT);
434 } 486 }
435} 487}
436 488
437static void 489static void
490periodics_reschedule (ev_tstamp diff)
491{
492 int i;
493
494 /* adjust periodics after time jump */
495 for (i = 0; i < periodiccnt; ++i)
496 {
497 struct ev_periodic *w = periodics [i];
498
499 if (w->interval)
500 {
501 ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval;
502
503 if (fabs (diff) >= 1e-4)
504 {
505 evperiodic_stop (w);
506 evperiodic_start (w);
507
508 i = 0; /* restart loop, inefficient, but time jumps should be rare */
509 }
510 }
511 }
512}
513
514static void
438time_update () 515time_update ()
439{ 516{
440 int i; 517 int i;
518
441 ev_now = ev_time (); 519 ev_now = ev_time ();
442 520
443 if (have_monotonic) 521 if (have_monotonic)
444 { 522 {
445 ev_tstamp odiff = diff; 523 ev_tstamp odiff = diff;
446 524
447 /* detecting time jumps is much more difficult */
448 for (i = 2; --i; ) /* loop a few times, before making important decisions */ 525 for (i = 4; --i; ) /* loop a few times, before making important decisions */
449 { 526 {
450 now = get_clock (); 527 now = get_clock ();
451 diff = ev_now - now; 528 diff = ev_now - now;
452 529
453 if (fabs (odiff - diff) < MIN_TIMEJUMP) 530 if (fabs (odiff - diff) < MIN_TIMEJUMP)
454 return; /* all is well */ 531 return; /* all is well */
455 532
456 ev_now = ev_time (); 533 ev_now = ev_time ();
457 } 534 }
458 535
459 /* time jump detected, reschedule atimers */ 536 periodics_reschedule (diff - odiff);
460 for (i = 0; i < atimercnt; ++i) 537 /* 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 } 538 }
466 else 539 else
467 { 540 {
468 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) 541 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)
469 /* time jump detected, adjust rtimers */ 542 {
543 periodics_reschedule (ev_now - now);
544
545 /* adjust timers. this is easy, as the offset is the same for all */
470 for (i = 0; i < rtimercnt; ++i) 546 for (i = 0; i < timercnt; ++i)
471 rtimers [i]->at += ev_now - now; 547 timers [i]->at += diff;
548 }
472 549
473 now = ev_now; 550 now = ev_now;
474 } 551 }
475} 552}
476 553
477int ev_loop_done; 554int ev_loop_done;
478 555
479void ev_loop (int flags) 556void ev_loop (int flags)
480{ 557{
481 double block; 558 double block;
482 ev_loop_done = flags & EVLOOP_ONESHOT; 559 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0;
483
484 if (checkcnt)
485 {
486 queue_events ((W *)checks, checkcnt, EV_CHECK);
487 call_pending ();
488 }
489 560
490 do 561 do
491 { 562 {
563 /* queue check watchers (and execute them) */
564 if (preparecnt)
565 {
566 queue_events ((W *)prepares, preparecnt, EV_PREPARE);
567 call_pending ();
568 }
569
492 /* update fd-related kernel structures */ 570 /* update fd-related kernel structures */
493 fd_reify (); 571 fd_reify ();
494 572
495 /* calculate blocking time */ 573 /* calculate blocking time */
574
575 /* we only need this for !monotonic clockor timers, but as we basically
576 always have timers, we just calculate it always */
577 ev_now = ev_time ();
578
496 if (flags & EVLOOP_NONBLOCK || idlecnt) 579 if (flags & EVLOOP_NONBLOCK || idlecnt)
497 block = 0.; 580 block = 0.;
498 else 581 else
499 { 582 {
500 block = MAX_BLOCKTIME; 583 block = MAX_BLOCKTIME;
501 584
502 if (rtimercnt) 585 if (timercnt)
503 { 586 {
504 ev_tstamp to = rtimers [0]->at - get_clock () + method_fudge; 587 ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge;
505 if (block > to) block = to; 588 if (block > to) block = to;
506 } 589 }
507 590
508 if (atimercnt) 591 if (periodiccnt)
509 { 592 {
510 ev_tstamp to = atimers [0]->at - ev_time () + method_fudge; 593 ev_tstamp to = periodics [0]->at - ev_now + method_fudge;
511 if (block > to) block = to; 594 if (block > to) block = to;
512 } 595 }
513 596
514 if (block < 0.) block = 0.; 597 if (block < 0.) block = 0.;
515 } 598 }
518 601
519 /* update ev_now, do magic */ 602 /* update ev_now, do magic */
520 time_update (); 603 time_update ();
521 604
522 /* queue pending timers and reschedule them */ 605 /* queue pending timers and reschedule them */
523 /* absolute timers first */ 606 timers_reify (); /* relative timers called last */
524 timers_reify (atimers, atimercnt, ev_now); 607 periodics_reify (); /* absolute timers called first */
525 /* relative timers second */
526 timers_reify (rtimers, rtimercnt, now);
527 608
528 /* queue idle watchers unless io or timers are pending */ 609 /* queue idle watchers unless io or timers are pending */
529 if (!pendingcnt) 610 if (!pendingcnt)
530 queue_events ((W *)idles, idlecnt, EV_IDLE); 611 queue_events ((W *)idles, idlecnt, EV_IDLE);
531 612
532 /* queue check and possibly idle watchers */ 613 /* queue check watchers, to be executed first */
614 if (checkcnt)
533 queue_events ((W *)checks, checkcnt, EV_CHECK); 615 queue_events ((W *)checks, checkcnt, EV_CHECK);
534 616
535 call_pending (); 617 call_pending ();
536 } 618 }
537 while (!ev_loop_done); 619 while (!ev_loop_done);
620
621 if (ev_loop_done != 2)
622 ev_loop_done = 0;
538} 623}
539 624
540/*****************************************************************************/ 625/*****************************************************************************/
541 626
542static void 627static void
560 head = &(*head)->next; 645 head = &(*head)->next;
561 } 646 }
562} 647}
563 648
564static void 649static void
650ev_clear (W w)
651{
652 if (w->pending)
653 {
654 pendings [w->pending - 1].w = 0;
655 w->pending = 0;
656 }
657}
658
659static void
565ev_start (W w, int active) 660ev_start (W w, int active)
566{ 661{
567 w->pending = 0;
568 w->active = active; 662 w->active = active;
569} 663}
570 664
571static void 665static void
572ev_stop (W w) 666ev_stop (W w)
573{ 667{
574 if (w->pending)
575 pendings [w->pending - 1].w = 0;
576
577 w->active = 0; 668 w->active = 0;
578 /* nop */
579} 669}
580 670
581/*****************************************************************************/ 671/*****************************************************************************/
582 672
583void 673void
598} 688}
599 689
600void 690void
601evio_stop (struct ev_io *w) 691evio_stop (struct ev_io *w)
602{ 692{
693 ev_clear ((W)w);
603 if (!ev_is_active (w)) 694 if (!ev_is_active (w))
604 return; 695 return;
605 696
606 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 697 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
607 ev_stop ((W)w); 698 ev_stop ((W)w);
615evtimer_start (struct ev_timer *w) 706evtimer_start (struct ev_timer *w)
616{ 707{
617 if (ev_is_active (w)) 708 if (ev_is_active (w))
618 return; 709 return;
619 710
620 if (w->is_abs) 711 w->at += now;
621 {
622 /* this formula differs from the one in timer_reify becuse we do not round up */
623 if (w->repeat)
624 w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat;
625 712
713 assert (("timer repeat value less than zero not allowed", w->repeat >= 0.));
714
626 ev_start ((W)w, ++atimercnt); 715 ev_start ((W)w, ++timercnt);
627 array_needsize (atimers, atimermax, atimercnt, ); 716 array_needsize (timers, timermax, timercnt, );
628 atimers [atimercnt - 1] = w; 717 timers [timercnt - 1] = w;
629 upheap (atimers, atimercnt - 1); 718 upheap ((WT *)timers, timercnt - 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} 719}
642 720
643void 721void
644evtimer_stop (struct ev_timer *w) 722evtimer_stop (struct ev_timer *w)
645{ 723{
724 ev_clear ((W)w);
646 if (!ev_is_active (w)) 725 if (!ev_is_active (w))
647 return; 726 return;
648 727
649 if (w->is_abs)
650 {
651 if (w->active < atimercnt--) 728 if (w->active < timercnt--)
652 { 729 {
653 atimers [w->active - 1] = atimers [atimercnt]; 730 timers [w->active - 1] = timers [timercnt];
731 downheap ((WT *)timers, timercnt, w->active - 1);
732 }
733
734 w->at = w->repeat;
735
736 ev_stop ((W)w);
737}
738
739void
740evtimer_again (struct ev_timer *w)
741{
742 if (ev_is_active (w))
743 {
744 if (w->repeat)
745 {
746 w->at = now + w->repeat;
654 downheap (atimers, atimercnt, w->active - 1); 747 downheap ((WT *)timers, timercnt, w->active - 1);
655 }
656 }
657 else
658 {
659 if (w->active < rtimercnt--)
660 { 748 }
661 rtimers [w->active - 1] = rtimers [rtimercnt]; 749 else
662 downheap (rtimers, rtimercnt, w->active - 1); 750 evtimer_stop (w);
663 } 751 }
752 else if (w->repeat)
753 evtimer_start (w);
754}
755
756void
757evperiodic_start (struct ev_periodic *w)
758{
759 if (ev_is_active (w))
760 return;
761
762 assert (("periodic interval value less than zero not allowed", w->interval >= 0.));
763
764 /* this formula differs from the one in periodic_reify because we do not always round up */
765 if (w->interval)
766 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
767
768 ev_start ((W)w, ++periodiccnt);
769 array_needsize (periodics, periodicmax, periodiccnt, );
770 periodics [periodiccnt - 1] = w;
771 upheap ((WT *)periodics, periodiccnt - 1);
772}
773
774void
775evperiodic_stop (struct ev_periodic *w)
776{
777 ev_clear ((W)w);
778 if (!ev_is_active (w))
779 return;
780
781 if (w->active < periodiccnt--)
782 {
783 periodics [w->active - 1] = periodics [periodiccnt];
784 downheap ((WT *)periodics, periodiccnt, w->active - 1);
664 } 785 }
665 786
666 ev_stop ((W)w); 787 ev_stop ((W)w);
667} 788}
668 789
687} 808}
688 809
689void 810void
690evsignal_stop (struct ev_signal *w) 811evsignal_stop (struct ev_signal *w)
691{ 812{
813 ev_clear ((W)w);
692 if (!ev_is_active (w)) 814 if (!ev_is_active (w))
693 return; 815 return;
694 816
695 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 817 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
696 ev_stop ((W)w); 818 ev_stop ((W)w);
709 idles [idlecnt - 1] = w; 831 idles [idlecnt - 1] = w;
710} 832}
711 833
712void evidle_stop (struct ev_idle *w) 834void evidle_stop (struct ev_idle *w)
713{ 835{
836 ev_clear ((W)w);
837 if (ev_is_active (w))
838 return;
839
714 idles [w->active - 1] = idles [--idlecnt]; 840 idles [w->active - 1] = idles [--idlecnt];
841 ev_stop ((W)w);
842}
843
844void evprepare_start (struct ev_prepare *w)
845{
846 if (ev_is_active (w))
847 return;
848
849 ev_start ((W)w, ++preparecnt);
850 array_needsize (prepares, preparemax, preparecnt, );
851 prepares [preparecnt - 1] = w;
852}
853
854void evprepare_stop (struct ev_prepare *w)
855{
856 ev_clear ((W)w);
857 if (ev_is_active (w))
858 return;
859
860 prepares [w->active - 1] = prepares [--preparecnt];
715 ev_stop ((W)w); 861 ev_stop ((W)w);
716} 862}
717 863
718void evcheck_start (struct ev_check *w) 864void evcheck_start (struct ev_check *w)
719{ 865{
725 checks [checkcnt - 1] = w; 871 checks [checkcnt - 1] = w;
726} 872}
727 873
728void evcheck_stop (struct ev_check *w) 874void evcheck_stop (struct ev_check *w)
729{ 875{
876 ev_clear ((W)w);
877 if (ev_is_active (w))
878 return;
879
730 checks [w->active - 1] = checks [--checkcnt]; 880 checks [w->active - 1] = checks [--checkcnt];
731 ev_stop ((W)w); 881 ev_stop ((W)w);
732} 882}
733 883
734/*****************************************************************************/ 884/*****************************************************************************/
735 885
886struct ev_once
887{
888 struct ev_io io;
889 struct ev_timer to;
890 void (*cb)(int revents, void *arg);
891 void *arg;
892};
893
894static void
895once_cb (struct ev_once *once, int revents)
896{
897 void (*cb)(int revents, void *arg) = once->cb;
898 void *arg = once->arg;
899
900 evio_stop (&once->io);
901 evtimer_stop (&once->to);
902 free (once);
903
904 cb (revents, arg);
905}
906
907static void
908once_cb_io (struct ev_io *w, int revents)
909{
910 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents);
911}
912
913static void
914once_cb_to (struct ev_timer *w, int revents)
915{
916 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents);
917}
918
919void
920ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
921{
922 struct ev_once *once = malloc (sizeof (struct ev_once));
923
924 if (!once)
925 cb (EV_ERROR, arg);
926 else
927 {
928 once->cb = cb;
929 once->arg = arg;
930
931 evw_init (&once->io, once_cb_io);
932
933 if (fd >= 0)
934 {
935 evio_set (&once->io, fd, events);
936 evio_start (&once->io);
937 }
938
939 evw_init (&once->to, once_cb_to);
940
941 if (timeout >= 0.)
942 {
943 evtimer_set (&once->to, timeout, 0.);
944 evtimer_start (&once->to);
945 }
946 }
947}
948
949/*****************************************************************************/
950
736#if 0 951#if 0
952
953struct ev_io wio;
737 954
738static void 955static void
739sin_cb (struct ev_io *w, int revents) 956sin_cb (struct ev_io *w, int revents)
740{ 957{
741 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); 958 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
751 968
752static void 969static void
753scb (struct ev_signal *w, int revents) 970scb (struct ev_signal *w, int revents)
754{ 971{
755 fprintf (stderr, "signal %x,%d\n", revents, w->signum); 972 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
973 evio_stop (&wio);
974 evio_start (&wio);
756} 975}
757 976
758static void 977static void
759gcb (struct ev_signal *w, int revents) 978gcb (struct ev_signal *w, int revents)
760{ 979{
761 fprintf (stderr, "generic %x\n", revents); 980 fprintf (stderr, "generic %x\n", revents);
981
762} 982}
763 983
764int main (void) 984int main (void)
765{ 985{
766 struct ev_io sin;
767
768 ev_init (0); 986 ev_init (0);
769 987
770 evw_init (&sin, sin_cb, 55);
771 evio_set (&sin, 0, EV_READ); 988 evio_init (&wio, sin_cb, 0, EV_READ);
772 evio_start (&sin); 989 evio_start (&wio);
773 990
774 struct ev_timer t[10000]; 991 struct ev_timer t[10000];
775 992
776#if 0 993#if 0
777 int i; 994 int i;
778 for (i = 0; i < 10000; ++i) 995 for (i = 0; i < 10000; ++i)
779 { 996 {
780 struct ev_timer *w = t + i; 997 struct ev_timer *w = t + i;
781 evw_init (w, ocb, i); 998 evw_init (w, ocb, i);
782 evtimer_set_abs (w, drand48 (), 0.99775533); 999 evtimer_init_abs (w, ocb, drand48 (), 0.99775533);
783 evtimer_start (w); 1000 evtimer_start (w);
784 if (drand48 () < 0.5) 1001 if (drand48 () < 0.5)
785 evtimer_stop (w); 1002 evtimer_stop (w);
786 } 1003 }
787#endif 1004#endif
788 1005
789 struct ev_timer t1; 1006 struct ev_timer t1;
790 evw_init (&t1, ocb, 0); 1007 evtimer_init (&t1, ocb, 5, 10);
791 evtimer_set_abs (&t1, 5, 10);
792 evtimer_start (&t1); 1008 evtimer_start (&t1);
793 1009
794 struct ev_signal sig; 1010 struct ev_signal sig;
795 evw_init (&sig, scb, 65535);
796 evsignal_set (&sig, SIGQUIT); 1011 evsignal_init (&sig, scb, SIGQUIT);
797 evsignal_start (&sig); 1012 evsignal_start (&sig);
798 1013
799 struct ev_check cw; 1014 struct ev_check cw;
800 evw_init (&cw, gcb, 0); 1015 evcheck_init (&cw, gcb);
801 evcheck_start (&cw); 1016 evcheck_start (&cw);
802 1017
803 struct ev_idle iw; 1018 struct ev_idle iw;
804 evw_init (&iw, gcb, 0); 1019 evidle_init (&iw, gcb);
805 evidle_start (&iw); 1020 evidle_start (&iw);
806 1021
807 ev_loop (0); 1022 ev_loop (0);
808 1023
809 return 0; 1024 return 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines