… | |
… | |
92 | |
92 | |
93 | #ifndef EV_USE_KQUEUE |
93 | #ifndef EV_USE_KQUEUE |
94 | # define EV_USE_KQUEUE 0 |
94 | # define EV_USE_KQUEUE 0 |
95 | #endif |
95 | #endif |
96 | |
96 | |
|
|
97 | #ifndef EV_USE_WIN32 |
|
|
98 | # ifdef WIN32 |
|
|
99 | # define EV_USE_WIN32 1 |
|
|
100 | # else |
|
|
101 | # define EV_USE_WIN32 0 |
|
|
102 | # endif |
|
|
103 | #endif |
|
|
104 | |
97 | #ifndef EV_USE_REALTIME |
105 | #ifndef EV_USE_REALTIME |
98 | # define EV_USE_REALTIME 1 |
106 | # define EV_USE_REALTIME 1 |
99 | #endif |
107 | #endif |
100 | |
108 | |
101 | /**/ |
109 | /**/ |
… | |
… | |
298 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
306 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
299 | events |= w->events; |
307 | events |= w->events; |
300 | |
308 | |
301 | anfd->reify = 0; |
309 | anfd->reify = 0; |
302 | |
310 | |
303 | if (anfd->events != events) |
|
|
304 | { |
|
|
305 | method_modify (EV_A_ fd, anfd->events, events); |
311 | method_modify (EV_A_ fd, anfd->events, events); |
306 | anfd->events = events; |
312 | anfd->events = events; |
307 | } |
|
|
308 | } |
313 | } |
309 | |
314 | |
310 | fdchangecnt = 0; |
315 | fdchangecnt = 0; |
311 | } |
316 | } |
312 | |
317 | |
… | |
… | |
349 | |
354 | |
350 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
355 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
351 | static void |
356 | static void |
352 | fd_enomem (EV_P) |
357 | fd_enomem (EV_P) |
353 | { |
358 | { |
354 | int fd = anfdmax; |
359 | int fd; |
355 | |
360 | |
356 | while (fd--) |
361 | for (fd = anfdmax; fd--; ) |
357 | if (anfds [fd].events) |
362 | if (anfds [fd].events) |
358 | { |
363 | { |
359 | close (fd); |
364 | close (fd); |
360 | fd_kill (EV_A_ fd); |
365 | fd_kill (EV_A_ fd); |
361 | return; |
366 | return; |
… | |
… | |
385 | WT w = heap [k]; |
390 | WT w = heap [k]; |
386 | |
391 | |
387 | while (k && heap [k >> 1]->at > w->at) |
392 | while (k && heap [k >> 1]->at > w->at) |
388 | { |
393 | { |
389 | heap [k] = heap [k >> 1]; |
394 | heap [k] = heap [k >> 1]; |
390 | heap [k]->active = k + 1; |
395 | ((W)heap [k])->active = k + 1; |
391 | k >>= 1; |
396 | k >>= 1; |
392 | } |
397 | } |
393 | |
398 | |
394 | heap [k] = w; |
399 | heap [k] = w; |
395 | heap [k]->active = k + 1; |
400 | ((W)heap [k])->active = k + 1; |
396 | |
401 | |
397 | } |
402 | } |
398 | |
403 | |
399 | static void |
404 | static void |
400 | downheap (WT *heap, int N, int k) |
405 | downheap (WT *heap, int N, int k) |
… | |
… | |
410 | |
415 | |
411 | if (w->at <= heap [j]->at) |
416 | if (w->at <= heap [j]->at) |
412 | break; |
417 | break; |
413 | |
418 | |
414 | heap [k] = heap [j]; |
419 | heap [k] = heap [j]; |
415 | heap [k]->active = k + 1; |
420 | ((W)heap [k])->active = k + 1; |
416 | k = j; |
421 | k = j; |
417 | } |
422 | } |
418 | |
423 | |
419 | heap [k] = w; |
424 | heap [k] = w; |
420 | heap [k]->active = k + 1; |
425 | ((W)heap [k])->active = k + 1; |
421 | } |
426 | } |
422 | |
427 | |
423 | /*****************************************************************************/ |
428 | /*****************************************************************************/ |
424 | |
429 | |
425 | typedef struct |
430 | typedef struct |
… | |
… | |
514 | struct ev_child *w; |
519 | struct ev_child *w; |
515 | |
520 | |
516 | for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) |
521 | for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) |
517 | if (w->pid == pid || !w->pid) |
522 | if (w->pid == pid || !w->pid) |
518 | { |
523 | { |
519 | w->priority = sw->priority; /* need to do it *now* */ |
524 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
520 | w->rpid = pid; |
525 | w->rpid = pid; |
521 | w->rstatus = status; |
526 | w->rstatus = status; |
522 | event (EV_A_ (W)w, EV_CHILD); |
527 | event (EV_A_ (W)w, EV_CHILD); |
523 | } |
528 | } |
524 | } |
529 | } |
525 | |
530 | |
526 | static void |
531 | static void |
… | |
… | |
608 | methods = atoi (getenv ("LIBEV_METHODS")); |
613 | methods = atoi (getenv ("LIBEV_METHODS")); |
609 | else |
614 | else |
610 | methods = EVMETHOD_ANY; |
615 | methods = EVMETHOD_ANY; |
611 | |
616 | |
612 | method = 0; |
617 | method = 0; |
|
|
618 | #if EV_USE_WIN32 |
|
|
619 | if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods); |
|
|
620 | #endif |
613 | #if EV_USE_KQUEUE |
621 | #if EV_USE_KQUEUE |
614 | if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); |
622 | if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); |
615 | #endif |
623 | #endif |
616 | #if EV_USE_EPOLL |
624 | #if EV_USE_EPOLL |
617 | if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); |
625 | if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); |
… | |
… | |
626 | } |
634 | } |
627 | |
635 | |
628 | void |
636 | void |
629 | loop_destroy (EV_P) |
637 | loop_destroy (EV_P) |
630 | { |
638 | { |
|
|
639 | #if EV_USE_WIN32 |
|
|
640 | if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A); |
|
|
641 | #endif |
631 | #if EV_USE_KQUEUE |
642 | #if EV_USE_KQUEUE |
632 | if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); |
643 | if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); |
633 | #endif |
644 | #endif |
634 | #if EV_USE_EPOLL |
645 | #if EV_USE_EPOLL |
635 | if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); |
646 | if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); |
… | |
… | |
782 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
793 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
783 | |
794 | |
784 | if (p->w) |
795 | if (p->w) |
785 | { |
796 | { |
786 | p->w->pending = 0; |
797 | p->w->pending = 0; |
|
|
798 | |
787 | p->w->cb (EV_A_ p->w, p->events); |
799 | (*(void (**)(EV_P_ W, int))&p->w->cb) (EV_A_ p->w, p->events); |
788 | } |
800 | } |
789 | } |
801 | } |
790 | } |
802 | } |
791 | |
803 | |
792 | static void |
804 | static void |
793 | timers_reify (EV_P) |
805 | timers_reify (EV_P) |
794 | { |
806 | { |
795 | while (timercnt && timers [0]->at <= mn_now) |
807 | while (timercnt && ((WT)timers [0])->at <= mn_now) |
796 | { |
808 | { |
797 | struct ev_timer *w = timers [0]; |
809 | struct ev_timer *w = timers [0]; |
798 | |
810 | |
799 | assert (("inactive timer on timer heap detected", ev_is_active (w))); |
811 | assert (("inactive timer on timer heap detected", ev_is_active (w))); |
800 | |
812 | |
801 | /* first reschedule or stop timer */ |
813 | /* first reschedule or stop timer */ |
802 | if (w->repeat) |
814 | if (w->repeat) |
803 | { |
815 | { |
804 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
816 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
805 | w->at = mn_now + w->repeat; |
817 | ((WT)w)->at = mn_now + w->repeat; |
806 | downheap ((WT *)timers, timercnt, 0); |
818 | downheap ((WT *)timers, timercnt, 0); |
807 | } |
819 | } |
808 | else |
820 | else |
809 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
821 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
810 | |
822 | |
… | |
… | |
813 | } |
825 | } |
814 | |
826 | |
815 | static void |
827 | static void |
816 | periodics_reify (EV_P) |
828 | periodics_reify (EV_P) |
817 | { |
829 | { |
818 | while (periodiccnt && periodics [0]->at <= rt_now) |
830 | while (periodiccnt && ((WT)periodics [0])->at <= rt_now) |
819 | { |
831 | { |
820 | struct ev_periodic *w = periodics [0]; |
832 | struct ev_periodic *w = periodics [0]; |
821 | |
833 | |
822 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
834 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
823 | |
835 | |
824 | /* first reschedule or stop timer */ |
836 | /* first reschedule or stop timer */ |
825 | if (w->interval) |
837 | if (w->interval) |
826 | { |
838 | { |
827 | w->at += floor ((rt_now - w->at) / w->interval + 1.) * w->interval; |
839 | ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; |
828 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > rt_now)); |
840 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now)); |
829 | downheap ((WT *)periodics, periodiccnt, 0); |
841 | downheap ((WT *)periodics, periodiccnt, 0); |
830 | } |
842 | } |
831 | else |
843 | else |
832 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
844 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
833 | |
845 | |
… | |
… | |
845 | { |
857 | { |
846 | struct ev_periodic *w = periodics [i]; |
858 | struct ev_periodic *w = periodics [i]; |
847 | |
859 | |
848 | if (w->interval) |
860 | if (w->interval) |
849 | { |
861 | { |
850 | ev_tstamp diff = ceil ((rt_now - w->at) / w->interval) * w->interval; |
862 | ev_tstamp diff = ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
851 | |
863 | |
852 | if (fabs (diff) >= 1e-4) |
864 | if (fabs (diff) >= 1e-4) |
853 | { |
865 | { |
854 | ev_periodic_stop (EV_A_ w); |
866 | ev_periodic_stop (EV_A_ w); |
855 | ev_periodic_start (EV_A_ w); |
867 | ev_periodic_start (EV_A_ w); |
… | |
… | |
916 | { |
928 | { |
917 | periodics_reschedule (EV_A); |
929 | periodics_reschedule (EV_A); |
918 | |
930 | |
919 | /* adjust timers. this is easy, as the offset is the same for all */ |
931 | /* adjust timers. this is easy, as the offset is the same for all */ |
920 | for (i = 0; i < timercnt; ++i) |
932 | for (i = 0; i < timercnt; ++i) |
921 | timers [i]->at += rt_now - mn_now; |
933 | ((WT)timers [i])->at += rt_now - mn_now; |
922 | } |
934 | } |
923 | |
935 | |
924 | mn_now = rt_now; |
936 | mn_now = rt_now; |
925 | } |
937 | } |
926 | } |
938 | } |
… | |
… | |
977 | { |
989 | { |
978 | block = MAX_BLOCKTIME; |
990 | block = MAX_BLOCKTIME; |
979 | |
991 | |
980 | if (timercnt) |
992 | if (timercnt) |
981 | { |
993 | { |
982 | ev_tstamp to = timers [0]->at - mn_now + method_fudge; |
994 | ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; |
983 | if (block > to) block = to; |
995 | if (block > to) block = to; |
984 | } |
996 | } |
985 | |
997 | |
986 | if (periodiccnt) |
998 | if (periodiccnt) |
987 | { |
999 | { |
988 | ev_tstamp to = periodics [0]->at - rt_now + method_fudge; |
1000 | ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge; |
989 | if (block > to) block = to; |
1001 | if (block > to) block = to; |
990 | } |
1002 | } |
991 | |
1003 | |
992 | if (block < 0.) block = 0.; |
1004 | if (block < 0.) block = 0.; |
993 | } |
1005 | } |
… | |
… | |
1110 | ev_timer_start (EV_P_ struct ev_timer *w) |
1122 | ev_timer_start (EV_P_ struct ev_timer *w) |
1111 | { |
1123 | { |
1112 | if (ev_is_active (w)) |
1124 | if (ev_is_active (w)) |
1113 | return; |
1125 | return; |
1114 | |
1126 | |
1115 | w->at += mn_now; |
1127 | ((WT)w)->at += mn_now; |
1116 | |
1128 | |
1117 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1129 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1118 | |
1130 | |
1119 | ev_start (EV_A_ (W)w, ++timercnt); |
1131 | ev_start (EV_A_ (W)w, ++timercnt); |
1120 | array_needsize (timers, timermax, timercnt, ); |
1132 | array_needsize (timers, timermax, timercnt, ); |
1121 | timers [timercnt - 1] = w; |
1133 | timers [timercnt - 1] = w; |
1122 | upheap ((WT *)timers, timercnt - 1); |
1134 | upheap ((WT *)timers, timercnt - 1); |
|
|
1135 | |
|
|
1136 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1123 | } |
1137 | } |
1124 | |
1138 | |
1125 | void |
1139 | void |
1126 | ev_timer_stop (EV_P_ struct ev_timer *w) |
1140 | ev_timer_stop (EV_P_ struct ev_timer *w) |
1127 | { |
1141 | { |
1128 | ev_clear_pending (EV_A_ (W)w); |
1142 | ev_clear_pending (EV_A_ (W)w); |
1129 | if (!ev_is_active (w)) |
1143 | if (!ev_is_active (w)) |
1130 | return; |
1144 | return; |
1131 | |
1145 | |
|
|
1146 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
|
|
1147 | |
1132 | if (w->active < timercnt--) |
1148 | if (((W)w)->active < timercnt--) |
1133 | { |
1149 | { |
1134 | timers [w->active - 1] = timers [timercnt]; |
1150 | timers [((W)w)->active - 1] = timers [timercnt]; |
1135 | downheap ((WT *)timers, timercnt, w->active - 1); |
1151 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1136 | } |
1152 | } |
1137 | |
1153 | |
1138 | w->at = w->repeat; |
1154 | ((WT)w)->at = w->repeat; |
1139 | |
1155 | |
1140 | ev_stop (EV_A_ (W)w); |
1156 | ev_stop (EV_A_ (W)w); |
1141 | } |
1157 | } |
1142 | |
1158 | |
1143 | void |
1159 | void |
… | |
… | |
1145 | { |
1161 | { |
1146 | if (ev_is_active (w)) |
1162 | if (ev_is_active (w)) |
1147 | { |
1163 | { |
1148 | if (w->repeat) |
1164 | if (w->repeat) |
1149 | { |
1165 | { |
1150 | w->at = mn_now + w->repeat; |
1166 | ((WT)w)->at = mn_now + w->repeat; |
1151 | downheap ((WT *)timers, timercnt, w->active - 1); |
1167 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1152 | } |
1168 | } |
1153 | else |
1169 | else |
1154 | ev_timer_stop (EV_A_ w); |
1170 | ev_timer_stop (EV_A_ w); |
1155 | } |
1171 | } |
1156 | else if (w->repeat) |
1172 | else if (w->repeat) |
… | |
… | |
1165 | |
1181 | |
1166 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1182 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1167 | |
1183 | |
1168 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1184 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1169 | if (w->interval) |
1185 | if (w->interval) |
1170 | w->at += ceil ((rt_now - w->at) / w->interval) * w->interval; |
1186 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1171 | |
1187 | |
1172 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1188 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1173 | array_needsize (periodics, periodicmax, periodiccnt, ); |
1189 | array_needsize (periodics, periodicmax, periodiccnt, ); |
1174 | periodics [periodiccnt - 1] = w; |
1190 | periodics [periodiccnt - 1] = w; |
1175 | upheap ((WT *)periodics, periodiccnt - 1); |
1191 | upheap ((WT *)periodics, periodiccnt - 1); |
|
|
1192 | |
|
|
1193 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1176 | } |
1194 | } |
1177 | |
1195 | |
1178 | void |
1196 | void |
1179 | ev_periodic_stop (EV_P_ struct ev_periodic *w) |
1197 | ev_periodic_stop (EV_P_ struct ev_periodic *w) |
1180 | { |
1198 | { |
1181 | ev_clear_pending (EV_A_ (W)w); |
1199 | ev_clear_pending (EV_A_ (W)w); |
1182 | if (!ev_is_active (w)) |
1200 | if (!ev_is_active (w)) |
1183 | return; |
1201 | return; |
1184 | |
1202 | |
|
|
1203 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
|
|
1204 | |
1185 | if (w->active < periodiccnt--) |
1205 | if (((W)w)->active < periodiccnt--) |
1186 | { |
1206 | { |
1187 | periodics [w->active - 1] = periodics [periodiccnt]; |
1207 | periodics [((W)w)->active - 1] = periodics [periodiccnt]; |
1188 | downheap ((WT *)periodics, periodiccnt, w->active - 1); |
1208 | downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); |
1189 | } |
1209 | } |
1190 | |
1210 | |
1191 | ev_stop (EV_A_ (W)w); |
1211 | ev_stop (EV_A_ (W)w); |
1192 | } |
1212 | } |
1193 | |
1213 | |
… | |
… | |
1207 | { |
1227 | { |
1208 | ev_clear_pending (EV_A_ (W)w); |
1228 | ev_clear_pending (EV_A_ (W)w); |
1209 | if (ev_is_active (w)) |
1229 | if (ev_is_active (w)) |
1210 | return; |
1230 | return; |
1211 | |
1231 | |
1212 | idles [w->active - 1] = idles [--idlecnt]; |
1232 | idles [((W)w)->active - 1] = idles [--idlecnt]; |
1213 | ev_stop (EV_A_ (W)w); |
1233 | ev_stop (EV_A_ (W)w); |
1214 | } |
1234 | } |
1215 | |
1235 | |
1216 | void |
1236 | void |
1217 | ev_prepare_start (EV_P_ struct ev_prepare *w) |
1237 | ev_prepare_start (EV_P_ struct ev_prepare *w) |
… | |
… | |
1229 | { |
1249 | { |
1230 | ev_clear_pending (EV_A_ (W)w); |
1250 | ev_clear_pending (EV_A_ (W)w); |
1231 | if (ev_is_active (w)) |
1251 | if (ev_is_active (w)) |
1232 | return; |
1252 | return; |
1233 | |
1253 | |
1234 | prepares [w->active - 1] = prepares [--preparecnt]; |
1254 | prepares [((W)w)->active - 1] = prepares [--preparecnt]; |
1235 | ev_stop (EV_A_ (W)w); |
1255 | ev_stop (EV_A_ (W)w); |
1236 | } |
1256 | } |
1237 | |
1257 | |
1238 | void |
1258 | void |
1239 | ev_check_start (EV_P_ struct ev_check *w) |
1259 | ev_check_start (EV_P_ struct ev_check *w) |
… | |
… | |
1251 | { |
1271 | { |
1252 | ev_clear_pending (EV_A_ (W)w); |
1272 | ev_clear_pending (EV_A_ (W)w); |
1253 | if (ev_is_active (w)) |
1273 | if (ev_is_active (w)) |
1254 | return; |
1274 | return; |
1255 | |
1275 | |
1256 | checks [w->active - 1] = checks [--checkcnt]; |
1276 | checks [((W)w)->active - 1] = checks [--checkcnt]; |
1257 | ev_stop (EV_A_ (W)w); |
1277 | ev_stop (EV_A_ (W)w); |
1258 | } |
1278 | } |
1259 | |
1279 | |
1260 | #ifndef SA_RESTART |
1280 | #ifndef SA_RESTART |
1261 | # define SA_RESTART 0 |
1281 | # define SA_RESTART 0 |
… | |
… | |
1274 | |
1294 | |
1275 | ev_start (EV_A_ (W)w, 1); |
1295 | ev_start (EV_A_ (W)w, 1); |
1276 | array_needsize (signals, signalmax, w->signum, signals_init); |
1296 | array_needsize (signals, signalmax, w->signum, signals_init); |
1277 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1297 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1278 | |
1298 | |
1279 | if (!w->next) |
1299 | if (!((WL)w)->next) |
1280 | { |
1300 | { |
1281 | struct sigaction sa; |
1301 | struct sigaction sa; |
1282 | sa.sa_handler = sighandler; |
1302 | sa.sa_handler = sighandler; |
1283 | sigfillset (&sa.sa_mask); |
1303 | sigfillset (&sa.sa_mask); |
1284 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
1304 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |