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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines