… | |
… | |
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 |
… | |
… | |
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 | #undef VAR |
234 | #undef VAR |
225 | }; |
235 | }; |
226 | #include "ev_wrap.h" |
236 | #include "ev_wrap.h" |
… | |
… | |
228 | struct ev_loop default_loop_struct; |
238 | struct ev_loop default_loop_struct; |
229 | static struct ev_loop *default_loop; |
239 | static struct ev_loop *default_loop; |
230 | |
240 | |
231 | #else |
241 | #else |
232 | |
242 | |
|
|
243 | ev_tstamp ev_rt_now; |
233 | #define VAR(name,decl) static decl; |
244 | #define VAR(name,decl) static decl; |
234 | #include "ev_vars.h" |
245 | #include "ev_vars.h" |
235 | #undef VAR |
246 | #undef VAR |
236 | |
247 | |
237 | static int default_loop; |
248 | static int default_loop; |
238 | |
249 | |
239 | #endif |
250 | #endif |
240 | |
251 | |
241 | /*****************************************************************************/ |
252 | /*****************************************************************************/ |
242 | |
253 | |
243 | inline ev_tstamp |
254 | ev_tstamp |
244 | ev_time (void) |
255 | ev_time (void) |
245 | { |
256 | { |
246 | #if EV_USE_REALTIME |
257 | #if EV_USE_REALTIME |
247 | struct timespec ts; |
258 | struct timespec ts; |
248 | clock_gettime (CLOCK_REALTIME, &ts); |
259 | clock_gettime (CLOCK_REALTIME, &ts); |
… | |
… | |
267 | #endif |
278 | #endif |
268 | |
279 | |
269 | return ev_time (); |
280 | return ev_time (); |
270 | } |
281 | } |
271 | |
282 | |
|
|
283 | #if EV_MULTIPLICITY |
272 | ev_tstamp |
284 | ev_tstamp |
273 | ev_now (EV_P) |
285 | ev_now (EV_P) |
274 | { |
286 | { |
275 | return rt_now; |
287 | return ev_rt_now; |
276 | } |
288 | } |
|
|
289 | #endif |
277 | |
290 | |
278 | #define array_roundsize(type,n) ((n) | 4 & ~3) |
291 | #define array_roundsize(type,n) ((n) | 4 & ~3) |
279 | |
292 | |
280 | #define array_needsize(type,base,cur,cnt,init) \ |
293 | #define array_needsize(type,base,cur,cnt,init) \ |
281 | if (expect_false ((cnt) > cur)) \ |
294 | if (expect_false ((cnt) > cur)) \ |
… | |
… | |
514 | |
527 | |
515 | heap [k] = w; |
528 | heap [k] = w; |
516 | ((W)heap [k])->active = k + 1; |
529 | ((W)heap [k])->active = k + 1; |
517 | } |
530 | } |
518 | |
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 | |
519 | /*****************************************************************************/ |
544 | /*****************************************************************************/ |
520 | |
545 | |
521 | typedef struct |
546 | typedef struct |
522 | { |
547 | { |
523 | WL head; |
548 | WL head; |
… | |
… | |
719 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
744 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
720 | have_monotonic = 1; |
745 | have_monotonic = 1; |
721 | } |
746 | } |
722 | #endif |
747 | #endif |
723 | |
748 | |
724 | rt_now = ev_time (); |
749 | ev_rt_now = ev_time (); |
725 | mn_now = get_clock (); |
750 | mn_now = get_clock (); |
726 | now_floor = mn_now; |
751 | now_floor = mn_now; |
727 | rtmn_diff = rt_now - mn_now; |
752 | rtmn_diff = ev_rt_now - mn_now; |
728 | |
753 | |
729 | if (methods == EVMETHOD_AUTO) |
754 | if (methods == EVMETHOD_AUTO) |
730 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
755 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
731 | methods = atoi (getenv ("LIBEV_METHODS")); |
756 | methods = atoi (getenv ("LIBEV_METHODS")); |
732 | else |
757 | else |
… | |
… | |
747 | #endif |
772 | #endif |
748 | #if EV_USE_SELECT |
773 | #if EV_USE_SELECT |
749 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
774 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
750 | #endif |
775 | #endif |
751 | |
776 | |
752 | ev_watcher_init (&sigev, sigcb); |
777 | ev_init (&sigev, sigcb); |
753 | ev_set_priority (&sigev, EV_MAXPRI); |
778 | ev_set_priority (&sigev, EV_MAXPRI); |
754 | } |
779 | } |
755 | } |
780 | } |
756 | |
781 | |
757 | void |
782 | void |
… | |
… | |
779 | array_free (pending, [i]); |
804 | array_free (pending, [i]); |
780 | |
805 | |
781 | /* have to use the microsoft-never-gets-it-right macro */ |
806 | /* have to use the microsoft-never-gets-it-right macro */ |
782 | array_free_microshit (fdchange); |
807 | array_free_microshit (fdchange); |
783 | array_free_microshit (timer); |
808 | array_free_microshit (timer); |
|
|
809 | #if EV_PERIODICS |
784 | array_free_microshit (periodic); |
810 | array_free_microshit (periodic); |
|
|
811 | #endif |
785 | array_free_microshit (idle); |
812 | array_free_microshit (idle); |
786 | array_free_microshit (prepare); |
813 | array_free_microshit (prepare); |
787 | array_free_microshit (check); |
814 | array_free_microshit (check); |
788 | |
815 | |
789 | method = 0; |
816 | method = 0; |
… | |
… | |
944 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
971 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
945 | |
972 | |
946 | if (p->w) |
973 | if (p->w) |
947 | { |
974 | { |
948 | p->w->pending = 0; |
975 | p->w->pending = 0; |
949 | p->w->cb (EV_A_ p->w, p->events); |
976 | EV_CB_INVOKE (p->w, p->events); |
950 | } |
977 | } |
951 | } |
978 | } |
952 | } |
979 | } |
953 | |
980 | |
954 | static void |
981 | static void |
… | |
… | |
962 | |
989 | |
963 | /* first reschedule or stop timer */ |
990 | /* first reschedule or stop timer */ |
964 | if (w->repeat) |
991 | if (w->repeat) |
965 | { |
992 | { |
966 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
993 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
|
|
994 | |
967 | ((WT)w)->at = mn_now + w->repeat; |
995 | ((WT)w)->at += w->repeat; |
|
|
996 | if (((WT)w)->at < mn_now) |
|
|
997 | ((WT)w)->at = mn_now; |
|
|
998 | |
968 | downheap ((WT *)timers, timercnt, 0); |
999 | downheap ((WT *)timers, timercnt, 0); |
969 | } |
1000 | } |
970 | else |
1001 | else |
971 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1002 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
972 | |
1003 | |
973 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
1004 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
974 | } |
1005 | } |
975 | } |
1006 | } |
976 | |
1007 | |
|
|
1008 | #if EV_PERIODICS |
977 | static void |
1009 | static void |
978 | periodics_reify (EV_P) |
1010 | periodics_reify (EV_P) |
979 | { |
1011 | { |
980 | while (periodiccnt && ((WT)periodics [0])->at <= rt_now) |
1012 | while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) |
981 | { |
1013 | { |
982 | struct ev_periodic *w = periodics [0]; |
1014 | struct ev_periodic *w = periodics [0]; |
983 | |
1015 | |
984 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
1016 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
985 | |
1017 | |
986 | /* first reschedule or stop timer */ |
1018 | /* first reschedule or stop timer */ |
987 | if (w->reschedule_cb) |
1019 | if (w->reschedule_cb) |
988 | { |
1020 | { |
989 | ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, rt_now + 0.0001); |
1021 | ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001); |
990 | |
1022 | |
991 | assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > rt_now)); |
1023 | assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now)); |
992 | downheap ((WT *)periodics, periodiccnt, 0); |
1024 | downheap ((WT *)periodics, periodiccnt, 0); |
993 | } |
1025 | } |
994 | else if (w->interval) |
1026 | else if (w->interval) |
995 | { |
1027 | { |
996 | ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; |
1028 | ((WT)w)->at += floor ((ev_rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; |
997 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now)); |
1029 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > ev_rt_now)); |
998 | downheap ((WT *)periodics, periodiccnt, 0); |
1030 | downheap ((WT *)periodics, periodiccnt, 0); |
999 | } |
1031 | } |
1000 | else |
1032 | else |
1001 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1033 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1002 | |
1034 | |
… | |
… | |
1013 | for (i = 0; i < periodiccnt; ++i) |
1045 | for (i = 0; i < periodiccnt; ++i) |
1014 | { |
1046 | { |
1015 | struct ev_periodic *w = periodics [i]; |
1047 | struct ev_periodic *w = periodics [i]; |
1016 | |
1048 | |
1017 | if (w->reschedule_cb) |
1049 | if (w->reschedule_cb) |
1018 | ((WT)w)->at = w->reschedule_cb (w, rt_now); |
1050 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
1019 | else if (w->interval) |
1051 | else if (w->interval) |
1020 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1052 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1021 | } |
1053 | } |
1022 | |
1054 | |
1023 | /* now rebuild the heap */ |
1055 | /* now rebuild the heap */ |
1024 | for (i = periodiccnt >> 1; i--; ) |
1056 | for (i = periodiccnt >> 1; i--; ) |
1025 | downheap ((WT *)periodics, periodiccnt, i); |
1057 | downheap ((WT *)periodics, periodiccnt, i); |
1026 | } |
1058 | } |
|
|
1059 | #endif |
1027 | |
1060 | |
1028 | inline int |
1061 | inline int |
1029 | time_update_monotonic (EV_P) |
1062 | time_update_monotonic (EV_P) |
1030 | { |
1063 | { |
1031 | mn_now = get_clock (); |
1064 | mn_now = get_clock (); |
1032 | |
1065 | |
1033 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
1066 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
1034 | { |
1067 | { |
1035 | rt_now = rtmn_diff + mn_now; |
1068 | ev_rt_now = rtmn_diff + mn_now; |
1036 | return 0; |
1069 | return 0; |
1037 | } |
1070 | } |
1038 | else |
1071 | else |
1039 | { |
1072 | { |
1040 | now_floor = mn_now; |
1073 | now_floor = mn_now; |
1041 | rt_now = ev_time (); |
1074 | ev_rt_now = ev_time (); |
1042 | return 1; |
1075 | return 1; |
1043 | } |
1076 | } |
1044 | } |
1077 | } |
1045 | |
1078 | |
1046 | static void |
1079 | static void |
… | |
… | |
1055 | { |
1088 | { |
1056 | ev_tstamp odiff = rtmn_diff; |
1089 | ev_tstamp odiff = rtmn_diff; |
1057 | |
1090 | |
1058 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
1091 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
1059 | { |
1092 | { |
1060 | rtmn_diff = rt_now - mn_now; |
1093 | rtmn_diff = ev_rt_now - mn_now; |
1061 | |
1094 | |
1062 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
1095 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
1063 | return; /* all is well */ |
1096 | return; /* all is well */ |
1064 | |
1097 | |
1065 | rt_now = ev_time (); |
1098 | ev_rt_now = ev_time (); |
1066 | mn_now = get_clock (); |
1099 | mn_now = get_clock (); |
1067 | now_floor = mn_now; |
1100 | now_floor = mn_now; |
1068 | } |
1101 | } |
1069 | |
1102 | |
|
|
1103 | # if EV_PERIODICS |
1070 | periodics_reschedule (EV_A); |
1104 | periodics_reschedule (EV_A); |
|
|
1105 | # endif |
1071 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
1106 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
1072 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
1107 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
1073 | } |
1108 | } |
1074 | } |
1109 | } |
1075 | else |
1110 | else |
1076 | #endif |
1111 | #endif |
1077 | { |
1112 | { |
1078 | rt_now = ev_time (); |
1113 | ev_rt_now = ev_time (); |
1079 | |
1114 | |
1080 | if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
1115 | if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
1081 | { |
1116 | { |
|
|
1117 | #if EV_PERIODICS |
1082 | periodics_reschedule (EV_A); |
1118 | periodics_reschedule (EV_A); |
|
|
1119 | #endif |
1083 | |
1120 | |
1084 | /* adjust timers. this is easy, as the offset is the same for all */ |
1121 | /* adjust timers. this is easy, as the offset is the same for all */ |
1085 | for (i = 0; i < timercnt; ++i) |
1122 | for (i = 0; i < timercnt; ++i) |
1086 | ((WT)timers [i])->at += rt_now - mn_now; |
1123 | ((WT)timers [i])->at += ev_rt_now - mn_now; |
1087 | } |
1124 | } |
1088 | |
1125 | |
1089 | mn_now = rt_now; |
1126 | mn_now = ev_rt_now; |
1090 | } |
1127 | } |
1091 | } |
1128 | } |
1092 | |
1129 | |
1093 | void |
1130 | void |
1094 | ev_ref (EV_P) |
1131 | ev_ref (EV_P) |
… | |
… | |
1134 | if (expect_true (have_monotonic)) |
1171 | if (expect_true (have_monotonic)) |
1135 | time_update_monotonic (EV_A); |
1172 | time_update_monotonic (EV_A); |
1136 | else |
1173 | else |
1137 | #endif |
1174 | #endif |
1138 | { |
1175 | { |
1139 | rt_now = ev_time (); |
1176 | ev_rt_now = ev_time (); |
1140 | mn_now = rt_now; |
1177 | mn_now = ev_rt_now; |
1141 | } |
1178 | } |
1142 | |
1179 | |
1143 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
1180 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
1144 | block = 0.; |
1181 | block = 0.; |
1145 | else |
1182 | else |
… | |
… | |
1150 | { |
1187 | { |
1151 | ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; |
1188 | ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; |
1152 | if (block > to) block = to; |
1189 | if (block > to) block = to; |
1153 | } |
1190 | } |
1154 | |
1191 | |
|
|
1192 | #if EV_PERIODICS |
1155 | if (periodiccnt) |
1193 | if (periodiccnt) |
1156 | { |
1194 | { |
1157 | ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge; |
1195 | ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge; |
1158 | if (block > to) block = to; |
1196 | if (block > to) block = to; |
1159 | } |
1197 | } |
|
|
1198 | #endif |
1160 | |
1199 | |
1161 | if (block < 0.) block = 0.; |
1200 | if (block < 0.) block = 0.; |
1162 | } |
1201 | } |
1163 | |
1202 | |
1164 | method_poll (EV_A_ block); |
1203 | method_poll (EV_A_ block); |
1165 | |
1204 | |
1166 | /* update rt_now, do magic */ |
1205 | /* update ev_rt_now, do magic */ |
1167 | time_update (EV_A); |
1206 | time_update (EV_A); |
1168 | |
1207 | |
1169 | /* queue pending timers and reschedule them */ |
1208 | /* queue pending timers and reschedule them */ |
1170 | timers_reify (EV_A); /* relative timers called last */ |
1209 | timers_reify (EV_A); /* relative timers called last */ |
|
|
1210 | #if EV_PERIODICS |
1171 | periodics_reify (EV_A); /* absolute timers called first */ |
1211 | periodics_reify (EV_A); /* absolute timers called first */ |
|
|
1212 | #endif |
1172 | |
1213 | |
1173 | /* queue idle watchers unless io or timers are pending */ |
1214 | /* queue idle watchers unless io or timers are pending */ |
1174 | if (idlecnt && !any_pending (EV_A)) |
1215 | if (idlecnt && !any_pending (EV_A)) |
1175 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1216 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1176 | |
1217 | |
… | |
… | |
1267 | { |
1308 | { |
1268 | ev_clear_pending (EV_A_ (W)w); |
1309 | ev_clear_pending (EV_A_ (W)w); |
1269 | if (!ev_is_active (w)) |
1310 | if (!ev_is_active (w)) |
1270 | return; |
1311 | return; |
1271 | |
1312 | |
|
|
1313 | assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax)); |
|
|
1314 | |
1272 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
1315 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
1273 | ev_stop (EV_A_ (W)w); |
1316 | ev_stop (EV_A_ (W)w); |
1274 | |
1317 | |
1275 | fd_change (EV_A_ w->fd); |
1318 | fd_change (EV_A_ w->fd); |
1276 | } |
1319 | } |
… | |
… | |
1306 | { |
1349 | { |
1307 | timers [((W)w)->active - 1] = timers [timercnt]; |
1350 | timers [((W)w)->active - 1] = timers [timercnt]; |
1308 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1351 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1309 | } |
1352 | } |
1310 | |
1353 | |
1311 | ((WT)w)->at = w->repeat; |
1354 | ((WT)w)->at -= mn_now; |
1312 | |
1355 | |
1313 | ev_stop (EV_A_ (W)w); |
1356 | ev_stop (EV_A_ (W)w); |
1314 | } |
1357 | } |
1315 | |
1358 | |
1316 | void |
1359 | void |
1317 | ev_timer_again (EV_P_ struct ev_timer *w) |
1360 | ev_timer_again (EV_P_ struct ev_timer *w) |
1318 | { |
1361 | { |
1319 | if (ev_is_active (w)) |
1362 | if (ev_is_active (w)) |
1320 | { |
1363 | { |
1321 | if (w->repeat) |
1364 | if (w->repeat) |
1322 | { |
|
|
1323 | ((WT)w)->at = mn_now + w->repeat; |
|
|
1324 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1365 | adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1, mn_now + w->repeat); |
1325 | } |
|
|
1326 | else |
1366 | else |
1327 | ev_timer_stop (EV_A_ w); |
1367 | ev_timer_stop (EV_A_ w); |
1328 | } |
1368 | } |
1329 | else if (w->repeat) |
1369 | else if (w->repeat) |
1330 | ev_timer_start (EV_A_ w); |
1370 | ev_timer_start (EV_A_ w); |
1331 | } |
1371 | } |
1332 | |
1372 | |
|
|
1373 | #if EV_PERIODICS |
1333 | void |
1374 | void |
1334 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1375 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1335 | { |
1376 | { |
1336 | if (ev_is_active (w)) |
1377 | if (ev_is_active (w)) |
1337 | return; |
1378 | return; |
1338 | |
1379 | |
1339 | if (w->reschedule_cb) |
1380 | if (w->reschedule_cb) |
1340 | ((WT)w)->at = w->reschedule_cb (w, rt_now); |
1381 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
1341 | else if (w->interval) |
1382 | else if (w->interval) |
1342 | { |
1383 | { |
1343 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1384 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1344 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1385 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1345 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1386 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1346 | } |
1387 | } |
1347 | |
1388 | |
1348 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1389 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1349 | array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); |
1390 | array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); |
1350 | periodics [periodiccnt - 1] = w; |
1391 | periodics [periodiccnt - 1] = w; |
… | |
… | |
1372 | } |
1413 | } |
1373 | |
1414 | |
1374 | void |
1415 | void |
1375 | ev_periodic_again (EV_P_ struct ev_periodic *w) |
1416 | ev_periodic_again (EV_P_ struct ev_periodic *w) |
1376 | { |
1417 | { |
|
|
1418 | /* TODO: use adjustheap and recalculation */ |
1377 | ev_periodic_stop (EV_A_ w); |
1419 | ev_periodic_stop (EV_A_ w); |
1378 | ev_periodic_start (EV_A_ w); |
1420 | ev_periodic_start (EV_A_ w); |
1379 | } |
1421 | } |
|
|
1422 | #endif |
1380 | |
1423 | |
1381 | void |
1424 | void |
1382 | ev_idle_start (EV_P_ struct ev_idle *w) |
1425 | ev_idle_start (EV_P_ struct ev_idle *w) |
1383 | { |
1426 | { |
1384 | if (ev_is_active (w)) |
1427 | if (ev_is_active (w)) |
… | |
… | |
1435 | |
1478 | |
1436 | void |
1479 | void |
1437 | ev_check_stop (EV_P_ struct ev_check *w) |
1480 | ev_check_stop (EV_P_ struct ev_check *w) |
1438 | { |
1481 | { |
1439 | ev_clear_pending (EV_A_ (W)w); |
1482 | ev_clear_pending (EV_A_ (W)w); |
1440 | if (ev_is_active (w)) |
1483 | if (!ev_is_active (w)) |
1441 | return; |
1484 | return; |
1442 | |
1485 | |
1443 | checks [((W)w)->active - 1] = checks [--checkcnt]; |
1486 | checks [((W)w)->active - 1] = checks [--checkcnt]; |
1444 | ev_stop (EV_A_ (W)w); |
1487 | ev_stop (EV_A_ (W)w); |
1445 | } |
1488 | } |
… | |
… | |
1506 | |
1549 | |
1507 | void |
1550 | void |
1508 | ev_child_stop (EV_P_ struct ev_child *w) |
1551 | ev_child_stop (EV_P_ struct ev_child *w) |
1509 | { |
1552 | { |
1510 | ev_clear_pending (EV_A_ (W)w); |
1553 | ev_clear_pending (EV_A_ (W)w); |
1511 | if (ev_is_active (w)) |
1554 | if (!ev_is_active (w)) |
1512 | return; |
1555 | return; |
1513 | |
1556 | |
1514 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1557 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1515 | ev_stop (EV_A_ (W)w); |
1558 | ev_stop (EV_A_ (W)w); |
1516 | } |
1559 | } |
… | |
… | |
1560 | else |
1603 | else |
1561 | { |
1604 | { |
1562 | once->cb = cb; |
1605 | once->cb = cb; |
1563 | once->arg = arg; |
1606 | once->arg = arg; |
1564 | |
1607 | |
1565 | ev_watcher_init (&once->io, once_cb_io); |
1608 | ev_init (&once->io, once_cb_io); |
1566 | if (fd >= 0) |
1609 | if (fd >= 0) |
1567 | { |
1610 | { |
1568 | ev_io_set (&once->io, fd, events); |
1611 | ev_io_set (&once->io, fd, events); |
1569 | ev_io_start (EV_A_ &once->io); |
1612 | ev_io_start (EV_A_ &once->io); |
1570 | } |
1613 | } |
1571 | |
1614 | |
1572 | ev_watcher_init (&once->to, once_cb_to); |
1615 | ev_init (&once->to, once_cb_to); |
1573 | if (timeout >= 0.) |
1616 | if (timeout >= 0.) |
1574 | { |
1617 | { |
1575 | ev_timer_set (&once->to, timeout, 0.); |
1618 | ev_timer_set (&once->to, timeout, 0.); |
1576 | ev_timer_start (EV_A_ &once->to); |
1619 | ev_timer_start (EV_A_ &once->to); |
1577 | } |
1620 | } |
1578 | } |
1621 | } |
1579 | } |
1622 | } |
1580 | |
1623 | |
|
|
1624 | #ifdef __cplusplus |
|
|
1625 | } |
|
|
1626 | #endif |
|
|
1627 | |