… | |
… | |
28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 | */ |
30 | */ |
31 | #ifndef EV_STANDALONE |
31 | #ifndef EV_STANDALONE |
32 | # include "config.h" |
32 | # include "config.h" |
|
|
33 | |
|
|
34 | # if HAVE_CLOCK_GETTIME |
|
|
35 | # define EV_USE_MONOTONIC 1 |
|
|
36 | # define EV_USE_REALTIME 1 |
|
|
37 | # endif |
|
|
38 | |
|
|
39 | # if HAVE_SELECT && HAVE_SYS_SELECT_H |
|
|
40 | # define EV_USE_SELECT 1 |
|
|
41 | # endif |
|
|
42 | |
|
|
43 | # if HAVE_POLL && HAVE_POLL_H |
|
|
44 | # define EV_USE_POLL 1 |
|
|
45 | # endif |
|
|
46 | |
|
|
47 | # if HAVE_EPOLL && HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H |
|
|
48 | # define EV_USE_EPOLL 1 |
|
|
49 | # endif |
|
|
50 | |
|
|
51 | # if HAVE_KQUEUE && HAVE_WORKING_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H |
|
|
52 | # define EV_USE_KQUEUE 1 |
|
|
53 | # endif |
|
|
54 | |
33 | #endif |
55 | #endif |
34 | |
56 | |
35 | #include <math.h> |
57 | #include <math.h> |
36 | #include <stdlib.h> |
58 | #include <stdlib.h> |
37 | #include <unistd.h> |
59 | #include <unistd.h> |
… | |
… | |
68 | # define EV_USE_EPOLL 0 |
90 | # define EV_USE_EPOLL 0 |
69 | #endif |
91 | #endif |
70 | |
92 | |
71 | #ifndef EV_USE_KQUEUE |
93 | #ifndef EV_USE_KQUEUE |
72 | # define EV_USE_KQUEUE 0 |
94 | # define EV_USE_KQUEUE 0 |
|
|
95 | #endif |
|
|
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 |
73 | #endif |
103 | #endif |
74 | |
104 | |
75 | #ifndef EV_USE_REALTIME |
105 | #ifndef EV_USE_REALTIME |
76 | # define EV_USE_REALTIME 1 |
106 | # define EV_USE_REALTIME 1 |
77 | #endif |
107 | #endif |
… | |
… | |
327 | |
357 | |
328 | /* 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 */ |
329 | static void |
359 | static void |
330 | fd_enomem (EV_P) |
360 | fd_enomem (EV_P) |
331 | { |
361 | { |
332 | int fd = anfdmax; |
362 | int fd; |
333 | |
363 | |
334 | while (fd--) |
364 | for (fd = anfdmax; fd--; ) |
335 | if (anfds [fd].events) |
365 | if (anfds [fd].events) |
336 | { |
366 | { |
337 | close (fd); |
367 | close (fd); |
338 | fd_kill (EV_A_ fd); |
368 | fd_kill (EV_A_ fd); |
339 | return; |
369 | return; |
… | |
… | |
349 | /* this should be highly optimised to not do anything but set a flag */ |
379 | /* this should be highly optimised to not do anything but set a flag */ |
350 | for (fd = 0; fd < anfdmax; ++fd) |
380 | for (fd = 0; fd < anfdmax; ++fd) |
351 | if (anfds [fd].events) |
381 | if (anfds [fd].events) |
352 | { |
382 | { |
353 | anfds [fd].events = 0; |
383 | anfds [fd].events = 0; |
354 | fd_change (fd); |
384 | fd_change (EV_A_ fd); |
355 | } |
385 | } |
356 | } |
386 | } |
357 | |
387 | |
358 | /*****************************************************************************/ |
388 | /*****************************************************************************/ |
359 | |
389 | |
… | |
… | |
363 | WT w = heap [k]; |
393 | WT w = heap [k]; |
364 | |
394 | |
365 | while (k && heap [k >> 1]->at > w->at) |
395 | while (k && heap [k >> 1]->at > w->at) |
366 | { |
396 | { |
367 | heap [k] = heap [k >> 1]; |
397 | heap [k] = heap [k >> 1]; |
368 | heap [k]->active = k + 1; |
398 | ((W)heap [k])->active = k + 1; |
369 | k >>= 1; |
399 | k >>= 1; |
370 | } |
400 | } |
371 | |
401 | |
372 | heap [k] = w; |
402 | heap [k] = w; |
373 | heap [k]->active = k + 1; |
403 | ((W)heap [k])->active = k + 1; |
374 | |
404 | |
375 | } |
405 | } |
376 | |
406 | |
377 | static void |
407 | static void |
378 | downheap (WT *heap, int N, int k) |
408 | downheap (WT *heap, int N, int k) |
… | |
… | |
388 | |
418 | |
389 | if (w->at <= heap [j]->at) |
419 | if (w->at <= heap [j]->at) |
390 | break; |
420 | break; |
391 | |
421 | |
392 | heap [k] = heap [j]; |
422 | heap [k] = heap [j]; |
393 | heap [k]->active = k + 1; |
423 | ((W)heap [k])->active = k + 1; |
394 | k = j; |
424 | k = j; |
395 | } |
425 | } |
396 | |
426 | |
397 | heap [k] = w; |
427 | heap [k] = w; |
398 | heap [k]->active = k + 1; |
428 | ((W)heap [k])->active = k + 1; |
399 | } |
429 | } |
400 | |
430 | |
401 | /*****************************************************************************/ |
431 | /*****************************************************************************/ |
402 | |
432 | |
403 | typedef struct |
433 | typedef struct |
… | |
… | |
586 | methods = atoi (getenv ("LIBEV_METHODS")); |
616 | methods = atoi (getenv ("LIBEV_METHODS")); |
587 | else |
617 | else |
588 | methods = EVMETHOD_ANY; |
618 | methods = EVMETHOD_ANY; |
589 | |
619 | |
590 | method = 0; |
620 | method = 0; |
|
|
621 | #if EV_USE_WIN32 |
|
|
622 | if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods); |
|
|
623 | #endif |
591 | #if EV_USE_KQUEUE |
624 | #if EV_USE_KQUEUE |
592 | if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); |
625 | if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); |
593 | #endif |
626 | #endif |
594 | #if EV_USE_EPOLL |
627 | #if EV_USE_EPOLL |
595 | if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); |
628 | if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); |
… | |
… | |
604 | } |
637 | } |
605 | |
638 | |
606 | void |
639 | void |
607 | loop_destroy (EV_P) |
640 | loop_destroy (EV_P) |
608 | { |
641 | { |
|
|
642 | #if EV_USE_WIN32 |
|
|
643 | if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A); |
|
|
644 | #endif |
609 | #if EV_USE_KQUEUE |
645 | #if EV_USE_KQUEUE |
610 | if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); |
646 | if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); |
611 | #endif |
647 | #endif |
612 | #if EV_USE_EPOLL |
648 | #if EV_USE_EPOLL |
613 | if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); |
649 | if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); |
… | |
… | |
641 | { |
677 | { |
642 | struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop)); |
678 | struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop)); |
643 | |
679 | |
644 | loop_init (EV_A_ methods); |
680 | loop_init (EV_A_ methods); |
645 | |
681 | |
646 | if (ev_methods (EV_A)) |
682 | if (ev_method (EV_A)) |
647 | return loop; |
683 | return loop; |
648 | |
684 | |
649 | return 0; |
685 | return 0; |
650 | } |
686 | } |
651 | |
687 | |
… | |
… | |
728 | |
764 | |
729 | loop_destroy (EV_A); |
765 | loop_destroy (EV_A); |
730 | } |
766 | } |
731 | |
767 | |
732 | void |
768 | void |
733 | ev_default_fork (EV_P) |
769 | ev_default_fork (void) |
734 | { |
770 | { |
|
|
771 | #if EV_MULTIPLICITY |
|
|
772 | struct ev_loop *loop = default_loop; |
|
|
773 | #endif |
|
|
774 | |
735 | loop_fork (EV_A); |
775 | loop_fork (EV_A); |
736 | |
776 | |
737 | ev_io_stop (EV_A_ &sigev); |
777 | ev_io_stop (EV_A_ &sigev); |
738 | close (sigpipe [0]); |
778 | close (sigpipe [0]); |
739 | close (sigpipe [1]); |
779 | close (sigpipe [1]); |
… | |
… | |
767 | timers_reify (EV_P) |
807 | timers_reify (EV_P) |
768 | { |
808 | { |
769 | while (timercnt && timers [0]->at <= mn_now) |
809 | while (timercnt && timers [0]->at <= mn_now) |
770 | { |
810 | { |
771 | 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))); |
772 | |
814 | |
773 | /* first reschedule or stop timer */ |
815 | /* first reschedule or stop timer */ |
774 | if (w->repeat) |
816 | if (w->repeat) |
775 | { |
817 | { |
776 | 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.)); |
… | |
… | |
789 | { |
831 | { |
790 | while (periodiccnt && periodics [0]->at <= rt_now) |
832 | while (periodiccnt && periodics [0]->at <= rt_now) |
791 | { |
833 | { |
792 | struct ev_periodic *w = periodics [0]; |
834 | struct ev_periodic *w = periodics [0]; |
793 | |
835 | |
|
|
836 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
|
|
837 | |
794 | /* first reschedule or stop timer */ |
838 | /* first reschedule or stop timer */ |
795 | if (w->interval) |
839 | if (w->interval) |
796 | { |
840 | { |
797 | 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; |
798 | 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)); |
… | |
… | |
1088 | |
1132 | |
1089 | ev_start (EV_A_ (W)w, ++timercnt); |
1133 | ev_start (EV_A_ (W)w, ++timercnt); |
1090 | array_needsize (timers, timermax, timercnt, ); |
1134 | array_needsize (timers, timermax, timercnt, ); |
1091 | timers [timercnt - 1] = w; |
1135 | timers [timercnt - 1] = w; |
1092 | upheap ((WT *)timers, timercnt - 1); |
1136 | upheap ((WT *)timers, timercnt - 1); |
|
|
1137 | |
|
|
1138 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1093 | } |
1139 | } |
1094 | |
1140 | |
1095 | void |
1141 | void |
1096 | ev_timer_stop (EV_P_ struct ev_timer *w) |
1142 | ev_timer_stop (EV_P_ struct ev_timer *w) |
1097 | { |
1143 | { |
1098 | ev_clear_pending (EV_A_ (W)w); |
1144 | ev_clear_pending (EV_A_ (W)w); |
1099 | if (!ev_is_active (w)) |
1145 | if (!ev_is_active (w)) |
1100 | return; |
1146 | return; |
1101 | |
1147 | |
|
|
1148 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
|
|
1149 | |
1102 | if (w->active < timercnt--) |
1150 | if (((W)w)->active < timercnt--) |
1103 | { |
1151 | { |
1104 | timers [w->active - 1] = timers [timercnt]; |
1152 | timers [((W)w)->active - 1] = timers [timercnt]; |
1105 | downheap ((WT *)timers, timercnt, w->active - 1); |
1153 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1106 | } |
1154 | } |
1107 | |
1155 | |
1108 | w->at = w->repeat; |
1156 | w->at = w->repeat; |
1109 | |
1157 | |
1110 | ev_stop (EV_A_ (W)w); |
1158 | ev_stop (EV_A_ (W)w); |
… | |
… | |
1116 | if (ev_is_active (w)) |
1164 | if (ev_is_active (w)) |
1117 | { |
1165 | { |
1118 | if (w->repeat) |
1166 | if (w->repeat) |
1119 | { |
1167 | { |
1120 | w->at = mn_now + w->repeat; |
1168 | w->at = mn_now + w->repeat; |
1121 | downheap ((WT *)timers, timercnt, w->active - 1); |
1169 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1122 | } |
1170 | } |
1123 | else |
1171 | else |
1124 | ev_timer_stop (EV_A_ w); |
1172 | ev_timer_stop (EV_A_ w); |
1125 | } |
1173 | } |
1126 | else if (w->repeat) |
1174 | else if (w->repeat) |
… | |
… | |
1141 | |
1189 | |
1142 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1190 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1143 | array_needsize (periodics, periodicmax, periodiccnt, ); |
1191 | array_needsize (periodics, periodicmax, periodiccnt, ); |
1144 | periodics [periodiccnt - 1] = w; |
1192 | periodics [periodiccnt - 1] = w; |
1145 | upheap ((WT *)periodics, periodiccnt - 1); |
1193 | upheap ((WT *)periodics, periodiccnt - 1); |
|
|
1194 | |
|
|
1195 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1146 | } |
1196 | } |
1147 | |
1197 | |
1148 | void |
1198 | void |
1149 | ev_periodic_stop (EV_P_ struct ev_periodic *w) |
1199 | ev_periodic_stop (EV_P_ struct ev_periodic *w) |
1150 | { |
1200 | { |
1151 | ev_clear_pending (EV_A_ (W)w); |
1201 | ev_clear_pending (EV_A_ (W)w); |
1152 | if (!ev_is_active (w)) |
1202 | if (!ev_is_active (w)) |
1153 | return; |
1203 | return; |
1154 | |
1204 | |
|
|
1205 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
|
|
1206 | |
1155 | if (w->active < periodiccnt--) |
1207 | if (((W)w)->active < periodiccnt--) |
1156 | { |
1208 | { |
1157 | periodics [w->active - 1] = periodics [periodiccnt]; |
1209 | periodics [((W)w)->active - 1] = periodics [periodiccnt]; |
1158 | downheap ((WT *)periodics, periodiccnt, w->active - 1); |
1210 | downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); |
1159 | } |
1211 | } |
1160 | |
1212 | |
1161 | ev_stop (EV_A_ (W)w); |
1213 | ev_stop (EV_A_ (W)w); |
1162 | } |
1214 | } |
1163 | |
1215 | |
… | |
… | |
1177 | { |
1229 | { |
1178 | ev_clear_pending (EV_A_ (W)w); |
1230 | ev_clear_pending (EV_A_ (W)w); |
1179 | if (ev_is_active (w)) |
1231 | if (ev_is_active (w)) |
1180 | return; |
1232 | return; |
1181 | |
1233 | |
1182 | idles [w->active - 1] = idles [--idlecnt]; |
1234 | idles [((W)w)->active - 1] = idles [--idlecnt]; |
1183 | ev_stop (EV_A_ (W)w); |
1235 | ev_stop (EV_A_ (W)w); |
1184 | } |
1236 | } |
1185 | |
1237 | |
1186 | void |
1238 | void |
1187 | ev_prepare_start (EV_P_ struct ev_prepare *w) |
1239 | ev_prepare_start (EV_P_ struct ev_prepare *w) |
… | |
… | |
1199 | { |
1251 | { |
1200 | ev_clear_pending (EV_A_ (W)w); |
1252 | ev_clear_pending (EV_A_ (W)w); |
1201 | if (ev_is_active (w)) |
1253 | if (ev_is_active (w)) |
1202 | return; |
1254 | return; |
1203 | |
1255 | |
1204 | prepares [w->active - 1] = prepares [--preparecnt]; |
1256 | prepares [((W)w)->active - 1] = prepares [--preparecnt]; |
1205 | ev_stop (EV_A_ (W)w); |
1257 | ev_stop (EV_A_ (W)w); |
1206 | } |
1258 | } |
1207 | |
1259 | |
1208 | void |
1260 | void |
1209 | ev_check_start (EV_P_ struct ev_check *w) |
1261 | ev_check_start (EV_P_ struct ev_check *w) |
… | |
… | |
1221 | { |
1273 | { |
1222 | ev_clear_pending (EV_A_ (W)w); |
1274 | ev_clear_pending (EV_A_ (W)w); |
1223 | if (ev_is_active (w)) |
1275 | if (ev_is_active (w)) |
1224 | return; |
1276 | return; |
1225 | |
1277 | |
1226 | checks [w->active - 1] = checks [--checkcnt]; |
1278 | checks [((W)w)->active - 1] = checks [--checkcnt]; |
1227 | ev_stop (EV_A_ (W)w); |
1279 | ev_stop (EV_A_ (W)w); |
1228 | } |
1280 | } |
1229 | |
1281 | |
1230 | #ifndef SA_RESTART |
1282 | #ifndef SA_RESTART |
1231 | # define SA_RESTART 0 |
1283 | # define SA_RESTART 0 |