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