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