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

Comparing libev/ev.c (file contents):
Revision 1.32 by root, Thu Nov 1 09:21:51 2007 UTC vs.
Revision 1.33 by root, Thu Nov 1 11:11:22 2007 UTC

139/*****************************************************************************/ 139/*****************************************************************************/
140 140
141typedef struct 141typedef struct
142{ 142{
143 struct ev_io *head; 143 struct ev_io *head;
144 int events; 144 unsigned char events;
145 unsigned char reify;
145} ANFD; 146} ANFD;
146 147
147static ANFD *anfds; 148static ANFD *anfds;
148static int anfdmax; 149static int anfdmax;
149 150
152{ 153{
153 while (count--) 154 while (count--)
154 { 155 {
155 base->head = 0; 156 base->head = 0;
156 base->events = EV_NONE; 157 base->events = EV_NONE;
158 base->reify = 0;
159
157 ++base; 160 ++base;
158 } 161 }
159} 162}
160 163
161typedef struct 164typedef struct
225 int events = 0; 228 int events = 0;
226 229
227 for (w = anfd->head; w; w = w->next) 230 for (w = anfd->head; w; w = w->next)
228 events |= w->events; 231 events |= w->events;
229 232
230 anfd->events &= ~EV_REIFY; 233 anfd->reify = 0;
231 234
232 if (anfd->events != events) 235 if (anfd->events != events)
233 { 236 {
234 method_modify (fd, anfd->events, events); 237 method_modify (fd, anfd->events, events);
235 anfd->events = events; 238 anfd->events = events;
240} 243}
241 244
242static void 245static void
243fd_change (int fd) 246fd_change (int fd)
244{ 247{
245 if (anfds [fd].events & EV_REIFY || fdchangecnt < 0) 248 if (anfds [fd].reify || fdchangecnt < 0)
246 return; 249 return;
247 250
248 anfds [fd].events |= EV_REIFY; 251 anfds [fd].reify = 1;
249 252
250 ++fdchangecnt; 253 ++fdchangecnt;
251 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 254 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
252 fdchanges [fdchangecnt - 1] = fd; 255 fdchanges [fdchangecnt - 1] = fd;
253} 256}
262 if (anfds [fd].events) 265 if (anfds [fd].events)
263 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 266 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
264 while (anfds [fd].head) 267 while (anfds [fd].head)
265 { 268 {
266 ev_io_stop (anfds [fd].head); 269 ev_io_stop (anfds [fd].head);
267 event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT); 270 event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE);
268 } 271 }
269} 272}
270 273
271/*****************************************************************************/ 274/*****************************************************************************/
272 275
337{ 340{
338 while (count--) 341 while (count--)
339 { 342 {
340 base->head = 0; 343 base->head = 0;
341 base->gotsig = 0; 344 base->gotsig = 0;
345
342 ++base; 346 ++base;
343 } 347 }
344} 348}
345 349
346static void 350static void
538 struct ev_timer *w = timers [0]; 542 struct ev_timer *w = timers [0];
539 543
540 /* first reschedule or stop timer */ 544 /* first reschedule or stop timer */
541 if (w->repeat) 545 if (w->repeat)
542 { 546 {
547 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
543 w->at = now + w->repeat; 548 w->at = now + w->repeat;
544 assert (("timer timeout in the past, negative repeat?", w->at > now));
545 downheap ((WT *)timers, timercnt, 0); 549 downheap ((WT *)timers, timercnt, 0);
546 } 550 }
547 else 551 else
548 ev_timer_stop (w); /* nonrepeating: stop timer */ 552 ev_timer_stop (w); /* nonrepeating: stop timer */
549 553
560 564
561 /* first reschedule or stop timer */ 565 /* first reschedule or stop timer */
562 if (w->interval) 566 if (w->interval)
563 { 567 {
564 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; 568 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval;
565 assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); 569 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > ev_now));
566 downheap ((WT *)periodics, periodiccnt, 0); 570 downheap ((WT *)periodics, periodiccnt, 0);
567 } 571 }
568 else 572 else
569 ev_periodic_stop (w); /* nonrepeating: stop timer */ 573 ev_periodic_stop (w); /* nonrepeating: stop timer */
570 574
571 event ((W)w, EV_TIMEOUT); 575 event ((W)w, EV_PERIODIC);
572 } 576 }
573} 577}
574 578
575static void 579static void
576periodics_reschedule (ev_tstamp diff) 580periodics_reschedule (ev_tstamp diff)
731 head = &(*head)->next; 735 head = &(*head)->next;
732 } 736 }
733} 737}
734 738
735static void 739static void
736ev_clear (W w) 740ev_clear_pending (W w)
737{ 741{
738 if (w->pending) 742 if (w->pending)
739 { 743 {
740 pendings [w->pending - 1].w = 0; 744 pendings [w->pending - 1].w = 0;
741 w->pending = 0; 745 w->pending = 0;
761{ 765{
762 if (ev_is_active (w)) 766 if (ev_is_active (w))
763 return; 767 return;
764 768
765 int fd = w->fd; 769 int fd = w->fd;
770
771 assert (("ev_io_start called with negative fd", fd >= 0));
766 772
767 ev_start ((W)w, 1); 773 ev_start ((W)w, 1);
768 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 774 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
769 wlist_add ((WL *)&anfds[fd].head, (WL)w); 775 wlist_add ((WL *)&anfds[fd].head, (WL)w);
770 776
772} 778}
773 779
774void 780void
775ev_io_stop (struct ev_io *w) 781ev_io_stop (struct ev_io *w)
776{ 782{
777 ev_clear ((W)w); 783 ev_clear_pending ((W)w);
778 if (!ev_is_active (w)) 784 if (!ev_is_active (w))
779 return; 785 return;
780 786
781 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 787 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
782 ev_stop ((W)w); 788 ev_stop ((W)w);
790 if (ev_is_active (w)) 796 if (ev_is_active (w))
791 return; 797 return;
792 798
793 w->at += now; 799 w->at += now;
794 800
795 assert (("timer repeat value less than zero not allowed", w->repeat >= 0.)); 801 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
796 802
797 ev_start ((W)w, ++timercnt); 803 ev_start ((W)w, ++timercnt);
798 array_needsize (timers, timermax, timercnt, ); 804 array_needsize (timers, timermax, timercnt, );
799 timers [timercnt - 1] = w; 805 timers [timercnt - 1] = w;
800 upheap ((WT *)timers, timercnt - 1); 806 upheap ((WT *)timers, timercnt - 1);
801} 807}
802 808
803void 809void
804ev_timer_stop (struct ev_timer *w) 810ev_timer_stop (struct ev_timer *w)
805{ 811{
806 ev_clear ((W)w); 812 ev_clear_pending ((W)w);
807 if (!ev_is_active (w)) 813 if (!ev_is_active (w))
808 return; 814 return;
809 815
810 if (w->active < timercnt--) 816 if (w->active < timercnt--)
811 { 817 {
839ev_periodic_start (struct ev_periodic *w) 845ev_periodic_start (struct ev_periodic *w)
840{ 846{
841 if (ev_is_active (w)) 847 if (ev_is_active (w))
842 return; 848 return;
843 849
844 assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); 850 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
845 851
846 /* this formula differs from the one in periodic_reify because we do not always round up */ 852 /* this formula differs from the one in periodic_reify because we do not always round up */
847 if (w->interval) 853 if (w->interval)
848 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; 854 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
849 855
854} 860}
855 861
856void 862void
857ev_periodic_stop (struct ev_periodic *w) 863ev_periodic_stop (struct ev_periodic *w)
858{ 864{
859 ev_clear ((W)w); 865 ev_clear_pending ((W)w);
860 if (!ev_is_active (w)) 866 if (!ev_is_active (w))
861 return; 867 return;
862 868
863 if (w->active < periodiccnt--) 869 if (w->active < periodiccnt--)
864 { 870 {
872void 878void
873ev_signal_start (struct ev_signal *w) 879ev_signal_start (struct ev_signal *w)
874{ 880{
875 if (ev_is_active (w)) 881 if (ev_is_active (w))
876 return; 882 return;
883
884 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
877 885
878 ev_start ((W)w, 1); 886 ev_start ((W)w, 1);
879 array_needsize (signals, signalmax, w->signum, signals_init); 887 array_needsize (signals, signalmax, w->signum, signals_init);
880 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 888 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
881 889
890} 898}
891 899
892void 900void
893ev_signal_stop (struct ev_signal *w) 901ev_signal_stop (struct ev_signal *w)
894{ 902{
895 ev_clear ((W)w); 903 ev_clear_pending ((W)w);
896 if (!ev_is_active (w)) 904 if (!ev_is_active (w))
897 return; 905 return;
898 906
899 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 907 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
900 ev_stop ((W)w); 908 ev_stop ((W)w);
915} 923}
916 924
917void 925void
918ev_idle_stop (struct ev_idle *w) 926ev_idle_stop (struct ev_idle *w)
919{ 927{
920 ev_clear ((W)w); 928 ev_clear_pending ((W)w);
921 if (ev_is_active (w)) 929 if (ev_is_active (w))
922 return; 930 return;
923 931
924 idles [w->active - 1] = idles [--idlecnt]; 932 idles [w->active - 1] = idles [--idlecnt];
925 ev_stop ((W)w); 933 ev_stop ((W)w);
937} 945}
938 946
939void 947void
940ev_prepare_stop (struct ev_prepare *w) 948ev_prepare_stop (struct ev_prepare *w)
941{ 949{
942 ev_clear ((W)w); 950 ev_clear_pending ((W)w);
943 if (ev_is_active (w)) 951 if (ev_is_active (w))
944 return; 952 return;
945 953
946 prepares [w->active - 1] = prepares [--preparecnt]; 954 prepares [w->active - 1] = prepares [--preparecnt];
947 ev_stop ((W)w); 955 ev_stop ((W)w);
959} 967}
960 968
961void 969void
962ev_check_stop (struct ev_check *w) 970ev_check_stop (struct ev_check *w)
963{ 971{
964 ev_clear ((W)w); 972 ev_clear_pending ((W)w);
965 if (ev_is_active (w)) 973 if (ev_is_active (w))
966 return; 974 return;
967 975
968 checks [w->active - 1] = checks [--checkcnt]; 976 checks [w->active - 1] = checks [--checkcnt];
969 ev_stop ((W)w); 977 ev_stop ((W)w);
980} 988}
981 989
982void 990void
983ev_child_stop (struct ev_child *w) 991ev_child_stop (struct ev_child *w)
984{ 992{
985 ev_clear ((W)w); 993 ev_clear_pending ((W)w);
986 if (ev_is_active (w)) 994 if (ev_is_active (w))
987 return; 995 return;
988 996
989 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 997 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
990 ev_stop ((W)w); 998 ev_stop ((W)w);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines