… | |
… | |
222 | #define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */ |
222 | #define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */ |
223 | /*#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */ |
223 | /*#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */ |
224 | |
224 | |
225 | #if __GNUC__ >= 3 |
225 | #if __GNUC__ >= 3 |
226 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
226 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
227 | # define inline_size static inline /* inline for codesize */ |
|
|
228 | # if EV_MINIMAL |
|
|
229 | # define noinline __attribute__ ((noinline)) |
227 | # define noinline __attribute__ ((noinline)) |
230 | # define inline_speed static noinline |
|
|
231 | # else |
|
|
232 | # define noinline |
|
|
233 | # define inline_speed static inline |
|
|
234 | # endif |
|
|
235 | #else |
228 | #else |
236 | # define expect(expr,value) (expr) |
229 | # define expect(expr,value) (expr) |
237 | # define inline_speed static |
|
|
238 | # define inline_size static |
|
|
239 | # define noinline |
230 | # define noinline |
|
|
231 | # if __STDC_VERSION__ < 199901L |
|
|
232 | # define inline |
|
|
233 | # endif |
240 | #endif |
234 | #endif |
241 | |
235 | |
242 | #define expect_false(expr) expect ((expr) != 0, 0) |
236 | #define expect_false(expr) expect ((expr) != 0, 0) |
243 | #define expect_true(expr) expect ((expr) != 0, 1) |
237 | #define expect_true(expr) expect ((expr) != 0, 1) |
|
|
238 | #define inline_size static inline |
|
|
239 | |
|
|
240 | #if EV_MINIMAL |
|
|
241 | # define inline_speed static noinline |
|
|
242 | #else |
|
|
243 | # define inline_speed static inline |
|
|
244 | #endif |
244 | |
245 | |
245 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
246 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
246 | #define ABSPRI(w) ((w)->priority - EV_MINPRI) |
247 | #define ABSPRI(w) (((W)w)->priority - EV_MINPRI) |
247 | |
248 | |
248 | #define EMPTY0 /* required for microsofts broken pseudo-c compiler */ |
249 | #define EMPTY /* required for microsofts broken pseudo-c compiler */ |
249 | #define EMPTY2(a,b) /* used to suppress some warnings */ |
250 | #define EMPTY2(a,b) /* used to suppress some warnings */ |
250 | |
251 | |
251 | typedef ev_watcher *W; |
252 | typedef ev_watcher *W; |
252 | typedef ev_watcher_list *WL; |
253 | typedef ev_watcher_list *WL; |
253 | typedef ev_watcher_time *WT; |
254 | typedef ev_watcher_time *WT; |
… | |
… | |
281 | perror (msg); |
282 | perror (msg); |
282 | abort (); |
283 | abort (); |
283 | } |
284 | } |
284 | } |
285 | } |
285 | |
286 | |
286 | static void *(*alloc)(void *ptr, size_t size) = realloc; |
287 | static void *(*alloc)(void *ptr, long size); |
287 | |
288 | |
288 | void |
289 | void |
289 | ev_set_allocator (void *(*cb)(void *ptr, size_t size)) |
290 | ev_set_allocator (void *(*cb)(void *ptr, long size)) |
290 | { |
291 | { |
291 | alloc = cb; |
292 | alloc = cb; |
292 | } |
293 | } |
293 | |
294 | |
294 | inline_speed void * |
295 | inline_speed void * |
295 | ev_realloc (void *ptr, size_t size) |
296 | ev_realloc (void *ptr, long size) |
296 | { |
297 | { |
297 | ptr = alloc (ptr, size); |
298 | ptr = alloc ? alloc (ptr, size) : realloc (ptr, size); |
298 | |
299 | |
299 | if (!ptr && size) |
300 | if (!ptr && size) |
300 | { |
301 | { |
301 | fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", (long)size); |
302 | fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size); |
302 | abort (); |
303 | abort (); |
303 | } |
304 | } |
304 | |
305 | |
305 | return ptr; |
306 | return ptr; |
306 | } |
307 | } |
… | |
… | |
324 | { |
325 | { |
325 | W w; |
326 | W w; |
326 | int events; |
327 | int events; |
327 | } ANPENDING; |
328 | } ANPENDING; |
328 | |
329 | |
|
|
330 | #if EV_USE_INOTIFY |
329 | typedef struct |
331 | typedef struct |
330 | { |
332 | { |
331 | #if EV_USE_INOTIFY |
|
|
332 | WL head; |
333 | WL head; |
333 | #endif |
|
|
334 | } ANFS; |
334 | } ANFS; |
|
|
335 | #endif |
335 | |
336 | |
336 | #if EV_MULTIPLICITY |
337 | #if EV_MULTIPLICITY |
337 | |
338 | |
338 | struct ev_loop |
339 | struct ev_loop |
339 | { |
340 | { |
… | |
… | |
396 | { |
397 | { |
397 | return ev_rt_now; |
398 | return ev_rt_now; |
398 | } |
399 | } |
399 | #endif |
400 | #endif |
400 | |
401 | |
401 | #define array_roundsize(type,n) (((n) | 4) & ~3) |
402 | int inline_size |
|
|
403 | array_nextsize (int elem, int cur, int cnt) |
|
|
404 | { |
|
|
405 | int ncur = cur + 1; |
|
|
406 | |
|
|
407 | do |
|
|
408 | ncur <<= 1; |
|
|
409 | while (cnt > ncur); |
|
|
410 | |
|
|
411 | /* if size > 4096, round to 4096 - 4 * longs to accomodate malloc overhead */ |
|
|
412 | if (elem * ncur > 4096) |
|
|
413 | { |
|
|
414 | ncur *= elem; |
|
|
415 | ncur = (ncur + elem + 4095 + sizeof (void *) * 4) & ~4095; |
|
|
416 | ncur = ncur - sizeof (void *) * 4; |
|
|
417 | ncur /= elem; |
|
|
418 | } |
|
|
419 | |
|
|
420 | return ncur; |
|
|
421 | } |
|
|
422 | |
|
|
423 | static noinline void * |
|
|
424 | array_realloc (int elem, void *base, int *cur, int cnt) |
|
|
425 | { |
|
|
426 | *cur = array_nextsize (elem, *cur, cnt); |
|
|
427 | return ev_realloc (base, elem * *cur); |
|
|
428 | } |
402 | |
429 | |
403 | #define array_needsize(type,base,cur,cnt,init) \ |
430 | #define array_needsize(type,base,cur,cnt,init) \ |
404 | if (expect_false ((cnt) > cur)) \ |
431 | if (expect_false ((cnt) > (cur))) \ |
405 | { \ |
432 | { \ |
406 | int newcnt = cur; \ |
433 | int ocur_ = (cur); \ |
407 | do \ |
434 | (base) = (type *)array_realloc \ |
408 | { \ |
435 | (sizeof (type), (base), &(cur), (cnt)); \ |
409 | newcnt = array_roundsize (type, newcnt << 1); \ |
436 | init ((base) + (ocur_), (cur) - ocur_); \ |
410 | } \ |
|
|
411 | while ((cnt) > newcnt); \ |
|
|
412 | \ |
|
|
413 | base = (type *)ev_realloc (base, sizeof (type) * (newcnt));\ |
|
|
414 | init (base + cur, newcnt - cur); \ |
|
|
415 | cur = newcnt; \ |
|
|
416 | } |
437 | } |
417 | |
438 | |
|
|
439 | #if 0 |
418 | #define array_slim(type,stem) \ |
440 | #define array_slim(type,stem) \ |
419 | if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ |
441 | if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ |
420 | { \ |
442 | { \ |
421 | stem ## max = array_roundsize (stem ## cnt >> 1); \ |
443 | stem ## max = array_roundsize (stem ## cnt >> 1); \ |
422 | base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ |
444 | base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ |
423 | fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
445 | fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
424 | } |
446 | } |
|
|
447 | #endif |
425 | |
448 | |
426 | #define array_free(stem, idx) \ |
449 | #define array_free(stem, idx) \ |
427 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; |
450 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; |
428 | |
451 | |
429 | /*****************************************************************************/ |
452 | /*****************************************************************************/ |
430 | |
453 | |
431 | void noinline |
454 | void noinline |
432 | ev_feed_event (EV_P_ void *w, int revents) |
455 | ev_feed_event (EV_P_ void *w, int revents) |
433 | { |
456 | { |
434 | W w_ = (W)w; |
457 | W w_ = (W)w; |
|
|
458 | int pri = ABSPRI (w_); |
435 | |
459 | |
436 | if (expect_false (w_->pending)) |
460 | if (expect_false (w_->pending)) |
|
|
461 | pendings [pri][w_->pending - 1].events |= revents; |
|
|
462 | else |
437 | { |
463 | { |
|
|
464 | w_->pending = ++pendingcnt [pri]; |
|
|
465 | array_needsize (ANPENDING, pendings [pri], pendingmax [pri], w_->pending, EMPTY2); |
|
|
466 | pendings [pri][w_->pending - 1].w = w_; |
438 | pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; |
467 | pendings [pri][w_->pending - 1].events = revents; |
439 | return; |
|
|
440 | } |
468 | } |
441 | |
|
|
442 | w_->pending = ++pendingcnt [ABSPRI (w_)]; |
|
|
443 | array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], EMPTY2); |
|
|
444 | pendings [ABSPRI (w_)][w_->pending - 1].w = w_; |
|
|
445 | pendings [ABSPRI (w_)][w_->pending - 1].events = revents; |
|
|
446 | } |
469 | } |
447 | |
470 | |
448 | void inline_size |
471 | void inline_size |
449 | queue_events (EV_P_ W *events, int eventcnt, int type) |
472 | queue_events (EV_P_ W *events, int eventcnt, int type) |
450 | { |
473 | { |
… | |
… | |
485 | } |
508 | } |
486 | |
509 | |
487 | void |
510 | void |
488 | ev_feed_fd_event (EV_P_ int fd, int revents) |
511 | ev_feed_fd_event (EV_P_ int fd, int revents) |
489 | { |
512 | { |
|
|
513 | if (fd >= 0 && fd < anfdmax) |
490 | fd_event (EV_A_ fd, revents); |
514 | fd_event (EV_A_ fd, revents); |
491 | } |
515 | } |
492 | |
516 | |
493 | void inline_size |
517 | void inline_size |
494 | fd_reify (EV_P) |
518 | fd_reify (EV_P) |
495 | { |
519 | { |
… | |
… | |
589 | static void noinline |
613 | static void noinline |
590 | fd_rearm_all (EV_P) |
614 | fd_rearm_all (EV_P) |
591 | { |
615 | { |
592 | int fd; |
616 | int fd; |
593 | |
617 | |
594 | /* this should be highly optimised to not do anything but set a flag */ |
|
|
595 | for (fd = 0; fd < anfdmax; ++fd) |
618 | for (fd = 0; fd < anfdmax; ++fd) |
596 | if (anfds [fd].events) |
619 | if (anfds [fd].events) |
597 | { |
620 | { |
598 | anfds [fd].events = 0; |
621 | anfds [fd].events = 0; |
599 | fd_change (EV_A_ fd); |
622 | fd_change (EV_A_ fd); |
… | |
… | |
726 | for (signum = signalmax; signum--; ) |
749 | for (signum = signalmax; signum--; ) |
727 | if (signals [signum].gotsig) |
750 | if (signals [signum].gotsig) |
728 | ev_feed_signal_event (EV_A_ signum + 1); |
751 | ev_feed_signal_event (EV_A_ signum + 1); |
729 | } |
752 | } |
730 | |
753 | |
731 | void inline_size |
754 | void inline_speed |
732 | fd_intern (int fd) |
755 | fd_intern (int fd) |
733 | { |
756 | { |
734 | #ifdef _WIN32 |
757 | #ifdef _WIN32 |
735 | int arg = 1; |
758 | int arg = 1; |
736 | ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); |
759 | ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); |
… | |
… | |
765 | ev_child *w; |
788 | ev_child *w; |
766 | |
789 | |
767 | for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) |
790 | for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) |
768 | if (w->pid == pid || !w->pid) |
791 | if (w->pid == pid || !w->pid) |
769 | { |
792 | { |
770 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
793 | ev_set_priority (w, ev_priority (sw)); /* need to do it *now* */ |
771 | w->rpid = pid; |
794 | w->rpid = pid; |
772 | w->rstatus = status; |
795 | w->rstatus = status; |
773 | ev_feed_event (EV_A_ (W)w, EV_CHILD); |
796 | ev_feed_event (EV_A_ (W)w, EV_CHILD); |
774 | } |
797 | } |
775 | } |
798 | } |
776 | |
799 | |
777 | #ifndef WCONTINUED |
800 | #ifndef WCONTINUED |
… | |
… | |
887 | ev_backend (EV_P) |
910 | ev_backend (EV_P) |
888 | { |
911 | { |
889 | return backend; |
912 | return backend; |
890 | } |
913 | } |
891 | |
914 | |
|
|
915 | unsigned int |
|
|
916 | ev_loop_count (EV_P) |
|
|
917 | { |
|
|
918 | return loop_count; |
|
|
919 | } |
|
|
920 | |
892 | static void noinline |
921 | static void noinline |
893 | loop_init (EV_P_ unsigned int flags) |
922 | loop_init (EV_P_ unsigned int flags) |
894 | { |
923 | { |
895 | if (!backend) |
924 | if (!backend) |
896 | { |
925 | { |
… | |
… | |
905 | ev_rt_now = ev_time (); |
934 | ev_rt_now = ev_time (); |
906 | mn_now = get_clock (); |
935 | mn_now = get_clock (); |
907 | now_floor = mn_now; |
936 | now_floor = mn_now; |
908 | rtmn_diff = ev_rt_now - mn_now; |
937 | rtmn_diff = ev_rt_now - mn_now; |
909 | |
938 | |
|
|
939 | /* pid check not overridable via env */ |
|
|
940 | #ifndef _WIN32 |
|
|
941 | if (flags & EVFLAG_FORKCHECK) |
|
|
942 | curpid = getpid (); |
|
|
943 | #endif |
|
|
944 | |
910 | if (!(flags & EVFLAG_NOENV) |
945 | if (!(flags & EVFLAG_NOENV) |
911 | && !enable_secure () |
946 | && !enable_secure () |
912 | && getenv ("LIBEV_FLAGS")) |
947 | && getenv ("LIBEV_FLAGS")) |
913 | flags = atoi (getenv ("LIBEV_FLAGS")); |
948 | flags = atoi (getenv ("LIBEV_FLAGS")); |
914 | |
949 | |
… | |
… | |
970 | #if EV_USE_SELECT |
1005 | #if EV_USE_SELECT |
971 | if (backend == EVBACKEND_SELECT) select_destroy (EV_A); |
1006 | if (backend == EVBACKEND_SELECT) select_destroy (EV_A); |
972 | #endif |
1007 | #endif |
973 | |
1008 | |
974 | for (i = NUMPRI; i--; ) |
1009 | for (i = NUMPRI; i--; ) |
|
|
1010 | { |
975 | array_free (pending, [i]); |
1011 | array_free (pending, [i]); |
|
|
1012 | #if EV_IDLE_ENABLE |
|
|
1013 | array_free (idle, [i]); |
|
|
1014 | #endif |
|
|
1015 | } |
976 | |
1016 | |
977 | /* have to use the microsoft-never-gets-it-right macro */ |
1017 | /* have to use the microsoft-never-gets-it-right macro */ |
978 | array_free (fdchange, EMPTY0); |
1018 | array_free (fdchange, EMPTY); |
979 | array_free (timer, EMPTY0); |
1019 | array_free (timer, EMPTY); |
980 | #if EV_PERIODIC_ENABLE |
1020 | #if EV_PERIODIC_ENABLE |
981 | array_free (periodic, EMPTY0); |
1021 | array_free (periodic, EMPTY); |
982 | #endif |
1022 | #endif |
983 | array_free (idle, EMPTY0); |
|
|
984 | array_free (prepare, EMPTY0); |
1023 | array_free (prepare, EMPTY); |
985 | array_free (check, EMPTY0); |
1024 | array_free (check, EMPTY); |
986 | |
1025 | |
987 | backend = 0; |
1026 | backend = 0; |
988 | } |
1027 | } |
|
|
1028 | |
|
|
1029 | void inline_size infy_fork (EV_P); |
989 | |
1030 | |
990 | void inline_size |
1031 | void inline_size |
991 | loop_fork (EV_P) |
1032 | loop_fork (EV_P) |
992 | { |
1033 | { |
993 | #if EV_USE_PORT |
1034 | #if EV_USE_PORT |
… | |
… | |
996 | #if EV_USE_KQUEUE |
1037 | #if EV_USE_KQUEUE |
997 | if (backend == EVBACKEND_KQUEUE) kqueue_fork (EV_A); |
1038 | if (backend == EVBACKEND_KQUEUE) kqueue_fork (EV_A); |
998 | #endif |
1039 | #endif |
999 | #if EV_USE_EPOLL |
1040 | #if EV_USE_EPOLL |
1000 | if (backend == EVBACKEND_EPOLL ) epoll_fork (EV_A); |
1041 | if (backend == EVBACKEND_EPOLL ) epoll_fork (EV_A); |
|
|
1042 | #endif |
|
|
1043 | #if EV_USE_INOTIFY |
|
|
1044 | infy_fork (EV_A); |
1001 | #endif |
1045 | #endif |
1002 | |
1046 | |
1003 | if (ev_is_active (&sigev)) |
1047 | if (ev_is_active (&sigev)) |
1004 | { |
1048 | { |
1005 | /* default loop */ |
1049 | /* default loop */ |
… | |
… | |
1121 | postfork = 1; |
1165 | postfork = 1; |
1122 | } |
1166 | } |
1123 | |
1167 | |
1124 | /*****************************************************************************/ |
1168 | /*****************************************************************************/ |
1125 | |
1169 | |
1126 | int inline_size |
1170 | void |
1127 | any_pending (EV_P) |
1171 | ev_invoke (EV_P_ void *w, int revents) |
1128 | { |
1172 | { |
1129 | int pri; |
1173 | EV_CB_INVOKE ((W)w, revents); |
1130 | |
|
|
1131 | for (pri = NUMPRI; pri--; ) |
|
|
1132 | if (pendingcnt [pri]) |
|
|
1133 | return 1; |
|
|
1134 | |
|
|
1135 | return 0; |
|
|
1136 | } |
1174 | } |
1137 | |
1175 | |
1138 | void inline_speed |
1176 | void inline_speed |
1139 | call_pending (EV_P) |
1177 | call_pending (EV_P) |
1140 | { |
1178 | { |
… | |
… | |
1193 | /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/ |
1231 | /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/ |
1194 | |
1232 | |
1195 | /* first reschedule or stop timer */ |
1233 | /* first reschedule or stop timer */ |
1196 | if (w->reschedule_cb) |
1234 | if (w->reschedule_cb) |
1197 | { |
1235 | { |
1198 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001); |
1236 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001220703125 /* 1/8192 */); |
1199 | assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now)); |
1237 | assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now)); |
1200 | downheap ((WT *)periodics, periodiccnt, 0); |
1238 | downheap ((WT *)periodics, periodiccnt, 0); |
1201 | } |
1239 | } |
1202 | else if (w->interval) |
1240 | else if (w->interval) |
1203 | { |
1241 | { |
1204 | ((WT)w)->at += floor ((ev_rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; |
1242 | ((WT)w)->at = w->offset + floor ((ev_rt_now - w->offset) / w->interval + 1.) * w->interval; |
1205 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > ev_rt_now)); |
1243 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > ev_rt_now)); |
1206 | downheap ((WT *)periodics, periodiccnt, 0); |
1244 | downheap ((WT *)periodics, periodiccnt, 0); |
1207 | } |
1245 | } |
1208 | else |
1246 | else |
1209 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1247 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
… | |
… | |
1223 | ev_periodic *w = periodics [i]; |
1261 | ev_periodic *w = periodics [i]; |
1224 | |
1262 | |
1225 | if (w->reschedule_cb) |
1263 | if (w->reschedule_cb) |
1226 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
1264 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
1227 | else if (w->interval) |
1265 | else if (w->interval) |
1228 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1266 | ((WT)w)->at = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; |
1229 | } |
1267 | } |
1230 | |
1268 | |
1231 | /* now rebuild the heap */ |
1269 | /* now rebuild the heap */ |
1232 | for (i = periodiccnt >> 1; i--; ) |
1270 | for (i = periodiccnt >> 1; i--; ) |
1233 | downheap ((WT *)periodics, periodiccnt, i); |
1271 | downheap ((WT *)periodics, periodiccnt, i); |
1234 | } |
1272 | } |
1235 | #endif |
1273 | #endif |
1236 | |
1274 | |
|
|
1275 | #if EV_IDLE_ENABLE |
|
|
1276 | void inline_size |
|
|
1277 | idle_reify (EV_P) |
|
|
1278 | { |
|
|
1279 | if (expect_false (idleall)) |
|
|
1280 | { |
|
|
1281 | int pri; |
|
|
1282 | |
|
|
1283 | for (pri = NUMPRI; pri--; ) |
|
|
1284 | { |
|
|
1285 | if (pendingcnt [pri]) |
|
|
1286 | break; |
|
|
1287 | |
|
|
1288 | if (idlecnt [pri]) |
|
|
1289 | { |
|
|
1290 | queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE); |
|
|
1291 | break; |
|
|
1292 | } |
|
|
1293 | } |
|
|
1294 | } |
|
|
1295 | } |
|
|
1296 | #endif |
|
|
1297 | |
1237 | int inline_size |
1298 | int inline_size |
1238 | time_update_monotonic (EV_P) |
1299 | time_update_monotonic (EV_P) |
1239 | { |
1300 | { |
1240 | mn_now = get_clock (); |
1301 | mn_now = get_clock (); |
1241 | |
1302 | |
… | |
… | |
1265 | ev_tstamp odiff = rtmn_diff; |
1326 | ev_tstamp odiff = rtmn_diff; |
1266 | |
1327 | |
1267 | /* loop a few times, before making important decisions. |
1328 | /* loop a few times, before making important decisions. |
1268 | * on the choice of "4": one iteration isn't enough, |
1329 | * on the choice of "4": one iteration isn't enough, |
1269 | * in case we get preempted during the calls to |
1330 | * in case we get preempted during the calls to |
1270 | * ev_time and get_clock. a second call is almost guarenteed |
1331 | * ev_time and get_clock. a second call is almost guaranteed |
1271 | * to succeed in that case, though. and looping a few more times |
1332 | * to succeed in that case, though. and looping a few more times |
1272 | * doesn't hurt either as we only do this on time-jumps or |
1333 | * doesn't hurt either as we only do this on time-jumps or |
1273 | * in the unlikely event of getting preempted here. |
1334 | * in the unlikely event of having been preempted here. |
1274 | */ |
1335 | */ |
1275 | for (i = 4; --i; ) |
1336 | for (i = 4; --i; ) |
1276 | { |
1337 | { |
1277 | rtmn_diff = ev_rt_now - mn_now; |
1338 | rtmn_diff = ev_rt_now - mn_now; |
1278 | |
1339 | |
… | |
… | |
1300 | { |
1361 | { |
1301 | #if EV_PERIODIC_ENABLE |
1362 | #if EV_PERIODIC_ENABLE |
1302 | periodics_reschedule (EV_A); |
1363 | periodics_reschedule (EV_A); |
1303 | #endif |
1364 | #endif |
1304 | |
1365 | |
1305 | /* adjust timers. this is easy, as the offset is the same for all */ |
1366 | /* adjust timers. this is easy, as the offset is the same for all of them */ |
1306 | for (i = 0; i < timercnt; ++i) |
1367 | for (i = 0; i < timercnt; ++i) |
1307 | ((WT)timers [i])->at += ev_rt_now - mn_now; |
1368 | ((WT)timers [i])->at += ev_rt_now - mn_now; |
1308 | } |
1369 | } |
1309 | |
1370 | |
1310 | mn_now = ev_rt_now; |
1371 | mn_now = ev_rt_now; |
… | |
… | |
1330 | { |
1391 | { |
1331 | loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) |
1392 | loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) |
1332 | ? EVUNLOOP_ONE |
1393 | ? EVUNLOOP_ONE |
1333 | : EVUNLOOP_CANCEL; |
1394 | : EVUNLOOP_CANCEL; |
1334 | |
1395 | |
1335 | while (activecnt) |
1396 | call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */ |
|
|
1397 | |
|
|
1398 | do |
1336 | { |
1399 | { |
1337 | /* we might have forked, so reify kernel state if necessary */ |
1400 | #ifndef _WIN32 |
|
|
1401 | if (expect_false (curpid)) /* penalise the forking check even more */ |
|
|
1402 | if (expect_false (getpid () != curpid)) |
|
|
1403 | { |
|
|
1404 | curpid = getpid (); |
|
|
1405 | postfork = 1; |
|
|
1406 | } |
|
|
1407 | #endif |
|
|
1408 | |
1338 | #if EV_FORK_ENABLE |
1409 | #if EV_FORK_ENABLE |
|
|
1410 | /* we might have forked, so queue fork handlers */ |
1339 | if (expect_false (postfork)) |
1411 | if (expect_false (postfork)) |
1340 | if (forkcnt) |
1412 | if (forkcnt) |
1341 | { |
1413 | { |
1342 | queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK); |
1414 | queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK); |
1343 | call_pending (EV_A); |
1415 | call_pending (EV_A); |
1344 | } |
1416 | } |
1345 | #endif |
1417 | #endif |
1346 | |
1418 | |
1347 | /* queue check watchers (and execute them) */ |
1419 | /* queue prepare watchers (and execute them) */ |
1348 | if (expect_false (preparecnt)) |
1420 | if (expect_false (preparecnt)) |
1349 | { |
1421 | { |
1350 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
1422 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
1351 | call_pending (EV_A); |
1423 | call_pending (EV_A); |
1352 | } |
1424 | } |
1353 | |
1425 | |
|
|
1426 | if (expect_false (!activecnt)) |
|
|
1427 | break; |
|
|
1428 | |
1354 | /* we might have forked, so reify kernel state if necessary */ |
1429 | /* we might have forked, so reify kernel state if necessary */ |
1355 | if (expect_false (postfork)) |
1430 | if (expect_false (postfork)) |
1356 | loop_fork (EV_A); |
1431 | loop_fork (EV_A); |
1357 | |
1432 | |
1358 | /* update fd-related kernel structures */ |
1433 | /* update fd-related kernel structures */ |
1359 | fd_reify (EV_A); |
1434 | fd_reify (EV_A); |
1360 | |
1435 | |
1361 | /* calculate blocking time */ |
1436 | /* calculate blocking time */ |
1362 | { |
1437 | { |
1363 | double block; |
1438 | ev_tstamp block; |
1364 | |
1439 | |
1365 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
1440 | if (expect_false (flags & EVLOOP_NONBLOCK || idleall || !activecnt)) |
1366 | block = 0.; /* do not block at all */ |
1441 | block = 0.; /* do not block at all */ |
1367 | else |
1442 | else |
1368 | { |
1443 | { |
1369 | /* update time to cancel out callback processing overhead */ |
1444 | /* update time to cancel out callback processing overhead */ |
1370 | #if EV_USE_MONOTONIC |
1445 | #if EV_USE_MONOTONIC |
… | |
… | |
1394 | #endif |
1469 | #endif |
1395 | |
1470 | |
1396 | if (expect_false (block < 0.)) block = 0.; |
1471 | if (expect_false (block < 0.)) block = 0.; |
1397 | } |
1472 | } |
1398 | |
1473 | |
|
|
1474 | ++loop_count; |
1399 | backend_poll (EV_A_ block); |
1475 | backend_poll (EV_A_ block); |
1400 | } |
1476 | } |
1401 | |
1477 | |
1402 | /* update ev_rt_now, do magic */ |
1478 | /* update ev_rt_now, do magic */ |
1403 | time_update (EV_A); |
1479 | time_update (EV_A); |
… | |
… | |
1406 | timers_reify (EV_A); /* relative timers called last */ |
1482 | timers_reify (EV_A); /* relative timers called last */ |
1407 | #if EV_PERIODIC_ENABLE |
1483 | #if EV_PERIODIC_ENABLE |
1408 | periodics_reify (EV_A); /* absolute timers called first */ |
1484 | periodics_reify (EV_A); /* absolute timers called first */ |
1409 | #endif |
1485 | #endif |
1410 | |
1486 | |
|
|
1487 | #if EV_IDLE_ENABLE |
1411 | /* queue idle watchers unless other events are pending */ |
1488 | /* queue idle watchers unless other events are pending */ |
1412 | if (idlecnt && !any_pending (EV_A)) |
1489 | idle_reify (EV_A); |
1413 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1490 | #endif |
1414 | |
1491 | |
1415 | /* queue check watchers, to be executed first */ |
1492 | /* queue check watchers, to be executed first */ |
1416 | if (expect_false (checkcnt)) |
1493 | if (expect_false (checkcnt)) |
1417 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
1494 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
1418 | |
1495 | |
1419 | call_pending (EV_A); |
1496 | call_pending (EV_A); |
1420 | |
1497 | |
1421 | if (expect_false (loop_done)) |
|
|
1422 | break; |
|
|
1423 | } |
1498 | } |
|
|
1499 | while (expect_true (activecnt && !loop_done)); |
1424 | |
1500 | |
1425 | if (loop_done == EVUNLOOP_ONE) |
1501 | if (loop_done == EVUNLOOP_ONE) |
1426 | loop_done = EVUNLOOP_CANCEL; |
1502 | loop_done = EVUNLOOP_CANCEL; |
1427 | } |
1503 | } |
1428 | |
1504 | |
… | |
… | |
1455 | head = &(*head)->next; |
1531 | head = &(*head)->next; |
1456 | } |
1532 | } |
1457 | } |
1533 | } |
1458 | |
1534 | |
1459 | void inline_speed |
1535 | void inline_speed |
1460 | ev_clear_pending (EV_P_ W w) |
1536 | clear_pending (EV_P_ W w) |
1461 | { |
1537 | { |
1462 | if (w->pending) |
1538 | if (w->pending) |
1463 | { |
1539 | { |
1464 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
1540 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
1465 | w->pending = 0; |
1541 | w->pending = 0; |
1466 | } |
1542 | } |
1467 | } |
1543 | } |
1468 | |
1544 | |
|
|
1545 | int |
|
|
1546 | ev_clear_pending (EV_P_ void *w) |
|
|
1547 | { |
|
|
1548 | W w_ = (W)w; |
|
|
1549 | int pending = w_->pending; |
|
|
1550 | |
|
|
1551 | if (expect_true (pending)) |
|
|
1552 | { |
|
|
1553 | ANPENDING *p = pendings [ABSPRI (w_)] + pending - 1; |
|
|
1554 | w_->pending = 0; |
|
|
1555 | p->w = 0; |
|
|
1556 | return p->events; |
|
|
1557 | } |
|
|
1558 | else |
|
|
1559 | return 0; |
|
|
1560 | } |
|
|
1561 | |
|
|
1562 | void inline_size |
|
|
1563 | pri_adjust (EV_P_ W w) |
|
|
1564 | { |
|
|
1565 | int pri = w->priority; |
|
|
1566 | pri = pri < EV_MINPRI ? EV_MINPRI : pri; |
|
|
1567 | pri = pri > EV_MAXPRI ? EV_MAXPRI : pri; |
|
|
1568 | w->priority = pri; |
|
|
1569 | } |
|
|
1570 | |
1469 | void inline_speed |
1571 | void inline_speed |
1470 | ev_start (EV_P_ W w, int active) |
1572 | ev_start (EV_P_ W w, int active) |
1471 | { |
1573 | { |
1472 | if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; |
1574 | pri_adjust (EV_A_ w); |
1473 | if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; |
|
|
1474 | |
|
|
1475 | w->active = active; |
1575 | w->active = active; |
1476 | ev_ref (EV_A); |
1576 | ev_ref (EV_A); |
1477 | } |
1577 | } |
1478 | |
1578 | |
1479 | void inline_size |
1579 | void inline_size |
… | |
… | |
1483 | w->active = 0; |
1583 | w->active = 0; |
1484 | } |
1584 | } |
1485 | |
1585 | |
1486 | /*****************************************************************************/ |
1586 | /*****************************************************************************/ |
1487 | |
1587 | |
1488 | void |
1588 | void noinline |
1489 | ev_io_start (EV_P_ ev_io *w) |
1589 | ev_io_start (EV_P_ ev_io *w) |
1490 | { |
1590 | { |
1491 | int fd = w->fd; |
1591 | int fd = w->fd; |
1492 | |
1592 | |
1493 | if (expect_false (ev_is_active (w))) |
1593 | if (expect_false (ev_is_active (w))) |
… | |
… | |
1500 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
1600 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
1501 | |
1601 | |
1502 | fd_change (EV_A_ fd); |
1602 | fd_change (EV_A_ fd); |
1503 | } |
1603 | } |
1504 | |
1604 | |
1505 | void |
1605 | void noinline |
1506 | ev_io_stop (EV_P_ ev_io *w) |
1606 | ev_io_stop (EV_P_ ev_io *w) |
1507 | { |
1607 | { |
1508 | ev_clear_pending (EV_A_ (W)w); |
1608 | clear_pending (EV_A_ (W)w); |
1509 | if (expect_false (!ev_is_active (w))) |
1609 | if (expect_false (!ev_is_active (w))) |
1510 | return; |
1610 | return; |
1511 | |
1611 | |
1512 | assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax)); |
1612 | assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax)); |
1513 | |
1613 | |
… | |
… | |
1515 | ev_stop (EV_A_ (W)w); |
1615 | ev_stop (EV_A_ (W)w); |
1516 | |
1616 | |
1517 | fd_change (EV_A_ w->fd); |
1617 | fd_change (EV_A_ w->fd); |
1518 | } |
1618 | } |
1519 | |
1619 | |
1520 | void |
1620 | void noinline |
1521 | ev_timer_start (EV_P_ ev_timer *w) |
1621 | ev_timer_start (EV_P_ ev_timer *w) |
1522 | { |
1622 | { |
1523 | if (expect_false (ev_is_active (w))) |
1623 | if (expect_false (ev_is_active (w))) |
1524 | return; |
1624 | return; |
1525 | |
1625 | |
… | |
… | |
1533 | upheap ((WT *)timers, timercnt - 1); |
1633 | upheap ((WT *)timers, timercnt - 1); |
1534 | |
1634 | |
1535 | /*assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));*/ |
1635 | /*assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));*/ |
1536 | } |
1636 | } |
1537 | |
1637 | |
1538 | void |
1638 | void noinline |
1539 | ev_timer_stop (EV_P_ ev_timer *w) |
1639 | ev_timer_stop (EV_P_ ev_timer *w) |
1540 | { |
1640 | { |
1541 | ev_clear_pending (EV_A_ (W)w); |
1641 | clear_pending (EV_A_ (W)w); |
1542 | if (expect_false (!ev_is_active (w))) |
1642 | if (expect_false (!ev_is_active (w))) |
1543 | return; |
1643 | return; |
1544 | |
1644 | |
1545 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1645 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1546 | |
1646 | |
… | |
… | |
1557 | ((WT)w)->at -= mn_now; |
1657 | ((WT)w)->at -= mn_now; |
1558 | |
1658 | |
1559 | ev_stop (EV_A_ (W)w); |
1659 | ev_stop (EV_A_ (W)w); |
1560 | } |
1660 | } |
1561 | |
1661 | |
1562 | void |
1662 | void noinline |
1563 | ev_timer_again (EV_P_ ev_timer *w) |
1663 | ev_timer_again (EV_P_ ev_timer *w) |
1564 | { |
1664 | { |
1565 | if (ev_is_active (w)) |
1665 | if (ev_is_active (w)) |
1566 | { |
1666 | { |
1567 | if (w->repeat) |
1667 | if (w->repeat) |
… | |
… | |
1578 | ev_timer_start (EV_A_ w); |
1678 | ev_timer_start (EV_A_ w); |
1579 | } |
1679 | } |
1580 | } |
1680 | } |
1581 | |
1681 | |
1582 | #if EV_PERIODIC_ENABLE |
1682 | #if EV_PERIODIC_ENABLE |
1583 | void |
1683 | void noinline |
1584 | ev_periodic_start (EV_P_ ev_periodic *w) |
1684 | ev_periodic_start (EV_P_ ev_periodic *w) |
1585 | { |
1685 | { |
1586 | if (expect_false (ev_is_active (w))) |
1686 | if (expect_false (ev_is_active (w))) |
1587 | return; |
1687 | return; |
1588 | |
1688 | |
… | |
… | |
1590 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
1690 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
1591 | else if (w->interval) |
1691 | else if (w->interval) |
1592 | { |
1692 | { |
1593 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1693 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1594 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1694 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1595 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1695 | ((WT)w)->at = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; |
1596 | } |
1696 | } |
|
|
1697 | else |
|
|
1698 | ((WT)w)->at = w->offset; |
1597 | |
1699 | |
1598 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1700 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1599 | array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); |
1701 | array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); |
1600 | periodics [periodiccnt - 1] = w; |
1702 | periodics [periodiccnt - 1] = w; |
1601 | upheap ((WT *)periodics, periodiccnt - 1); |
1703 | upheap ((WT *)periodics, periodiccnt - 1); |
1602 | |
1704 | |
1603 | /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/ |
1705 | /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/ |
1604 | } |
1706 | } |
1605 | |
1707 | |
1606 | void |
1708 | void noinline |
1607 | ev_periodic_stop (EV_P_ ev_periodic *w) |
1709 | ev_periodic_stop (EV_P_ ev_periodic *w) |
1608 | { |
1710 | { |
1609 | ev_clear_pending (EV_A_ (W)w); |
1711 | clear_pending (EV_A_ (W)w); |
1610 | if (expect_false (!ev_is_active (w))) |
1712 | if (expect_false (!ev_is_active (w))) |
1611 | return; |
1713 | return; |
1612 | |
1714 | |
1613 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1715 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1614 | |
1716 | |
… | |
… | |
1623 | } |
1725 | } |
1624 | |
1726 | |
1625 | ev_stop (EV_A_ (W)w); |
1727 | ev_stop (EV_A_ (W)w); |
1626 | } |
1728 | } |
1627 | |
1729 | |
1628 | void |
1730 | void noinline |
1629 | ev_periodic_again (EV_P_ ev_periodic *w) |
1731 | ev_periodic_again (EV_P_ ev_periodic *w) |
1630 | { |
1732 | { |
1631 | /* TODO: use adjustheap and recalculation */ |
1733 | /* TODO: use adjustheap and recalculation */ |
1632 | ev_periodic_stop (EV_A_ w); |
1734 | ev_periodic_stop (EV_A_ w); |
1633 | ev_periodic_start (EV_A_ w); |
1735 | ev_periodic_start (EV_A_ w); |
… | |
… | |
1636 | |
1738 | |
1637 | #ifndef SA_RESTART |
1739 | #ifndef SA_RESTART |
1638 | # define SA_RESTART 0 |
1740 | # define SA_RESTART 0 |
1639 | #endif |
1741 | #endif |
1640 | |
1742 | |
1641 | void |
1743 | void noinline |
1642 | ev_signal_start (EV_P_ ev_signal *w) |
1744 | ev_signal_start (EV_P_ ev_signal *w) |
1643 | { |
1745 | { |
1644 | #if EV_MULTIPLICITY |
1746 | #if EV_MULTIPLICITY |
1645 | assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr)); |
1747 | assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr)); |
1646 | #endif |
1748 | #endif |
… | |
… | |
1665 | sigaction (w->signum, &sa, 0); |
1767 | sigaction (w->signum, &sa, 0); |
1666 | #endif |
1768 | #endif |
1667 | } |
1769 | } |
1668 | } |
1770 | } |
1669 | |
1771 | |
1670 | void |
1772 | void noinline |
1671 | ev_signal_stop (EV_P_ ev_signal *w) |
1773 | ev_signal_stop (EV_P_ ev_signal *w) |
1672 | { |
1774 | { |
1673 | ev_clear_pending (EV_A_ (W)w); |
1775 | clear_pending (EV_A_ (W)w); |
1674 | if (expect_false (!ev_is_active (w))) |
1776 | if (expect_false (!ev_is_active (w))) |
1675 | return; |
1777 | return; |
1676 | |
1778 | |
1677 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
1779 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
1678 | ev_stop (EV_A_ (W)w); |
1780 | ev_stop (EV_A_ (W)w); |
… | |
… | |
1695 | } |
1797 | } |
1696 | |
1798 | |
1697 | void |
1799 | void |
1698 | ev_child_stop (EV_P_ ev_child *w) |
1800 | ev_child_stop (EV_P_ ev_child *w) |
1699 | { |
1801 | { |
1700 | ev_clear_pending (EV_A_ (W)w); |
1802 | clear_pending (EV_A_ (W)w); |
1701 | if (expect_false (!ev_is_active (w))) |
1803 | if (expect_false (!ev_is_active (w))) |
1702 | return; |
1804 | return; |
1703 | |
1805 | |
1704 | wlist_del ((WL *)&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w); |
1806 | wlist_del ((WL *)&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w); |
1705 | ev_stop (EV_A_ (W)w); |
1807 | ev_stop (EV_A_ (W)w); |
… | |
… | |
1713 | # endif |
1815 | # endif |
1714 | |
1816 | |
1715 | #define DEF_STAT_INTERVAL 5.0074891 |
1817 | #define DEF_STAT_INTERVAL 5.0074891 |
1716 | #define MIN_STAT_INTERVAL 0.1074891 |
1818 | #define MIN_STAT_INTERVAL 0.1074891 |
1717 | |
1819 | |
1718 | void noinline stat_timer_cb (EV_P_ ev_timer *w_, int revents); |
1820 | static void noinline stat_timer_cb (EV_P_ ev_timer *w_, int revents); |
1719 | |
1821 | |
1720 | #if EV_USE_INOTIFY |
1822 | #if EV_USE_INOTIFY |
1721 | # define EV_INOTIFY_BUFSIZE ((PATH_MAX + sizeof (struct inotify_event)) + 2048) |
1823 | # define EV_INOTIFY_BUFSIZE 8192 |
1722 | |
1824 | |
1723 | static void noinline |
1825 | static void noinline |
1724 | infy_add (EV_P_ ev_stat *w) |
1826 | infy_add (EV_P_ ev_stat *w) |
1725 | { |
1827 | { |
1726 | w->wd = inotify_add_watch (fs_fd, w->path, IN_ATTRIB | IN_DELETE_SELF | IN_MOVE_SELF | IN_MODIFY | IN_DONT_FOLLOW | IN_MASK_ADD); |
1828 | w->wd = inotify_add_watch (fs_fd, w->path, IN_ATTRIB | IN_DELETE_SELF | IN_MOVE_SELF | IN_MODIFY | IN_DONT_FOLLOW | IN_MASK_ADD); |
… | |
… | |
1728 | if (w->wd < 0) |
1830 | if (w->wd < 0) |
1729 | { |
1831 | { |
1730 | ev_timer_start (EV_A_ &w->timer); /* this is not race-free, so we still need to recheck periodically */ |
1832 | ev_timer_start (EV_A_ &w->timer); /* this is not race-free, so we still need to recheck periodically */ |
1731 | |
1833 | |
1732 | /* monitor some parent directory for speedup hints */ |
1834 | /* monitor some parent directory for speedup hints */ |
1733 | if (errno == ENOENT || errno == EACCES) |
1835 | if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096) |
1734 | { |
1836 | { |
1735 | char path [PATH_MAX]; |
1837 | char path [4096]; |
1736 | strcpy (path, w->path); |
1838 | strcpy (path, w->path); |
1737 | |
1839 | |
1738 | do |
1840 | do |
1739 | { |
1841 | { |
1740 | int mask = IN_MASK_ADD | IN_DELETE_SELF | IN_MOVE_SELF |
1842 | int mask = IN_MASK_ADD | IN_DELETE_SELF | IN_MOVE_SELF |
… | |
… | |
1744 | |
1846 | |
1745 | if (!pend) |
1847 | if (!pend) |
1746 | break; /* whoops, no '/', complain to your admin */ |
1848 | break; /* whoops, no '/', complain to your admin */ |
1747 | |
1849 | |
1748 | *pend = 0; |
1850 | *pend = 0; |
1749 | w->wd = inotify_add_watch (fs_fd, path, IN_DELETE_SELF | IN_CREATE | IN_MOVED_TO | IN_MASK_ADD); |
1851 | w->wd = inotify_add_watch (fs_fd, path, mask); |
1750 | } |
1852 | } |
1751 | while (w->wd < 0 && (errno == ENOENT || errno == EACCES)); |
1853 | while (w->wd < 0 && (errno == ENOENT || errno == EACCES)); |
1752 | } |
1854 | } |
1753 | } |
1855 | } |
1754 | else |
1856 | else |
… | |
… | |
1759 | } |
1861 | } |
1760 | |
1862 | |
1761 | static void noinline |
1863 | static void noinline |
1762 | infy_del (EV_P_ ev_stat *w) |
1864 | infy_del (EV_P_ ev_stat *w) |
1763 | { |
1865 | { |
1764 | WL w_; |
|
|
1765 | int slot; |
1866 | int slot; |
1766 | int wd = w->wd; |
1867 | int wd = w->wd; |
1767 | |
1868 | |
1768 | if (wd < 0) |
1869 | if (wd < 0) |
1769 | return; |
1870 | return; |
… | |
… | |
1798 | { |
1899 | { |
1799 | w->wd = -1; |
1900 | w->wd = -1; |
1800 | infy_add (EV_A_ w); /* re-add, no matter what */ |
1901 | infy_add (EV_A_ w); /* re-add, no matter what */ |
1801 | } |
1902 | } |
1802 | |
1903 | |
1803 | stat_timer_cb (EV_P_ &w->timer, 0); |
1904 | stat_timer_cb (EV_A_ &w->timer, 0); |
1804 | } |
1905 | } |
1805 | } |
1906 | } |
1806 | } |
1907 | } |
1807 | } |
1908 | } |
1808 | |
1909 | |
… | |
… | |
1832 | ev_set_priority (&fs_w, EV_MAXPRI); |
1933 | ev_set_priority (&fs_w, EV_MAXPRI); |
1833 | ev_io_start (EV_A_ &fs_w); |
1934 | ev_io_start (EV_A_ &fs_w); |
1834 | } |
1935 | } |
1835 | } |
1936 | } |
1836 | |
1937 | |
|
|
1938 | void inline_size |
|
|
1939 | infy_fork (EV_P) |
|
|
1940 | { |
|
|
1941 | int slot; |
|
|
1942 | |
|
|
1943 | if (fs_fd < 0) |
|
|
1944 | return; |
|
|
1945 | |
|
|
1946 | close (fs_fd); |
|
|
1947 | fs_fd = inotify_init (); |
|
|
1948 | |
|
|
1949 | for (slot = 0; slot < EV_INOTIFY_HASHSIZE; ++slot) |
|
|
1950 | { |
|
|
1951 | WL w_ = fs_hash [slot].head; |
|
|
1952 | fs_hash [slot].head = 0; |
|
|
1953 | |
|
|
1954 | while (w_) |
|
|
1955 | { |
|
|
1956 | ev_stat *w = (ev_stat *)w_; |
|
|
1957 | w_ = w_->next; /* lets us add this watcher */ |
|
|
1958 | |
|
|
1959 | w->wd = -1; |
|
|
1960 | |
|
|
1961 | if (fs_fd >= 0) |
|
|
1962 | infy_add (EV_A_ w); /* re-add, no matter what */ |
|
|
1963 | else |
|
|
1964 | ev_timer_start (EV_A_ &w->timer); |
|
|
1965 | } |
|
|
1966 | |
|
|
1967 | } |
|
|
1968 | } |
|
|
1969 | |
1837 | #endif |
1970 | #endif |
1838 | |
1971 | |
1839 | void |
1972 | void |
1840 | ev_stat_stat (EV_P_ ev_stat *w) |
1973 | ev_stat_stat (EV_P_ ev_stat *w) |
1841 | { |
1974 | { |
… | |
… | |
1843 | w->attr.st_nlink = 0; |
1976 | w->attr.st_nlink = 0; |
1844 | else if (!w->attr.st_nlink) |
1977 | else if (!w->attr.st_nlink) |
1845 | w->attr.st_nlink = 1; |
1978 | w->attr.st_nlink = 1; |
1846 | } |
1979 | } |
1847 | |
1980 | |
1848 | void noinline |
1981 | static void noinline |
1849 | stat_timer_cb (EV_P_ ev_timer *w_, int revents) |
1982 | stat_timer_cb (EV_P_ ev_timer *w_, int revents) |
1850 | { |
1983 | { |
1851 | ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer)); |
1984 | ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer)); |
1852 | |
1985 | |
1853 | /* we copy this here each the time so that */ |
1986 | /* we copy this here each the time so that */ |
1854 | /* prev has the old value when the callback gets invoked */ |
1987 | /* prev has the old value when the callback gets invoked */ |
1855 | w->prev = w->attr; |
1988 | w->prev = w->attr; |
1856 | ev_stat_stat (EV_A_ w); |
1989 | ev_stat_stat (EV_A_ w); |
1857 | |
1990 | |
1858 | if (memcmp (&w->prev, &w->attr, sizeof (ev_statdata))) |
1991 | /* memcmp doesn't work on netbsd, they.... do stuff to their struct stat */ |
|
|
1992 | if ( |
|
|
1993 | w->prev.st_dev != w->attr.st_dev |
|
|
1994 | || w->prev.st_ino != w->attr.st_ino |
|
|
1995 | || w->prev.st_mode != w->attr.st_mode |
|
|
1996 | || w->prev.st_nlink != w->attr.st_nlink |
|
|
1997 | || w->prev.st_uid != w->attr.st_uid |
|
|
1998 | || w->prev.st_gid != w->attr.st_gid |
|
|
1999 | || w->prev.st_rdev != w->attr.st_rdev |
|
|
2000 | || w->prev.st_size != w->attr.st_size |
|
|
2001 | || w->prev.st_atime != w->attr.st_atime |
|
|
2002 | || w->prev.st_mtime != w->attr.st_mtime |
|
|
2003 | || w->prev.st_ctime != w->attr.st_ctime |
1859 | { |
2004 | ) { |
1860 | #if EV_USE_INOTIFY |
2005 | #if EV_USE_INOTIFY |
1861 | infy_del (EV_A_ w); |
2006 | infy_del (EV_A_ w); |
1862 | infy_add (EV_A_ w); |
2007 | infy_add (EV_A_ w); |
1863 | ev_stat_stat (EV_A_ w); /* avoid race... */ |
2008 | ev_stat_stat (EV_A_ w); /* avoid race... */ |
1864 | #endif |
2009 | #endif |
… | |
… | |
1898 | } |
2043 | } |
1899 | |
2044 | |
1900 | void |
2045 | void |
1901 | ev_stat_stop (EV_P_ ev_stat *w) |
2046 | ev_stat_stop (EV_P_ ev_stat *w) |
1902 | { |
2047 | { |
1903 | ev_clear_pending (EV_A_ (W)w); |
2048 | clear_pending (EV_A_ (W)w); |
1904 | if (expect_false (!ev_is_active (w))) |
2049 | if (expect_false (!ev_is_active (w))) |
1905 | return; |
2050 | return; |
1906 | |
2051 | |
1907 | #if EV_USE_INOTIFY |
2052 | #if EV_USE_INOTIFY |
1908 | infy_del (EV_A_ w); |
2053 | infy_del (EV_A_ w); |
… | |
… | |
1911 | |
2056 | |
1912 | ev_stop (EV_A_ (W)w); |
2057 | ev_stop (EV_A_ (W)w); |
1913 | } |
2058 | } |
1914 | #endif |
2059 | #endif |
1915 | |
2060 | |
|
|
2061 | #if EV_IDLE_ENABLE |
1916 | void |
2062 | void |
1917 | ev_idle_start (EV_P_ ev_idle *w) |
2063 | ev_idle_start (EV_P_ ev_idle *w) |
1918 | { |
2064 | { |
1919 | if (expect_false (ev_is_active (w))) |
2065 | if (expect_false (ev_is_active (w))) |
1920 | return; |
2066 | return; |
1921 | |
2067 | |
|
|
2068 | pri_adjust (EV_A_ (W)w); |
|
|
2069 | |
|
|
2070 | { |
|
|
2071 | int active = ++idlecnt [ABSPRI (w)]; |
|
|
2072 | |
|
|
2073 | ++idleall; |
1922 | ev_start (EV_A_ (W)w, ++idlecnt); |
2074 | ev_start (EV_A_ (W)w, active); |
|
|
2075 | |
1923 | array_needsize (ev_idle *, idles, idlemax, idlecnt, EMPTY2); |
2076 | array_needsize (ev_idle *, idles [ABSPRI (w)], idlemax [ABSPRI (w)], active, EMPTY2); |
1924 | idles [idlecnt - 1] = w; |
2077 | idles [ABSPRI (w)][active - 1] = w; |
|
|
2078 | } |
1925 | } |
2079 | } |
1926 | |
2080 | |
1927 | void |
2081 | void |
1928 | ev_idle_stop (EV_P_ ev_idle *w) |
2082 | ev_idle_stop (EV_P_ ev_idle *w) |
1929 | { |
2083 | { |
1930 | ev_clear_pending (EV_A_ (W)w); |
2084 | clear_pending (EV_A_ (W)w); |
1931 | if (expect_false (!ev_is_active (w))) |
2085 | if (expect_false (!ev_is_active (w))) |
1932 | return; |
2086 | return; |
1933 | |
2087 | |
1934 | { |
2088 | { |
1935 | int active = ((W)w)->active; |
2089 | int active = ((W)w)->active; |
1936 | idles [active - 1] = idles [--idlecnt]; |
2090 | |
|
|
2091 | idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]]; |
1937 | ((W)idles [active - 1])->active = active; |
2092 | ((W)idles [ABSPRI (w)][active - 1])->active = active; |
|
|
2093 | |
|
|
2094 | ev_stop (EV_A_ (W)w); |
|
|
2095 | --idleall; |
1938 | } |
2096 | } |
1939 | |
|
|
1940 | ev_stop (EV_A_ (W)w); |
|
|
1941 | } |
2097 | } |
|
|
2098 | #endif |
1942 | |
2099 | |
1943 | void |
2100 | void |
1944 | ev_prepare_start (EV_P_ ev_prepare *w) |
2101 | ev_prepare_start (EV_P_ ev_prepare *w) |
1945 | { |
2102 | { |
1946 | if (expect_false (ev_is_active (w))) |
2103 | if (expect_false (ev_is_active (w))) |
… | |
… | |
1952 | } |
2109 | } |
1953 | |
2110 | |
1954 | void |
2111 | void |
1955 | ev_prepare_stop (EV_P_ ev_prepare *w) |
2112 | ev_prepare_stop (EV_P_ ev_prepare *w) |
1956 | { |
2113 | { |
1957 | ev_clear_pending (EV_A_ (W)w); |
2114 | clear_pending (EV_A_ (W)w); |
1958 | if (expect_false (!ev_is_active (w))) |
2115 | if (expect_false (!ev_is_active (w))) |
1959 | return; |
2116 | return; |
1960 | |
2117 | |
1961 | { |
2118 | { |
1962 | int active = ((W)w)->active; |
2119 | int active = ((W)w)->active; |
… | |
… | |
1979 | } |
2136 | } |
1980 | |
2137 | |
1981 | void |
2138 | void |
1982 | ev_check_stop (EV_P_ ev_check *w) |
2139 | ev_check_stop (EV_P_ ev_check *w) |
1983 | { |
2140 | { |
1984 | ev_clear_pending (EV_A_ (W)w); |
2141 | clear_pending (EV_A_ (W)w); |
1985 | if (expect_false (!ev_is_active (w))) |
2142 | if (expect_false (!ev_is_active (w))) |
1986 | return; |
2143 | return; |
1987 | |
2144 | |
1988 | { |
2145 | { |
1989 | int active = ((W)w)->active; |
2146 | int active = ((W)w)->active; |
… | |
… | |
2031 | } |
2188 | } |
2032 | |
2189 | |
2033 | void |
2190 | void |
2034 | ev_embed_stop (EV_P_ ev_embed *w) |
2191 | ev_embed_stop (EV_P_ ev_embed *w) |
2035 | { |
2192 | { |
2036 | ev_clear_pending (EV_A_ (W)w); |
2193 | clear_pending (EV_A_ (W)w); |
2037 | if (expect_false (!ev_is_active (w))) |
2194 | if (expect_false (!ev_is_active (w))) |
2038 | return; |
2195 | return; |
2039 | |
2196 | |
2040 | ev_io_stop (EV_A_ &w->io); |
2197 | ev_io_stop (EV_A_ &w->io); |
2041 | |
2198 | |
… | |
… | |
2056 | } |
2213 | } |
2057 | |
2214 | |
2058 | void |
2215 | void |
2059 | ev_fork_stop (EV_P_ ev_fork *w) |
2216 | ev_fork_stop (EV_P_ ev_fork *w) |
2060 | { |
2217 | { |
2061 | ev_clear_pending (EV_A_ (W)w); |
2218 | clear_pending (EV_A_ (W)w); |
2062 | if (expect_false (!ev_is_active (w))) |
2219 | if (expect_false (!ev_is_active (w))) |
2063 | return; |
2220 | return; |
2064 | |
2221 | |
2065 | { |
2222 | { |
2066 | int active = ((W)w)->active; |
2223 | int active = ((W)w)->active; |