… | |
… | |
26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
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 | |
|
|
32 | #ifdef __cplusplus |
|
|
33 | extern "C" { |
|
|
34 | #endif |
|
|
35 | |
31 | #ifndef EV_STANDALONE |
36 | #ifndef EV_STANDALONE |
32 | # include "config.h" |
37 | # include "config.h" |
33 | |
38 | |
34 | # if HAVE_CLOCK_GETTIME |
39 | # if HAVE_CLOCK_GETTIME |
35 | # define EV_USE_MONOTONIC 1 |
40 | # define EV_USE_MONOTONIC 1 |
… | |
… | |
46 | |
51 | |
47 | # if HAVE_EPOLL && HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H |
52 | # if HAVE_EPOLL && HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H |
48 | # define EV_USE_EPOLL 1 |
53 | # define EV_USE_EPOLL 1 |
49 | # endif |
54 | # endif |
50 | |
55 | |
51 | # if HAVE_KQUEUE && HAVE_WORKING_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H |
56 | # if HAVE_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H |
52 | # define EV_USE_KQUEUE 1 |
57 | # define EV_USE_KQUEUE 1 |
53 | # endif |
58 | # endif |
54 | |
59 | |
55 | #endif |
60 | #endif |
56 | |
61 | |
… | |
… | |
126 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
131 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
127 | #define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */ |
132 | #define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */ |
128 | #define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ |
133 | #define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ |
129 | /*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ |
134 | /*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ |
130 | |
135 | |
|
|
136 | #ifdef EV_H |
|
|
137 | # include EV_H |
|
|
138 | #else |
131 | #include "ev.h" |
139 | # include "ev.h" |
|
|
140 | #endif |
132 | |
141 | |
133 | #if __GNUC__ >= 3 |
142 | #if __GNUC__ >= 3 |
134 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
143 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
135 | # define inline inline |
144 | # define inline inline |
136 | #else |
145 | #else |
… | |
… | |
215 | int events; |
224 | int events; |
216 | } ANPENDING; |
225 | } ANPENDING; |
217 | |
226 | |
218 | #if EV_MULTIPLICITY |
227 | #if EV_MULTIPLICITY |
219 | |
228 | |
220 | struct ev_loop |
229 | struct ev_loop |
221 | { |
230 | { |
|
|
231 | ev_tstamp ev_rt_now; |
222 | # define VAR(name,decl) decl; |
232 | #define VAR(name,decl) decl; |
223 | # include "ev_vars.h" |
233 | #include "ev_vars.h" |
224 | }; |
|
|
225 | # undef VAR |
234 | #undef VAR |
|
|
235 | }; |
226 | # include "ev_wrap.h" |
236 | #include "ev_wrap.h" |
|
|
237 | |
|
|
238 | struct ev_loop default_loop_struct; |
|
|
239 | static struct ev_loop *default_loop; |
227 | |
240 | |
228 | #else |
241 | #else |
229 | |
242 | |
|
|
243 | ev_tstamp ev_rt_now; |
230 | # define VAR(name,decl) static decl; |
244 | #define VAR(name,decl) static decl; |
231 | # include "ev_vars.h" |
245 | #include "ev_vars.h" |
232 | # undef VAR |
246 | #undef VAR |
|
|
247 | |
|
|
248 | static int default_loop; |
233 | |
249 | |
234 | #endif |
250 | #endif |
235 | |
251 | |
236 | /*****************************************************************************/ |
252 | /*****************************************************************************/ |
237 | |
253 | |
238 | inline ev_tstamp |
254 | ev_tstamp |
239 | ev_time (void) |
255 | ev_time (void) |
240 | { |
256 | { |
241 | #if EV_USE_REALTIME |
257 | #if EV_USE_REALTIME |
242 | struct timespec ts; |
258 | struct timespec ts; |
243 | clock_gettime (CLOCK_REALTIME, &ts); |
259 | clock_gettime (CLOCK_REALTIME, &ts); |
… | |
… | |
262 | #endif |
278 | #endif |
263 | |
279 | |
264 | return ev_time (); |
280 | return ev_time (); |
265 | } |
281 | } |
266 | |
282 | |
|
|
283 | #if EV_MULTIPLICITY |
267 | ev_tstamp |
284 | ev_tstamp |
268 | ev_now (EV_P) |
285 | ev_now (EV_P) |
269 | { |
286 | { |
270 | return rt_now; |
287 | return ev_rt_now; |
271 | } |
288 | } |
|
|
289 | #endif |
272 | |
290 | |
273 | #define array_roundsize(type,n) ((n) | 4 & ~3) |
291 | #define array_roundsize(type,n) ((n) | 4 & ~3) |
274 | |
292 | |
275 | #define array_needsize(type,base,cur,cnt,init) \ |
293 | #define array_needsize(type,base,cur,cnt,init) \ |
276 | if (expect_false ((cnt) > cur)) \ |
294 | if (expect_false ((cnt) > cur)) \ |
… | |
… | |
509 | |
527 | |
510 | heap [k] = w; |
528 | heap [k] = w; |
511 | ((W)heap [k])->active = k + 1; |
529 | ((W)heap [k])->active = k + 1; |
512 | } |
530 | } |
513 | |
531 | |
|
|
532 | inline void |
|
|
533 | adjustheap (WT *heap, int N, int k, ev_tstamp at) |
|
|
534 | { |
|
|
535 | ev_tstamp old_at = heap [k]->at; |
|
|
536 | heap [k]->at = at; |
|
|
537 | |
|
|
538 | if (old_at < at) |
|
|
539 | downheap (heap, N, k); |
|
|
540 | else |
|
|
541 | upheap (heap, k); |
|
|
542 | } |
|
|
543 | |
514 | /*****************************************************************************/ |
544 | /*****************************************************************************/ |
515 | |
545 | |
516 | typedef struct |
546 | typedef struct |
517 | { |
547 | { |
518 | WL head; |
548 | WL head; |
… | |
… | |
561 | } |
591 | } |
562 | |
592 | |
563 | void |
593 | void |
564 | ev_feed_signal_event (EV_P_ int signum) |
594 | ev_feed_signal_event (EV_P_ int signum) |
565 | { |
595 | { |
|
|
596 | WL w; |
|
|
597 | |
566 | #if EV_MULTIPLICITY |
598 | #if EV_MULTIPLICITY |
567 | assert (("feeding signal events is only supported in the default loop", loop == default_loop)); |
599 | assert (("feeding signal events is only supported in the default loop", loop == default_loop)); |
568 | #endif |
600 | #endif |
569 | |
601 | |
570 | --signum; |
602 | --signum; |
… | |
… | |
579 | } |
611 | } |
580 | |
612 | |
581 | static void |
613 | static void |
582 | sigcb (EV_P_ struct ev_io *iow, int revents) |
614 | sigcb (EV_P_ struct ev_io *iow, int revents) |
583 | { |
615 | { |
584 | WL w; |
|
|
585 | int signum; |
616 | int signum; |
586 | |
617 | |
587 | #ifdef WIN32 |
618 | #ifdef WIN32 |
588 | recv (sigpipe [0], &revents, 1, MSG_DONTWAIT); |
619 | recv (sigpipe [0], &revents, 1, MSG_DONTWAIT); |
589 | #else |
620 | #else |
… | |
… | |
591 | #endif |
622 | #endif |
592 | gotsig = 0; |
623 | gotsig = 0; |
593 | |
624 | |
594 | for (signum = signalmax; signum--; ) |
625 | for (signum = signalmax; signum--; ) |
595 | if (signals [signum].gotsig) |
626 | if (signals [signum].gotsig) |
596 | sigevent (EV_A_ signum + 1); |
627 | ev_feed_signal_event (EV_A_ signum + 1); |
597 | } |
628 | } |
598 | |
629 | |
599 | static void |
630 | static void |
600 | siginit (EV_P) |
631 | siginit (EV_P) |
601 | { |
632 | { |
… | |
… | |
713 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
744 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
714 | have_monotonic = 1; |
745 | have_monotonic = 1; |
715 | } |
746 | } |
716 | #endif |
747 | #endif |
717 | |
748 | |
718 | rt_now = ev_time (); |
749 | ev_rt_now = ev_time (); |
719 | mn_now = get_clock (); |
750 | mn_now = get_clock (); |
720 | now_floor = mn_now; |
751 | now_floor = mn_now; |
721 | rtmn_diff = rt_now - mn_now; |
752 | rtmn_diff = ev_rt_now - mn_now; |
722 | |
753 | |
723 | if (methods == EVMETHOD_AUTO) |
754 | if (methods == EVMETHOD_AUTO) |
724 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
755 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
725 | methods = atoi (getenv ("LIBEV_METHODS")); |
756 | methods = atoi (getenv ("LIBEV_METHODS")); |
726 | else |
757 | else |
… | |
… | |
741 | #endif |
772 | #endif |
742 | #if EV_USE_SELECT |
773 | #if EV_USE_SELECT |
743 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
774 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
744 | #endif |
775 | #endif |
745 | |
776 | |
746 | ev_watcher_init (&sigev, sigcb); |
777 | ev_init (&sigev, sigcb); |
747 | ev_set_priority (&sigev, EV_MAXPRI); |
778 | ev_set_priority (&sigev, EV_MAXPRI); |
748 | } |
779 | } |
749 | } |
780 | } |
750 | |
781 | |
751 | void |
782 | void |
… | |
… | |
841 | } |
872 | } |
842 | |
873 | |
843 | #endif |
874 | #endif |
844 | |
875 | |
845 | #if EV_MULTIPLICITY |
876 | #if EV_MULTIPLICITY |
846 | struct ev_loop default_loop_struct; |
|
|
847 | static struct ev_loop *default_loop; |
|
|
848 | |
|
|
849 | struct ev_loop * |
877 | struct ev_loop * |
850 | #else |
878 | #else |
851 | static int default_loop; |
|
|
852 | |
|
|
853 | int |
879 | int |
854 | #endif |
880 | #endif |
855 | ev_default_loop (int methods) |
881 | ev_default_loop (int methods) |
856 | { |
882 | { |
857 | if (sigpipe [0] == sigpipe [1]) |
883 | if (sigpipe [0] == sigpipe [1]) |
… | |
… | |
943 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
969 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
944 | |
970 | |
945 | if (p->w) |
971 | if (p->w) |
946 | { |
972 | { |
947 | p->w->pending = 0; |
973 | p->w->pending = 0; |
948 | p->w->cb (EV_A_ p->w, p->events); |
974 | EV_CB_INVOKE (p->w, p->events); |
949 | } |
975 | } |
950 | } |
976 | } |
951 | } |
977 | } |
952 | |
978 | |
953 | static void |
979 | static void |
… | |
… | |
961 | |
987 | |
962 | /* first reschedule or stop timer */ |
988 | /* first reschedule or stop timer */ |
963 | if (w->repeat) |
989 | if (w->repeat) |
964 | { |
990 | { |
965 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
991 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
|
|
992 | |
966 | ((WT)w)->at = mn_now + w->repeat; |
993 | ((WT)w)->at += w->repeat; |
|
|
994 | if (((WT)w)->at < mn_now) |
|
|
995 | ((WT)w)->at = mn_now; |
|
|
996 | |
967 | downheap ((WT *)timers, timercnt, 0); |
997 | downheap ((WT *)timers, timercnt, 0); |
968 | } |
998 | } |
969 | else |
999 | else |
970 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1000 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
971 | |
1001 | |
… | |
… | |
974 | } |
1004 | } |
975 | |
1005 | |
976 | static void |
1006 | static void |
977 | periodics_reify (EV_P) |
1007 | periodics_reify (EV_P) |
978 | { |
1008 | { |
979 | while (periodiccnt && ((WT)periodics [0])->at <= rt_now) |
1009 | while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) |
980 | { |
1010 | { |
981 | struct ev_periodic *w = periodics [0]; |
1011 | struct ev_periodic *w = periodics [0]; |
982 | |
1012 | |
983 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
1013 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
984 | |
1014 | |
985 | /* first reschedule or stop timer */ |
1015 | /* first reschedule or stop timer */ |
986 | if (w->reschedule_cb) |
1016 | if (w->reschedule_cb) |
987 | { |
1017 | { |
988 | ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, rt_now + 0.0001); |
1018 | ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001); |
989 | |
1019 | |
990 | assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > rt_now)); |
1020 | assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now)); |
991 | downheap ((WT *)periodics, periodiccnt, 0); |
1021 | downheap ((WT *)periodics, periodiccnt, 0); |
992 | } |
1022 | } |
993 | else if (w->interval) |
1023 | else if (w->interval) |
994 | { |
1024 | { |
995 | ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; |
1025 | ((WT)w)->at += floor ((ev_rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; |
996 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now)); |
1026 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > ev_rt_now)); |
997 | downheap ((WT *)periodics, periodiccnt, 0); |
1027 | downheap ((WT *)periodics, periodiccnt, 0); |
998 | } |
1028 | } |
999 | else |
1029 | else |
1000 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1030 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1001 | |
1031 | |
… | |
… | |
1012 | for (i = 0; i < periodiccnt; ++i) |
1042 | for (i = 0; i < periodiccnt; ++i) |
1013 | { |
1043 | { |
1014 | struct ev_periodic *w = periodics [i]; |
1044 | struct ev_periodic *w = periodics [i]; |
1015 | |
1045 | |
1016 | if (w->reschedule_cb) |
1046 | if (w->reschedule_cb) |
1017 | ((WT)w)->at = w->reschedule_cb (w, rt_now); |
1047 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
1018 | else if (w->interval) |
1048 | else if (w->interval) |
1019 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1049 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1020 | } |
1050 | } |
1021 | |
1051 | |
1022 | /* now rebuild the heap */ |
1052 | /* now rebuild the heap */ |
1023 | for (i = periodiccnt >> 1; i--; ) |
1053 | for (i = periodiccnt >> 1; i--; ) |
1024 | downheap ((WT *)periodics, periodiccnt, i); |
1054 | downheap ((WT *)periodics, periodiccnt, i); |
… | |
… | |
1029 | { |
1059 | { |
1030 | mn_now = get_clock (); |
1060 | mn_now = get_clock (); |
1031 | |
1061 | |
1032 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
1062 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
1033 | { |
1063 | { |
1034 | rt_now = rtmn_diff + mn_now; |
1064 | ev_rt_now = rtmn_diff + mn_now; |
1035 | return 0; |
1065 | return 0; |
1036 | } |
1066 | } |
1037 | else |
1067 | else |
1038 | { |
1068 | { |
1039 | now_floor = mn_now; |
1069 | now_floor = mn_now; |
1040 | rt_now = ev_time (); |
1070 | ev_rt_now = ev_time (); |
1041 | return 1; |
1071 | return 1; |
1042 | } |
1072 | } |
1043 | } |
1073 | } |
1044 | |
1074 | |
1045 | static void |
1075 | static void |
… | |
… | |
1054 | { |
1084 | { |
1055 | ev_tstamp odiff = rtmn_diff; |
1085 | ev_tstamp odiff = rtmn_diff; |
1056 | |
1086 | |
1057 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
1087 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
1058 | { |
1088 | { |
1059 | rtmn_diff = rt_now - mn_now; |
1089 | rtmn_diff = ev_rt_now - mn_now; |
1060 | |
1090 | |
1061 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
1091 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
1062 | return; /* all is well */ |
1092 | return; /* all is well */ |
1063 | |
1093 | |
1064 | rt_now = ev_time (); |
1094 | ev_rt_now = ev_time (); |
1065 | mn_now = get_clock (); |
1095 | mn_now = get_clock (); |
1066 | now_floor = mn_now; |
1096 | now_floor = mn_now; |
1067 | } |
1097 | } |
1068 | |
1098 | |
1069 | periodics_reschedule (EV_A); |
1099 | periodics_reschedule (EV_A); |
… | |
… | |
1072 | } |
1102 | } |
1073 | } |
1103 | } |
1074 | else |
1104 | else |
1075 | #endif |
1105 | #endif |
1076 | { |
1106 | { |
1077 | rt_now = ev_time (); |
1107 | ev_rt_now = ev_time (); |
1078 | |
1108 | |
1079 | if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
1109 | if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
1080 | { |
1110 | { |
1081 | periodics_reschedule (EV_A); |
1111 | periodics_reschedule (EV_A); |
1082 | |
1112 | |
1083 | /* adjust timers. this is easy, as the offset is the same for all */ |
1113 | /* adjust timers. this is easy, as the offset is the same for all */ |
1084 | for (i = 0; i < timercnt; ++i) |
1114 | for (i = 0; i < timercnt; ++i) |
1085 | ((WT)timers [i])->at += rt_now - mn_now; |
1115 | ((WT)timers [i])->at += ev_rt_now - mn_now; |
1086 | } |
1116 | } |
1087 | |
1117 | |
1088 | mn_now = rt_now; |
1118 | mn_now = ev_rt_now; |
1089 | } |
1119 | } |
1090 | } |
1120 | } |
1091 | |
1121 | |
1092 | void |
1122 | void |
1093 | ev_ref (EV_P) |
1123 | ev_ref (EV_P) |
… | |
… | |
1133 | if (expect_true (have_monotonic)) |
1163 | if (expect_true (have_monotonic)) |
1134 | time_update_monotonic (EV_A); |
1164 | time_update_monotonic (EV_A); |
1135 | else |
1165 | else |
1136 | #endif |
1166 | #endif |
1137 | { |
1167 | { |
1138 | rt_now = ev_time (); |
1168 | ev_rt_now = ev_time (); |
1139 | mn_now = rt_now; |
1169 | mn_now = ev_rt_now; |
1140 | } |
1170 | } |
1141 | |
1171 | |
1142 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
1172 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
1143 | block = 0.; |
1173 | block = 0.; |
1144 | else |
1174 | else |
… | |
… | |
1151 | if (block > to) block = to; |
1181 | if (block > to) block = to; |
1152 | } |
1182 | } |
1153 | |
1183 | |
1154 | if (periodiccnt) |
1184 | if (periodiccnt) |
1155 | { |
1185 | { |
1156 | ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge; |
1186 | ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge; |
1157 | if (block > to) block = to; |
1187 | if (block > to) block = to; |
1158 | } |
1188 | } |
1159 | |
1189 | |
1160 | if (block < 0.) block = 0.; |
1190 | if (block < 0.) block = 0.; |
1161 | } |
1191 | } |
1162 | |
1192 | |
1163 | method_poll (EV_A_ block); |
1193 | method_poll (EV_A_ block); |
1164 | |
1194 | |
1165 | /* update rt_now, do magic */ |
1195 | /* update ev_rt_now, do magic */ |
1166 | time_update (EV_A); |
1196 | time_update (EV_A); |
1167 | |
1197 | |
1168 | /* queue pending timers and reschedule them */ |
1198 | /* queue pending timers and reschedule them */ |
1169 | timers_reify (EV_A); /* relative timers called last */ |
1199 | timers_reify (EV_A); /* relative timers called last */ |
1170 | periodics_reify (EV_A); /* absolute timers called first */ |
1200 | periodics_reify (EV_A); /* absolute timers called first */ |
… | |
… | |
1266 | { |
1296 | { |
1267 | ev_clear_pending (EV_A_ (W)w); |
1297 | ev_clear_pending (EV_A_ (W)w); |
1268 | if (!ev_is_active (w)) |
1298 | if (!ev_is_active (w)) |
1269 | return; |
1299 | return; |
1270 | |
1300 | |
|
|
1301 | assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax)); |
|
|
1302 | |
1271 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
1303 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
1272 | ev_stop (EV_A_ (W)w); |
1304 | ev_stop (EV_A_ (W)w); |
1273 | |
1305 | |
1274 | fd_change (EV_A_ w->fd); |
1306 | fd_change (EV_A_ w->fd); |
1275 | } |
1307 | } |
… | |
… | |
1305 | { |
1337 | { |
1306 | timers [((W)w)->active - 1] = timers [timercnt]; |
1338 | timers [((W)w)->active - 1] = timers [timercnt]; |
1307 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1339 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1308 | } |
1340 | } |
1309 | |
1341 | |
1310 | ((WT)w)->at = w->repeat; |
1342 | ((WT)w)->at -= mn_now; |
1311 | |
1343 | |
1312 | ev_stop (EV_A_ (W)w); |
1344 | ev_stop (EV_A_ (W)w); |
1313 | } |
1345 | } |
1314 | |
1346 | |
1315 | void |
1347 | void |
1316 | ev_timer_again (EV_P_ struct ev_timer *w) |
1348 | ev_timer_again (EV_P_ struct ev_timer *w) |
1317 | { |
1349 | { |
1318 | if (ev_is_active (w)) |
1350 | if (ev_is_active (w)) |
1319 | { |
1351 | { |
1320 | if (w->repeat) |
1352 | if (w->repeat) |
1321 | { |
|
|
1322 | ((WT)w)->at = mn_now + w->repeat; |
|
|
1323 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1353 | adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1, mn_now + w->repeat); |
1324 | } |
|
|
1325 | else |
1354 | else |
1326 | ev_timer_stop (EV_A_ w); |
1355 | ev_timer_stop (EV_A_ w); |
1327 | } |
1356 | } |
1328 | else if (w->repeat) |
1357 | else if (w->repeat) |
1329 | ev_timer_start (EV_A_ w); |
1358 | ev_timer_start (EV_A_ w); |
… | |
… | |
1334 | { |
1363 | { |
1335 | if (ev_is_active (w)) |
1364 | if (ev_is_active (w)) |
1336 | return; |
1365 | return; |
1337 | |
1366 | |
1338 | if (w->reschedule_cb) |
1367 | if (w->reschedule_cb) |
1339 | ((WT)w)->at = w->reschedule_cb (w, rt_now); |
1368 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
1340 | else if (w->interval) |
1369 | else if (w->interval) |
1341 | { |
1370 | { |
1342 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1371 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1343 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1372 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1344 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1373 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1345 | } |
1374 | } |
1346 | |
1375 | |
1347 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1376 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1348 | array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); |
1377 | array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); |
1349 | periodics [periodiccnt - 1] = w; |
1378 | periodics [periodiccnt - 1] = w; |
… | |
… | |
1371 | } |
1400 | } |
1372 | |
1401 | |
1373 | void |
1402 | void |
1374 | ev_periodic_again (EV_P_ struct ev_periodic *w) |
1403 | ev_periodic_again (EV_P_ struct ev_periodic *w) |
1375 | { |
1404 | { |
|
|
1405 | /* TODO: use adjustheap and recalculation */ |
1376 | ev_periodic_stop (EV_A_ w); |
1406 | ev_periodic_stop (EV_A_ w); |
1377 | ev_periodic_start (EV_A_ w); |
1407 | ev_periodic_start (EV_A_ w); |
1378 | } |
1408 | } |
1379 | |
1409 | |
1380 | void |
1410 | void |
… | |
… | |
1559 | else |
1589 | else |
1560 | { |
1590 | { |
1561 | once->cb = cb; |
1591 | once->cb = cb; |
1562 | once->arg = arg; |
1592 | once->arg = arg; |
1563 | |
1593 | |
1564 | ev_watcher_init (&once->io, once_cb_io); |
1594 | ev_init (&once->io, once_cb_io); |
1565 | if (fd >= 0) |
1595 | if (fd >= 0) |
1566 | { |
1596 | { |
1567 | ev_io_set (&once->io, fd, events); |
1597 | ev_io_set (&once->io, fd, events); |
1568 | ev_io_start (EV_A_ &once->io); |
1598 | ev_io_start (EV_A_ &once->io); |
1569 | } |
1599 | } |
1570 | |
1600 | |
1571 | ev_watcher_init (&once->to, once_cb_to); |
1601 | ev_init (&once->to, once_cb_to); |
1572 | if (timeout >= 0.) |
1602 | if (timeout >= 0.) |
1573 | { |
1603 | { |
1574 | ev_timer_set (&once->to, timeout, 0.); |
1604 | ev_timer_set (&once->to, timeout, 0.); |
1575 | ev_timer_start (EV_A_ &once->to); |
1605 | ev_timer_start (EV_A_ &once->to); |
1576 | } |
1606 | } |
1577 | } |
1607 | } |
1578 | } |
1608 | } |
1579 | |
1609 | |
|
|
1610 | #ifdef __cplusplus |
|
|
1611 | } |
|
|
1612 | #endif |
|
|
1613 | |