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.17 by root, Wed Oct 31 14:44:15 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{
419{ 439{
420 while (timercnt && timers [0]->at <= now) 440 while (timercnt && timers [0]->at <= now)
421 { 441 {
422 struct ev_timer *w = timers [0]; 442 struct ev_timer *w = timers [0];
423 443
444 event ((W)w, EV_TIMEOUT);
445
424 /* first reschedule or stop timer */ 446 /* first reschedule or stop timer */
425 if (w->repeat) 447 if (w->repeat)
426 { 448 {
427 w->at = now + w->repeat; 449 w->at = now + w->repeat;
428 assert (("timer timeout in the past, negative repeat?", w->at > now)); 450 assert (("timer timeout in the past, negative repeat?", w->at > now));
429 downheap ((WT *)timers, timercnt, 0); 451 downheap ((WT *)timers, timercnt, 0);
430 } 452 }
431 else 453 else
432 evtimer_stop (w); /* nonrepeating: stop timer */ 454 evtimer_stop (w); /* nonrepeating: stop timer */
433
434 event ((W)w, EV_TIMEOUT);
435 } 455 }
436} 456}
437 457
438static void 458static void
439periodics_reify () 459periodics_reify ()
455 event ((W)w, EV_TIMEOUT); 475 event ((W)w, EV_TIMEOUT);
456 } 476 }
457} 477}
458 478
459static void 479static void
460time_jump (ev_tstamp diff) 480periodics_reschedule (ev_tstamp diff)
461{ 481{
462 int i; 482 int i;
463 483
464 /* adjust periodics */ 484 /* adjust periodics after time jump */
465 for (i = 0; i < periodiccnt; ++i) 485 for (i = 0; i < periodiccnt; ++i)
466 { 486 {
467 struct ev_periodic *w = periodics [i]; 487 struct ev_periodic *w = periodics [i];
468 488
469 if (w->interval) 489 if (w->interval)
477 497
478 i = 0; /* restart loop, inefficient, but time jumps should be rare */ 498 i = 0; /* restart loop, inefficient, but time jumps should be rare */
479 } 499 }
480 } 500 }
481 } 501 }
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} 502}
487 503
488static void 504static void
489time_update () 505time_update ()
490{ 506{
505 return; /* all is well */ 521 return; /* all is well */
506 522
507 ev_now = ev_time (); 523 ev_now = ev_time ();
508 } 524 }
509 525
510 time_jump (diff - odiff); 526 periodics_reschedule (diff - odiff);
527 /* no timer adjustment, as the monotonic clock doesn't jump */
511 } 528 }
512 else 529 else
513 { 530 {
514 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) 531 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)
515 time_jump (ev_now - now); 532 {
533 periodics_reschedule (ev_now - now);
534
535 /* adjust timers. this is easy, as the offset is the same for all */
536 for (i = 0; i < timercnt; ++i)
537 timers [i]->at += diff;
538 }
516 539
517 now = ev_now; 540 now = ev_now;
518 } 541 }
519} 542}
520 543
521int ev_loop_done; 544int ev_loop_done;
522 545
523void ev_loop (int flags) 546void ev_loop (int flags)
524{ 547{
525 double block; 548 double block;
526 ev_loop_done = flags & EVLOOP_ONESHOT; 549 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0;
527 550
528 if (checkcnt) 551 if (checkcnt)
529 { 552 {
530 queue_events ((W *)checks, checkcnt, EV_CHECK); 553 queue_events ((W *)checks, checkcnt, EV_CHECK);
531 call_pending (); 554 call_pending ();
547 { 570 {
548 block = MAX_BLOCKTIME; 571 block = MAX_BLOCKTIME;
549 572
550 if (timercnt) 573 if (timercnt)
551 { 574 {
552 ev_tstamp to = timers [0]->at - get_clock () + method_fudge; 575 ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge;
553 if (block > to) block = to; 576 if (block > to) block = to;
554 } 577 }
555 578
556 if (periodiccnt) 579 if (periodiccnt)
557 { 580 {
579 queue_events ((W *)checks, checkcnt, EV_CHECK); 602 queue_events ((W *)checks, checkcnt, EV_CHECK);
580 603
581 call_pending (); 604 call_pending ();
582 } 605 }
583 while (!ev_loop_done); 606 while (!ev_loop_done);
607
608 if (ev_loop_done != 2)
609 ev_loop_done = 0;
584} 610}
585 611
586/*****************************************************************************/ 612/*****************************************************************************/
587 613
588static void 614static void
606 head = &(*head)->next; 632 head = &(*head)->next;
607 } 633 }
608} 634}
609 635
610static void 636static void
637ev_clear (W w)
638{
639 if (w->pending)
640 {
641 pendings [w->pending - 1].w = 0;
642 w->pending = 0;
643 }
644}
645
646static void
611ev_start (W w, int active) 647ev_start (W w, int active)
612{ 648{
613 w->pending = 0;
614 w->active = active; 649 w->active = active;
615} 650}
616 651
617static void 652static void
618ev_stop (W w) 653ev_stop (W w)
619{ 654{
620 if (w->pending)
621 pendings [w->pending - 1].w = 0;
622
623 w->active = 0; 655 w->active = 0;
624} 656}
625 657
626/*****************************************************************************/ 658/*****************************************************************************/
627 659
643} 675}
644 676
645void 677void
646evio_stop (struct ev_io *w) 678evio_stop (struct ev_io *w)
647{ 679{
680 ev_clear ((W)w);
648 if (!ev_is_active (w)) 681 if (!ev_is_active (w))
649 return; 682 return;
650 683
651 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 684 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
652 ev_stop ((W)w); 685 ev_stop ((W)w);
654 ++fdchangecnt; 687 ++fdchangecnt;
655 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 688 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
656 fdchanges [fdchangecnt - 1] = w->fd; 689 fdchanges [fdchangecnt - 1] = w->fd;
657} 690}
658 691
659
660void 692void
661evtimer_start (struct ev_timer *w) 693evtimer_start (struct ev_timer *w)
662{ 694{
663 if (ev_is_active (w)) 695 if (ev_is_active (w))
664 return; 696 return;
665 697
666 w->at += now; 698 w->at += now;
699
700 assert (("timer repeat value less than zero not allowed", w->repeat >= 0.));
667 701
668 ev_start ((W)w, ++timercnt); 702 ev_start ((W)w, ++timercnt);
669 array_needsize (timers, timermax, timercnt, ); 703 array_needsize (timers, timermax, timercnt, );
670 timers [timercnt - 1] = w; 704 timers [timercnt - 1] = w;
671 upheap ((WT *)timers, timercnt - 1); 705 upheap ((WT *)timers, timercnt - 1);
672} 706}
673 707
674void 708void
675evtimer_stop (struct ev_timer *w) 709evtimer_stop (struct ev_timer *w)
676{ 710{
711 ev_clear ((W)w);
677 if (!ev_is_active (w)) 712 if (!ev_is_active (w))
678 return; 713 return;
679 714
680 if (w->active < timercnt--) 715 if (w->active < timercnt--)
681 { 716 {
682 timers [w->active - 1] = timers [timercnt]; 717 timers [w->active - 1] = timers [timercnt];
683 downheap ((WT *)timers, timercnt, w->active - 1); 718 downheap ((WT *)timers, timercnt, w->active - 1);
684 } 719 }
685 720
721 w->at = w->repeat;
722
686 ev_stop ((W)w); 723 ev_stop ((W)w);
724}
725
726void
727evtimer_again (struct ev_timer *w)
728{
729 if (ev_is_active (w))
730 {
731 if (w->repeat)
732 {
733 w->at = now + w->repeat;
734 downheap ((WT *)timers, timercnt, w->active - 1);
735 }
736 else
737 evtimer_stop (w);
738 }
739 else if (w->repeat)
740 evtimer_start (w);
687} 741}
688 742
689void 743void
690evperiodic_start (struct ev_periodic *w) 744evperiodic_start (struct ev_periodic *w)
691{ 745{
692 if (ev_is_active (w)) 746 if (ev_is_active (w))
693 return; 747 return;
748
749 assert (("periodic interval value less than zero not allowed", w->interval >= 0.));
694 750
695 /* this formula differs from the one in periodic_reify because we do not always round up */ 751 /* this formula differs from the one in periodic_reify because we do not always round up */
696 if (w->interval) 752 if (w->interval)
697 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; 753 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
698 754
703} 759}
704 760
705void 761void
706evperiodic_stop (struct ev_periodic *w) 762evperiodic_stop (struct ev_periodic *w)
707{ 763{
764 ev_clear ((W)w);
708 if (!ev_is_active (w)) 765 if (!ev_is_active (w))
709 return; 766 return;
710 767
711 if (w->active < periodiccnt--) 768 if (w->active < periodiccnt--)
712 { 769 {
738} 795}
739 796
740void 797void
741evsignal_stop (struct ev_signal *w) 798evsignal_stop (struct ev_signal *w)
742{ 799{
800 ev_clear ((W)w);
743 if (!ev_is_active (w)) 801 if (!ev_is_active (w))
744 return; 802 return;
745 803
746 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 804 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
747 ev_stop ((W)w); 805 ev_stop ((W)w);
760 idles [idlecnt - 1] = w; 818 idles [idlecnt - 1] = w;
761} 819}
762 820
763void evidle_stop (struct ev_idle *w) 821void evidle_stop (struct ev_idle *w)
764{ 822{
823 ev_clear ((W)w);
824 if (ev_is_active (w))
825 return;
826
765 idles [w->active - 1] = idles [--idlecnt]; 827 idles [w->active - 1] = idles [--idlecnt];
766 ev_stop ((W)w); 828 ev_stop ((W)w);
767} 829}
768 830
769void evcheck_start (struct ev_check *w) 831void evcheck_start (struct ev_check *w)
776 checks [checkcnt - 1] = w; 838 checks [checkcnt - 1] = w;
777} 839}
778 840
779void evcheck_stop (struct ev_check *w) 841void evcheck_stop (struct ev_check *w)
780{ 842{
843 ev_clear ((W)w);
844 if (ev_is_active (w))
845 return;
846
781 checks [w->active - 1] = checks [--checkcnt]; 847 checks [w->active - 1] = checks [--checkcnt];
782 ev_stop ((W)w); 848 ev_stop ((W)w);
783} 849}
784 850
785/*****************************************************************************/ 851/*****************************************************************************/
786 852
853struct ev_once
854{
855 struct ev_io io;
856 struct ev_timer to;
857 void (*cb)(int revents, void *arg);
858 void *arg;
859};
860
861static void
862once_cb (struct ev_once *once, int revents)
863{
864 void (*cb)(int revents, void *arg) = once->cb;
865 void *arg = once->arg;
866
867 evio_stop (&once->io);
868 evtimer_stop (&once->to);
869 free (once);
870
871 cb (revents, arg);
872}
873
874static void
875once_cb_io (struct ev_io *w, int revents)
876{
877 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents);
878}
879
880static void
881once_cb_to (struct ev_timer *w, int revents)
882{
883 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents);
884}
885
886void
887ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
888{
889 struct ev_once *once = malloc (sizeof (struct ev_once));
890
891 if (!once)
892 cb (EV_ERROR, arg);
893 else
894 {
895 once->cb = cb;
896 once->arg = arg;
897
898 evw_init (&once->io, once_cb_io);
899
900 if (fd >= 0)
901 {
902 evio_set (&once->io, fd, events);
903 evio_start (&once->io);
904 }
905
906 evw_init (&once->to, once_cb_to);
907
908 if (timeout >= 0.)
909 {
910 evtimer_set (&once->to, timeout, 0.);
911 evtimer_start (&once->to);
912 }
913 }
914}
915
916/*****************************************************************************/
917
787#if 1 918#if 0
788 919
789struct ev_io wio; 920struct ev_io wio;
790 921
791static void 922static void
792sin_cb (struct ev_io *w, int revents) 923sin_cb (struct ev_io *w, int revents)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines