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.36 by root, Thu Nov 1 13:11:11 2007 UTC

1/* 1/*
2 * libev event processing core, watcher management
3 *
2 * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> 4 * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de>
3 * All rights reserved. 5 * All rights reserved.
4 * 6 *
5 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are 8 * modification, are permitted provided that the following conditions are
139/*****************************************************************************/ 141/*****************************************************************************/
140 142
141typedef struct 143typedef struct
142{ 144{
143 struct ev_io *head; 145 struct ev_io *head;
144 int events; 146 unsigned char events;
147 unsigned char reify;
145} ANFD; 148} ANFD;
146 149
147static ANFD *anfds; 150static ANFD *anfds;
148static int anfdmax; 151static int anfdmax;
149 152
152{ 155{
153 while (count--) 156 while (count--)
154 { 157 {
155 base->head = 0; 158 base->head = 0;
156 base->events = EV_NONE; 159 base->events = EV_NONE;
160 base->reify = 0;
161
157 ++base; 162 ++base;
158 } 163 }
159} 164}
160 165
161typedef struct 166typedef struct
225 int events = 0; 230 int events = 0;
226 231
227 for (w = anfd->head; w; w = w->next) 232 for (w = anfd->head; w; w = w->next)
228 events |= w->events; 233 events |= w->events;
229 234
230 anfd->events &= ~EV_REIFY; 235 anfd->reify = 0;
231 236
232 if (anfd->events != events) 237 if (anfd->events != events)
233 { 238 {
234 method_modify (fd, anfd->events, events); 239 method_modify (fd, anfd->events, events);
235 anfd->events = events; 240 anfd->events = events;
240} 245}
241 246
242static void 247static void
243fd_change (int fd) 248fd_change (int fd)
244{ 249{
245 if (anfds [fd].events & EV_REIFY || fdchangecnt < 0) 250 if (anfds [fd].reify || fdchangecnt < 0)
246 return; 251 return;
247 252
248 anfds [fd].events |= EV_REIFY; 253 anfds [fd].reify = 1;
249 254
250 ++fdchangecnt; 255 ++fdchangecnt;
251 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 256 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
252 fdchanges [fdchangecnt - 1] = fd; 257 fdchanges [fdchangecnt - 1] = fd;
253} 258}
262 if (anfds [fd].events) 267 if (anfds [fd].events)
263 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 268 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
264 while (anfds [fd].head) 269 while (anfds [fd].head)
265 { 270 {
266 ev_io_stop (anfds [fd].head); 271 ev_io_stop (anfds [fd].head);
267 event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT); 272 event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE);
268 } 273 }
269} 274}
270 275
271/*****************************************************************************/ 276/*****************************************************************************/
272 277
320/*****************************************************************************/ 325/*****************************************************************************/
321 326
322typedef struct 327typedef struct
323{ 328{
324 struct ev_signal *head; 329 struct ev_signal *head;
325 sig_atomic_t gotsig; 330 sig_atomic_t volatile gotsig;
326} ANSIG; 331} ANSIG;
327 332
328static ANSIG *signals; 333static ANSIG *signals;
329static int signalmax; 334static int signalmax;
330 335
331static int sigpipe [2]; 336static int sigpipe [2];
332static sig_atomic_t gotsig; 337static sig_atomic_t volatile gotsig;
333static struct ev_io sigev; 338static struct ev_io sigev;
334 339
335static void 340static void
336signals_init (ANSIG *base, int count) 341signals_init (ANSIG *base, int count)
337{ 342{
338 while (count--) 343 while (count--)
339 { 344 {
340 base->head = 0; 345 base->head = 0;
341 base->gotsig = 0; 346 base->gotsig = 0;
347
342 ++base; 348 ++base;
343 } 349 }
344} 350}
345 351
346static void 352static void
349 signals [signum - 1].gotsig = 1; 355 signals [signum - 1].gotsig = 1;
350 356
351 if (!gotsig) 357 if (!gotsig)
352 { 358 {
353 gotsig = 1; 359 gotsig = 1;
354 write (sigpipe [1], &gotsig, 1); 360 write (sigpipe [1], &signum, 1);
355 } 361 }
356} 362}
357 363
358static void 364static void
359sigcb (struct ev_io *iow, int revents) 365sigcb (struct ev_io *iow, int revents)
360{ 366{
361 struct ev_signal *w; 367 struct ev_signal *w;
362 int sig; 368 int sig;
363 369
370 read (sigpipe [0], &revents, 1);
364 gotsig = 0; 371 gotsig = 0;
365 read (sigpipe [0], &revents, 1);
366 372
367 for (sig = signalmax; sig--; ) 373 for (sig = signalmax; sig--; )
368 if (signals [sig].gotsig) 374 if (signals [sig].gotsig)
369 { 375 {
370 signals [sig].gotsig = 0; 376 signals [sig].gotsig = 0;
485} 491}
486 492
487/*****************************************************************************/ 493/*****************************************************************************/
488 494
489void 495void
490ev_prefork (void) 496ev_fork_prepare (void)
491{ 497{
492 /* nop */ 498 /* nop */
493} 499}
494 500
495void 501void
496ev_postfork_parent (void) 502ev_fork_parent (void)
497{ 503{
498 /* nop */ 504 /* nop */
499} 505}
500 506
501void 507void
502ev_postfork_child (void) 508ev_fork_child (void)
503{ 509{
504#if EV_USE_EPOLL 510#if EV_USE_EPOLL
505 if (ev_method == EVMETHOD_EPOLL) 511 if (ev_method == EVMETHOD_EPOLL)
506 epoll_postfork_child (); 512 epoll_postfork_child ();
507#endif 513#endif
538 struct ev_timer *w = timers [0]; 544 struct ev_timer *w = timers [0];
539 545
540 /* first reschedule or stop timer */ 546 /* first reschedule or stop timer */
541 if (w->repeat) 547 if (w->repeat)
542 { 548 {
549 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
543 w->at = now + w->repeat; 550 w->at = now + w->repeat;
544 assert (("timer timeout in the past, negative repeat?", w->at > now));
545 downheap ((WT *)timers, timercnt, 0); 551 downheap ((WT *)timers, timercnt, 0);
546 } 552 }
547 else 553 else
548 ev_timer_stop (w); /* nonrepeating: stop timer */ 554 ev_timer_stop (w); /* nonrepeating: stop timer */
549 555
560 566
561 /* first reschedule or stop timer */ 567 /* first reschedule or stop timer */
562 if (w->interval) 568 if (w->interval)
563 { 569 {
564 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; 570 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)); 571 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > ev_now));
566 downheap ((WT *)periodics, periodiccnt, 0); 572 downheap ((WT *)periodics, periodiccnt, 0);
567 } 573 }
568 else 574 else
569 ev_periodic_stop (w); /* nonrepeating: stop timer */ 575 ev_periodic_stop (w); /* nonrepeating: stop timer */
570 576
571 event ((W)w, EV_TIMEOUT); 577 event ((W)w, EV_PERIODIC);
572 } 578 }
573} 579}
574 580
575static void 581static void
576periodics_reschedule (ev_tstamp diff) 582periodics_reschedule (ev_tstamp diff)
731 head = &(*head)->next; 737 head = &(*head)->next;
732 } 738 }
733} 739}
734 740
735static void 741static void
736ev_clear (W w) 742ev_clear_pending (W w)
737{ 743{
738 if (w->pending) 744 if (w->pending)
739 { 745 {
740 pendings [w->pending - 1].w = 0; 746 pendings [w->pending - 1].w = 0;
741 w->pending = 0; 747 w->pending = 0;
761{ 767{
762 if (ev_is_active (w)) 768 if (ev_is_active (w))
763 return; 769 return;
764 770
765 int fd = w->fd; 771 int fd = w->fd;
772
773 assert (("ev_io_start called with negative fd", fd >= 0));
766 774
767 ev_start ((W)w, 1); 775 ev_start ((W)w, 1);
768 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 776 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
769 wlist_add ((WL *)&anfds[fd].head, (WL)w); 777 wlist_add ((WL *)&anfds[fd].head, (WL)w);
770 778
772} 780}
773 781
774void 782void
775ev_io_stop (struct ev_io *w) 783ev_io_stop (struct ev_io *w)
776{ 784{
777 ev_clear ((W)w); 785 ev_clear_pending ((W)w);
778 if (!ev_is_active (w)) 786 if (!ev_is_active (w))
779 return; 787 return;
780 788
781 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 789 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
782 ev_stop ((W)w); 790 ev_stop ((W)w);
790 if (ev_is_active (w)) 798 if (ev_is_active (w))
791 return; 799 return;
792 800
793 w->at += now; 801 w->at += now;
794 802
795 assert (("timer repeat value less than zero not allowed", w->repeat >= 0.)); 803 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
796 804
797 ev_start ((W)w, ++timercnt); 805 ev_start ((W)w, ++timercnt);
798 array_needsize (timers, timermax, timercnt, ); 806 array_needsize (timers, timermax, timercnt, );
799 timers [timercnt - 1] = w; 807 timers [timercnt - 1] = w;
800 upheap ((WT *)timers, timercnt - 1); 808 upheap ((WT *)timers, timercnt - 1);
801} 809}
802 810
803void 811void
804ev_timer_stop (struct ev_timer *w) 812ev_timer_stop (struct ev_timer *w)
805{ 813{
806 ev_clear ((W)w); 814 ev_clear_pending ((W)w);
807 if (!ev_is_active (w)) 815 if (!ev_is_active (w))
808 return; 816 return;
809 817
810 if (w->active < timercnt--) 818 if (w->active < timercnt--)
811 { 819 {
839ev_periodic_start (struct ev_periodic *w) 847ev_periodic_start (struct ev_periodic *w)
840{ 848{
841 if (ev_is_active (w)) 849 if (ev_is_active (w))
842 return; 850 return;
843 851
844 assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); 852 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
845 853
846 /* this formula differs from the one in periodic_reify because we do not always round up */ 854 /* this formula differs from the one in periodic_reify because we do not always round up */
847 if (w->interval) 855 if (w->interval)
848 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; 856 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
849 857
854} 862}
855 863
856void 864void
857ev_periodic_stop (struct ev_periodic *w) 865ev_periodic_stop (struct ev_periodic *w)
858{ 866{
859 ev_clear ((W)w); 867 ev_clear_pending ((W)w);
860 if (!ev_is_active (w)) 868 if (!ev_is_active (w))
861 return; 869 return;
862 870
863 if (w->active < periodiccnt--) 871 if (w->active < periodiccnt--)
864 { 872 {
872void 880void
873ev_signal_start (struct ev_signal *w) 881ev_signal_start (struct ev_signal *w)
874{ 882{
875 if (ev_is_active (w)) 883 if (ev_is_active (w))
876 return; 884 return;
885
886 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
877 887
878 ev_start ((W)w, 1); 888 ev_start ((W)w, 1);
879 array_needsize (signals, signalmax, w->signum, signals_init); 889 array_needsize (signals, signalmax, w->signum, signals_init);
880 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 890 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
881 891
890} 900}
891 901
892void 902void
893ev_signal_stop (struct ev_signal *w) 903ev_signal_stop (struct ev_signal *w)
894{ 904{
895 ev_clear ((W)w); 905 ev_clear_pending ((W)w);
896 if (!ev_is_active (w)) 906 if (!ev_is_active (w))
897 return; 907 return;
898 908
899 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 909 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
900 ev_stop ((W)w); 910 ev_stop ((W)w);
915} 925}
916 926
917void 927void
918ev_idle_stop (struct ev_idle *w) 928ev_idle_stop (struct ev_idle *w)
919{ 929{
920 ev_clear ((W)w); 930 ev_clear_pending ((W)w);
921 if (ev_is_active (w)) 931 if (ev_is_active (w))
922 return; 932 return;
923 933
924 idles [w->active - 1] = idles [--idlecnt]; 934 idles [w->active - 1] = idles [--idlecnt];
925 ev_stop ((W)w); 935 ev_stop ((W)w);
937} 947}
938 948
939void 949void
940ev_prepare_stop (struct ev_prepare *w) 950ev_prepare_stop (struct ev_prepare *w)
941{ 951{
942 ev_clear ((W)w); 952 ev_clear_pending ((W)w);
943 if (ev_is_active (w)) 953 if (ev_is_active (w))
944 return; 954 return;
945 955
946 prepares [w->active - 1] = prepares [--preparecnt]; 956 prepares [w->active - 1] = prepares [--preparecnt];
947 ev_stop ((W)w); 957 ev_stop ((W)w);
959} 969}
960 970
961void 971void
962ev_check_stop (struct ev_check *w) 972ev_check_stop (struct ev_check *w)
963{ 973{
964 ev_clear ((W)w); 974 ev_clear_pending ((W)w);
965 if (ev_is_active (w)) 975 if (ev_is_active (w))
966 return; 976 return;
967 977
968 checks [w->active - 1] = checks [--checkcnt]; 978 checks [w->active - 1] = checks [--checkcnt];
969 ev_stop ((W)w); 979 ev_stop ((W)w);
980} 990}
981 991
982void 992void
983ev_child_stop (struct ev_child *w) 993ev_child_stop (struct ev_child *w)
984{ 994{
985 ev_clear ((W)w); 995 ev_clear_pending ((W)w);
986 if (ev_is_active (w)) 996 if (ev_is_active (w))
987 return; 997 return;
988 998
989 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 999 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
990 ev_stop ((W)w); 1000 ev_stop ((W)w);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines