… | |
… | |
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 |
… | |
… | |
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); |
… | |
… | |
793 | timers_reify (EV_P) |
807 | timers_reify (EV_P) |
794 | { |
808 | { |
795 | while (timercnt && timers [0]->at <= mn_now) |
809 | while (timercnt && timers [0]->at <= mn_now) |
796 | { |
810 | { |
797 | struct ev_timer *w = timers [0]; |
811 | struct ev_timer *w = timers [0]; |
|
|
812 | |
|
|
813 | assert (("inactive timer on timer heap detected", ev_is_active (w))); |
798 | |
814 | |
799 | /* first reschedule or stop timer */ |
815 | /* first reschedule or stop timer */ |
800 | if (w->repeat) |
816 | if (w->repeat) |
801 | { |
817 | { |
802 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
818 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
… | |
… | |
815 | { |
831 | { |
816 | while (periodiccnt && periodics [0]->at <= rt_now) |
832 | while (periodiccnt && periodics [0]->at <= rt_now) |
817 | { |
833 | { |
818 | struct ev_periodic *w = periodics [0]; |
834 | struct ev_periodic *w = periodics [0]; |
819 | |
835 | |
|
|
836 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
|
|
837 | |
820 | /* first reschedule or stop timer */ |
838 | /* first reschedule or stop timer */ |
821 | if (w->interval) |
839 | if (w->interval) |
822 | { |
840 | { |
823 | w->at += floor ((rt_now - w->at) / w->interval + 1.) * w->interval; |
841 | w->at += floor ((rt_now - 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)); |
842 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > rt_now)); |
… | |
… | |
1114 | |
1132 | |
1115 | ev_start (EV_A_ (W)w, ++timercnt); |
1133 | ev_start (EV_A_ (W)w, ++timercnt); |
1116 | array_needsize (timers, timermax, timercnt, ); |
1134 | array_needsize (timers, timermax, timercnt, ); |
1117 | timers [timercnt - 1] = w; |
1135 | timers [timercnt - 1] = w; |
1118 | upheap ((WT *)timers, timercnt - 1); |
1136 | upheap ((WT *)timers, timercnt - 1); |
|
|
1137 | |
|
|
1138 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1119 | } |
1139 | } |
1120 | |
1140 | |
1121 | void |
1141 | void |
1122 | ev_timer_stop (EV_P_ struct ev_timer *w) |
1142 | ev_timer_stop (EV_P_ struct ev_timer *w) |
1123 | { |
1143 | { |
1124 | ev_clear_pending (EV_A_ (W)w); |
1144 | ev_clear_pending (EV_A_ (W)w); |
1125 | if (!ev_is_active (w)) |
1145 | if (!ev_is_active (w)) |
1126 | return; |
1146 | return; |
1127 | |
1147 | |
|
|
1148 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
|
|
1149 | |
1128 | if (w->active < timercnt--) |
1150 | if (((W)w)->active < timercnt--) |
1129 | { |
1151 | { |
1130 | timers [w->active - 1] = timers [timercnt]; |
1152 | timers [((W)w)->active - 1] = timers [timercnt]; |
1131 | downheap ((WT *)timers, timercnt, w->active - 1); |
1153 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1132 | } |
1154 | } |
1133 | |
1155 | |
1134 | w->at = w->repeat; |
1156 | w->at = w->repeat; |
1135 | |
1157 | |
1136 | ev_stop (EV_A_ (W)w); |
1158 | ev_stop (EV_A_ (W)w); |
… | |
… | |
1142 | if (ev_is_active (w)) |
1164 | if (ev_is_active (w)) |
1143 | { |
1165 | { |
1144 | if (w->repeat) |
1166 | if (w->repeat) |
1145 | { |
1167 | { |
1146 | w->at = mn_now + w->repeat; |
1168 | w->at = mn_now + w->repeat; |
1147 | downheap ((WT *)timers, timercnt, w->active - 1); |
1169 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1148 | } |
1170 | } |
1149 | else |
1171 | else |
1150 | ev_timer_stop (EV_A_ w); |
1172 | ev_timer_stop (EV_A_ w); |
1151 | } |
1173 | } |
1152 | else if (w->repeat) |
1174 | else if (w->repeat) |
… | |
… | |
1167 | |
1189 | |
1168 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1190 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1169 | array_needsize (periodics, periodicmax, periodiccnt, ); |
1191 | array_needsize (periodics, periodicmax, periodiccnt, ); |
1170 | periodics [periodiccnt - 1] = w; |
1192 | periodics [periodiccnt - 1] = w; |
1171 | upheap ((WT *)periodics, periodiccnt - 1); |
1193 | upheap ((WT *)periodics, periodiccnt - 1); |
|
|
1194 | |
|
|
1195 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1172 | } |
1196 | } |
1173 | |
1197 | |
1174 | void |
1198 | void |
1175 | ev_periodic_stop (EV_P_ struct ev_periodic *w) |
1199 | ev_periodic_stop (EV_P_ struct ev_periodic *w) |
1176 | { |
1200 | { |
1177 | ev_clear_pending (EV_A_ (W)w); |
1201 | ev_clear_pending (EV_A_ (W)w); |
1178 | if (!ev_is_active (w)) |
1202 | if (!ev_is_active (w)) |
1179 | return; |
1203 | return; |
1180 | |
1204 | |
|
|
1205 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
|
|
1206 | |
1181 | if (w->active < periodiccnt--) |
1207 | if (((W)w)->active < periodiccnt--) |
1182 | { |
1208 | { |
1183 | periodics [w->active - 1] = periodics [periodiccnt]; |
1209 | periodics [((W)w)->active - 1] = periodics [periodiccnt]; |
1184 | downheap ((WT *)periodics, periodiccnt, w->active - 1); |
1210 | downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); |
1185 | } |
1211 | } |
1186 | |
1212 | |
1187 | ev_stop (EV_A_ (W)w); |
1213 | ev_stop (EV_A_ (W)w); |
1188 | } |
1214 | } |
1189 | |
1215 | |
… | |
… | |
1203 | { |
1229 | { |
1204 | ev_clear_pending (EV_A_ (W)w); |
1230 | ev_clear_pending (EV_A_ (W)w); |
1205 | if (ev_is_active (w)) |
1231 | if (ev_is_active (w)) |
1206 | return; |
1232 | return; |
1207 | |
1233 | |
1208 | idles [w->active - 1] = idles [--idlecnt]; |
1234 | idles [((W)w)->active - 1] = idles [--idlecnt]; |
1209 | ev_stop (EV_A_ (W)w); |
1235 | ev_stop (EV_A_ (W)w); |
1210 | } |
1236 | } |
1211 | |
1237 | |
1212 | void |
1238 | void |
1213 | ev_prepare_start (EV_P_ struct ev_prepare *w) |
1239 | ev_prepare_start (EV_P_ struct ev_prepare *w) |
… | |
… | |
1225 | { |
1251 | { |
1226 | ev_clear_pending (EV_A_ (W)w); |
1252 | ev_clear_pending (EV_A_ (W)w); |
1227 | if (ev_is_active (w)) |
1253 | if (ev_is_active (w)) |
1228 | return; |
1254 | return; |
1229 | |
1255 | |
1230 | prepares [w->active - 1] = prepares [--preparecnt]; |
1256 | prepares [((W)w)->active - 1] = prepares [--preparecnt]; |
1231 | ev_stop (EV_A_ (W)w); |
1257 | ev_stop (EV_A_ (W)w); |
1232 | } |
1258 | } |
1233 | |
1259 | |
1234 | void |
1260 | void |
1235 | ev_check_start (EV_P_ struct ev_check *w) |
1261 | ev_check_start (EV_P_ struct ev_check *w) |
… | |
… | |
1247 | { |
1273 | { |
1248 | ev_clear_pending (EV_A_ (W)w); |
1274 | ev_clear_pending (EV_A_ (W)w); |
1249 | if (ev_is_active (w)) |
1275 | if (ev_is_active (w)) |
1250 | return; |
1276 | return; |
1251 | |
1277 | |
1252 | checks [w->active - 1] = checks [--checkcnt]; |
1278 | checks [((W)w)->active - 1] = checks [--checkcnt]; |
1253 | ev_stop (EV_A_ (W)w); |
1279 | ev_stop (EV_A_ (W)w); |
1254 | } |
1280 | } |
1255 | |
1281 | |
1256 | #ifndef SA_RESTART |
1282 | #ifndef SA_RESTART |
1257 | # define SA_RESTART 0 |
1283 | # define SA_RESTART 0 |