… | |
… | |
145 | typedef struct ev_watcher_list *WL; |
145 | typedef struct ev_watcher_list *WL; |
146 | typedef struct ev_watcher_time *WT; |
146 | typedef struct ev_watcher_time *WT; |
147 | |
147 | |
148 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
148 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
149 | |
149 | |
|
|
150 | #if WIN32 |
|
|
151 | /* note: the comment below could not be substantiated, but what would I care */ |
|
|
152 | /* MSDN says this is required to handle SIGFPE */ |
|
|
153 | volatile double SIGFPE_REQ = 0.0f; |
|
|
154 | #endif |
|
|
155 | |
150 | /*****************************************************************************/ |
156 | /*****************************************************************************/ |
151 | |
157 | |
152 | typedef struct |
158 | typedef struct |
153 | { |
159 | { |
154 | struct ev_watcher_list *head; |
160 | struct ev_watcher_list *head; |
… | |
… | |
232 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
238 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
233 | init (base + cur, newcnt - cur); \ |
239 | init (base + cur, newcnt - cur); \ |
234 | cur = newcnt; \ |
240 | cur = newcnt; \ |
235 | } |
241 | } |
236 | |
242 | |
|
|
243 | #define array_slim(stem) \ |
|
|
244 | if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ |
|
|
245 | { \ |
|
|
246 | stem ## max = array_roundsize (stem ## cnt >> 1); \ |
|
|
247 | base = realloc (base, sizeof (*base) * (stem ## max)); \ |
|
|
248 | fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
|
|
249 | } |
|
|
250 | |
|
|
251 | #define array_free(stem, idx) \ |
|
|
252 | free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; |
|
|
253 | |
237 | /*****************************************************************************/ |
254 | /*****************************************************************************/ |
238 | |
255 | |
239 | static void |
256 | static void |
240 | anfds_init (ANFD *base, int count) |
257 | anfds_init (ANFD *base, int count) |
241 | { |
258 | { |
… | |
… | |
306 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
323 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
307 | events |= w->events; |
324 | events |= w->events; |
308 | |
325 | |
309 | anfd->reify = 0; |
326 | anfd->reify = 0; |
310 | |
327 | |
311 | if (anfd->events != events) |
|
|
312 | { |
|
|
313 | method_modify (EV_A_ fd, anfd->events, events); |
328 | method_modify (EV_A_ fd, anfd->events, events); |
314 | anfd->events = events; |
329 | anfd->events = events; |
315 | } |
|
|
316 | } |
330 | } |
317 | |
331 | |
318 | fdchangecnt = 0; |
332 | fdchangecnt = 0; |
319 | } |
333 | } |
320 | |
334 | |
… | |
… | |
456 | } |
470 | } |
457 | |
471 | |
458 | static void |
472 | static void |
459 | sighandler (int signum) |
473 | sighandler (int signum) |
460 | { |
474 | { |
|
|
475 | #if WIN32 |
|
|
476 | signal (signum, sighandler); |
|
|
477 | #endif |
|
|
478 | |
461 | signals [signum - 1].gotsig = 1; |
479 | signals [signum - 1].gotsig = 1; |
462 | |
480 | |
463 | if (!gotsig) |
481 | if (!gotsig) |
464 | { |
482 | { |
465 | int old_errno = errno; |
483 | int old_errno = errno; |
… | |
… | |
522 | struct ev_child *w; |
540 | struct ev_child *w; |
523 | |
541 | |
524 | for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) |
542 | for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) |
525 | if (w->pid == pid || !w->pid) |
543 | if (w->pid == pid || !w->pid) |
526 | { |
544 | { |
527 | w->priority = sw->priority; /* need to do it *now* */ |
545 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
528 | w->rpid = pid; |
546 | w->rpid = pid; |
529 | w->rstatus = status; |
547 | w->rstatus = status; |
530 | event (EV_A_ (W)w, EV_CHILD); |
548 | event (EV_A_ (W)w, EV_CHILD); |
531 | } |
549 | } |
532 | } |
550 | } |
533 | |
551 | |
534 | static void |
552 | static void |
… | |
… | |
637 | } |
655 | } |
638 | |
656 | |
639 | void |
657 | void |
640 | loop_destroy (EV_P) |
658 | loop_destroy (EV_P) |
641 | { |
659 | { |
|
|
660 | int i; |
|
|
661 | |
642 | #if EV_USE_WIN32 |
662 | #if EV_USE_WIN32 |
643 | if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A); |
663 | if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A); |
644 | #endif |
664 | #endif |
645 | #if EV_USE_KQUEUE |
665 | #if EV_USE_KQUEUE |
646 | if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); |
666 | if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); |
… | |
… | |
652 | if (method == EVMETHOD_POLL ) poll_destroy (EV_A); |
672 | if (method == EVMETHOD_POLL ) poll_destroy (EV_A); |
653 | #endif |
673 | #endif |
654 | #if EV_USE_SELECT |
674 | #if EV_USE_SELECT |
655 | if (method == EVMETHOD_SELECT) select_destroy (EV_A); |
675 | if (method == EVMETHOD_SELECT) select_destroy (EV_A); |
656 | #endif |
676 | #endif |
|
|
677 | |
|
|
678 | for (i = NUMPRI; i--; ) |
|
|
679 | array_free (pending, [i]); |
|
|
680 | |
|
|
681 | array_free (fdchange, ); |
|
|
682 | array_free (timer, ); |
|
|
683 | array_free (periodic, ); |
|
|
684 | array_free (idle, ); |
|
|
685 | array_free (prepare, ); |
|
|
686 | array_free (check, ); |
657 | |
687 | |
658 | method = 0; |
688 | method = 0; |
659 | /*TODO*/ |
689 | /*TODO*/ |
660 | } |
690 | } |
661 | |
691 | |
… | |
… | |
804 | } |
834 | } |
805 | |
835 | |
806 | static void |
836 | static void |
807 | timers_reify (EV_P) |
837 | timers_reify (EV_P) |
808 | { |
838 | { |
809 | while (timercnt && timers [0]->at <= mn_now) |
839 | while (timercnt && ((WT)timers [0])->at <= mn_now) |
810 | { |
840 | { |
811 | struct ev_timer *w = timers [0]; |
841 | struct ev_timer *w = timers [0]; |
812 | |
842 | |
813 | assert (("inactive timer on timer heap detected", ev_is_active (w))); |
843 | assert (("inactive timer on timer heap detected", ev_is_active (w))); |
814 | |
844 | |
815 | /* first reschedule or stop timer */ |
845 | /* first reschedule or stop timer */ |
816 | if (w->repeat) |
846 | if (w->repeat) |
817 | { |
847 | { |
818 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
848 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
819 | w->at = mn_now + w->repeat; |
849 | ((WT)w)->at = mn_now + w->repeat; |
820 | downheap ((WT *)timers, timercnt, 0); |
850 | downheap ((WT *)timers, timercnt, 0); |
821 | } |
851 | } |
822 | else |
852 | else |
823 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
853 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
824 | |
854 | |
… | |
… | |
827 | } |
857 | } |
828 | |
858 | |
829 | static void |
859 | static void |
830 | periodics_reify (EV_P) |
860 | periodics_reify (EV_P) |
831 | { |
861 | { |
832 | while (periodiccnt && periodics [0]->at <= rt_now) |
862 | while (periodiccnt && ((WT)periodics [0])->at <= rt_now) |
833 | { |
863 | { |
834 | struct ev_periodic *w = periodics [0]; |
864 | struct ev_periodic *w = periodics [0]; |
835 | |
865 | |
836 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
866 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
837 | |
867 | |
838 | /* first reschedule or stop timer */ |
868 | /* first reschedule or stop timer */ |
839 | if (w->interval) |
869 | if (w->interval) |
840 | { |
870 | { |
841 | w->at += floor ((rt_now - w->at) / w->interval + 1.) * w->interval; |
871 | ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; |
842 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > rt_now)); |
872 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now)); |
843 | downheap ((WT *)periodics, periodiccnt, 0); |
873 | downheap ((WT *)periodics, periodiccnt, 0); |
844 | } |
874 | } |
845 | else |
875 | else |
846 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
876 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
847 | |
877 | |
… | |
… | |
859 | { |
889 | { |
860 | struct ev_periodic *w = periodics [i]; |
890 | struct ev_periodic *w = periodics [i]; |
861 | |
891 | |
862 | if (w->interval) |
892 | if (w->interval) |
863 | { |
893 | { |
864 | ev_tstamp diff = ceil ((rt_now - w->at) / w->interval) * w->interval; |
894 | ev_tstamp diff = ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
865 | |
895 | |
866 | if (fabs (diff) >= 1e-4) |
896 | if (fabs (diff) >= 1e-4) |
867 | { |
897 | { |
868 | ev_periodic_stop (EV_A_ w); |
898 | ev_periodic_stop (EV_A_ w); |
869 | ev_periodic_start (EV_A_ w); |
899 | ev_periodic_start (EV_A_ w); |
… | |
… | |
930 | { |
960 | { |
931 | periodics_reschedule (EV_A); |
961 | periodics_reschedule (EV_A); |
932 | |
962 | |
933 | /* adjust timers. this is easy, as the offset is the same for all */ |
963 | /* adjust timers. this is easy, as the offset is the same for all */ |
934 | for (i = 0; i < timercnt; ++i) |
964 | for (i = 0; i < timercnt; ++i) |
935 | timers [i]->at += rt_now - mn_now; |
965 | ((WT)timers [i])->at += rt_now - mn_now; |
936 | } |
966 | } |
937 | |
967 | |
938 | mn_now = rt_now; |
968 | mn_now = rt_now; |
939 | } |
969 | } |
940 | } |
970 | } |
… | |
… | |
991 | { |
1021 | { |
992 | block = MAX_BLOCKTIME; |
1022 | block = MAX_BLOCKTIME; |
993 | |
1023 | |
994 | if (timercnt) |
1024 | if (timercnt) |
995 | { |
1025 | { |
996 | ev_tstamp to = timers [0]->at - mn_now + method_fudge; |
1026 | ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; |
997 | if (block > to) block = to; |
1027 | if (block > to) block = to; |
998 | } |
1028 | } |
999 | |
1029 | |
1000 | if (periodiccnt) |
1030 | if (periodiccnt) |
1001 | { |
1031 | { |
1002 | ev_tstamp to = periodics [0]->at - rt_now + method_fudge; |
1032 | ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge; |
1003 | if (block > to) block = to; |
1033 | if (block > to) block = to; |
1004 | } |
1034 | } |
1005 | |
1035 | |
1006 | if (block < 0.) block = 0.; |
1036 | if (block < 0.) block = 0.; |
1007 | } |
1037 | } |
… | |
… | |
1124 | ev_timer_start (EV_P_ struct ev_timer *w) |
1154 | ev_timer_start (EV_P_ struct ev_timer *w) |
1125 | { |
1155 | { |
1126 | if (ev_is_active (w)) |
1156 | if (ev_is_active (w)) |
1127 | return; |
1157 | return; |
1128 | |
1158 | |
1129 | w->at += mn_now; |
1159 | ((WT)w)->at += mn_now; |
1130 | |
1160 | |
1131 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1161 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1132 | |
1162 | |
1133 | ev_start (EV_A_ (W)w, ++timercnt); |
1163 | ev_start (EV_A_ (W)w, ++timercnt); |
1134 | array_needsize (timers, timermax, timercnt, ); |
1164 | array_needsize (timers, timermax, timercnt, ); |
… | |
… | |
1151 | { |
1181 | { |
1152 | timers [((W)w)->active - 1] = timers [timercnt]; |
1182 | timers [((W)w)->active - 1] = timers [timercnt]; |
1153 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1183 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1154 | } |
1184 | } |
1155 | |
1185 | |
1156 | w->at = w->repeat; |
1186 | ((WT)w)->at = w->repeat; |
1157 | |
1187 | |
1158 | ev_stop (EV_A_ (W)w); |
1188 | ev_stop (EV_A_ (W)w); |
1159 | } |
1189 | } |
1160 | |
1190 | |
1161 | void |
1191 | void |
… | |
… | |
1163 | { |
1193 | { |
1164 | if (ev_is_active (w)) |
1194 | if (ev_is_active (w)) |
1165 | { |
1195 | { |
1166 | if (w->repeat) |
1196 | if (w->repeat) |
1167 | { |
1197 | { |
1168 | w->at = mn_now + w->repeat; |
1198 | ((WT)w)->at = mn_now + w->repeat; |
1169 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1199 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1170 | } |
1200 | } |
1171 | else |
1201 | else |
1172 | ev_timer_stop (EV_A_ w); |
1202 | ev_timer_stop (EV_A_ w); |
1173 | } |
1203 | } |
… | |
… | |
1183 | |
1213 | |
1184 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1214 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1185 | |
1215 | |
1186 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1216 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1187 | if (w->interval) |
1217 | if (w->interval) |
1188 | w->at += ceil ((rt_now - w->at) / w->interval) * w->interval; |
1218 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1189 | |
1219 | |
1190 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1220 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1191 | array_needsize (periodics, periodicmax, periodiccnt, ); |
1221 | array_needsize (periodics, periodicmax, periodiccnt, ); |
1192 | periodics [periodiccnt - 1] = w; |
1222 | periodics [periodiccnt - 1] = w; |
1193 | upheap ((WT *)periodics, periodiccnt - 1); |
1223 | upheap ((WT *)periodics, periodiccnt - 1); |
… | |
… | |
1296 | |
1326 | |
1297 | ev_start (EV_A_ (W)w, 1); |
1327 | ev_start (EV_A_ (W)w, 1); |
1298 | array_needsize (signals, signalmax, w->signum, signals_init); |
1328 | array_needsize (signals, signalmax, w->signum, signals_init); |
1299 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1329 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1300 | |
1330 | |
1301 | if (!w->next) |
1331 | if (!((WL)w)->next) |
1302 | { |
1332 | { |
|
|
1333 | #if WIN32 |
|
|
1334 | signal (w->signum, sighandler); |
|
|
1335 | #else |
1303 | struct sigaction sa; |
1336 | struct sigaction sa; |
1304 | sa.sa_handler = sighandler; |
1337 | sa.sa_handler = sighandler; |
1305 | sigfillset (&sa.sa_mask); |
1338 | sigfillset (&sa.sa_mask); |
1306 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
1339 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
1307 | sigaction (w->signum, &sa, 0); |
1340 | sigaction (w->signum, &sa, 0); |
|
|
1341 | #endif |
1308 | } |
1342 | } |
1309 | } |
1343 | } |
1310 | |
1344 | |
1311 | void |
1345 | void |
1312 | ev_signal_stop (EV_P_ struct ev_signal *w) |
1346 | ev_signal_stop (EV_P_ struct ev_signal *w) |