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

Comparing libev/ev.c (file contents):
Revision 1.27 by root, Wed Oct 31 22:16:36 2007 UTC vs.
Revision 1.28 by root, Thu Nov 1 06:48:49 2007 UTC

250 if (anfds [fd].events) 250 if (anfds [fd].events)
251 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 251 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
252 while (anfds [fd].head) 252 while (anfds [fd].head)
253 { 253 {
254 event ((W)anfds [fd].head, EV_ERROR); 254 event ((W)anfds [fd].head, EV_ERROR);
255 evio_stop (anfds [fd].head); 255 ev_io_stop (anfds [fd].head);
256 } 256 }
257} 257}
258 258
259/*****************************************************************************/ 259/*****************************************************************************/
260 260
370 370
371 /* rather than sort out wether we really need nb, set it */ 371 /* rather than sort out wether we really need nb, set it */
372 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); 372 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
373 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 373 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
374 374
375 evio_set (&sigev, sigpipe [0], EV_READ); 375 ev_io_set (&sigev, sigpipe [0], EV_READ);
376 evio_start (&sigev); 376 ev_io_start (&sigev);
377} 377}
378 378
379/*****************************************************************************/ 379/*****************************************************************************/
380 380
381static struct ev_idle **idles; 381static struct ev_idle **idles;
459 if (ev_method == EVMETHOD_NONE) select_init (flags); 459 if (ev_method == EVMETHOD_NONE) select_init (flags);
460#endif 460#endif
461 461
462 if (ev_method) 462 if (ev_method)
463 { 463 {
464 evw_init (&sigev, sigcb); 464 ev_watcher_init (&sigev, sigcb);
465 siginit (); 465 siginit ();
466 466
467 evsignal_init (&childev, childcb, SIGCHLD); 467 ev_signal_init (&childev, childcb, SIGCHLD);
468 evsignal_start (&childev); 468 ev_signal_start (&childev);
469 } 469 }
470 } 470 }
471 471
472 return ev_method; 472 return ev_method;
473} 473}
492#if HAVE_EPOLL 492#if HAVE_EPOLL
493 if (ev_method == EVMETHOD_EPOLL) 493 if (ev_method == EVMETHOD_EPOLL)
494 epoll_postfork_child (); 494 epoll_postfork_child ();
495#endif 495#endif
496 496
497 evio_stop (&sigev); 497 ev_io_stop (&sigev);
498 close (sigpipe [0]); 498 close (sigpipe [0]);
499 close (sigpipe [1]); 499 close (sigpipe [1]);
500 pipe (sigpipe); 500 pipe (sigpipe);
501 siginit (); 501 siginit ();
502} 502}
533 w->at = now + w->repeat; 533 w->at = now + w->repeat;
534 assert (("timer timeout in the past, negative repeat?", w->at > now)); 534 assert (("timer timeout in the past, negative repeat?", w->at > now));
535 downheap ((WT *)timers, timercnt, 0); 535 downheap ((WT *)timers, timercnt, 0);
536 } 536 }
537 else 537 else
538 evtimer_stop (w); /* nonrepeating: stop timer */ 538 ev_timer_stop (w); /* nonrepeating: stop timer */
539 } 539 }
540} 540}
541 541
542static void 542static void
543periodics_reify (void) 543periodics_reify (void)
552 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; 552 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval;
553 assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); 553 assert (("periodic timeout in the past, negative interval?", w->at > ev_now));
554 downheap ((WT *)periodics, periodiccnt, 0); 554 downheap ((WT *)periodics, periodiccnt, 0);
555 } 555 }
556 else 556 else
557 evperiodic_stop (w); /* nonrepeating: stop timer */ 557 ev_periodic_stop (w); /* nonrepeating: stop timer */
558 558
559 event ((W)w, EV_TIMEOUT); 559 event ((W)w, EV_TIMEOUT);
560 } 560 }
561} 561}
562 562
574 { 574 {
575 ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; 575 ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval;
576 576
577 if (fabs (diff) >= 1e-4) 577 if (fabs (diff) >= 1e-4)
578 { 578 {
579 evperiodic_stop (w); 579 ev_periodic_stop (w);
580 evperiodic_start (w); 580 ev_periodic_start (w);
581 581
582 i = 0; /* restart loop, inefficient, but time jumps should be rare */ 582 i = 0; /* restart loop, inefficient, but time jumps should be rare */
583 } 583 }
584 } 584 }
585 } 585 }
743} 743}
744 744
745/*****************************************************************************/ 745/*****************************************************************************/
746 746
747void 747void
748evio_start (struct ev_io *w) 748ev_io_start (struct ev_io *w)
749{ 749{
750 if (ev_is_active (w)) 750 if (ev_is_active (w))
751 return; 751 return;
752 752
753 int fd = w->fd; 753 int fd = w->fd;
758 758
759 fd_change (fd); 759 fd_change (fd);
760} 760}
761 761
762void 762void
763evio_stop (struct ev_io *w) 763ev_io_stop (struct ev_io *w)
764{ 764{
765 ev_clear ((W)w); 765 ev_clear ((W)w);
766 if (!ev_is_active (w)) 766 if (!ev_is_active (w))
767 return; 767 return;
768 768
771 771
772 fd_change (w->fd); 772 fd_change (w->fd);
773} 773}
774 774
775void 775void
776evtimer_start (struct ev_timer *w) 776ev_timer_start (struct ev_timer *w)
777{ 777{
778 if (ev_is_active (w)) 778 if (ev_is_active (w))
779 return; 779 return;
780 780
781 w->at += now; 781 w->at += now;
787 timers [timercnt - 1] = w; 787 timers [timercnt - 1] = w;
788 upheap ((WT *)timers, timercnt - 1); 788 upheap ((WT *)timers, timercnt - 1);
789} 789}
790 790
791void 791void
792evtimer_stop (struct ev_timer *w) 792ev_timer_stop (struct ev_timer *w)
793{ 793{
794 ev_clear ((W)w); 794 ev_clear ((W)w);
795 if (!ev_is_active (w)) 795 if (!ev_is_active (w))
796 return; 796 return;
797 797
805 805
806 ev_stop ((W)w); 806 ev_stop ((W)w);
807} 807}
808 808
809void 809void
810evtimer_again (struct ev_timer *w) 810ev_timer_again (struct ev_timer *w)
811{ 811{
812 if (ev_is_active (w)) 812 if (ev_is_active (w))
813 { 813 {
814 if (w->repeat) 814 if (w->repeat)
815 { 815 {
816 w->at = now + w->repeat; 816 w->at = now + w->repeat;
817 downheap ((WT *)timers, timercnt, w->active - 1); 817 downheap ((WT *)timers, timercnt, w->active - 1);
818 } 818 }
819 else 819 else
820 evtimer_stop (w); 820 ev_timer_stop (w);
821 } 821 }
822 else if (w->repeat) 822 else if (w->repeat)
823 evtimer_start (w); 823 ev_timer_start (w);
824} 824}
825 825
826void 826void
827evperiodic_start (struct ev_periodic *w) 827ev_periodic_start (struct ev_periodic *w)
828{ 828{
829 if (ev_is_active (w)) 829 if (ev_is_active (w))
830 return; 830 return;
831 831
832 assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); 832 assert (("periodic interval value less than zero not allowed", w->interval >= 0.));
840 periodics [periodiccnt - 1] = w; 840 periodics [periodiccnt - 1] = w;
841 upheap ((WT *)periodics, periodiccnt - 1); 841 upheap ((WT *)periodics, periodiccnt - 1);
842} 842}
843 843
844void 844void
845evperiodic_stop (struct ev_periodic *w) 845ev_periodic_stop (struct ev_periodic *w)
846{ 846{
847 ev_clear ((W)w); 847 ev_clear ((W)w);
848 if (!ev_is_active (w)) 848 if (!ev_is_active (w))
849 return; 849 return;
850 850
856 856
857 ev_stop ((W)w); 857 ev_stop ((W)w);
858} 858}
859 859
860void 860void
861evsignal_start (struct ev_signal *w) 861ev_signal_start (struct ev_signal *w)
862{ 862{
863 if (ev_is_active (w)) 863 if (ev_is_active (w))
864 return; 864 return;
865 865
866 ev_start ((W)w, 1); 866 ev_start ((W)w, 1);
876 sigaction (w->signum, &sa, 0); 876 sigaction (w->signum, &sa, 0);
877 } 877 }
878} 878}
879 879
880void 880void
881evsignal_stop (struct ev_signal *w) 881ev_signal_stop (struct ev_signal *w)
882{ 882{
883 ev_clear ((W)w); 883 ev_clear ((W)w);
884 if (!ev_is_active (w)) 884 if (!ev_is_active (w))
885 return; 885 return;
886 886
889 889
890 if (!signals [w->signum - 1].head) 890 if (!signals [w->signum - 1].head)
891 signal (w->signum, SIG_DFL); 891 signal (w->signum, SIG_DFL);
892} 892}
893 893
894void
894void evidle_start (struct ev_idle *w) 895ev_idle_start (struct ev_idle *w)
895{ 896{
896 if (ev_is_active (w)) 897 if (ev_is_active (w))
897 return; 898 return;
898 899
899 ev_start ((W)w, ++idlecnt); 900 ev_start ((W)w, ++idlecnt);
900 array_needsize (idles, idlemax, idlecnt, ); 901 array_needsize (idles, idlemax, idlecnt, );
901 idles [idlecnt - 1] = w; 902 idles [idlecnt - 1] = w;
902} 903}
903 904
905void
904void evidle_stop (struct ev_idle *w) 906ev_idle_stop (struct ev_idle *w)
905{ 907{
906 ev_clear ((W)w); 908 ev_clear ((W)w);
907 if (ev_is_active (w)) 909 if (ev_is_active (w))
908 return; 910 return;
909 911
910 idles [w->active - 1] = idles [--idlecnt]; 912 idles [w->active - 1] = idles [--idlecnt];
911 ev_stop ((W)w); 913 ev_stop ((W)w);
912} 914}
913 915
916void
914void evprepare_start (struct ev_prepare *w) 917ev_prepare_start (struct ev_prepare *w)
915{ 918{
916 if (ev_is_active (w)) 919 if (ev_is_active (w))
917 return; 920 return;
918 921
919 ev_start ((W)w, ++preparecnt); 922 ev_start ((W)w, ++preparecnt);
920 array_needsize (prepares, preparemax, preparecnt, ); 923 array_needsize (prepares, preparemax, preparecnt, );
921 prepares [preparecnt - 1] = w; 924 prepares [preparecnt - 1] = w;
922} 925}
923 926
927void
924void evprepare_stop (struct ev_prepare *w) 928ev_prepare_stop (struct ev_prepare *w)
925{ 929{
926 ev_clear ((W)w); 930 ev_clear ((W)w);
927 if (ev_is_active (w)) 931 if (ev_is_active (w))
928 return; 932 return;
929 933
930 prepares [w->active - 1] = prepares [--preparecnt]; 934 prepares [w->active - 1] = prepares [--preparecnt];
931 ev_stop ((W)w); 935 ev_stop ((W)w);
932} 936}
933 937
938void
934void evcheck_start (struct ev_check *w) 939ev_check_start (struct ev_check *w)
935{ 940{
936 if (ev_is_active (w)) 941 if (ev_is_active (w))
937 return; 942 return;
938 943
939 ev_start ((W)w, ++checkcnt); 944 ev_start ((W)w, ++checkcnt);
940 array_needsize (checks, checkmax, checkcnt, ); 945 array_needsize (checks, checkmax, checkcnt, );
941 checks [checkcnt - 1] = w; 946 checks [checkcnt - 1] = w;
942} 947}
943 948
949void
944void evcheck_stop (struct ev_check *w) 950ev_check_stop (struct ev_check *w)
945{ 951{
946 ev_clear ((W)w); 952 ev_clear ((W)w);
947 if (ev_is_active (w)) 953 if (ev_is_active (w))
948 return; 954 return;
949 955
950 checks [w->active - 1] = checks [--checkcnt]; 956 checks [w->active - 1] = checks [--checkcnt];
951 ev_stop ((W)w); 957 ev_stop ((W)w);
952} 958}
953 959
960void
954void evchild_start (struct ev_child *w) 961ev_child_start (struct ev_child *w)
955{ 962{
956 if (ev_is_active (w)) 963 if (ev_is_active (w))
957 return; 964 return;
958 965
959 ev_start ((W)w, 1); 966 ev_start ((W)w, 1);
960 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 967 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
961} 968}
962 969
970void
963void evchild_stop (struct ev_child *w) 971ev_child_stop (struct ev_child *w)
964{ 972{
965 ev_clear ((W)w); 973 ev_clear ((W)w);
966 if (ev_is_active (w)) 974 if (ev_is_active (w))
967 return; 975 return;
968 976
984once_cb (struct ev_once *once, int revents) 992once_cb (struct ev_once *once, int revents)
985{ 993{
986 void (*cb)(int revents, void *arg) = once->cb; 994 void (*cb)(int revents, void *arg) = once->cb;
987 void *arg = once->arg; 995 void *arg = once->arg;
988 996
989 evio_stop (&once->io); 997 ev_io_stop (&once->io);
990 evtimer_stop (&once->to); 998 ev_timer_stop (&once->to);
991 free (once); 999 free (once);
992 1000
993 cb (revents, arg); 1001 cb (revents, arg);
994} 1002}
995 1003
1015 else 1023 else
1016 { 1024 {
1017 once->cb = cb; 1025 once->cb = cb;
1018 once->arg = arg; 1026 once->arg = arg;
1019 1027
1020 evw_init (&once->io, once_cb_io); 1028 ev_watcher_init (&once->io, once_cb_io);
1021 1029
1022 if (fd >= 0) 1030 if (fd >= 0)
1023 { 1031 {
1024 evio_set (&once->io, fd, events); 1032 ev_io_set (&once->io, fd, events);
1025 evio_start (&once->io); 1033 ev_io_start (&once->io);
1026 } 1034 }
1027 1035
1028 evw_init (&once->to, once_cb_to); 1036 ev_watcher_init (&once->to, once_cb_to);
1029 1037
1030 if (timeout >= 0.) 1038 if (timeout >= 0.)
1031 { 1039 {
1032 evtimer_set (&once->to, timeout, 0.); 1040 ev_timer_set (&once->to, timeout, 0.);
1033 evtimer_start (&once->to); 1041 ev_timer_start (&once->to);
1034 } 1042 }
1035 } 1043 }
1036} 1044}
1037 1045
1038/*****************************************************************************/ 1046/*****************************************************************************/
1049 1057
1050static void 1058static void
1051ocb (struct ev_timer *w, int revents) 1059ocb (struct ev_timer *w, int revents)
1052{ 1060{
1053 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); 1061 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data);
1054 evtimer_stop (w); 1062 ev_timer_stop (w);
1055 evtimer_start (w); 1063 ev_timer_start (w);
1056} 1064}
1057 1065
1058static void 1066static void
1059scb (struct ev_signal *w, int revents) 1067scb (struct ev_signal *w, int revents)
1060{ 1068{
1061 fprintf (stderr, "signal %x,%d\n", revents, w->signum); 1069 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
1062 evio_stop (&wio); 1070 ev_io_stop (&wio);
1063 evio_start (&wio); 1071 ev_io_start (&wio);
1064} 1072}
1065 1073
1066static void 1074static void
1067gcb (struct ev_signal *w, int revents) 1075gcb (struct ev_signal *w, int revents)
1068{ 1076{
1072 1080
1073int main (void) 1081int main (void)
1074{ 1082{
1075 ev_init (0); 1083 ev_init (0);
1076 1084
1077 evio_init (&wio, sin_cb, 0, EV_READ); 1085 ev_io_init (&wio, sin_cb, 0, EV_READ);
1078 evio_start (&wio); 1086 ev_io_start (&wio);
1079 1087
1080 struct ev_timer t[10000]; 1088 struct ev_timer t[10000];
1081 1089
1082#if 0 1090#if 0
1083 int i; 1091 int i;
1084 for (i = 0; i < 10000; ++i) 1092 for (i = 0; i < 10000; ++i)
1085 { 1093 {
1086 struct ev_timer *w = t + i; 1094 struct ev_timer *w = t + i;
1087 evw_init (w, ocb, i); 1095 ev_watcher_init (w, ocb, i);
1088 evtimer_init_abs (w, ocb, drand48 (), 0.99775533); 1096 ev_timer_init_abs (w, ocb, drand48 (), 0.99775533);
1089 evtimer_start (w); 1097 ev_timer_start (w);
1090 if (drand48 () < 0.5) 1098 if (drand48 () < 0.5)
1091 evtimer_stop (w); 1099 ev_timer_stop (w);
1092 } 1100 }
1093#endif 1101#endif
1094 1102
1095 struct ev_timer t1; 1103 struct ev_timer t1;
1096 evtimer_init (&t1, ocb, 5, 10); 1104 ev_timer_init (&t1, ocb, 5, 10);
1097 evtimer_start (&t1); 1105 ev_timer_start (&t1);
1098 1106
1099 struct ev_signal sig; 1107 struct ev_signal sig;
1100 evsignal_init (&sig, scb, SIGQUIT); 1108 ev_signal_init (&sig, scb, SIGQUIT);
1101 evsignal_start (&sig); 1109 ev_signal_start (&sig);
1102 1110
1103 struct ev_check cw; 1111 struct ev_check cw;
1104 evcheck_init (&cw, gcb); 1112 ev_check_init (&cw, gcb);
1105 evcheck_start (&cw); 1113 ev_check_start (&cw);
1106 1114
1107 struct ev_idle iw; 1115 struct ev_idle iw;
1108 evidle_init (&iw, gcb); 1116 ev_idle_init (&iw, gcb);
1109 evidle_start (&iw); 1117 ev_idle_start (&iw);
1110 1118
1111 ev_loop (0); 1119 ev_loop (0);
1112 1120
1113 return 0; 1121 return 0;
1114} 1122}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines