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.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}
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 (checkcnt)
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 clock, but as we always have timers, we just calculate it every time */
576 ev_now = ev_time ();
577
496 if (flags & EVLOOP_NONBLOCK || idlecnt) 578 if (flags & EVLOOP_NONBLOCK || idlecnt)
497 block = 0.; 579 block = 0.;
498 else 580 else
499 { 581 {
500 block = MAX_BLOCKTIME; 582 block = MAX_BLOCKTIME;
501 583
502 if (rtimercnt) 584 if (timercnt)
503 { 585 {
504 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;
505 if (block > to) block = to; 587 if (block > to) block = to;
506 } 588 }
507 589
508 if (atimercnt) 590 if (periodiccnt)
509 { 591 {
510 ev_tstamp to = atimers [0]->at - ev_time () + method_fudge; 592 ev_tstamp to = periodics [0]->at - ev_now + method_fudge;
511 if (block > to) block = to; 593 if (block > to) block = to;
512 } 594 }
513 595
514 if (block < 0.) block = 0.; 596 if (block < 0.) block = 0.;
515 } 597 }
518 600
519 /* update ev_now, do magic */ 601 /* update ev_now, do magic */
520 time_update (); 602 time_update ();
521 603
522 /* queue pending timers and reschedule them */ 604 /* queue pending timers and reschedule them */
523 /* absolute timers first */ 605 timers_reify (); /* relative timers called last */
524 timers_reify (atimers, atimercnt, ev_now); 606 periodics_reify (); /* absolute timers called first */
525 /* relative timers second */
526 timers_reify (rtimers, rtimercnt, now);
527 607
528 /* queue idle watchers unless io or timers are pending */ 608 /* queue idle watchers unless io or timers are pending */
529 if (!pendingcnt) 609 if (!pendingcnt)
530 queue_events ((W *)idles, idlecnt, EV_IDLE); 610 queue_events ((W *)idles, idlecnt, EV_IDLE);
531 611
532 /* queue check and possibly idle watchers */ 612 /* queue check watchers, to be executed first */
613 if (checkcnt)
533 queue_events ((W *)checks, checkcnt, EV_CHECK); 614 queue_events ((W *)checks, checkcnt, EV_CHECK);
534 615
535 call_pending (); 616 call_pending ();
536 } 617 }
537 while (!ev_loop_done); 618 while (!ev_loop_done);
619
620 if (ev_loop_done != 2)
621 ev_loop_done = 0;
538} 622}
539 623
540/*****************************************************************************/ 624/*****************************************************************************/
541 625
542static void 626static void
560 head = &(*head)->next; 644 head = &(*head)->next;
561 } 645 }
562} 646}
563 647
564static 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
565ev_start (W w, int active) 659ev_start (W w, int active)
566{ 660{
567 w->pending = 0;
568 w->active = active; 661 w->active = active;
569} 662}
570 663
571static void 664static void
572ev_stop (W w) 665ev_stop (W w)
573{ 666{
574 if (w->pending)
575 pendings [w->pending - 1].w = 0;
576
577 w->active = 0; 667 w->active = 0;
578 /* nop */
579} 668}
580 669
581/*****************************************************************************/ 670/*****************************************************************************/
582 671
583void 672void
598} 687}
599 688
600void 689void
601evio_stop (struct ev_io *w) 690evio_stop (struct ev_io *w)
602{ 691{
692 ev_clear ((W)w);
603 if (!ev_is_active (w)) 693 if (!ev_is_active (w))
604 return; 694 return;
605 695
606 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 696 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
607 ev_stop ((W)w); 697 ev_stop ((W)w);
615evtimer_start (struct ev_timer *w) 705evtimer_start (struct ev_timer *w)
616{ 706{
617 if (ev_is_active (w)) 707 if (ev_is_active (w))
618 return; 708 return;
619 709
620 if (w->is_abs) 710 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 711
712 assert (("timer repeat value less than zero not allowed", w->repeat >= 0.));
713
626 ev_start ((W)w, ++atimercnt); 714 ev_start ((W)w, ++timercnt);
627 array_needsize (atimers, atimermax, atimercnt, ); 715 array_needsize (timers, timermax, timercnt, );
628 atimers [atimercnt - 1] = w; 716 timers [timercnt - 1] = w;
629 upheap (atimers, atimercnt - 1); 717 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} 718}
642 719
643void 720void
644evtimer_stop (struct ev_timer *w) 721evtimer_stop (struct ev_timer *w)
645{ 722{
723 ev_clear ((W)w);
646 if (!ev_is_active (w)) 724 if (!ev_is_active (w))
647 return; 725 return;
648 726
649 if (w->is_abs)
650 {
651 if (w->active < atimercnt--) 727 if (w->active < timercnt--)
652 { 728 {
653 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;
654 downheap (atimers, atimercnt, w->active - 1); 746 downheap ((WT *)timers, timercnt, w->active - 1);
655 }
656 }
657 else
658 {
659 if (w->active < rtimercnt--)
660 { 747 }
661 rtimers [w->active - 1] = rtimers [rtimercnt]; 748 else
662 downheap (rtimers, rtimercnt, w->active - 1); 749 evtimer_stop (w);
663 } 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);
664 } 784 }
665 785
666 ev_stop ((W)w); 786 ev_stop ((W)w);
667} 787}
668 788
687} 807}
688 808
689void 809void
690evsignal_stop (struct ev_signal *w) 810evsignal_stop (struct ev_signal *w)
691{ 811{
812 ev_clear ((W)w);
692 if (!ev_is_active (w)) 813 if (!ev_is_active (w))
693 return; 814 return;
694 815
695 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 816 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
696 ev_stop ((W)w); 817 ev_stop ((W)w);
709 idles [idlecnt - 1] = w; 830 idles [idlecnt - 1] = w;
710} 831}
711 832
712void evidle_stop (struct ev_idle *w) 833void evidle_stop (struct ev_idle *w)
713{ 834{
835 ev_clear ((W)w);
836 if (ev_is_active (w))
837 return;
838
714 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];
715 ev_stop ((W)w); 860 ev_stop ((W)w);
716} 861}
717 862
718void evcheck_start (struct ev_check *w) 863void evcheck_start (struct ev_check *w)
719{ 864{
725 checks [checkcnt - 1] = w; 870 checks [checkcnt - 1] = w;
726} 871}
727 872
728void evcheck_stop (struct ev_check *w) 873void evcheck_stop (struct ev_check *w)
729{ 874{
875 ev_clear ((W)w);
876 if (ev_is_active (w))
877 return;
878
730 checks [w->active - 1] = checks [--checkcnt]; 879 checks [w->active - 1] = checks [--checkcnt];
731 ev_stop ((W)w); 880 ev_stop ((W)w);
732} 881}
733 882
734/*****************************************************************************/ 883/*****************************************************************************/
735 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
736#if 0 950#if 0
951
952struct ev_io wio;
737 953
738static void 954static void
739sin_cb (struct ev_io *w, int revents) 955sin_cb (struct ev_io *w, int revents)
740{ 956{
741 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); 957 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
751 967
752static void 968static void
753scb (struct ev_signal *w, int revents) 969scb (struct ev_signal *w, int revents)
754{ 970{
755 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);
756} 974}
757 975
758static void 976static void
759gcb (struct ev_signal *w, int revents) 977gcb (struct ev_signal *w, int revents)
760{ 978{
761 fprintf (stderr, "generic %x\n", revents); 979 fprintf (stderr, "generic %x\n", revents);
980
762} 981}
763 982
764int main (void) 983int main (void)
765{ 984{
766 struct ev_io sin;
767
768 ev_init (0); 985 ev_init (0);
769 986
770 evw_init (&sin, sin_cb, 55);
771 evio_set (&sin, 0, EV_READ); 987 evio_init (&wio, sin_cb, 0, EV_READ);
772 evio_start (&sin); 988 evio_start (&wio);
773 989
774 struct ev_timer t[10000]; 990 struct ev_timer t[10000];
775 991
776#if 0 992#if 0
777 int i; 993 int i;
778 for (i = 0; i < 10000; ++i) 994 for (i = 0; i < 10000; ++i)
779 { 995 {
780 struct ev_timer *w = t + i; 996 struct ev_timer *w = t + i;
781 evw_init (w, ocb, i); 997 evw_init (w, ocb, i);
782 evtimer_set_abs (w, drand48 (), 0.99775533); 998 evtimer_init_abs (w, ocb, drand48 (), 0.99775533);
783 evtimer_start (w); 999 evtimer_start (w);
784 if (drand48 () < 0.5) 1000 if (drand48 () < 0.5)
785 evtimer_stop (w); 1001 evtimer_stop (w);
786 } 1002 }
787#endif 1003#endif
788 1004
789 struct ev_timer t1; 1005 struct ev_timer t1;
790 evw_init (&t1, ocb, 0); 1006 evtimer_init (&t1, ocb, 5, 10);
791 evtimer_set_abs (&t1, 5, 10);
792 evtimer_start (&t1); 1007 evtimer_start (&t1);
793 1008
794 struct ev_signal sig; 1009 struct ev_signal sig;
795 evw_init (&sig, scb, 65535);
796 evsignal_set (&sig, SIGQUIT); 1010 evsignal_init (&sig, scb, SIGQUIT);
797 evsignal_start (&sig); 1011 evsignal_start (&sig);
798 1012
799 struct ev_check cw; 1013 struct ev_check cw;
800 evw_init (&cw, gcb, 0); 1014 evcheck_init (&cw, gcb);
801 evcheck_start (&cw); 1015 evcheck_start (&cw);
802 1016
803 struct ev_idle iw; 1017 struct ev_idle iw;
804 evw_init (&iw, gcb, 0); 1018 evidle_init (&iw, gcb);
805 evidle_start (&iw); 1019 evidle_start (&iw);
806 1020
807 ev_loop (0); 1021 ev_loop (0);
808 1022
809 return 0; 1023 return 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines