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

Comparing libev/ev.c (file contents):
Revision 1.29 by root, Thu Nov 1 08:10:03 2007 UTC vs.
Revision 1.35 by root, Thu Nov 1 11:55:54 2007 UTC

58 58
59#ifndef EV_USE_EPOLL 59#ifndef EV_USE_EPOLL
60# define EV_USE_EPOLL 0 60# define EV_USE_EPOLL 0
61#endif 61#endif
62 62
63#ifndef CLOCK_REALTIME
64# define EV_USE_REALTIME 0
65#endif
63#ifndef EV_USE_REALTIME 66#ifndef EV_USE_REALTIME
64# define EV_USE_REALTIME 1 /* posix requirement, but might be slower */ 67# define EV_USE_REALTIME 1 /* posix requirement, but might be slower */
65#endif 68#endif
66 69
67#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 70#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
68#define MAX_BLOCKTIME 59.731 71#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detetc time jumps) */
69#define PID_HASHSIZE 16 /* size of pid hahs table, must be power of two */ 72#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */
73#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */
70 74
71#include "ev.h" 75#include "ev.h"
72 76
73typedef struct ev_watcher *W; 77typedef struct ev_watcher *W;
74typedef struct ev_watcher_list *WL; 78typedef struct ev_watcher_list *WL;
113#endif 117#endif
114 118
115 return ev_time (); 119 return ev_time ();
116} 120}
117 121
118#define array_nextsize(n) (((n) << 1) | 4 & ~3) 122#define array_roundsize(base,n) ((n) | 4 & ~3)
119#define array_prevsize(n) (((n) >> 1) | 4 & ~3)
120 123
121#define array_needsize(base,cur,cnt,init) \ 124#define array_needsize(base,cur,cnt,init) \
122 if ((cnt) > cur) \ 125 if ((cnt) > cur) \
123 { \ 126 { \
124 int newcnt = cur; \ 127 int newcnt = cur; \
125 do \ 128 do \
126 { \ 129 { \
127 newcnt = array_nextsize (newcnt); \ 130 newcnt = array_roundsize (base, newcnt << 1); \
128 } \ 131 } \
129 while ((cnt) > newcnt); \ 132 while ((cnt) > newcnt); \
130 \ 133 \
131 base = realloc (base, sizeof (*base) * (newcnt)); \ 134 base = realloc (base, sizeof (*base) * (newcnt)); \
132 init (base + cur, newcnt - cur); \ 135 init (base + cur, newcnt - cur); \
136/*****************************************************************************/ 139/*****************************************************************************/
137 140
138typedef struct 141typedef struct
139{ 142{
140 struct ev_io *head; 143 struct ev_io *head;
141 int events; 144 unsigned char events;
145 unsigned char reify;
142} ANFD; 146} ANFD;
143 147
144static ANFD *anfds; 148static ANFD *anfds;
145static int anfdmax; 149static int anfdmax;
146 150
149{ 153{
150 while (count--) 154 while (count--)
151 { 155 {
152 base->head = 0; 156 base->head = 0;
153 base->events = EV_NONE; 157 base->events = EV_NONE;
158 base->reify = 0;
159
154 ++base; 160 ++base;
155 } 161 }
156} 162}
157 163
158typedef struct 164typedef struct
165static int pendingmax, pendingcnt; 171static int pendingmax, pendingcnt;
166 172
167static void 173static void
168event (W w, int events) 174event (W w, int events)
169{ 175{
170 if (w->active) 176 if (w->pending)
177 {
178 pendings [w->pending - 1].events |= events;
179 return;
171 { 180 }
181
172 w->pending = ++pendingcnt; 182 w->pending = ++pendingcnt;
173 array_needsize (pendings, pendingmax, pendingcnt, ); 183 array_needsize (pendings, pendingmax, pendingcnt, );
174 pendings [pendingcnt - 1].w = w; 184 pendings [pendingcnt - 1].w = w;
175 pendings [pendingcnt - 1].events = events; 185 pendings [pendingcnt - 1].events = events;
176 }
177} 186}
178 187
179static void 188static void
180queue_events (W *events, int eventcnt, int type) 189queue_events (W *events, int eventcnt, int type)
181{ 190{
219 int events = 0; 228 int events = 0;
220 229
221 for (w = anfd->head; w; w = w->next) 230 for (w = anfd->head; w; w = w->next)
222 events |= w->events; 231 events |= w->events;
223 232
224 anfd->events &= ~EV_REIFY; 233 anfd->reify = 0;
225 234
226 if (anfd->events != events) 235 if (anfd->events != events)
227 { 236 {
228 method_modify (fd, anfd->events, events); 237 method_modify (fd, anfd->events, events);
229 anfd->events = events; 238 anfd->events = events;
234} 243}
235 244
236static void 245static void
237fd_change (int fd) 246fd_change (int fd)
238{ 247{
239 if (anfds [fd].events & EV_REIFY || fdchangecnt < 0) 248 if (anfds [fd].reify || fdchangecnt < 0)
240 return; 249 return;
241 250
242 anfds [fd].events |= EV_REIFY; 251 anfds [fd].reify = 1;
243 252
244 ++fdchangecnt; 253 ++fdchangecnt;
245 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 254 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
246 fdchanges [fdchangecnt - 1] = fd; 255 fdchanges [fdchangecnt - 1] = fd;
247} 256}
255 for (fd = 0; fd < anfdmax; ++fd) 264 for (fd = 0; fd < anfdmax; ++fd)
256 if (anfds [fd].events) 265 if (anfds [fd].events)
257 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 266 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
258 while (anfds [fd].head) 267 while (anfds [fd].head)
259 { 268 {
260 event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT);
261 ev_io_stop (anfds [fd].head); 269 ev_io_stop (anfds [fd].head);
270 event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE);
262 } 271 }
263} 272}
264 273
265/*****************************************************************************/ 274/*****************************************************************************/
266 275
314/*****************************************************************************/ 323/*****************************************************************************/
315 324
316typedef struct 325typedef struct
317{ 326{
318 struct ev_signal *head; 327 struct ev_signal *head;
319 sig_atomic_t gotsig; 328 sig_atomic_t volatile gotsig;
320} ANSIG; 329} ANSIG;
321 330
322static ANSIG *signals; 331static ANSIG *signals;
323static int signalmax; 332static int signalmax;
324 333
325static int sigpipe [2]; 334static int sigpipe [2];
326static sig_atomic_t gotsig; 335static sig_atomic_t volatile gotsig;
327static struct ev_io sigev; 336static struct ev_io sigev;
328 337
329static void 338static void
330signals_init (ANSIG *base, int count) 339signals_init (ANSIG *base, int count)
331{ 340{
332 while (count--) 341 while (count--)
333 { 342 {
334 base->head = 0; 343 base->head = 0;
335 base->gotsig = 0; 344 base->gotsig = 0;
345
336 ++base; 346 ++base;
337 } 347 }
338} 348}
339 349
340static void 350static void
343 signals [signum - 1].gotsig = 1; 353 signals [signum - 1].gotsig = 1;
344 354
345 if (!gotsig) 355 if (!gotsig)
346 { 356 {
347 gotsig = 1; 357 gotsig = 1;
348 write (sigpipe [1], &gotsig, 1); 358 write (sigpipe [1], &signum, 1);
349 } 359 }
350} 360}
351 361
352static void 362static void
353sigcb (struct ev_io *iow, int revents) 363sigcb (struct ev_io *iow, int revents)
354{ 364{
355 struct ev_signal *w; 365 struct ev_signal *w;
356 int sig; 366 int sig;
357 367
368 read (sigpipe [0], &revents, 1);
358 gotsig = 0; 369 gotsig = 0;
359 read (sigpipe [0], &revents, 1);
360 370
361 for (sig = signalmax; sig--; ) 371 for (sig = signalmax; sig--; )
362 if (signals [sig].gotsig) 372 if (signals [sig].gotsig)
363 { 373 {
364 signals [sig].gotsig = 0; 374 signals [sig].gotsig = 0;
479} 489}
480 490
481/*****************************************************************************/ 491/*****************************************************************************/
482 492
483void 493void
484ev_prefork (void) 494ev_fork_prepare (void)
485{ 495{
486 /* nop */ 496 /* nop */
487} 497}
488 498
489void 499void
490ev_postfork_parent (void) 500ev_fork_parent (void)
491{ 501{
492 /* nop */ 502 /* nop */
493} 503}
494 504
495void 505void
496ev_postfork_child (void) 506ev_fork_child (void)
497{ 507{
498#if EV_USE_EPOLL 508#if EV_USE_EPOLL
499 if (ev_method == EVMETHOD_EPOLL) 509 if (ev_method == EVMETHOD_EPOLL)
500 epoll_postfork_child (); 510 epoll_postfork_child ();
501#endif 511#endif
529{ 539{
530 while (timercnt && timers [0]->at <= now) 540 while (timercnt && timers [0]->at <= now)
531 { 541 {
532 struct ev_timer *w = timers [0]; 542 struct ev_timer *w = timers [0];
533 543
534 event ((W)w, EV_TIMEOUT);
535
536 /* first reschedule or stop timer */ 544 /* first reschedule or stop timer */
537 if (w->repeat) 545 if (w->repeat)
538 { 546 {
547 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
539 w->at = now + w->repeat; 548 w->at = now + w->repeat;
540 assert (("timer timeout in the past, negative repeat?", w->at > now));
541 downheap ((WT *)timers, timercnt, 0); 549 downheap ((WT *)timers, timercnt, 0);
542 } 550 }
543 else 551 else
544 ev_timer_stop (w); /* nonrepeating: stop timer */ 552 ev_timer_stop (w); /* nonrepeating: stop timer */
553
554 event ((W)w, EV_TIMEOUT);
545 } 555 }
546} 556}
547 557
548static void 558static void
549periodics_reify (void) 559periodics_reify (void)
554 564
555 /* first reschedule or stop timer */ 565 /* first reschedule or stop timer */
556 if (w->interval) 566 if (w->interval)
557 { 567 {
558 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;
559 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));
560 downheap ((WT *)periodics, periodiccnt, 0); 570 downheap ((WT *)periodics, periodiccnt, 0);
561 } 571 }
562 else 572 else
563 ev_periodic_stop (w); /* nonrepeating: stop timer */ 573 ev_periodic_stop (w); /* nonrepeating: stop timer */
564 574
565 event ((W)w, EV_TIMEOUT); 575 event ((W)w, EV_PERIODIC);
566 } 576 }
567} 577}
568 578
569static void 579static void
570periodics_reschedule (ev_tstamp diff) 580periodics_reschedule (ev_tstamp diff)
725 head = &(*head)->next; 735 head = &(*head)->next;
726 } 736 }
727} 737}
728 738
729static void 739static void
730ev_clear (W w) 740ev_clear_pending (W w)
731{ 741{
732 if (w->pending) 742 if (w->pending)
733 { 743 {
734 pendings [w->pending - 1].w = 0; 744 pendings [w->pending - 1].w = 0;
735 w->pending = 0; 745 w->pending = 0;
755{ 765{
756 if (ev_is_active (w)) 766 if (ev_is_active (w))
757 return; 767 return;
758 768
759 int fd = w->fd; 769 int fd = w->fd;
770
771 assert (("ev_io_start called with negative fd", fd >= 0));
760 772
761 ev_start ((W)w, 1); 773 ev_start ((W)w, 1);
762 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 774 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
763 wlist_add ((WL *)&anfds[fd].head, (WL)w); 775 wlist_add ((WL *)&anfds[fd].head, (WL)w);
764 776
766} 778}
767 779
768void 780void
769ev_io_stop (struct ev_io *w) 781ev_io_stop (struct ev_io *w)
770{ 782{
771 ev_clear ((W)w); 783 ev_clear_pending ((W)w);
772 if (!ev_is_active (w)) 784 if (!ev_is_active (w))
773 return; 785 return;
774 786
775 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 787 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
776 ev_stop ((W)w); 788 ev_stop ((W)w);
784 if (ev_is_active (w)) 796 if (ev_is_active (w))
785 return; 797 return;
786 798
787 w->at += now; 799 w->at += now;
788 800
789 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.));
790 802
791 ev_start ((W)w, ++timercnt); 803 ev_start ((W)w, ++timercnt);
792 array_needsize (timers, timermax, timercnt, ); 804 array_needsize (timers, timermax, timercnt, );
793 timers [timercnt - 1] = w; 805 timers [timercnt - 1] = w;
794 upheap ((WT *)timers, timercnt - 1); 806 upheap ((WT *)timers, timercnt - 1);
795} 807}
796 808
797void 809void
798ev_timer_stop (struct ev_timer *w) 810ev_timer_stop (struct ev_timer *w)
799{ 811{
800 ev_clear ((W)w); 812 ev_clear_pending ((W)w);
801 if (!ev_is_active (w)) 813 if (!ev_is_active (w))
802 return; 814 return;
803 815
804 if (w->active < timercnt--) 816 if (w->active < timercnt--)
805 { 817 {
833ev_periodic_start (struct ev_periodic *w) 845ev_periodic_start (struct ev_periodic *w)
834{ 846{
835 if (ev_is_active (w)) 847 if (ev_is_active (w))
836 return; 848 return;
837 849
838 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.));
839 851
840 /* 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 */
841 if (w->interval) 853 if (w->interval)
842 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; 854 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
843 855
848} 860}
849 861
850void 862void
851ev_periodic_stop (struct ev_periodic *w) 863ev_periodic_stop (struct ev_periodic *w)
852{ 864{
853 ev_clear ((W)w); 865 ev_clear_pending ((W)w);
854 if (!ev_is_active (w)) 866 if (!ev_is_active (w))
855 return; 867 return;
856 868
857 if (w->active < periodiccnt--) 869 if (w->active < periodiccnt--)
858 { 870 {
866void 878void
867ev_signal_start (struct ev_signal *w) 879ev_signal_start (struct ev_signal *w)
868{ 880{
869 if (ev_is_active (w)) 881 if (ev_is_active (w))
870 return; 882 return;
883
884 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
871 885
872 ev_start ((W)w, 1); 886 ev_start ((W)w, 1);
873 array_needsize (signals, signalmax, w->signum, signals_init); 887 array_needsize (signals, signalmax, w->signum, signals_init);
874 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 888 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
875 889
884} 898}
885 899
886void 900void
887ev_signal_stop (struct ev_signal *w) 901ev_signal_stop (struct ev_signal *w)
888{ 902{
889 ev_clear ((W)w); 903 ev_clear_pending ((W)w);
890 if (!ev_is_active (w)) 904 if (!ev_is_active (w))
891 return; 905 return;
892 906
893 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 907 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
894 ev_stop ((W)w); 908 ev_stop ((W)w);
909} 923}
910 924
911void 925void
912ev_idle_stop (struct ev_idle *w) 926ev_idle_stop (struct ev_idle *w)
913{ 927{
914 ev_clear ((W)w); 928 ev_clear_pending ((W)w);
915 if (ev_is_active (w)) 929 if (ev_is_active (w))
916 return; 930 return;
917 931
918 idles [w->active - 1] = idles [--idlecnt]; 932 idles [w->active - 1] = idles [--idlecnt];
919 ev_stop ((W)w); 933 ev_stop ((W)w);
931} 945}
932 946
933void 947void
934ev_prepare_stop (struct ev_prepare *w) 948ev_prepare_stop (struct ev_prepare *w)
935{ 949{
936 ev_clear ((W)w); 950 ev_clear_pending ((W)w);
937 if (ev_is_active (w)) 951 if (ev_is_active (w))
938 return; 952 return;
939 953
940 prepares [w->active - 1] = prepares [--preparecnt]; 954 prepares [w->active - 1] = prepares [--preparecnt];
941 ev_stop ((W)w); 955 ev_stop ((W)w);
953} 967}
954 968
955void 969void
956ev_check_stop (struct ev_check *w) 970ev_check_stop (struct ev_check *w)
957{ 971{
958 ev_clear ((W)w); 972 ev_clear_pending ((W)w);
959 if (ev_is_active (w)) 973 if (ev_is_active (w))
960 return; 974 return;
961 975
962 checks [w->active - 1] = checks [--checkcnt]; 976 checks [w->active - 1] = checks [--checkcnt];
963 ev_stop ((W)w); 977 ev_stop ((W)w);
974} 988}
975 989
976void 990void
977ev_child_stop (struct ev_child *w) 991ev_child_stop (struct ev_child *w)
978{ 992{
979 ev_clear ((W)w); 993 ev_clear_pending ((W)w);
980 if (ev_is_active (w)) 994 if (ev_is_active (w))
981 return; 995 return;
982 996
983 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 997 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
984 ev_stop ((W)w); 998 ev_stop ((W)w);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines