… | |
… | |
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 | |
73 | typedef struct ev_watcher *W; |
77 | typedef struct ev_watcher *W; |
74 | typedef struct ev_watcher_list *WL; |
78 | typedef 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 | |
138 | typedef struct |
141 | typedef 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 | |
144 | static ANFD *anfds; |
148 | static ANFD *anfds; |
145 | static int anfdmax; |
149 | static 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 | |
158 | typedef struct |
164 | typedef struct |
… | |
… | |
165 | static int pendingmax, pendingcnt; |
171 | static int pendingmax, pendingcnt; |
166 | |
172 | |
167 | static void |
173 | static void |
168 | event (W w, int events) |
174 | event (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 | |
179 | static void |
188 | static void |
180 | queue_events (W *events, int eventcnt, int type) |
189 | queue_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 | |
236 | static void |
245 | static void |
237 | fd_change (int fd) |
246 | fd_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 | |
316 | typedef struct |
325 | typedef 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 | |
322 | static ANSIG *signals; |
331 | static ANSIG *signals; |
323 | static int signalmax; |
332 | static int signalmax; |
324 | |
333 | |
325 | static int sigpipe [2]; |
334 | static int sigpipe [2]; |
326 | static sig_atomic_t gotsig; |
335 | static sig_atomic_t volatile gotsig; |
327 | static struct ev_io sigev; |
336 | static struct ev_io sigev; |
328 | |
337 | |
329 | static void |
338 | static void |
330 | signals_init (ANSIG *base, int count) |
339 | signals_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 | |
340 | static void |
350 | static 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 | |
352 | static void |
362 | static void |
353 | sigcb (struct ev_io *iow, int revents) |
363 | sigcb (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 | |
483 | void |
493 | void |
484 | ev_prefork (void) |
494 | ev_fork_prepare (void) |
485 | { |
495 | { |
486 | /* nop */ |
496 | /* nop */ |
487 | } |
497 | } |
488 | |
498 | |
489 | void |
499 | void |
490 | ev_postfork_parent (void) |
500 | ev_fork_parent (void) |
491 | { |
501 | { |
492 | /* nop */ |
502 | /* nop */ |
493 | } |
503 | } |
494 | |
504 | |
495 | void |
505 | void |
496 | ev_postfork_child (void) |
506 | ev_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 | |
548 | static void |
558 | static void |
549 | periodics_reify (void) |
559 | periodics_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 | |
569 | static void |
579 | static void |
570 | periodics_reschedule (ev_tstamp diff) |
580 | periodics_reschedule (ev_tstamp diff) |
… | |
… | |
725 | head = &(*head)->next; |
735 | head = &(*head)->next; |
726 | } |
736 | } |
727 | } |
737 | } |
728 | |
738 | |
729 | static void |
739 | static void |
730 | ev_clear (W w) |
740 | ev_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 | |
768 | void |
780 | void |
769 | ev_io_stop (struct ev_io *w) |
781 | ev_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 | |
797 | void |
809 | void |
798 | ev_timer_stop (struct ev_timer *w) |
810 | ev_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 | { |
… | |
… | |
833 | ev_periodic_start (struct ev_periodic *w) |
845 | ev_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 | |
850 | void |
862 | void |
851 | ev_periodic_stop (struct ev_periodic *w) |
863 | ev_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 | { |
… | |
… | |
866 | void |
878 | void |
867 | ev_signal_start (struct ev_signal *w) |
879 | ev_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 | |
886 | void |
900 | void |
887 | ev_signal_stop (struct ev_signal *w) |
901 | ev_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 | |
911 | void |
925 | void |
912 | ev_idle_stop (struct ev_idle *w) |
926 | ev_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 | |
933 | void |
947 | void |
934 | ev_prepare_stop (struct ev_prepare *w) |
948 | ev_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 | |
955 | void |
969 | void |
956 | ev_check_stop (struct ev_check *w) |
970 | ev_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 | |
976 | void |
990 | void |
977 | ev_child_stop (struct ev_child *w) |
991 | ev_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); |