… | |
… | |
325 | |
325 | |
326 | typedef ev_watcher *W; |
326 | typedef ev_watcher *W; |
327 | typedef ev_watcher_list *WL; |
327 | typedef ev_watcher_list *WL; |
328 | typedef ev_watcher_time *WT; |
328 | typedef ev_watcher_time *WT; |
329 | |
329 | |
|
|
330 | #define ev_active(w) ((W)(w))->active |
330 | #define ev_at(w) ((WT)(w))->at |
331 | #define ev_at(w) ((WT)(w))->at |
331 | |
332 | |
332 | #if EV_USE_MONOTONIC |
333 | #if EV_USE_MONOTONIC |
333 | /* sig_atomic_t is used to avoid per-thread variables or locking but still */ |
334 | /* sig_atomic_t is used to avoid per-thread variables or locking but still */ |
334 | /* giving it a reasonably high chance of working on typical architetcures */ |
335 | /* giving it a reasonably high chance of working on typical architetcures */ |
… | |
… | |
519 | } |
520 | } |
520 | } |
521 | } |
521 | |
522 | |
522 | /*****************************************************************************/ |
523 | /*****************************************************************************/ |
523 | |
524 | |
|
|
525 | #define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */ |
|
|
526 | |
524 | int inline_size |
527 | int inline_size |
525 | array_nextsize (int elem, int cur, int cnt) |
528 | array_nextsize (int elem, int cur, int cnt) |
526 | { |
529 | { |
527 | int ncur = cur + 1; |
530 | int ncur = cur + 1; |
528 | |
531 | |
529 | do |
532 | do |
530 | ncur <<= 1; |
533 | ncur <<= 1; |
531 | while (cnt > ncur); |
534 | while (cnt > ncur); |
532 | |
535 | |
533 | /* if size > 4096, round to 4096 - 4 * longs to accomodate malloc overhead */ |
536 | /* if size is large, round to MALLOC_ROUND - 4 * longs to accomodate malloc overhead */ |
534 | if (elem * ncur > 4096) |
537 | if (elem * ncur > MALLOC_ROUND - sizeof (void *) * 4) |
535 | { |
538 | { |
536 | ncur *= elem; |
539 | ncur *= elem; |
537 | ncur = (ncur + elem + 4095 + sizeof (void *) * 4) & ~4095; |
540 | ncur = (ncur + elem + (MALLOC_ROUND - 1) + sizeof (void *) * 4) & ~(MALLOC_ROUND - 1); |
538 | ncur = ncur - sizeof (void *) * 4; |
541 | ncur = ncur - sizeof (void *) * 4; |
539 | ncur /= elem; |
542 | ncur /= elem; |
540 | } |
543 | } |
541 | |
544 | |
542 | return ncur; |
545 | return ncur; |
… | |
… | |
771 | /* maybe we could use a dummy element at heap [0]? */ |
774 | /* maybe we could use a dummy element at heap [0]? */ |
772 | if (!p || heap [p]->at <= w->at) |
775 | if (!p || heap [p]->at <= w->at) |
773 | break; |
776 | break; |
774 | |
777 | |
775 | heap [k] = heap [p]; |
778 | heap [k] = heap [p]; |
776 | ((W)heap [k])->active = k; |
779 | ev_active (heap [k]) = k; |
777 | k = p; |
780 | k = p; |
778 | } |
781 | } |
779 | |
782 | |
780 | heap [k] = w; |
783 | heap [k] = w; |
781 | ((W)heap [k])->active = k; |
784 | ev_active (heap [k]) = k; |
782 | } |
785 | } |
783 | |
786 | |
784 | /* away from the root */ |
787 | /* away from the root */ |
785 | void inline_speed |
788 | void inline_speed |
786 | downheap (WT *heap, int N, int k) |
789 | downheap (WT *heap, int N, int k) |
… | |
… | |
799 | |
802 | |
800 | if (w->at <= heap [c]->at) |
803 | if (w->at <= heap [c]->at) |
801 | break; |
804 | break; |
802 | |
805 | |
803 | heap [k] = heap [c]; |
806 | heap [k] = heap [c]; |
804 | ((W)heap [k])->active = k; |
807 | ev_active (heap [k]) = k; |
805 | |
808 | |
806 | k = c; |
809 | k = c; |
807 | } |
810 | } |
808 | |
811 | |
809 | heap [k] = w; |
812 | heap [k] = w; |
810 | ((W)heap [k])->active = k; |
813 | ev_active (heap [k]) = k; |
811 | } |
814 | } |
812 | |
815 | |
813 | void inline_size |
816 | void inline_size |
814 | adjustheap (WT *heap, int N, int k) |
817 | adjustheap (WT *heap, int N, int k) |
815 | { |
818 | { |
… | |
… | |
911 | pipecb (EV_P_ ev_io *iow, int revents) |
914 | pipecb (EV_P_ ev_io *iow, int revents) |
912 | { |
915 | { |
913 | #if EV_USE_EVENTFD |
916 | #if EV_USE_EVENTFD |
914 | if (evfd >= 0) |
917 | if (evfd >= 0) |
915 | { |
918 | { |
916 | uint64_t counter = 1; |
919 | uint64_t counter; |
917 | read (evfd, &counter, sizeof (uint64_t)); |
920 | read (evfd, &counter, sizeof (uint64_t)); |
918 | } |
921 | } |
919 | else |
922 | else |
920 | #endif |
923 | #endif |
921 | { |
924 | { |
… | |
… | |
1522 | periodics_reschedule (EV_P) |
1525 | periodics_reschedule (EV_P) |
1523 | { |
1526 | { |
1524 | int i; |
1527 | int i; |
1525 | |
1528 | |
1526 | /* adjust periodics after time jump */ |
1529 | /* adjust periodics after time jump */ |
1527 | for (i = 0; i < periodiccnt; ++i) |
1530 | for (i = 1; i <= periodiccnt; ++i) |
1528 | { |
1531 | { |
1529 | ev_periodic *w = (ev_periodic *)periodics [i]; |
1532 | ev_periodic *w = (ev_periodic *)periodics [i]; |
1530 | |
1533 | |
1531 | if (w->reschedule_cb) |
1534 | if (w->reschedule_cb) |
1532 | ev_at (w) = w->reschedule_cb (w, ev_rt_now); |
1535 | ev_at (w) = w->reschedule_cb (w, ev_rt_now); |
… | |
… | |
1894 | ev_start (EV_A_ (W)w, ++timercnt); |
1897 | ev_start (EV_A_ (W)w, ++timercnt); |
1895 | array_needsize (WT, timers, timermax, timercnt + 1, EMPTY2); |
1898 | array_needsize (WT, timers, timermax, timercnt + 1, EMPTY2); |
1896 | timers [timercnt] = (WT)w; |
1899 | timers [timercnt] = (WT)w; |
1897 | upheap (timers, timercnt); |
1900 | upheap (timers, timercnt); |
1898 | |
1901 | |
1899 | /*assert (("internal timer heap corruption", timers [((W)w)->active] == w));*/ |
1902 | /*assert (("internal timer heap corruption", timers [ev_active (w)] == w));*/ |
1900 | } |
1903 | } |
1901 | |
1904 | |
1902 | void noinline |
1905 | void noinline |
1903 | ev_timer_stop (EV_P_ ev_timer *w) |
1906 | ev_timer_stop (EV_P_ ev_timer *w) |
1904 | { |
1907 | { |
1905 | clear_pending (EV_A_ (W)w); |
1908 | clear_pending (EV_A_ (W)w); |
1906 | if (expect_false (!ev_is_active (w))) |
1909 | if (expect_false (!ev_is_active (w))) |
1907 | return; |
1910 | return; |
1908 | |
1911 | |
1909 | assert (("internal timer heap corruption", timers [((W)w)->active] == (WT)w)); |
|
|
1910 | |
|
|
1911 | { |
1912 | { |
1912 | int active = ((W)w)->active; |
1913 | int active = ev_active (w); |
|
|
1914 | |
|
|
1915 | assert (("internal timer heap corruption", timers [active] == (WT)w)); |
1913 | |
1916 | |
1914 | if (expect_true (active < timercnt)) |
1917 | if (expect_true (active < timercnt)) |
1915 | { |
1918 | { |
1916 | timers [active] = timers [timercnt]; |
1919 | timers [active] = timers [timercnt]; |
1917 | adjustheap (timers, timercnt, active); |
1920 | adjustheap (timers, timercnt, active); |
… | |
… | |
1931 | if (ev_is_active (w)) |
1934 | if (ev_is_active (w)) |
1932 | { |
1935 | { |
1933 | if (w->repeat) |
1936 | if (w->repeat) |
1934 | { |
1937 | { |
1935 | ev_at (w) = mn_now + w->repeat; |
1938 | ev_at (w) = mn_now + w->repeat; |
1936 | adjustheap (timers, timercnt, ((W)w)->active); |
1939 | adjustheap (timers, timercnt, ev_active (w)); |
1937 | } |
1940 | } |
1938 | else |
1941 | else |
1939 | ev_timer_stop (EV_A_ w); |
1942 | ev_timer_stop (EV_A_ w); |
1940 | } |
1943 | } |
1941 | else if (w->repeat) |
1944 | else if (w->repeat) |
1942 | { |
1945 | { |
1943 | w->at = w->repeat; |
1946 | ev_at (w) = w->repeat; |
1944 | ev_timer_start (EV_A_ w); |
1947 | ev_timer_start (EV_A_ w); |
1945 | } |
1948 | } |
1946 | } |
1949 | } |
1947 | |
1950 | |
1948 | #if EV_PERIODIC_ENABLE |
1951 | #if EV_PERIODIC_ENABLE |
… | |
… | |
1966 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1969 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1967 | array_needsize (WT, periodics, periodicmax, periodiccnt + 1, EMPTY2); |
1970 | array_needsize (WT, periodics, periodicmax, periodiccnt + 1, EMPTY2); |
1968 | periodics [periodiccnt] = (WT)w; |
1971 | periodics [periodiccnt] = (WT)w; |
1969 | upheap (periodics, periodiccnt); |
1972 | upheap (periodics, periodiccnt); |
1970 | |
1973 | |
1971 | /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/ |
1974 | /*assert (("internal periodic heap corruption", periodics [ev_active (w)] == w));*/ |
1972 | } |
1975 | } |
1973 | |
1976 | |
1974 | void noinline |
1977 | void noinline |
1975 | ev_periodic_stop (EV_P_ ev_periodic *w) |
1978 | ev_periodic_stop (EV_P_ ev_periodic *w) |
1976 | { |
1979 | { |
1977 | clear_pending (EV_A_ (W)w); |
1980 | clear_pending (EV_A_ (W)w); |
1978 | if (expect_false (!ev_is_active (w))) |
1981 | if (expect_false (!ev_is_active (w))) |
1979 | return; |
1982 | return; |
1980 | |
1983 | |
1981 | assert (("internal periodic heap corruption", periodics [((W)w)->active] == (WT)w)); |
|
|
1982 | |
|
|
1983 | { |
1984 | { |
1984 | int active = ((W)w)->active; |
1985 | int active = ev_active (w); |
|
|
1986 | |
|
|
1987 | assert (("internal periodic heap corruption", periodics [active] == (WT)w)); |
1985 | |
1988 | |
1986 | if (expect_true (active < periodiccnt)) |
1989 | if (expect_true (active < periodiccnt)) |
1987 | { |
1990 | { |
1988 | periodics [active] = periodics [periodiccnt]; |
1991 | periodics [active] = periodics [periodiccnt]; |
1989 | adjustheap (periodics, periodiccnt, active); |
1992 | adjustheap (periodics, periodiccnt, active); |
… | |
… | |
2113 | if (w->wd < 0) |
2116 | if (w->wd < 0) |
2114 | { |
2117 | { |
2115 | ev_timer_start (EV_A_ &w->timer); /* this is not race-free, so we still need to recheck periodically */ |
2118 | ev_timer_start (EV_A_ &w->timer); /* this is not race-free, so we still need to recheck periodically */ |
2116 | |
2119 | |
2117 | /* monitor some parent directory for speedup hints */ |
2120 | /* monitor some parent directory for speedup hints */ |
|
|
2121 | /* note that exceeding the hardcoded limit is not a correctness issue, */ |
|
|
2122 | /* but an efficiency issue only */ |
2118 | if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096) |
2123 | if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096) |
2119 | { |
2124 | { |
2120 | char path [4096]; |
2125 | char path [4096]; |
2121 | strcpy (path, w->path); |
2126 | strcpy (path, w->path); |
2122 | |
2127 | |
… | |
… | |
2367 | clear_pending (EV_A_ (W)w); |
2372 | clear_pending (EV_A_ (W)w); |
2368 | if (expect_false (!ev_is_active (w))) |
2373 | if (expect_false (!ev_is_active (w))) |
2369 | return; |
2374 | return; |
2370 | |
2375 | |
2371 | { |
2376 | { |
2372 | int active = ((W)w)->active; |
2377 | int active = ev_active (w); |
2373 | |
2378 | |
2374 | idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]]; |
2379 | idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]]; |
2375 | ((W)idles [ABSPRI (w)][active - 1])->active = active; |
2380 | ev_active (idles [ABSPRI (w)][active - 1]) = active; |
2376 | |
2381 | |
2377 | ev_stop (EV_A_ (W)w); |
2382 | ev_stop (EV_A_ (W)w); |
2378 | --idleall; |
2383 | --idleall; |
2379 | } |
2384 | } |
2380 | } |
2385 | } |
… | |
… | |
2397 | clear_pending (EV_A_ (W)w); |
2402 | clear_pending (EV_A_ (W)w); |
2398 | if (expect_false (!ev_is_active (w))) |
2403 | if (expect_false (!ev_is_active (w))) |
2399 | return; |
2404 | return; |
2400 | |
2405 | |
2401 | { |
2406 | { |
2402 | int active = ((W)w)->active; |
2407 | int active = ev_active (w); |
|
|
2408 | |
2403 | prepares [active - 1] = prepares [--preparecnt]; |
2409 | prepares [active - 1] = prepares [--preparecnt]; |
2404 | ((W)prepares [active - 1])->active = active; |
2410 | ev_active (prepares [active - 1]) = active; |
2405 | } |
2411 | } |
2406 | |
2412 | |
2407 | ev_stop (EV_A_ (W)w); |
2413 | ev_stop (EV_A_ (W)w); |
2408 | } |
2414 | } |
2409 | |
2415 | |
… | |
… | |
2424 | clear_pending (EV_A_ (W)w); |
2430 | clear_pending (EV_A_ (W)w); |
2425 | if (expect_false (!ev_is_active (w))) |
2431 | if (expect_false (!ev_is_active (w))) |
2426 | return; |
2432 | return; |
2427 | |
2433 | |
2428 | { |
2434 | { |
2429 | int active = ((W)w)->active; |
2435 | int active = ev_active (w); |
|
|
2436 | |
2430 | checks [active - 1] = checks [--checkcnt]; |
2437 | checks [active - 1] = checks [--checkcnt]; |
2431 | ((W)checks [active - 1])->active = active; |
2438 | ev_active (checks [active - 1]) = active; |
2432 | } |
2439 | } |
2433 | |
2440 | |
2434 | ev_stop (EV_A_ (W)w); |
2441 | ev_stop (EV_A_ (W)w); |
2435 | } |
2442 | } |
2436 | |
2443 | |
… | |
… | |
2532 | clear_pending (EV_A_ (W)w); |
2539 | clear_pending (EV_A_ (W)w); |
2533 | if (expect_false (!ev_is_active (w))) |
2540 | if (expect_false (!ev_is_active (w))) |
2534 | return; |
2541 | return; |
2535 | |
2542 | |
2536 | { |
2543 | { |
2537 | int active = ((W)w)->active; |
2544 | int active = ev_active (w); |
|
|
2545 | |
2538 | forks [active - 1] = forks [--forkcnt]; |
2546 | forks [active - 1] = forks [--forkcnt]; |
2539 | ((W)forks [active - 1])->active = active; |
2547 | ev_active (forks [active - 1]) = active; |
2540 | } |
2548 | } |
2541 | |
2549 | |
2542 | ev_stop (EV_A_ (W)w); |
2550 | ev_stop (EV_A_ (W)w); |
2543 | } |
2551 | } |
2544 | #endif |
2552 | #endif |
… | |
… | |
2563 | clear_pending (EV_A_ (W)w); |
2571 | clear_pending (EV_A_ (W)w); |
2564 | if (expect_false (!ev_is_active (w))) |
2572 | if (expect_false (!ev_is_active (w))) |
2565 | return; |
2573 | return; |
2566 | |
2574 | |
2567 | { |
2575 | { |
2568 | int active = ((W)w)->active; |
2576 | int active = ev_active (w); |
|
|
2577 | |
2569 | asyncs [active - 1] = asyncs [--asynccnt]; |
2578 | asyncs [active - 1] = asyncs [--asynccnt]; |
2570 | ((W)asyncs [active - 1])->active = active; |
2579 | ev_active (asyncs [active - 1]) = active; |
2571 | } |
2580 | } |
2572 | |
2581 | |
2573 | ev_stop (EV_A_ (W)w); |
2582 | ev_stop (EV_A_ (W)w); |
2574 | } |
2583 | } |
2575 | |
2584 | |