… | |
… | |
156 | |
156 | |
157 | #ifndef EV_USE_PORT |
157 | #ifndef EV_USE_PORT |
158 | # define EV_USE_PORT 0 |
158 | # define EV_USE_PORT 0 |
159 | #endif |
159 | #endif |
160 | |
160 | |
|
|
161 | #ifndef EV_PID_HASHSIZE |
|
|
162 | # if EV_MINIMAL |
|
|
163 | # define EV_PID_HASHSIZE 1 |
|
|
164 | # else |
|
|
165 | # define EV_PID_HASHSIZE 16 |
|
|
166 | # endif |
|
|
167 | #endif |
|
|
168 | |
161 | /**/ |
169 | /**/ |
162 | |
170 | |
163 | #ifndef CLOCK_MONOTONIC |
171 | #ifndef CLOCK_MONOTONIC |
164 | # undef EV_USE_MONOTONIC |
172 | # undef EV_USE_MONOTONIC |
165 | # define EV_USE_MONOTONIC 0 |
173 | # define EV_USE_MONOTONIC 0 |
… | |
… | |
176 | |
184 | |
177 | /**/ |
185 | /**/ |
178 | |
186 | |
179 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
187 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
180 | #define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */ |
188 | #define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */ |
181 | #define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ |
|
|
182 | /*#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */ |
189 | /*#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */ |
183 | |
190 | |
184 | #ifdef EV_H |
191 | #ifdef EV_H |
185 | # include EV_H |
192 | # include EV_H |
186 | #else |
193 | #else |
… | |
… | |
198 | # define inline_speed static inline |
205 | # define inline_speed static inline |
199 | # endif |
206 | # endif |
200 | #else |
207 | #else |
201 | # define expect(expr,value) (expr) |
208 | # define expect(expr,value) (expr) |
202 | # define inline_speed static |
209 | # define inline_speed static |
203 | # define inline_minimal static |
210 | # define inline_size static |
204 | # define noinline |
211 | # define noinline |
205 | #endif |
212 | #endif |
206 | |
213 | |
207 | #define expect_false(expr) expect ((expr) != 0, 0) |
214 | #define expect_false(expr) expect ((expr) != 0, 0) |
208 | #define expect_true(expr) expect ((expr) != 0, 1) |
215 | #define expect_true(expr) expect ((expr) != 0, 1) |
… | |
… | |
225 | |
232 | |
226 | /*****************************************************************************/ |
233 | /*****************************************************************************/ |
227 | |
234 | |
228 | static void (*syserr_cb)(const char *msg); |
235 | static void (*syserr_cb)(const char *msg); |
229 | |
236 | |
|
|
237 | void |
230 | void ev_set_syserr_cb (void (*cb)(const char *msg)) |
238 | ev_set_syserr_cb (void (*cb)(const char *msg)) |
231 | { |
239 | { |
232 | syserr_cb = cb; |
240 | syserr_cb = cb; |
233 | } |
241 | } |
234 | |
242 | |
235 | static void |
243 | static void noinline |
236 | syserr (const char *msg) |
244 | syserr (const char *msg) |
237 | { |
245 | { |
238 | if (!msg) |
246 | if (!msg) |
239 | msg = "(libev) system error"; |
247 | msg = "(libev) system error"; |
240 | |
248 | |
… | |
… | |
245 | perror (msg); |
253 | perror (msg); |
246 | abort (); |
254 | abort (); |
247 | } |
255 | } |
248 | } |
256 | } |
249 | |
257 | |
250 | static void *(*alloc)(void *ptr, long size); |
258 | static void *(*alloc)(void *ptr, size_t size) = realloc; |
251 | |
259 | |
|
|
260 | void |
252 | void ev_set_allocator (void *(*cb)(void *ptr, long size)) |
261 | ev_set_allocator (void *(*cb)(void *ptr, size_t size)) |
253 | { |
262 | { |
254 | alloc = cb; |
263 | alloc = cb; |
255 | } |
264 | } |
256 | |
265 | |
257 | static void * |
266 | inline_speed void * |
258 | ev_realloc (void *ptr, long size) |
267 | ev_realloc (void *ptr, size_t size) |
259 | { |
268 | { |
260 | ptr = alloc ? alloc (ptr, size) : realloc (ptr, size); |
269 | ptr = alloc (ptr, size); |
261 | |
270 | |
262 | if (!ptr && size) |
271 | if (!ptr && size) |
263 | { |
272 | { |
264 | fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size); |
273 | fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", (long)size); |
265 | abort (); |
274 | abort (); |
266 | } |
275 | } |
267 | |
276 | |
268 | return ptr; |
277 | return ptr; |
269 | } |
278 | } |
… | |
… | |
315 | |
324 | |
316 | #endif |
325 | #endif |
317 | |
326 | |
318 | /*****************************************************************************/ |
327 | /*****************************************************************************/ |
319 | |
328 | |
320 | ev_tstamp noinline |
329 | ev_tstamp |
321 | ev_time (void) |
330 | ev_time (void) |
322 | { |
331 | { |
323 | #if EV_USE_REALTIME |
332 | #if EV_USE_REALTIME |
324 | struct timespec ts; |
333 | struct timespec ts; |
325 | clock_gettime (CLOCK_REALTIME, &ts); |
334 | clock_gettime (CLOCK_REALTIME, &ts); |
… | |
… | |
382 | #define array_free(stem, idx) \ |
391 | #define array_free(stem, idx) \ |
383 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; |
392 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; |
384 | |
393 | |
385 | /*****************************************************************************/ |
394 | /*****************************************************************************/ |
386 | |
395 | |
387 | void inline_size |
|
|
388 | anfds_init (ANFD *base, int count) |
|
|
389 | { |
|
|
390 | while (count--) |
|
|
391 | { |
|
|
392 | base->head = 0; |
|
|
393 | base->events = EV_NONE; |
|
|
394 | base->reify = 0; |
|
|
395 | |
|
|
396 | ++base; |
|
|
397 | } |
|
|
398 | } |
|
|
399 | |
|
|
400 | void noinline |
396 | void noinline |
401 | ev_feed_event (EV_P_ void *w, int revents) |
397 | ev_feed_event (EV_P_ void *w, int revents) |
402 | { |
398 | { |
403 | W w_ = (W)w; |
399 | W w_ = (W)w; |
404 | |
400 | |
… | |
… | |
412 | array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], EMPTY2); |
408 | array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], EMPTY2); |
413 | pendings [ABSPRI (w_)][w_->pending - 1].w = w_; |
409 | pendings [ABSPRI (w_)][w_->pending - 1].w = w_; |
414 | pendings [ABSPRI (w_)][w_->pending - 1].events = revents; |
410 | pendings [ABSPRI (w_)][w_->pending - 1].events = revents; |
415 | } |
411 | } |
416 | |
412 | |
417 | static void |
413 | void inline_size |
418 | queue_events (EV_P_ W *events, int eventcnt, int type) |
414 | queue_events (EV_P_ W *events, int eventcnt, int type) |
419 | { |
415 | { |
420 | int i; |
416 | int i; |
421 | |
417 | |
422 | for (i = 0; i < eventcnt; ++i) |
418 | for (i = 0; i < eventcnt; ++i) |
423 | ev_feed_event (EV_A_ events [i], type); |
419 | ev_feed_event (EV_A_ events [i], type); |
424 | } |
420 | } |
425 | |
421 | |
|
|
422 | /*****************************************************************************/ |
|
|
423 | |
|
|
424 | void inline_size |
|
|
425 | anfds_init (ANFD *base, int count) |
|
|
426 | { |
|
|
427 | while (count--) |
|
|
428 | { |
|
|
429 | base->head = 0; |
|
|
430 | base->events = EV_NONE; |
|
|
431 | base->reify = 0; |
|
|
432 | |
|
|
433 | ++base; |
|
|
434 | } |
|
|
435 | } |
|
|
436 | |
426 | void inline_speed |
437 | void inline_speed |
427 | fd_event (EV_P_ int fd, int revents) |
438 | fd_event (EV_P_ int fd, int revents) |
428 | { |
439 | { |
429 | ANFD *anfd = anfds + fd; |
440 | ANFD *anfd = anfds + fd; |
430 | ev_io *w; |
441 | ev_io *w; |
… | |
… | |
441 | void |
452 | void |
442 | ev_feed_fd_event (EV_P_ int fd, int revents) |
453 | ev_feed_fd_event (EV_P_ int fd, int revents) |
443 | { |
454 | { |
444 | fd_event (EV_A_ fd, revents); |
455 | fd_event (EV_A_ fd, revents); |
445 | } |
456 | } |
446 | |
|
|
447 | /*****************************************************************************/ |
|
|
448 | |
457 | |
449 | void inline_size |
458 | void inline_size |
450 | fd_reify (EV_P) |
459 | fd_reify (EV_P) |
451 | { |
460 | { |
452 | int i; |
461 | int i; |
… | |
… | |
707 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
716 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
708 | } |
717 | } |
709 | |
718 | |
710 | /*****************************************************************************/ |
719 | /*****************************************************************************/ |
711 | |
720 | |
712 | static ev_child *childs [PID_HASHSIZE]; |
721 | static ev_child *childs [EV_PID_HASHSIZE]; |
713 | |
722 | |
714 | #ifndef _WIN32 |
723 | #ifndef _WIN32 |
715 | |
724 | |
716 | static ev_signal childev; |
725 | static ev_signal childev; |
717 | |
|
|
718 | #ifndef WCONTINUED |
|
|
719 | # define WCONTINUED 0 |
|
|
720 | #endif |
|
|
721 | |
726 | |
722 | void inline_speed |
727 | void inline_speed |
723 | child_reap (EV_P_ ev_signal *sw, int chain, int pid, int status) |
728 | child_reap (EV_P_ ev_signal *sw, int chain, int pid, int status) |
724 | { |
729 | { |
725 | ev_child *w; |
730 | ev_child *w; |
726 | |
731 | |
727 | for (w = (ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) |
732 | for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) |
728 | if (w->pid == pid || !w->pid) |
733 | if (w->pid == pid || !w->pid) |
729 | { |
734 | { |
730 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
735 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
731 | w->rpid = pid; |
736 | w->rpid = pid; |
732 | w->rstatus = status; |
737 | w->rstatus = status; |
733 | ev_feed_event (EV_A_ (W)w, EV_CHILD); |
738 | ev_feed_event (EV_A_ (W)w, EV_CHILD); |
734 | } |
739 | } |
735 | } |
740 | } |
736 | |
741 | |
|
|
742 | #ifndef WCONTINUED |
|
|
743 | # define WCONTINUED 0 |
|
|
744 | #endif |
|
|
745 | |
737 | static void |
746 | static void |
738 | childcb (EV_P_ ev_signal *sw, int revents) |
747 | childcb (EV_P_ ev_signal *sw, int revents) |
739 | { |
748 | { |
740 | int pid, status; |
749 | int pid, status; |
741 | |
750 | |
|
|
751 | /* some systems define WCONTINUED but then fail to support it (linux 2.4) */ |
742 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
752 | if (0 >= (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
743 | { |
753 | if (!WCONTINUED |
|
|
754 | || errno != EINVAL |
|
|
755 | || 0 >= (pid = waitpid (-1, &status, WNOHANG | WUNTRACED))) |
|
|
756 | return; |
|
|
757 | |
744 | /* make sure we are called again until all childs have been reaped */ |
758 | /* make sure we are called again until all childs have been reaped */ |
745 | /* we need to do it this way so that the callback gets called before we continue */ |
759 | /* we need to do it this way so that the callback gets called before we continue */ |
746 | ev_feed_event (EV_A_ (W)sw, EV_SIGNAL); |
760 | ev_feed_event (EV_A_ (W)sw, EV_SIGNAL); |
747 | |
761 | |
748 | child_reap (EV_A_ sw, pid, pid, status); |
762 | child_reap (EV_A_ sw, pid, pid, status); |
|
|
763 | if (EV_PID_HASHSIZE > 1) |
749 | child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but feed_event catches that */ |
764 | child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but feed_event catches that */ |
750 | } |
|
|
751 | } |
765 | } |
752 | |
766 | |
753 | #endif |
767 | #endif |
754 | |
768 | |
755 | /*****************************************************************************/ |
769 | /*****************************************************************************/ |
… | |
… | |
838 | ev_backend (EV_P) |
852 | ev_backend (EV_P) |
839 | { |
853 | { |
840 | return backend; |
854 | return backend; |
841 | } |
855 | } |
842 | |
856 | |
843 | static void |
857 | static void noinline |
844 | loop_init (EV_P_ unsigned int flags) |
858 | loop_init (EV_P_ unsigned int flags) |
845 | { |
859 | { |
846 | if (!backend) |
860 | if (!backend) |
847 | { |
861 | { |
848 | #if EV_USE_MONOTONIC |
862 | #if EV_USE_MONOTONIC |
… | |
… | |
886 | ev_init (&sigev, sigcb); |
900 | ev_init (&sigev, sigcb); |
887 | ev_set_priority (&sigev, EV_MAXPRI); |
901 | ev_set_priority (&sigev, EV_MAXPRI); |
888 | } |
902 | } |
889 | } |
903 | } |
890 | |
904 | |
891 | static void |
905 | static void noinline |
892 | loop_destroy (EV_P) |
906 | loop_destroy (EV_P) |
893 | { |
907 | { |
894 | int i; |
908 | int i; |
895 | |
909 | |
896 | #if EV_USE_PORT |
910 | #if EV_USE_PORT |
… | |
… | |
923 | array_free (check, EMPTY0); |
937 | array_free (check, EMPTY0); |
924 | |
938 | |
925 | backend = 0; |
939 | backend = 0; |
926 | } |
940 | } |
927 | |
941 | |
928 | static void |
942 | void inline_size |
929 | loop_fork (EV_P) |
943 | loop_fork (EV_P) |
930 | { |
944 | { |
931 | #if EV_USE_PORT |
945 | #if EV_USE_PORT |
932 | if (backend == EVBACKEND_PORT ) port_fork (EV_A); |
946 | if (backend == EVBACKEND_PORT ) port_fork (EV_A); |
933 | #endif |
947 | #endif |
… | |
… | |
1083 | { |
1097 | { |
1084 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
1098 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
1085 | |
1099 | |
1086 | if (expect_true (p->w)) |
1100 | if (expect_true (p->w)) |
1087 | { |
1101 | { |
1088 | assert (("non-pending watcher on pending list", p->w->pending)); |
1102 | /*assert (("non-pending watcher on pending list", p->w->pending));*/ |
1089 | |
1103 | |
1090 | p->w->pending = 0; |
1104 | p->w->pending = 0; |
1091 | EV_CB_INVOKE (p->w, p->events); |
1105 | EV_CB_INVOKE (p->w, p->events); |
1092 | } |
1106 | } |
1093 | } |
1107 | } |
… | |
… | |
1098 | { |
1112 | { |
1099 | while (timercnt && ((WT)timers [0])->at <= mn_now) |
1113 | while (timercnt && ((WT)timers [0])->at <= mn_now) |
1100 | { |
1114 | { |
1101 | ev_timer *w = timers [0]; |
1115 | ev_timer *w = timers [0]; |
1102 | |
1116 | |
1103 | assert (("inactive timer on timer heap detected", ev_is_active (w))); |
1117 | /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/ |
1104 | |
1118 | |
1105 | /* first reschedule or stop timer */ |
1119 | /* first reschedule or stop timer */ |
1106 | if (w->repeat) |
1120 | if (w->repeat) |
1107 | { |
1121 | { |
1108 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
1122 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
… | |
… | |
1126 | { |
1140 | { |
1127 | while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) |
1141 | while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) |
1128 | { |
1142 | { |
1129 | ev_periodic *w = periodics [0]; |
1143 | ev_periodic *w = periodics [0]; |
1130 | |
1144 | |
1131 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
1145 | /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/ |
1132 | |
1146 | |
1133 | /* first reschedule or stop timer */ |
1147 | /* first reschedule or stop timer */ |
1134 | if (w->reschedule_cb) |
1148 | if (w->reschedule_cb) |
1135 | { |
1149 | { |
1136 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001); |
1150 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001); |
… | |
… | |
1270 | ? EVUNLOOP_ONE |
1284 | ? EVUNLOOP_ONE |
1271 | : EVUNLOOP_CANCEL; |
1285 | : EVUNLOOP_CANCEL; |
1272 | |
1286 | |
1273 | while (activecnt) |
1287 | while (activecnt) |
1274 | { |
1288 | { |
|
|
1289 | /* we might have forked, so reify kernel state if necessary */ |
|
|
1290 | #if EV_FORK_ENABLE |
|
|
1291 | if (expect_false (postfork)) |
|
|
1292 | if (forkcnt) |
|
|
1293 | { |
|
|
1294 | queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK); |
|
|
1295 | call_pending (EV_A); |
|
|
1296 | } |
|
|
1297 | #endif |
|
|
1298 | |
1275 | /* queue check watchers (and execute them) */ |
1299 | /* queue check watchers (and execute them) */ |
1276 | if (expect_false (preparecnt)) |
1300 | if (expect_false (preparecnt)) |
1277 | { |
1301 | { |
1278 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
1302 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
1279 | call_pending (EV_A); |
1303 | call_pending (EV_A); |
… | |
… | |
1458 | ev_start (EV_A_ (W)w, ++timercnt); |
1482 | ev_start (EV_A_ (W)w, ++timercnt); |
1459 | array_needsize (ev_timer *, timers, timermax, timercnt, EMPTY2); |
1483 | array_needsize (ev_timer *, timers, timermax, timercnt, EMPTY2); |
1460 | timers [timercnt - 1] = w; |
1484 | timers [timercnt - 1] = w; |
1461 | upheap ((WT *)timers, timercnt - 1); |
1485 | upheap ((WT *)timers, timercnt - 1); |
1462 | |
1486 | |
1463 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1487 | /*assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));*/ |
1464 | } |
1488 | } |
1465 | |
1489 | |
1466 | void |
1490 | void |
1467 | ev_timer_stop (EV_P_ ev_timer *w) |
1491 | ev_timer_stop (EV_P_ ev_timer *w) |
1468 | { |
1492 | { |
… | |
… | |
1470 | if (expect_false (!ev_is_active (w))) |
1494 | if (expect_false (!ev_is_active (w))) |
1471 | return; |
1495 | return; |
1472 | |
1496 | |
1473 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1497 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1474 | |
1498 | |
|
|
1499 | { |
|
|
1500 | int active = ((W)w)->active; |
|
|
1501 | |
1475 | if (expect_true (((W)w)->active < timercnt--)) |
1502 | if (expect_true (--active < --timercnt)) |
1476 | { |
1503 | { |
1477 | timers [((W)w)->active - 1] = timers [timercnt]; |
1504 | timers [active] = timers [timercnt]; |
1478 | adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1505 | adjustheap ((WT *)timers, timercnt, active); |
1479 | } |
1506 | } |
|
|
1507 | } |
1480 | |
1508 | |
1481 | ((WT)w)->at -= mn_now; |
1509 | ((WT)w)->at -= mn_now; |
1482 | |
1510 | |
1483 | ev_stop (EV_A_ (W)w); |
1511 | ev_stop (EV_A_ (W)w); |
1484 | } |
1512 | } |
… | |
… | |
1522 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1550 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1523 | array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); |
1551 | array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); |
1524 | periodics [periodiccnt - 1] = w; |
1552 | periodics [periodiccnt - 1] = w; |
1525 | upheap ((WT *)periodics, periodiccnt - 1); |
1553 | upheap ((WT *)periodics, periodiccnt - 1); |
1526 | |
1554 | |
1527 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1555 | /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/ |
1528 | } |
1556 | } |
1529 | |
1557 | |
1530 | void |
1558 | void |
1531 | ev_periodic_stop (EV_P_ ev_periodic *w) |
1559 | ev_periodic_stop (EV_P_ ev_periodic *w) |
1532 | { |
1560 | { |
… | |
… | |
1534 | if (expect_false (!ev_is_active (w))) |
1562 | if (expect_false (!ev_is_active (w))) |
1535 | return; |
1563 | return; |
1536 | |
1564 | |
1537 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1565 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1538 | |
1566 | |
|
|
1567 | { |
|
|
1568 | int active = ((W)w)->active; |
|
|
1569 | |
1539 | if (expect_true (((W)w)->active < periodiccnt--)) |
1570 | if (expect_true (--active < --periodiccnt)) |
1540 | { |
1571 | { |
1541 | periodics [((W)w)->active - 1] = periodics [periodiccnt]; |
1572 | periodics [active] = periodics [periodiccnt]; |
1542 | adjustheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); |
1573 | adjustheap ((WT *)periodics, periodiccnt, active); |
1543 | } |
1574 | } |
|
|
1575 | } |
1544 | |
1576 | |
1545 | ev_stop (EV_A_ (W)w); |
1577 | ev_stop (EV_A_ (W)w); |
1546 | } |
1578 | } |
1547 | |
1579 | |
1548 | void |
1580 | void |
… | |
… | |
1551 | /* TODO: use adjustheap and recalculation */ |
1583 | /* TODO: use adjustheap and recalculation */ |
1552 | ev_periodic_stop (EV_A_ w); |
1584 | ev_periodic_stop (EV_A_ w); |
1553 | ev_periodic_start (EV_A_ w); |
1585 | ev_periodic_start (EV_A_ w); |
1554 | } |
1586 | } |
1555 | #endif |
1587 | #endif |
1556 | |
|
|
1557 | void |
|
|
1558 | ev_idle_start (EV_P_ ev_idle *w) |
|
|
1559 | { |
|
|
1560 | if (expect_false (ev_is_active (w))) |
|
|
1561 | return; |
|
|
1562 | |
|
|
1563 | ev_start (EV_A_ (W)w, ++idlecnt); |
|
|
1564 | array_needsize (ev_idle *, idles, idlemax, idlecnt, EMPTY2); |
|
|
1565 | idles [idlecnt - 1] = w; |
|
|
1566 | } |
|
|
1567 | |
|
|
1568 | void |
|
|
1569 | ev_idle_stop (EV_P_ ev_idle *w) |
|
|
1570 | { |
|
|
1571 | ev_clear_pending (EV_A_ (W)w); |
|
|
1572 | if (expect_false (!ev_is_active (w))) |
|
|
1573 | return; |
|
|
1574 | |
|
|
1575 | { |
|
|
1576 | int active = ((W)w)->active; |
|
|
1577 | idles [active - 1] = idles [--idlecnt]; |
|
|
1578 | ((W)idles [active - 1])->active = active; |
|
|
1579 | } |
|
|
1580 | |
|
|
1581 | ev_stop (EV_A_ (W)w); |
|
|
1582 | } |
|
|
1583 | |
|
|
1584 | void |
|
|
1585 | ev_prepare_start (EV_P_ ev_prepare *w) |
|
|
1586 | { |
|
|
1587 | if (expect_false (ev_is_active (w))) |
|
|
1588 | return; |
|
|
1589 | |
|
|
1590 | ev_start (EV_A_ (W)w, ++preparecnt); |
|
|
1591 | array_needsize (ev_prepare *, prepares, preparemax, preparecnt, EMPTY2); |
|
|
1592 | prepares [preparecnt - 1] = w; |
|
|
1593 | } |
|
|
1594 | |
|
|
1595 | void |
|
|
1596 | ev_prepare_stop (EV_P_ ev_prepare *w) |
|
|
1597 | { |
|
|
1598 | ev_clear_pending (EV_A_ (W)w); |
|
|
1599 | if (expect_false (!ev_is_active (w))) |
|
|
1600 | return; |
|
|
1601 | |
|
|
1602 | { |
|
|
1603 | int active = ((W)w)->active; |
|
|
1604 | prepares [active - 1] = prepares [--preparecnt]; |
|
|
1605 | ((W)prepares [active - 1])->active = active; |
|
|
1606 | } |
|
|
1607 | |
|
|
1608 | ev_stop (EV_A_ (W)w); |
|
|
1609 | } |
|
|
1610 | |
|
|
1611 | void |
|
|
1612 | ev_check_start (EV_P_ ev_check *w) |
|
|
1613 | { |
|
|
1614 | if (expect_false (ev_is_active (w))) |
|
|
1615 | return; |
|
|
1616 | |
|
|
1617 | ev_start (EV_A_ (W)w, ++checkcnt); |
|
|
1618 | array_needsize (ev_check *, checks, checkmax, checkcnt, EMPTY2); |
|
|
1619 | checks [checkcnt - 1] = w; |
|
|
1620 | } |
|
|
1621 | |
|
|
1622 | void |
|
|
1623 | ev_check_stop (EV_P_ ev_check *w) |
|
|
1624 | { |
|
|
1625 | ev_clear_pending (EV_A_ (W)w); |
|
|
1626 | if (expect_false (!ev_is_active (w))) |
|
|
1627 | return; |
|
|
1628 | |
|
|
1629 | { |
|
|
1630 | int active = ((W)w)->active; |
|
|
1631 | checks [active - 1] = checks [--checkcnt]; |
|
|
1632 | ((W)checks [active - 1])->active = active; |
|
|
1633 | } |
|
|
1634 | |
|
|
1635 | ev_stop (EV_A_ (W)w); |
|
|
1636 | } |
|
|
1637 | |
1588 | |
1638 | #ifndef SA_RESTART |
1589 | #ifndef SA_RESTART |
1639 | # define SA_RESTART 0 |
1590 | # define SA_RESTART 0 |
1640 | #endif |
1591 | #endif |
1641 | |
1592 | |
… | |
… | |
1690 | #endif |
1641 | #endif |
1691 | if (expect_false (ev_is_active (w))) |
1642 | if (expect_false (ev_is_active (w))) |
1692 | return; |
1643 | return; |
1693 | |
1644 | |
1694 | ev_start (EV_A_ (W)w, 1); |
1645 | ev_start (EV_A_ (W)w, 1); |
1695 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1646 | wlist_add ((WL *)&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w); |
1696 | } |
1647 | } |
1697 | |
1648 | |
1698 | void |
1649 | void |
1699 | ev_child_stop (EV_P_ ev_child *w) |
1650 | ev_child_stop (EV_P_ ev_child *w) |
1700 | { |
1651 | { |
1701 | ev_clear_pending (EV_A_ (W)w); |
1652 | ev_clear_pending (EV_A_ (W)w); |
1702 | if (expect_false (!ev_is_active (w))) |
1653 | if (expect_false (!ev_is_active (w))) |
1703 | return; |
1654 | return; |
1704 | |
1655 | |
1705 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1656 | wlist_del ((WL *)&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w); |
|
|
1657 | ev_stop (EV_A_ (W)w); |
|
|
1658 | } |
|
|
1659 | |
|
|
1660 | #if EV_STAT_ENABLE |
|
|
1661 | |
|
|
1662 | # ifdef _WIN32 |
|
|
1663 | # undef lstat |
|
|
1664 | # define lstat(a,b) _stati64 (a,b) |
|
|
1665 | # endif |
|
|
1666 | |
|
|
1667 | #define DEF_STAT_INTERVAL 5.0074891 |
|
|
1668 | #define MIN_STAT_INTERVAL 0.1074891 |
|
|
1669 | |
|
|
1670 | void |
|
|
1671 | ev_stat_stat (EV_P_ ev_stat *w) |
|
|
1672 | { |
|
|
1673 | if (lstat (w->path, &w->attr) < 0) |
|
|
1674 | w->attr.st_nlink = 0; |
|
|
1675 | else if (!w->attr.st_nlink) |
|
|
1676 | w->attr.st_nlink = 1; |
|
|
1677 | } |
|
|
1678 | |
|
|
1679 | static void |
|
|
1680 | stat_timer_cb (EV_P_ ev_timer *w_, int revents) |
|
|
1681 | { |
|
|
1682 | ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer)); |
|
|
1683 | |
|
|
1684 | /* we copy this here each the time so that */ |
|
|
1685 | /* prev has the old value when the callback gets invoked */ |
|
|
1686 | w->prev = w->attr; |
|
|
1687 | ev_stat_stat (EV_A_ w); |
|
|
1688 | |
|
|
1689 | if (memcmp (&w->prev, &w->attr, sizeof (ev_statdata))) |
|
|
1690 | ev_feed_event (EV_A_ w, EV_STAT); |
|
|
1691 | } |
|
|
1692 | |
|
|
1693 | void |
|
|
1694 | ev_stat_start (EV_P_ ev_stat *w) |
|
|
1695 | { |
|
|
1696 | if (expect_false (ev_is_active (w))) |
|
|
1697 | return; |
|
|
1698 | |
|
|
1699 | /* since we use memcmp, we need to clear any padding data etc. */ |
|
|
1700 | memset (&w->prev, 0, sizeof (ev_statdata)); |
|
|
1701 | memset (&w->attr, 0, sizeof (ev_statdata)); |
|
|
1702 | |
|
|
1703 | ev_stat_stat (EV_A_ w); |
|
|
1704 | |
|
|
1705 | if (w->interval < MIN_STAT_INTERVAL) |
|
|
1706 | w->interval = w->interval ? MIN_STAT_INTERVAL : DEF_STAT_INTERVAL; |
|
|
1707 | |
|
|
1708 | ev_timer_init (&w->timer, stat_timer_cb, w->interval, w->interval); |
|
|
1709 | ev_set_priority (&w->timer, ev_priority (w)); |
|
|
1710 | ev_timer_start (EV_A_ &w->timer); |
|
|
1711 | |
|
|
1712 | ev_start (EV_A_ (W)w, 1); |
|
|
1713 | } |
|
|
1714 | |
|
|
1715 | void |
|
|
1716 | ev_stat_stop (EV_P_ ev_stat *w) |
|
|
1717 | { |
|
|
1718 | ev_clear_pending (EV_A_ (W)w); |
|
|
1719 | if (expect_false (!ev_is_active (w))) |
|
|
1720 | return; |
|
|
1721 | |
|
|
1722 | ev_timer_stop (EV_A_ &w->timer); |
|
|
1723 | |
|
|
1724 | ev_stop (EV_A_ (W)w); |
|
|
1725 | } |
|
|
1726 | #endif |
|
|
1727 | |
|
|
1728 | void |
|
|
1729 | ev_idle_start (EV_P_ ev_idle *w) |
|
|
1730 | { |
|
|
1731 | if (expect_false (ev_is_active (w))) |
|
|
1732 | return; |
|
|
1733 | |
|
|
1734 | ev_start (EV_A_ (W)w, ++idlecnt); |
|
|
1735 | array_needsize (ev_idle *, idles, idlemax, idlecnt, EMPTY2); |
|
|
1736 | idles [idlecnt - 1] = w; |
|
|
1737 | } |
|
|
1738 | |
|
|
1739 | void |
|
|
1740 | ev_idle_stop (EV_P_ ev_idle *w) |
|
|
1741 | { |
|
|
1742 | ev_clear_pending (EV_A_ (W)w); |
|
|
1743 | if (expect_false (!ev_is_active (w))) |
|
|
1744 | return; |
|
|
1745 | |
|
|
1746 | { |
|
|
1747 | int active = ((W)w)->active; |
|
|
1748 | idles [active - 1] = idles [--idlecnt]; |
|
|
1749 | ((W)idles [active - 1])->active = active; |
|
|
1750 | } |
|
|
1751 | |
|
|
1752 | ev_stop (EV_A_ (W)w); |
|
|
1753 | } |
|
|
1754 | |
|
|
1755 | void |
|
|
1756 | ev_prepare_start (EV_P_ ev_prepare *w) |
|
|
1757 | { |
|
|
1758 | if (expect_false (ev_is_active (w))) |
|
|
1759 | return; |
|
|
1760 | |
|
|
1761 | ev_start (EV_A_ (W)w, ++preparecnt); |
|
|
1762 | array_needsize (ev_prepare *, prepares, preparemax, preparecnt, EMPTY2); |
|
|
1763 | prepares [preparecnt - 1] = w; |
|
|
1764 | } |
|
|
1765 | |
|
|
1766 | void |
|
|
1767 | ev_prepare_stop (EV_P_ ev_prepare *w) |
|
|
1768 | { |
|
|
1769 | ev_clear_pending (EV_A_ (W)w); |
|
|
1770 | if (expect_false (!ev_is_active (w))) |
|
|
1771 | return; |
|
|
1772 | |
|
|
1773 | { |
|
|
1774 | int active = ((W)w)->active; |
|
|
1775 | prepares [active - 1] = prepares [--preparecnt]; |
|
|
1776 | ((W)prepares [active - 1])->active = active; |
|
|
1777 | } |
|
|
1778 | |
|
|
1779 | ev_stop (EV_A_ (W)w); |
|
|
1780 | } |
|
|
1781 | |
|
|
1782 | void |
|
|
1783 | ev_check_start (EV_P_ ev_check *w) |
|
|
1784 | { |
|
|
1785 | if (expect_false (ev_is_active (w))) |
|
|
1786 | return; |
|
|
1787 | |
|
|
1788 | ev_start (EV_A_ (W)w, ++checkcnt); |
|
|
1789 | array_needsize (ev_check *, checks, checkmax, checkcnt, EMPTY2); |
|
|
1790 | checks [checkcnt - 1] = w; |
|
|
1791 | } |
|
|
1792 | |
|
|
1793 | void |
|
|
1794 | ev_check_stop (EV_P_ ev_check *w) |
|
|
1795 | { |
|
|
1796 | ev_clear_pending (EV_A_ (W)w); |
|
|
1797 | if (expect_false (!ev_is_active (w))) |
|
|
1798 | return; |
|
|
1799 | |
|
|
1800 | { |
|
|
1801 | int active = ((W)w)->active; |
|
|
1802 | checks [active - 1] = checks [--checkcnt]; |
|
|
1803 | ((W)checks [active - 1])->active = active; |
|
|
1804 | } |
|
|
1805 | |
1706 | ev_stop (EV_A_ (W)w); |
1806 | ev_stop (EV_A_ (W)w); |
1707 | } |
1807 | } |
1708 | |
1808 | |
1709 | #if EV_EMBED_ENABLE |
1809 | #if EV_EMBED_ENABLE |
1710 | void noinline |
1810 | void noinline |
… | |
… | |
1753 | |
1853 | |
1754 | ev_stop (EV_A_ (W)w); |
1854 | ev_stop (EV_A_ (W)w); |
1755 | } |
1855 | } |
1756 | #endif |
1856 | #endif |
1757 | |
1857 | |
1758 | #if EV_STAT_ENABLE |
1858 | #if EV_FORK_ENABLE |
1759 | |
|
|
1760 | # ifdef _WIN32 |
|
|
1761 | # define lstat(a,b) stat(a,b) |
|
|
1762 | # endif |
|
|
1763 | |
|
|
1764 | void |
1859 | void |
1765 | ev_stat_stat (EV_P_ ev_stat *w) |
|
|
1766 | { |
|
|
1767 | if (lstat (w->path, &w->attr) < 0) |
|
|
1768 | w->attr.st_nlink = 0; |
|
|
1769 | else if (!w->attr.st_nlink) |
|
|
1770 | w->attr.st_nlink = 1; |
|
|
1771 | } |
|
|
1772 | |
|
|
1773 | static void |
|
|
1774 | stat_timer_cb (EV_P_ ev_timer *w_, int revents) |
|
|
1775 | { |
|
|
1776 | ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer)); |
|
|
1777 | |
|
|
1778 | /* we copy this here each the time so that */ |
|
|
1779 | /* prev has the old value when the callback gets invoked */ |
|
|
1780 | w->prev = w->attr; |
|
|
1781 | ev_stat_stat (EV_A_ w); |
|
|
1782 | |
|
|
1783 | if (memcmp (&w->prev, &w->attr, sizeof (ev_statdata))) |
|
|
1784 | ev_feed_event (EV_A_ w, EV_STAT); |
|
|
1785 | } |
|
|
1786 | |
|
|
1787 | void |
|
|
1788 | ev_stat_start (EV_P_ ev_stat *w) |
1860 | ev_fork_start (EV_P_ ev_fork *w) |
1789 | { |
1861 | { |
1790 | if (expect_false (ev_is_active (w))) |
1862 | if (expect_false (ev_is_active (w))) |
1791 | return; |
1863 | return; |
1792 | |
1864 | |
1793 | /* since we use memcmp, we need to clear any padding data etc. */ |
|
|
1794 | memset (&w->prev, 0, sizeof (ev_statdata)); |
|
|
1795 | memset (&w->attr, 0, sizeof (ev_statdata)); |
|
|
1796 | |
|
|
1797 | ev_stat_stat (EV_A_ w); |
|
|
1798 | |
|
|
1799 | ev_timer_init (&w->timer, stat_timer_cb, w->interval, w->interval); |
|
|
1800 | ev_set_priority (&w->timer, ev_priority (w)); |
|
|
1801 | ev_timer_start (EV_A_ &w->timer); |
|
|
1802 | |
|
|
1803 | ev_start (EV_A_ (W)w, 1); |
1865 | ev_start (EV_A_ (W)w, ++forkcnt); |
|
|
1866 | array_needsize (ev_fork *, forks, forkmax, forkcnt, EMPTY2); |
|
|
1867 | forks [forkcnt - 1] = w; |
1804 | } |
1868 | } |
1805 | |
1869 | |
1806 | void |
1870 | void |
1807 | ev_stat_stop (EV_P_ ev_stat *w) |
1871 | ev_fork_stop (EV_P_ ev_fork *w) |
1808 | { |
1872 | { |
1809 | ev_clear_pending (EV_A_ (W)w); |
1873 | ev_clear_pending (EV_A_ (W)w); |
1810 | if (expect_false (!ev_is_active (w))) |
1874 | if (expect_false (!ev_is_active (w))) |
1811 | return; |
1875 | return; |
1812 | |
1876 | |
1813 | ev_timer_stop (EV_A_ &w->timer); |
1877 | { |
|
|
1878 | int active = ((W)w)->active; |
|
|
1879 | forks [active - 1] = forks [--forkcnt]; |
|
|
1880 | ((W)forks [active - 1])->active = active; |
|
|
1881 | } |
1814 | |
1882 | |
1815 | ev_stop (EV_A_ (W)w); |
1883 | ev_stop (EV_A_ (W)w); |
1816 | } |
1884 | } |
1817 | #endif |
1885 | #endif |
1818 | |
1886 | |