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

Comparing libev/ev.c (file contents):
Revision 1.11 by root, Wed Oct 31 07:40:49 2007 UTC vs.
Revision 1.20 by root, Wed Oct 31 18:28:00 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}
389} 430}
390 431
391static void 432static void
392call_pending () 433call_pending ()
393{ 434{
394 int i; 435 while (pendingcnt)
395
396 for (i = 0; i < pendingcnt; ++i)
397 { 436 {
398 ANPENDING *p = pendings + i; 437 ANPENDING *p = pendings + --pendingcnt;
399 438
400 if (p->w) 439 if (p->w)
401 { 440 {
402 p->w->pending = 0; 441 p->w->pending = 0;
403 p->w->cb (p->w, p->events); 442 p->w->cb (p->w, p->events);
404 } 443 }
405 } 444 }
406
407 pendingcnt = 0;
408} 445}
409 446
410static void 447static void
411timers_reify (struct ev_timer **timers, int timercnt, ev_tstamp now) 448timers_reify ()
412{ 449{
413 while (timercnt && timers [0]->at <= now) 450 while (timercnt && timers [0]->at <= now)
414 { 451 {
415 struct ev_timer *w = timers [0]; 452 struct ev_timer *w = timers [0];
453
454 event ((W)w, EV_TIMEOUT);
416 455
417 /* first reschedule or stop timer */ 456 /* first reschedule or stop timer */
418 if (w->repeat) 457 if (w->repeat)
419 { 458 {
420 if (w->is_abs)
421 w->at += floor ((now - w->at) / w->repeat + 1.) * w->repeat;
422 else
423 w->at = now + w->repeat; 459 w->at = now + w->repeat;
424 460 assert (("timer timeout in the past, negative repeat?", w->at > now));
425 assert (w->at > now);
426
427 downheap (timers, timercnt, 0); 461 downheap ((WT *)timers, timercnt, 0);
428 } 462 }
429 else 463 else
430 {
431 evtimer_stop (w); /* nonrepeating: stop timer */ 464 evtimer_stop (w); /* nonrepeating: stop timer */
432 --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)
433 } 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 */
434 484
435 event ((W)w, EV_TIMEOUT); 485 event ((W)w, EV_TIMEOUT);
436 } 486 }
437} 487}
438 488
439static 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
440time_update () 515time_update ()
441{ 516{
442 int i; 517 int i;
518
443 ev_now = ev_time (); 519 ev_now = ev_time ();
444 520
445 if (have_monotonic) 521 if (have_monotonic)
446 { 522 {
447 ev_tstamp odiff = diff; 523 ev_tstamp odiff = diff;
448 524
449 /* detecting time jumps is much more difficult */
450 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 */
451 { 526 {
452 now = get_clock (); 527 now = get_clock ();
453 diff = ev_now - now; 528 diff = ev_now - now;
454 529
455 if (fabs (odiff - diff) < MIN_TIMEJUMP) 530 if (fabs (odiff - diff) < MIN_TIMEJUMP)
456 return; /* all is well */ 531 return; /* all is well */
457 532
458 ev_now = ev_time (); 533 ev_now = ev_time ();
459 } 534 }
460 535
461 /* time jump detected, reschedule atimers */ 536 periodics_reschedule (diff - odiff);
462 for (i = 0; i < atimercnt; ++i) 537 /* no timer adjustment, as the monotonic clock doesn't jump */
463 {
464 struct ev_timer *w = atimers [i];
465 w->at += ceil ((ev_now - w->at) / w->repeat + 1.) * w->repeat;
466 }
467 } 538 }
468 else 539 else
469 { 540 {
470 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) 541 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)
471 /* 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 */
472 for (i = 0; i < rtimercnt; ++i) 546 for (i = 0; i < timercnt; ++i)
473 rtimers [i]->at += ev_now - now; 547 timers [i]->at += diff;
548 }
474 549
475 now = ev_now; 550 now = ev_now;
476 } 551 }
477} 552}
478 553
479int ev_loop_done; 554int ev_loop_done;
480 555
481void ev_loop (int flags) 556void ev_loop (int flags)
482{ 557{
483 double block; 558 double block;
484 ev_loop_done = flags & EVLOOP_ONESHOT; 559 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0;
485
486 if (checkcnt)
487 {
488 queue_events ((W *)checks, checkcnt, EV_CHECK);
489 call_pending ();
490 }
491 560
492 do 561 do
493 { 562 {
563 /* queue check watchers (and execute them) */
564 if (checkcnt)
565 {
566 queue_events ((W *)prepares, preparecnt, EV_PREPARE);
567 call_pending ();
568 }
569
494 /* update fd-related kernel structures */ 570 /* update fd-related kernel structures */
495 fd_reify (); 571 fd_reify ();
496 572
497 /* calculate blocking time */ 573 /* calculate blocking time */
574
575 /* we only need this for !monotonic clock, but as we always have timers, we just calculate it every time */
576 ev_now = ev_time ();
577
498 if (flags & EVLOOP_NONBLOCK || idlecnt) 578 if (flags & EVLOOP_NONBLOCK || idlecnt)
499 block = 0.; 579 block = 0.;
500 else 580 else
501 { 581 {
502 block = MAX_BLOCKTIME; 582 block = MAX_BLOCKTIME;
503 583
504 if (rtimercnt) 584 if (timercnt)
505 { 585 {
506 ev_tstamp to = rtimers [0]->at - get_clock () + method_fudge; 586 ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge;
507 if (block > to) block = to; 587 if (block > to) block = to;
508 } 588 }
509 589
510 if (atimercnt) 590 if (periodiccnt)
511 { 591 {
512 ev_tstamp to = atimers [0]->at - ev_time () + method_fudge; 592 ev_tstamp to = periodics [0]->at - ev_now + method_fudge;
513 if (block > to) block = to; 593 if (block > to) block = to;
514 } 594 }
515 595
516 if (block < 0.) block = 0.; 596 if (block < 0.) block = 0.;
517 } 597 }
520 600
521 /* update ev_now, do magic */ 601 /* update ev_now, do magic */
522 time_update (); 602 time_update ();
523 603
524 /* queue pending timers and reschedule them */ 604 /* queue pending timers and reschedule them */
525 /* absolute timers first */ 605 timers_reify (); /* relative timers called last */
526 timers_reify (atimers, atimercnt, ev_now); 606 periodics_reify (); /* absolute timers called first */
527 /* relative timers second */
528 timers_reify (rtimers, rtimercnt, now);
529 607
530 /* queue idle watchers unless io or timers are pending */ 608 /* queue idle watchers unless io or timers are pending */
531 if (!pendingcnt) 609 if (!pendingcnt)
532 queue_events ((W *)idles, idlecnt, EV_IDLE); 610 queue_events ((W *)idles, idlecnt, EV_IDLE);
533 611
534 /* queue check and possibly idle watchers */ 612 /* queue check watchers, to be executed first */
613 if (checkcnt)
535 queue_events ((W *)checks, checkcnt, EV_CHECK); 614 queue_events ((W *)checks, checkcnt, EV_CHECK);
536 615
537 call_pending (); 616 call_pending ();
538 } 617 }
539 while (!ev_loop_done); 618 while (!ev_loop_done);
619
620 if (ev_loop_done != 2)
621 ev_loop_done = 0;
540} 622}
541 623
542/*****************************************************************************/ 624/*****************************************************************************/
543 625
544static void 626static void
562 head = &(*head)->next; 644 head = &(*head)->next;
563 } 645 }
564} 646}
565 647
566static void 648static void
649ev_clear (W w)
650{
651 if (w->pending)
652 {
653 pendings [w->pending - 1].w = 0;
654 w->pending = 0;
655 }
656}
657
658static void
567ev_start (W w, int active) 659ev_start (W w, int active)
568{ 660{
569 w->pending = 0;
570 w->active = active; 661 w->active = active;
571} 662}
572 663
573static void 664static void
574ev_stop (W w) 665ev_stop (W w)
575{ 666{
576 if (w->pending)
577 pendings [w->pending - 1].w = 0;
578
579 w->active = 0; 667 w->active = 0;
580} 668}
581 669
582/*****************************************************************************/ 670/*****************************************************************************/
583 671
599} 687}
600 688
601void 689void
602evio_stop (struct ev_io *w) 690evio_stop (struct ev_io *w)
603{ 691{
692 ev_clear ((W)w);
604 if (!ev_is_active (w)) 693 if (!ev_is_active (w))
605 return; 694 return;
606 695
607 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 696 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
608 ev_stop ((W)w); 697 ev_stop ((W)w);
616evtimer_start (struct ev_timer *w) 705evtimer_start (struct ev_timer *w)
617{ 706{
618 if (ev_is_active (w)) 707 if (ev_is_active (w))
619 return; 708 return;
620 709
621 if (w->is_abs) 710 w->at += now;
622 {
623 /* this formula differs from the one in timer_reify becuse we do not round up */
624 if (w->repeat)
625 w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat;
626 711
712 assert (("timer repeat value less than zero not allowed", w->repeat >= 0.));
713
627 ev_start ((W)w, ++atimercnt); 714 ev_start ((W)w, ++timercnt);
628 array_needsize (atimers, atimermax, atimercnt, ); 715 array_needsize (timers, timermax, timercnt, );
629 atimers [atimercnt - 1] = w; 716 timers [timercnt - 1] = w;
630 upheap (atimers, atimercnt - 1); 717 upheap ((WT *)timers, timercnt - 1);
631 }
632 else
633 {
634 w->at += now;
635
636 ev_start ((W)w, ++rtimercnt);
637 array_needsize (rtimers, rtimermax, rtimercnt, );
638 rtimers [rtimercnt - 1] = w;
639 upheap (rtimers, rtimercnt - 1);
640 }
641
642} 718}
643 719
644void 720void
645evtimer_stop (struct ev_timer *w) 721evtimer_stop (struct ev_timer *w)
646{ 722{
723 ev_clear ((W)w);
647 if (!ev_is_active (w)) 724 if (!ev_is_active (w))
648 return; 725 return;
649 726
650 if (w->is_abs)
651 {
652 if (w->active < atimercnt--) 727 if (w->active < timercnt--)
653 { 728 {
654 atimers [w->active - 1] = atimers [atimercnt]; 729 timers [w->active - 1] = timers [timercnt];
730 downheap ((WT *)timers, timercnt, w->active - 1);
731 }
732
733 w->at = w->repeat;
734
735 ev_stop ((W)w);
736}
737
738void
739evtimer_again (struct ev_timer *w)
740{
741 if (ev_is_active (w))
742 {
743 if (w->repeat)
744 {
745 w->at = now + w->repeat;
655 downheap (atimers, atimercnt, w->active - 1); 746 downheap ((WT *)timers, timercnt, w->active - 1);
656 }
657 }
658 else
659 {
660 if (w->active < rtimercnt--)
661 { 747 }
662 rtimers [w->active - 1] = rtimers [rtimercnt]; 748 else
663 downheap (rtimers, rtimercnt, w->active - 1); 749 evtimer_stop (w);
664 } 750 }
751 else if (w->repeat)
752 evtimer_start (w);
753}
754
755void
756evperiodic_start (struct ev_periodic *w)
757{
758 if (ev_is_active (w))
759 return;
760
761 assert (("periodic interval value less than zero not allowed", w->interval >= 0.));
762
763 /* this formula differs from the one in periodic_reify because we do not always round up */
764 if (w->interval)
765 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
766
767 ev_start ((W)w, ++periodiccnt);
768 array_needsize (periodics, periodicmax, periodiccnt, );
769 periodics [periodiccnt - 1] = w;
770 upheap ((WT *)periodics, periodiccnt - 1);
771}
772
773void
774evperiodic_stop (struct ev_periodic *w)
775{
776 ev_clear ((W)w);
777 if (!ev_is_active (w))
778 return;
779
780 if (w->active < periodiccnt--)
781 {
782 periodics [w->active - 1] = periodics [periodiccnt];
783 downheap ((WT *)periodics, periodiccnt, w->active - 1);
665 } 784 }
666 785
667 ev_stop ((W)w); 786 ev_stop ((W)w);
668} 787}
669 788
688} 807}
689 808
690void 809void
691evsignal_stop (struct ev_signal *w) 810evsignal_stop (struct ev_signal *w)
692{ 811{
812 ev_clear ((W)w);
693 if (!ev_is_active (w)) 813 if (!ev_is_active (w))
694 return; 814 return;
695 815
696 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 816 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
697 ev_stop ((W)w); 817 ev_stop ((W)w);
710 idles [idlecnt - 1] = w; 830 idles [idlecnt - 1] = w;
711} 831}
712 832
713void evidle_stop (struct ev_idle *w) 833void evidle_stop (struct ev_idle *w)
714{ 834{
835 ev_clear ((W)w);
836 if (ev_is_active (w))
837 return;
838
715 idles [w->active - 1] = idles [--idlecnt]; 839 idles [w->active - 1] = idles [--idlecnt];
840 ev_stop ((W)w);
841}
842
843void evprepare_start (struct ev_prepare *w)
844{
845 if (ev_is_active (w))
846 return;
847
848 ev_start ((W)w, ++preparecnt);
849 array_needsize (prepares, preparemax, preparecnt, );
850 prepares [preparecnt - 1] = w;
851}
852
853void evprepare_stop (struct ev_prepare *w)
854{
855 ev_clear ((W)w);
856 if (ev_is_active (w))
857 return;
858
859 prepares [w->active - 1] = prepares [--preparecnt];
716 ev_stop ((W)w); 860 ev_stop ((W)w);
717} 861}
718 862
719void evcheck_start (struct ev_check *w) 863void evcheck_start (struct ev_check *w)
720{ 864{
726 checks [checkcnt - 1] = w; 870 checks [checkcnt - 1] = w;
727} 871}
728 872
729void evcheck_stop (struct ev_check *w) 873void evcheck_stop (struct ev_check *w)
730{ 874{
875 ev_clear ((W)w);
876 if (ev_is_active (w))
877 return;
878
731 checks [w->active - 1] = checks [--checkcnt]; 879 checks [w->active - 1] = checks [--checkcnt];
732 ev_stop ((W)w); 880 ev_stop ((W)w);
733} 881}
734 882
735/*****************************************************************************/ 883/*****************************************************************************/
736 884
885struct ev_once
886{
887 struct ev_io io;
888 struct ev_timer to;
889 void (*cb)(int revents, void *arg);
890 void *arg;
891};
892
893static void
894once_cb (struct ev_once *once, int revents)
895{
896 void (*cb)(int revents, void *arg) = once->cb;
897 void *arg = once->arg;
898
899 evio_stop (&once->io);
900 evtimer_stop (&once->to);
901 free (once);
902
903 cb (revents, arg);
904}
905
906static void
907once_cb_io (struct ev_io *w, int revents)
908{
909 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents);
910}
911
912static void
913once_cb_to (struct ev_timer *w, int revents)
914{
915 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents);
916}
917
918void
919ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
920{
921 struct ev_once *once = malloc (sizeof (struct ev_once));
922
923 if (!once)
924 cb (EV_ERROR, arg);
925 else
926 {
927 once->cb = cb;
928 once->arg = arg;
929
930 evw_init (&once->io, once_cb_io);
931
932 if (fd >= 0)
933 {
934 evio_set (&once->io, fd, events);
935 evio_start (&once->io);
936 }
937
938 evw_init (&once->to, once_cb_to);
939
940 if (timeout >= 0.)
941 {
942 evtimer_set (&once->to, timeout, 0.);
943 evtimer_start (&once->to);
944 }
945 }
946}
947
948/*****************************************************************************/
949
737#if 0 950#if 0
951
952struct ev_io wio;
738 953
739static void 954static void
740sin_cb (struct ev_io *w, int revents) 955sin_cb (struct ev_io *w, int revents)
741{ 956{
742 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); 957 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
752 967
753static void 968static void
754scb (struct ev_signal *w, int revents) 969scb (struct ev_signal *w, int revents)
755{ 970{
756 fprintf (stderr, "signal %x,%d\n", revents, w->signum); 971 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
972 evio_stop (&wio);
973 evio_start (&wio);
757} 974}
758 975
759static void 976static void
760gcb (struct ev_signal *w, int revents) 977gcb (struct ev_signal *w, int revents)
761{ 978{
762 fprintf (stderr, "generic %x\n", revents); 979 fprintf (stderr, "generic %x\n", revents);
980
763} 981}
764 982
765int main (void) 983int main (void)
766{ 984{
767 struct ev_io sin;
768
769 ev_init (0); 985 ev_init (0);
770 986
771 evw_init (&sin, sin_cb, 55);
772 evio_set (&sin, 0, EV_READ); 987 evio_init (&wio, sin_cb, 0, EV_READ);
773 evio_start (&sin); 988 evio_start (&wio);
774 989
775 struct ev_timer t[10000]; 990 struct ev_timer t[10000];
776 991
777#if 0 992#if 0
778 int i; 993 int i;
779 for (i = 0; i < 10000; ++i) 994 for (i = 0; i < 10000; ++i)
780 { 995 {
781 struct ev_timer *w = t + i; 996 struct ev_timer *w = t + i;
782 evw_init (w, ocb, i); 997 evw_init (w, ocb, i);
783 evtimer_set_abs (w, drand48 (), 0.99775533); 998 evtimer_init_abs (w, ocb, drand48 (), 0.99775533);
784 evtimer_start (w); 999 evtimer_start (w);
785 if (drand48 () < 0.5) 1000 if (drand48 () < 0.5)
786 evtimer_stop (w); 1001 evtimer_stop (w);
787 } 1002 }
788#endif 1003#endif
789 1004
790 struct ev_timer t1; 1005 struct ev_timer t1;
791 evw_init (&t1, ocb, 0); 1006 evtimer_init (&t1, ocb, 5, 10);
792 evtimer_set_abs (&t1, 5, 10);
793 evtimer_start (&t1); 1007 evtimer_start (&t1);
794 1008
795 struct ev_signal sig; 1009 struct ev_signal sig;
796 evw_init (&sig, scb, 65535);
797 evsignal_set (&sig, SIGQUIT); 1010 evsignal_init (&sig, scb, SIGQUIT);
798 evsignal_start (&sig); 1011 evsignal_start (&sig);
799 1012
800 struct ev_check cw; 1013 struct ev_check cw;
801 evw_init (&cw, gcb, 0); 1014 evcheck_init (&cw, gcb);
802 evcheck_start (&cw); 1015 evcheck_start (&cw);
803 1016
804 struct ev_idle iw; 1017 struct ev_idle iw;
805 evw_init (&iw, gcb, 0); 1018 evidle_init (&iw, gcb);
806 evidle_start (&iw); 1019 evidle_start (&iw);
807 1020
808 ev_loop (0); 1021 ev_loop (0);
809 1022
810 return 0; 1023 return 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines