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.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>
11#include <sys/time.h> 41#include <sys/time.h>
12#include <time.h> 42#include <time.h>
13 43
14#define HAVE_EPOLL 1
15
16#ifndef HAVE_MONOTONIC 44#ifndef HAVE_MONOTONIC
17# ifdef CLOCK_MONOTONIC 45# ifdef CLOCK_MONOTONIC
18# define HAVE_MONOTONIC 1 46# define HAVE_MONOTONIC 1
19# endif 47# endif
20#endif 48#endif
33 61
34#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) */
35#define MAX_BLOCKTIME 60. 63#define MAX_BLOCKTIME 60.
36 64
37#include "ev.h" 65#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 66
51typedef struct ev_watcher *W; 67typedef struct ev_watcher *W;
52typedef struct ev_watcher_list *WL; 68typedef struct ev_watcher_list *WL;
53typedef struct ev_watcher_time *WT; 69typedef struct ev_watcher_time *WT;
54 70
95 111
96#define array_needsize(base,cur,cnt,init) \ 112#define array_needsize(base,cur,cnt,init) \
97 if ((cnt) > cur) \ 113 if ((cnt) > cur) \
98 { \ 114 { \
99 int newcnt = cur ? cur << 1 : 16; \ 115 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)); \ 116 base = realloc (base, sizeof (*base) * (newcnt)); \
102 init (base + cur, newcnt - cur); \ 117 init (base + cur, newcnt - cur); \
103 cur = newcnt; \ 118 cur = newcnt; \
104 } 119 }
105 120
138static int pendingmax, pendingcnt; 153static int pendingmax, pendingcnt;
139 154
140static void 155static void
141event (W w, int events) 156event (W w, int events)
142{ 157{
158 if (w->active)
159 {
143 w->pending = ++pendingcnt; 160 w->pending = ++pendingcnt;
144 array_needsize (pendings, pendingmax, pendingcnt, ); 161 array_needsize (pendings, pendingmax, pendingcnt, );
145 pendings [pendingcnt - 1].w = w; 162 pendings [pendingcnt - 1].w = w;
146 pendings [pendingcnt - 1].events = events; 163 pendings [pendingcnt - 1].events = events;
164 }
147} 165}
148 166
149static void 167static void
150fd_event (int fd, int events) 168fd_event (int fd, int events)
151{ 169{
166{ 184{
167 int i; 185 int i;
168 186
169 for (i = 0; i < eventcnt; ++i) 187 for (i = 0; i < eventcnt; ++i)
170 event (events [i], type); 188 event (events [i], type);
189}
190
191/* called on EBADF to verify fds */
192static void
193fd_recheck ()
194{
195 int fd;
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);
171} 202}
172 203
173/*****************************************************************************/ 204/*****************************************************************************/
174 205
175static struct ev_timer **timers; 206static struct ev_timer **timers;
292 323
293/*****************************************************************************/ 324/*****************************************************************************/
294 325
295static struct ev_idle **idles; 326static struct ev_idle **idles;
296static int idlemax, idlecnt; 327static int idlemax, idlecnt;
328
329static struct ev_prepare **prepares;
330static int preparemax, preparecnt;
297 331
298static struct ev_check **checks; 332static struct ev_check **checks;
299static int checkmax, checkcnt; 333static int checkmax, checkcnt;
300 334
301/*****************************************************************************/ 335/*****************************************************************************/
396} 430}
397 431
398static void 432static void
399call_pending () 433call_pending ()
400{ 434{
401 int i; 435 while (pendingcnt)
402
403 for (i = 0; i < pendingcnt; ++i)
404 { 436 {
405 ANPENDING *p = pendings + i; 437 ANPENDING *p = pendings + --pendingcnt;
406 438
407 if (p->w) 439 if (p->w)
408 { 440 {
409 p->w->pending = 0; 441 p->w->pending = 0;
410 p->w->cb (p->w, p->events); 442 p->w->cb (p->w, p->events);
411 } 443 }
412 } 444 }
413
414 pendingcnt = 0;
415} 445}
416 446
417static void 447static void
418timers_reify () 448timers_reify ()
419{ 449{
420 while (timercnt && timers [0]->at <= now) 450 while (timercnt && timers [0]->at <= now)
421 { 451 {
422 struct ev_timer *w = timers [0]; 452 struct ev_timer *w = timers [0];
453
454 event ((W)w, EV_TIMEOUT);
423 455
424 /* first reschedule or stop timer */ 456 /* first reschedule or stop timer */
425 if (w->repeat) 457 if (w->repeat)
426 { 458 {
427 w->at = now + w->repeat; 459 w->at = now + w->repeat;
428 assert (("timer timeout in the past, negative repeat?", w->at > now)); 460 assert (("timer timeout in the past, negative repeat?", w->at > now));
429 downheap ((WT *)timers, timercnt, 0); 461 downheap ((WT *)timers, timercnt, 0);
430 } 462 }
431 else 463 else
432 evtimer_stop (w); /* nonrepeating: stop timer */ 464 evtimer_stop (w); /* nonrepeating: stop timer */
433
434 event ((W)w, EV_TIMEOUT);
435 } 465 }
436} 466}
437 467
438static void 468static void
439periodics_reify () 469periodics_reify ()
455 event ((W)w, EV_TIMEOUT); 485 event ((W)w, EV_TIMEOUT);
456 } 486 }
457} 487}
458 488
459static void 489static void
460time_jump (ev_tstamp diff) 490periodics_reschedule (ev_tstamp diff)
461{ 491{
462 int i; 492 int i;
463 493
464 /* adjust periodics */ 494 /* adjust periodics after time jump */
465 for (i = 0; i < periodiccnt; ++i) 495 for (i = 0; i < periodiccnt; ++i)
466 { 496 {
467 struct ev_periodic *w = periodics [i]; 497 struct ev_periodic *w = periodics [i];
468 498
469 if (w->interval) 499 if (w->interval)
477 507
478 i = 0; /* restart loop, inefficient, but time jumps should be rare */ 508 i = 0; /* restart loop, inefficient, but time jumps should be rare */
479 } 509 }
480 } 510 }
481 } 511 }
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} 512}
487 513
488static void 514static void
489time_update () 515time_update ()
490{ 516{
505 return; /* all is well */ 531 return; /* all is well */
506 532
507 ev_now = ev_time (); 533 ev_now = ev_time ();
508 } 534 }
509 535
510 time_jump (diff - odiff); 536 periodics_reschedule (diff - odiff);
537 /* no timer adjustment, as the monotonic clock doesn't jump */
511 } 538 }
512 else 539 else
513 { 540 {
514 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) 541 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)
515 time_jump (ev_now - now); 542 {
543 periodics_reschedule (ev_now - now);
544
545 /* adjust timers. this is easy, as the offset is the same for all */
546 for (i = 0; i < timercnt; ++i)
547 timers [i]->at += diff;
548 }
516 549
517 now = ev_now; 550 now = ev_now;
518 } 551 }
519} 552}
520 553
521int ev_loop_done; 554int ev_loop_done;
522 555
523void ev_loop (int flags) 556void ev_loop (int flags)
524{ 557{
525 double block; 558 double block;
526 ev_loop_done = flags & EVLOOP_ONESHOT; 559 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0;
527
528 if (checkcnt)
529 {
530 queue_events ((W *)checks, checkcnt, EV_CHECK);
531 call_pending ();
532 }
533 560
534 do 561 do
535 { 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
536 /* update fd-related kernel structures */ 570 /* update fd-related kernel structures */
537 fd_reify (); 571 fd_reify ();
538 572
539 /* calculate blocking time */ 573 /* calculate blocking time */
540 574
547 { 581 {
548 block = MAX_BLOCKTIME; 582 block = MAX_BLOCKTIME;
549 583
550 if (timercnt) 584 if (timercnt)
551 { 585 {
552 ev_tstamp to = timers [0]->at - get_clock () + method_fudge; 586 ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge;
553 if (block > to) block = to; 587 if (block > to) block = to;
554 } 588 }
555 589
556 if (periodiccnt) 590 if (periodiccnt)
557 { 591 {
566 600
567 /* update ev_now, do magic */ 601 /* update ev_now, do magic */
568 time_update (); 602 time_update ();
569 603
570 /* queue pending timers and reschedule them */ 604 /* queue pending timers and reschedule them */
605 timers_reify (); /* relative timers called last */
571 periodics_reify (); /* absolute timers first */ 606 periodics_reify (); /* absolute timers called first */
572 timers_reify (); /* relative timers second */
573 607
574 /* queue idle watchers unless io or timers are pending */ 608 /* queue idle watchers unless io or timers are pending */
575 if (!pendingcnt) 609 if (!pendingcnt)
576 queue_events ((W *)idles, idlecnt, EV_IDLE); 610 queue_events ((W *)idles, idlecnt, EV_IDLE);
577 611
578 /* queue check and possibly idle watchers */ 612 /* queue check watchers, to be executed first */
613 if (checkcnt)
579 queue_events ((W *)checks, checkcnt, EV_CHECK); 614 queue_events ((W *)checks, checkcnt, EV_CHECK);
580 615
581 call_pending (); 616 call_pending ();
582 } 617 }
583 while (!ev_loop_done); 618 while (!ev_loop_done);
619
620 if (ev_loop_done != 2)
621 ev_loop_done = 0;
584} 622}
585 623
586/*****************************************************************************/ 624/*****************************************************************************/
587 625
588static void 626static void
606 head = &(*head)->next; 644 head = &(*head)->next;
607 } 645 }
608} 646}
609 647
610static 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
611ev_start (W w, int active) 659ev_start (W w, int active)
612{ 660{
613 w->pending = 0;
614 w->active = active; 661 w->active = active;
615} 662}
616 663
617static void 664static void
618ev_stop (W w) 665ev_stop (W w)
619{ 666{
620 if (w->pending)
621 pendings [w->pending - 1].w = 0;
622
623 w->active = 0; 667 w->active = 0;
624} 668}
625 669
626/*****************************************************************************/ 670/*****************************************************************************/
627 671
643} 687}
644 688
645void 689void
646evio_stop (struct ev_io *w) 690evio_stop (struct ev_io *w)
647{ 691{
692 ev_clear ((W)w);
648 if (!ev_is_active (w)) 693 if (!ev_is_active (w))
649 return; 694 return;
650 695
651 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 696 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
652 ev_stop ((W)w); 697 ev_stop ((W)w);
654 ++fdchangecnt; 699 ++fdchangecnt;
655 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 700 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
656 fdchanges [fdchangecnt - 1] = w->fd; 701 fdchanges [fdchangecnt - 1] = w->fd;
657} 702}
658 703
659
660void 704void
661evtimer_start (struct ev_timer *w) 705evtimer_start (struct ev_timer *w)
662{ 706{
663 if (ev_is_active (w)) 707 if (ev_is_active (w))
664 return; 708 return;
665 709
666 w->at += now; 710 w->at += now;
711
712 assert (("timer repeat value less than zero not allowed", w->repeat >= 0.));
667 713
668 ev_start ((W)w, ++timercnt); 714 ev_start ((W)w, ++timercnt);
669 array_needsize (timers, timermax, timercnt, ); 715 array_needsize (timers, timermax, timercnt, );
670 timers [timercnt - 1] = w; 716 timers [timercnt - 1] = w;
671 upheap ((WT *)timers, timercnt - 1); 717 upheap ((WT *)timers, timercnt - 1);
672} 718}
673 719
674void 720void
675evtimer_stop (struct ev_timer *w) 721evtimer_stop (struct ev_timer *w)
676{ 722{
723 ev_clear ((W)w);
677 if (!ev_is_active (w)) 724 if (!ev_is_active (w))
678 return; 725 return;
679 726
680 if (w->active < timercnt--) 727 if (w->active < timercnt--)
681 { 728 {
682 timers [w->active - 1] = timers [timercnt]; 729 timers [w->active - 1] = timers [timercnt];
683 downheap ((WT *)timers, timercnt, w->active - 1); 730 downheap ((WT *)timers, timercnt, w->active - 1);
684 } 731 }
685 732
733 w->at = w->repeat;
734
686 ev_stop ((W)w); 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;
746 downheap ((WT *)timers, timercnt, w->active - 1);
747 }
748 else
749 evtimer_stop (w);
750 }
751 else if (w->repeat)
752 evtimer_start (w);
687} 753}
688 754
689void 755void
690evperiodic_start (struct ev_periodic *w) 756evperiodic_start (struct ev_periodic *w)
691{ 757{
692 if (ev_is_active (w)) 758 if (ev_is_active (w))
693 return; 759 return;
760
761 assert (("periodic interval value less than zero not allowed", w->interval >= 0.));
694 762
695 /* this formula differs from the one in periodic_reify because we do not always round up */ 763 /* this formula differs from the one in periodic_reify because we do not always round up */
696 if (w->interval) 764 if (w->interval)
697 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; 765 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
698 766
703} 771}
704 772
705void 773void
706evperiodic_stop (struct ev_periodic *w) 774evperiodic_stop (struct ev_periodic *w)
707{ 775{
776 ev_clear ((W)w);
708 if (!ev_is_active (w)) 777 if (!ev_is_active (w))
709 return; 778 return;
710 779
711 if (w->active < periodiccnt--) 780 if (w->active < periodiccnt--)
712 { 781 {
738} 807}
739 808
740void 809void
741evsignal_stop (struct ev_signal *w) 810evsignal_stop (struct ev_signal *w)
742{ 811{
812 ev_clear ((W)w);
743 if (!ev_is_active (w)) 813 if (!ev_is_active (w))
744 return; 814 return;
745 815
746 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 816 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
747 ev_stop ((W)w); 817 ev_stop ((W)w);
760 idles [idlecnt - 1] = w; 830 idles [idlecnt - 1] = w;
761} 831}
762 832
763void evidle_stop (struct ev_idle *w) 833void evidle_stop (struct ev_idle *w)
764{ 834{
835 ev_clear ((W)w);
836 if (ev_is_active (w))
837 return;
838
765 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];
766 ev_stop ((W)w); 860 ev_stop ((W)w);
767} 861}
768 862
769void evcheck_start (struct ev_check *w) 863void evcheck_start (struct ev_check *w)
770{ 864{
776 checks [checkcnt - 1] = w; 870 checks [checkcnt - 1] = w;
777} 871}
778 872
779void evcheck_stop (struct ev_check *w) 873void evcheck_stop (struct ev_check *w)
780{ 874{
875 ev_clear ((W)w);
876 if (ev_is_active (w))
877 return;
878
781 checks [w->active - 1] = checks [--checkcnt]; 879 checks [w->active - 1] = checks [--checkcnt];
782 ev_stop ((W)w); 880 ev_stop ((W)w);
783} 881}
784 882
785/*****************************************************************************/ 883/*****************************************************************************/
786 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
787#if 1 950#if 0
788 951
789struct ev_io wio; 952struct ev_io wio;
790 953
791static void 954static void
792sin_cb (struct ev_io *w, int revents) 955sin_cb (struct ev_io *w, int revents)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines