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

Comparing libev/ev.c (file contents):
Revision 1.12 by root, Wed Oct 31 09:23:17 2007 UTC vs.
Revision 1.18 by root, Wed Oct 31 16:29:52 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>
33 63
34#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 64#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
35#define MAX_BLOCKTIME 60. 65#define MAX_BLOCKTIME 60.
36 66
37#include "ev.h" 67#include "ev.h"
38
39struct ev_watcher {
40 EV_WATCHER (ev_watcher);
41};
42
43struct ev_watcher_list {
44 EV_WATCHER_LIST (ev_watcher_list);
45};
46
47struct ev_watcher_time {
48 EV_WATCHER_TIME (ev_watcher_time);
49};
50 68
51typedef struct ev_watcher *W; 69typedef struct ev_watcher *W;
52typedef struct ev_watcher_list *WL; 70typedef struct ev_watcher_list *WL;
53typedef struct ev_watcher_time *WT; 71typedef struct ev_watcher_time *WT;
54 72
95 113
96#define array_needsize(base,cur,cnt,init) \ 114#define array_needsize(base,cur,cnt,init) \
97 if ((cnt) > cur) \ 115 if ((cnt) > cur) \
98 { \ 116 { \
99 int newcnt = cur ? cur << 1 : 16; \ 117 int newcnt = cur ? cur << 1 : 16; \
100 fprintf (stderr, "resize(" # base ") from %d to %d\n", cur, newcnt);\
101 base = realloc (base, sizeof (*base) * (newcnt)); \ 118 base = realloc (base, sizeof (*base) * (newcnt)); \
102 init (base + cur, newcnt - cur); \ 119 init (base + cur, newcnt - cur); \
103 cur = newcnt; \ 120 cur = newcnt; \
104 } 121 }
105 122
138static int pendingmax, pendingcnt; 155static int pendingmax, pendingcnt;
139 156
140static void 157static void
141event (W w, int events) 158event (W w, int events)
142{ 159{
160 if (w->active)
161 {
143 w->pending = ++pendingcnt; 162 w->pending = ++pendingcnt;
144 array_needsize (pendings, pendingmax, pendingcnt, ); 163 array_needsize (pendings, pendingmax, pendingcnt, );
145 pendings [pendingcnt - 1].w = w; 164 pendings [pendingcnt - 1].w = w;
146 pendings [pendingcnt - 1].events = events; 165 pendings [pendingcnt - 1].events = events;
166 }
147} 167}
148 168
149static void 169static void
150fd_event (int fd, int events) 170fd_event (int fd, int events)
151{ 171{
396} 416}
397 417
398static void 418static void
399call_pending () 419call_pending ()
400{ 420{
401 int i; 421 while (pendingcnt)
402
403 for (i = 0; i < pendingcnt; ++i)
404 { 422 {
405 ANPENDING *p = pendings + i; 423 ANPENDING *p = pendings + --pendingcnt;
406 424
407 if (p->w) 425 if (p->w)
408 { 426 {
409 p->w->pending = 0; 427 p->w->pending = 0;
410 p->w->cb (p->w, p->events); 428 p->w->cb (p->w, p->events);
411 } 429 }
412 } 430 }
413
414 pendingcnt = 0;
415} 431}
416 432
417static void 433static void
418timers_reify () 434timers_reify ()
419{ 435{
420 while (timercnt && timers [0]->at <= now) 436 while (timercnt && timers [0]->at <= now)
421 { 437 {
422 struct ev_timer *w = timers [0]; 438 struct ev_timer *w = timers [0];
439
440 event ((W)w, EV_TIMEOUT);
423 441
424 /* first reschedule or stop timer */ 442 /* first reschedule or stop timer */
425 if (w->repeat) 443 if (w->repeat)
426 { 444 {
427 w->at = now + w->repeat; 445 w->at = now + w->repeat;
428 assert (("timer timeout in the past, negative repeat?", w->at > now)); 446 assert (("timer timeout in the past, negative repeat?", w->at > now));
429 downheap ((WT *)timers, timercnt, 0); 447 downheap ((WT *)timers, timercnt, 0);
430 } 448 }
431 else 449 else
432 evtimer_stop (w); /* nonrepeating: stop timer */ 450 evtimer_stop (w); /* nonrepeating: stop timer */
433
434 event ((W)w, EV_TIMEOUT);
435 } 451 }
436} 452}
437 453
438static void 454static void
439periodics_reify () 455periodics_reify ()
455 event ((W)w, EV_TIMEOUT); 471 event ((W)w, EV_TIMEOUT);
456 } 472 }
457} 473}
458 474
459static void 475static void
460time_jump (ev_tstamp diff) 476periodics_reschedule (ev_tstamp diff)
461{ 477{
462 int i; 478 int i;
463 479
464 /* adjust periodics */ 480 /* adjust periodics after time jump */
465 for (i = 0; i < periodiccnt; ++i) 481 for (i = 0; i < periodiccnt; ++i)
466 { 482 {
467 struct ev_periodic *w = periodics [i]; 483 struct ev_periodic *w = periodics [i];
468 484
469 if (w->interval) 485 if (w->interval)
477 493
478 i = 0; /* restart loop, inefficient, but time jumps should be rare */ 494 i = 0; /* restart loop, inefficient, but time jumps should be rare */
479 } 495 }
480 } 496 }
481 } 497 }
482
483 /* adjust timers. this is easy, as the offset is the same for all */
484 for (i = 0; i < timercnt; ++i)
485 timers [i]->at += diff;
486} 498}
487 499
488static void 500static void
489time_update () 501time_update ()
490{ 502{
505 return; /* all is well */ 517 return; /* all is well */
506 518
507 ev_now = ev_time (); 519 ev_now = ev_time ();
508 } 520 }
509 521
510 time_jump (diff - odiff); 522 periodics_reschedule (diff - odiff);
523 /* no timer adjustment, as the monotonic clock doesn't jump */
511 } 524 }
512 else 525 else
513 { 526 {
514 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) 527 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)
515 time_jump (ev_now - now); 528 {
529 periodics_reschedule (ev_now - now);
530
531 /* adjust timers. this is easy, as the offset is the same for all */
532 for (i = 0; i < timercnt; ++i)
533 timers [i]->at += diff;
534 }
516 535
517 now = ev_now; 536 now = ev_now;
518 } 537 }
519} 538}
520 539
521int ev_loop_done; 540int ev_loop_done;
522 541
523void ev_loop (int flags) 542void ev_loop (int flags)
524{ 543{
525 double block; 544 double block;
526 ev_loop_done = flags & EVLOOP_ONESHOT; 545 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0;
527 546
528 if (checkcnt) 547 if (checkcnt)
529 { 548 {
530 queue_events ((W *)checks, checkcnt, EV_CHECK); 549 queue_events ((W *)checks, checkcnt, EV_CHECK);
531 call_pending (); 550 call_pending ();
547 { 566 {
548 block = MAX_BLOCKTIME; 567 block = MAX_BLOCKTIME;
549 568
550 if (timercnt) 569 if (timercnt)
551 { 570 {
552 ev_tstamp to = timers [0]->at - get_clock () + method_fudge; 571 ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge;
553 if (block > to) block = to; 572 if (block > to) block = to;
554 } 573 }
555 574
556 if (periodiccnt) 575 if (periodiccnt)
557 { 576 {
579 queue_events ((W *)checks, checkcnt, EV_CHECK); 598 queue_events ((W *)checks, checkcnt, EV_CHECK);
580 599
581 call_pending (); 600 call_pending ();
582 } 601 }
583 while (!ev_loop_done); 602 while (!ev_loop_done);
603
604 if (ev_loop_done != 2)
605 ev_loop_done = 0;
584} 606}
585 607
586/*****************************************************************************/ 608/*****************************************************************************/
587 609
588static void 610static void
606 head = &(*head)->next; 628 head = &(*head)->next;
607 } 629 }
608} 630}
609 631
610static void 632static void
633ev_clear (W w)
634{
635 if (w->pending)
636 {
637 pendings [w->pending - 1].w = 0;
638 w->pending = 0;
639 }
640}
641
642static void
611ev_start (W w, int active) 643ev_start (W w, int active)
612{ 644{
613 w->pending = 0;
614 w->active = active; 645 w->active = active;
615} 646}
616 647
617static void 648static void
618ev_stop (W w) 649ev_stop (W w)
619{ 650{
620 if (w->pending)
621 pendings [w->pending - 1].w = 0;
622
623 w->active = 0; 651 w->active = 0;
624} 652}
625 653
626/*****************************************************************************/ 654/*****************************************************************************/
627 655
643} 671}
644 672
645void 673void
646evio_stop (struct ev_io *w) 674evio_stop (struct ev_io *w)
647{ 675{
676 ev_clear ((W)w);
648 if (!ev_is_active (w)) 677 if (!ev_is_active (w))
649 return; 678 return;
650 679
651 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 680 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
652 ev_stop ((W)w); 681 ev_stop ((W)w);
654 ++fdchangecnt; 683 ++fdchangecnt;
655 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 684 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
656 fdchanges [fdchangecnt - 1] = w->fd; 685 fdchanges [fdchangecnt - 1] = w->fd;
657} 686}
658 687
659
660void 688void
661evtimer_start (struct ev_timer *w) 689evtimer_start (struct ev_timer *w)
662{ 690{
663 if (ev_is_active (w)) 691 if (ev_is_active (w))
664 return; 692 return;
665 693
666 w->at += now; 694 w->at += now;
695
696 assert (("timer repeat value less than zero not allowed", w->repeat >= 0.));
667 697
668 ev_start ((W)w, ++timercnt); 698 ev_start ((W)w, ++timercnt);
669 array_needsize (timers, timermax, timercnt, ); 699 array_needsize (timers, timermax, timercnt, );
670 timers [timercnt - 1] = w; 700 timers [timercnt - 1] = w;
671 upheap ((WT *)timers, timercnt - 1); 701 upheap ((WT *)timers, timercnt - 1);
672} 702}
673 703
674void 704void
675evtimer_stop (struct ev_timer *w) 705evtimer_stop (struct ev_timer *w)
676{ 706{
707 ev_clear ((W)w);
677 if (!ev_is_active (w)) 708 if (!ev_is_active (w))
678 return; 709 return;
679 710
680 if (w->active < timercnt--) 711 if (w->active < timercnt--)
681 { 712 {
682 timers [w->active - 1] = timers [timercnt]; 713 timers [w->active - 1] = timers [timercnt];
683 downheap ((WT *)timers, timercnt, w->active - 1); 714 downheap ((WT *)timers, timercnt, w->active - 1);
684 } 715 }
685 716
717 w->at = w->repeat;
718
686 ev_stop ((W)w); 719 ev_stop ((W)w);
720}
721
722void
723evtimer_again (struct ev_timer *w)
724{
725 if (ev_is_active (w))
726 {
727 if (w->repeat)
728 {
729 w->at = now + w->repeat;
730 downheap ((WT *)timers, timercnt, w->active - 1);
731 }
732 else
733 evtimer_stop (w);
734 }
735 else if (w->repeat)
736 evtimer_start (w);
687} 737}
688 738
689void 739void
690evperiodic_start (struct ev_periodic *w) 740evperiodic_start (struct ev_periodic *w)
691{ 741{
692 if (ev_is_active (w)) 742 if (ev_is_active (w))
693 return; 743 return;
744
745 assert (("periodic interval value less than zero not allowed", w->interval >= 0.));
694 746
695 /* this formula differs from the one in periodic_reify because we do not always round up */ 747 /* this formula differs from the one in periodic_reify because we do not always round up */
696 if (w->interval) 748 if (w->interval)
697 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; 749 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
698 750
703} 755}
704 756
705void 757void
706evperiodic_stop (struct ev_periodic *w) 758evperiodic_stop (struct ev_periodic *w)
707{ 759{
760 ev_clear ((W)w);
708 if (!ev_is_active (w)) 761 if (!ev_is_active (w))
709 return; 762 return;
710 763
711 if (w->active < periodiccnt--) 764 if (w->active < periodiccnt--)
712 { 765 {
738} 791}
739 792
740void 793void
741evsignal_stop (struct ev_signal *w) 794evsignal_stop (struct ev_signal *w)
742{ 795{
796 ev_clear ((W)w);
743 if (!ev_is_active (w)) 797 if (!ev_is_active (w))
744 return; 798 return;
745 799
746 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 800 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
747 ev_stop ((W)w); 801 ev_stop ((W)w);
760 idles [idlecnt - 1] = w; 814 idles [idlecnt - 1] = w;
761} 815}
762 816
763void evidle_stop (struct ev_idle *w) 817void evidle_stop (struct ev_idle *w)
764{ 818{
819 ev_clear ((W)w);
820 if (ev_is_active (w))
821 return;
822
765 idles [w->active - 1] = idles [--idlecnt]; 823 idles [w->active - 1] = idles [--idlecnt];
766 ev_stop ((W)w); 824 ev_stop ((W)w);
767} 825}
768 826
769void evcheck_start (struct ev_check *w) 827void evcheck_start (struct ev_check *w)
776 checks [checkcnt - 1] = w; 834 checks [checkcnt - 1] = w;
777} 835}
778 836
779void evcheck_stop (struct ev_check *w) 837void evcheck_stop (struct ev_check *w)
780{ 838{
839 ev_clear ((W)w);
840 if (ev_is_active (w))
841 return;
842
781 checks [w->active - 1] = checks [--checkcnt]; 843 checks [w->active - 1] = checks [--checkcnt];
782 ev_stop ((W)w); 844 ev_stop ((W)w);
783} 845}
784 846
785/*****************************************************************************/ 847/*****************************************************************************/
786 848
849struct ev_once
850{
851 struct ev_io io;
852 struct ev_timer to;
853 void (*cb)(int revents, void *arg);
854 void *arg;
855};
856
857static void
858once_cb (struct ev_once *once, int revents)
859{
860 void (*cb)(int revents, void *arg) = once->cb;
861 void *arg = once->arg;
862
863 evio_stop (&once->io);
864 evtimer_stop (&once->to);
865 free (once);
866
867 cb (revents, arg);
868}
869
870static void
871once_cb_io (struct ev_io *w, int revents)
872{
873 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents);
874}
875
876static void
877once_cb_to (struct ev_timer *w, int revents)
878{
879 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents);
880}
881
882void
883ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
884{
885 struct ev_once *once = malloc (sizeof (struct ev_once));
886
887 if (!once)
888 cb (EV_ERROR, arg);
889 else
890 {
891 once->cb = cb;
892 once->arg = arg;
893
894 evw_init (&once->io, once_cb_io);
895
896 if (fd >= 0)
897 {
898 evio_set (&once->io, fd, events);
899 evio_start (&once->io);
900 }
901
902 evw_init (&once->to, once_cb_to);
903
904 if (timeout >= 0.)
905 {
906 evtimer_set (&once->to, timeout, 0.);
907 evtimer_start (&once->to);
908 }
909 }
910}
911
912/*****************************************************************************/
913
787#if 1 914#if 0
788 915
789struct ev_io wio; 916struct ev_io wio;
790 917
791static void 918static void
792sin_cb (struct ev_io *w, int revents) 919sin_cb (struct ev_io *w, int revents)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines