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