… | |
… | |
520 | } |
520 | } |
521 | } |
521 | } |
522 | |
522 | |
523 | /*****************************************************************************/ |
523 | /*****************************************************************************/ |
524 | |
524 | |
|
|
525 | #define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */ |
|
|
526 | |
525 | int inline_size |
527 | int inline_size |
526 | array_nextsize (int elem, int cur, int cnt) |
528 | array_nextsize (int elem, int cur, int cnt) |
527 | { |
529 | { |
528 | int ncur = cur + 1; |
530 | int ncur = cur + 1; |
529 | |
531 | |
530 | do |
532 | do |
531 | ncur <<= 1; |
533 | ncur <<= 1; |
532 | while (cnt > ncur); |
534 | while (cnt > ncur); |
533 | |
535 | |
534 | /* 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 */ |
535 | if (elem * ncur > 4096) |
537 | if (elem * ncur > MALLOC_ROUND - sizeof (void *) * 4) |
536 | { |
538 | { |
537 | ncur *= elem; |
539 | ncur *= elem; |
538 | ncur = (ncur + elem + 4095 + sizeof (void *) * 4) & ~4095; |
540 | ncur = (ncur + elem + (MALLOC_ROUND - 1) + sizeof (void *) * 4) & ~(MALLOC_ROUND - 1); |
539 | ncur = ncur - sizeof (void *) * 4; |
541 | ncur = ncur - sizeof (void *) * 4; |
540 | ncur /= elem; |
542 | ncur /= elem; |
541 | } |
543 | } |
542 | |
544 | |
543 | return ncur; |
545 | return ncur; |
… | |
… | |
772 | /* maybe we could use a dummy element at heap [0]? */ |
774 | /* maybe we could use a dummy element at heap [0]? */ |
773 | if (!p || heap [p]->at <= w->at) |
775 | if (!p || heap [p]->at <= w->at) |
774 | break; |
776 | break; |
775 | |
777 | |
776 | heap [k] = heap [p]; |
778 | heap [k] = heap [p]; |
777 | ((W)heap [k])->active = k; |
779 | ev_active (heap [k]) = k; |
778 | k = p; |
780 | k = p; |
779 | } |
781 | } |
780 | |
782 | |
781 | heap [k] = w; |
783 | heap [k] = w; |
782 | ((W)heap [k])->active = k; |
784 | ev_active (heap [k]) = k; |
783 | } |
785 | } |
784 | |
786 | |
785 | /* away from the root */ |
787 | /* away from the root */ |
786 | void inline_speed |
788 | void inline_speed |
787 | downheap (WT *heap, int N, int k) |
789 | downheap (WT *heap, int N, int k) |
… | |
… | |
800 | |
802 | |
801 | if (w->at <= heap [c]->at) |
803 | if (w->at <= heap [c]->at) |
802 | break; |
804 | break; |
803 | |
805 | |
804 | heap [k] = heap [c]; |
806 | heap [k] = heap [c]; |
805 | ((W)heap [k])->active = k; |
807 | ev_active (heap [k]) = k; |
806 | |
808 | |
807 | k = c; |
809 | k = c; |
808 | } |
810 | } |
809 | |
811 | |
810 | heap [k] = w; |
812 | heap [k] = w; |
811 | ((W)heap [k])->active = k; |
813 | ev_active (heap [k]) = k; |
812 | } |
814 | } |
813 | |
815 | |
814 | void inline_size |
816 | void inline_size |
815 | adjustheap (WT *heap, int N, int k) |
817 | adjustheap (WT *heap, int N, int k) |
816 | { |
818 | { |
… | |
… | |
912 | pipecb (EV_P_ ev_io *iow, int revents) |
914 | pipecb (EV_P_ ev_io *iow, int revents) |
913 | { |
915 | { |
914 | #if EV_USE_EVENTFD |
916 | #if EV_USE_EVENTFD |
915 | if (evfd >= 0) |
917 | if (evfd >= 0) |
916 | { |
918 | { |
917 | uint64_t counter = 1; |
919 | uint64_t counter; |
918 | read (evfd, &counter, sizeof (uint64_t)); |
920 | read (evfd, &counter, sizeof (uint64_t)); |
919 | } |
921 | } |
920 | else |
922 | else |
921 | #endif |
923 | #endif |
922 | { |
924 | { |
… | |
… | |
1368 | void |
1370 | void |
1369 | ev_loop_fork (EV_P) |
1371 | ev_loop_fork (EV_P) |
1370 | { |
1372 | { |
1371 | postfork = 1; /* must be in line with ev_default_fork */ |
1373 | postfork = 1; /* must be in line with ev_default_fork */ |
1372 | } |
1374 | } |
1373 | |
|
|
1374 | #endif |
1375 | #endif |
1375 | |
1376 | |
1376 | #if EV_MULTIPLICITY |
1377 | #if EV_MULTIPLICITY |
1377 | struct ev_loop * |
1378 | struct ev_loop * |
1378 | ev_default_loop_init (unsigned int flags) |
1379 | ev_default_loop_init (unsigned int flags) |
… | |
… | |
1458 | p->w->pending = 0; |
1459 | p->w->pending = 0; |
1459 | EV_CB_INVOKE (p->w, p->events); |
1460 | EV_CB_INVOKE (p->w, p->events); |
1460 | } |
1461 | } |
1461 | } |
1462 | } |
1462 | } |
1463 | } |
|
|
1464 | |
|
|
1465 | #if EV_IDLE_ENABLE |
|
|
1466 | void inline_size |
|
|
1467 | idle_reify (EV_P) |
|
|
1468 | { |
|
|
1469 | if (expect_false (idleall)) |
|
|
1470 | { |
|
|
1471 | int pri; |
|
|
1472 | |
|
|
1473 | for (pri = NUMPRI; pri--; ) |
|
|
1474 | { |
|
|
1475 | if (pendingcnt [pri]) |
|
|
1476 | break; |
|
|
1477 | |
|
|
1478 | if (idlecnt [pri]) |
|
|
1479 | { |
|
|
1480 | queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE); |
|
|
1481 | break; |
|
|
1482 | } |
|
|
1483 | } |
|
|
1484 | } |
|
|
1485 | } |
|
|
1486 | #endif |
1463 | |
1487 | |
1464 | void inline_size |
1488 | void inline_size |
1465 | timers_reify (EV_P) |
1489 | timers_reify (EV_P) |
1466 | { |
1490 | { |
1467 | while (timercnt && ev_at (timers [1]) <= mn_now) |
1491 | while (timercnt && ev_at (timers [1]) <= mn_now) |
… | |
… | |
1523 | periodics_reschedule (EV_P) |
1547 | periodics_reschedule (EV_P) |
1524 | { |
1548 | { |
1525 | int i; |
1549 | int i; |
1526 | |
1550 | |
1527 | /* adjust periodics after time jump */ |
1551 | /* adjust periodics after time jump */ |
1528 | for (i = 0; i < periodiccnt; ++i) |
1552 | for (i = 1; i <= periodiccnt; ++i) |
1529 | { |
1553 | { |
1530 | ev_periodic *w = (ev_periodic *)periodics [i]; |
1554 | ev_periodic *w = (ev_periodic *)periodics [i]; |
1531 | |
1555 | |
1532 | if (w->reschedule_cb) |
1556 | if (w->reschedule_cb) |
1533 | ev_at (w) = w->reschedule_cb (w, ev_rt_now); |
1557 | ev_at (w) = w->reschedule_cb (w, ev_rt_now); |
… | |
… | |
1536 | } |
1560 | } |
1537 | |
1561 | |
1538 | /* now rebuild the heap */ |
1562 | /* now rebuild the heap */ |
1539 | for (i = periodiccnt >> 1; i--; ) |
1563 | for (i = periodiccnt >> 1; i--; ) |
1540 | downheap (periodics, periodiccnt, i); |
1564 | downheap (periodics, periodiccnt, i); |
1541 | } |
|
|
1542 | #endif |
|
|
1543 | |
|
|
1544 | #if EV_IDLE_ENABLE |
|
|
1545 | void inline_size |
|
|
1546 | idle_reify (EV_P) |
|
|
1547 | { |
|
|
1548 | if (expect_false (idleall)) |
|
|
1549 | { |
|
|
1550 | int pri; |
|
|
1551 | |
|
|
1552 | for (pri = NUMPRI; pri--; ) |
|
|
1553 | { |
|
|
1554 | if (pendingcnt [pri]) |
|
|
1555 | break; |
|
|
1556 | |
|
|
1557 | if (idlecnt [pri]) |
|
|
1558 | { |
|
|
1559 | queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE); |
|
|
1560 | break; |
|
|
1561 | } |
|
|
1562 | } |
|
|
1563 | } |
|
|
1564 | } |
1565 | } |
1565 | #endif |
1566 | #endif |
1566 | |
1567 | |
1567 | void inline_speed |
1568 | void inline_speed |
1568 | time_update (EV_P_ ev_tstamp max_block) |
1569 | time_update (EV_P_ ev_tstamp max_block) |
… | |
… | |
1597 | */ |
1598 | */ |
1598 | for (i = 4; --i; ) |
1599 | for (i = 4; --i; ) |
1599 | { |
1600 | { |
1600 | rtmn_diff = ev_rt_now - mn_now; |
1601 | rtmn_diff = ev_rt_now - mn_now; |
1601 | |
1602 | |
1602 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
1603 | if (expect_true (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)) |
1603 | return; /* all is well */ |
1604 | return; /* all is well */ |
1604 | |
1605 | |
1605 | ev_rt_now = ev_time (); |
1606 | ev_rt_now = ev_time (); |
1606 | mn_now = get_clock (); |
1607 | mn_now = get_clock (); |
1607 | now_floor = mn_now; |
1608 | now_floor = mn_now; |
… | |
… | |
1895 | ev_start (EV_A_ (W)w, ++timercnt); |
1896 | ev_start (EV_A_ (W)w, ++timercnt); |
1896 | array_needsize (WT, timers, timermax, timercnt + 1, EMPTY2); |
1897 | array_needsize (WT, timers, timermax, timercnt + 1, EMPTY2); |
1897 | timers [timercnt] = (WT)w; |
1898 | timers [timercnt] = (WT)w; |
1898 | upheap (timers, timercnt); |
1899 | upheap (timers, timercnt); |
1899 | |
1900 | |
1900 | /*assert (("internal timer heap corruption", timers [((W)w)->active] == w));*/ |
1901 | /*assert (("internal timer heap corruption", timers [ev_active (w)] == w));*/ |
1901 | } |
1902 | } |
1902 | |
1903 | |
1903 | void noinline |
1904 | void noinline |
1904 | ev_timer_stop (EV_P_ ev_timer *w) |
1905 | ev_timer_stop (EV_P_ ev_timer *w) |
1905 | { |
1906 | { |
1906 | clear_pending (EV_A_ (W)w); |
1907 | clear_pending (EV_A_ (W)w); |
1907 | if (expect_false (!ev_is_active (w))) |
1908 | if (expect_false (!ev_is_active (w))) |
1908 | return; |
1909 | return; |
1909 | |
1910 | |
1910 | assert (("internal timer heap corruption", timers [((W)w)->active] == (WT)w)); |
|
|
1911 | |
|
|
1912 | { |
1911 | { |
1913 | int active = ((W)w)->active; |
1912 | int active = ev_active (w); |
|
|
1913 | |
|
|
1914 | assert (("internal timer heap corruption", timers [active] == (WT)w)); |
1914 | |
1915 | |
1915 | if (expect_true (active < timercnt)) |
1916 | if (expect_true (active < timercnt)) |
1916 | { |
1917 | { |
1917 | timers [active] = timers [timercnt]; |
1918 | timers [active] = timers [timercnt]; |
1918 | adjustheap (timers, timercnt, active); |
1919 | adjustheap (timers, timercnt, active); |
… | |
… | |
1932 | if (ev_is_active (w)) |
1933 | if (ev_is_active (w)) |
1933 | { |
1934 | { |
1934 | if (w->repeat) |
1935 | if (w->repeat) |
1935 | { |
1936 | { |
1936 | ev_at (w) = mn_now + w->repeat; |
1937 | ev_at (w) = mn_now + w->repeat; |
1937 | adjustheap (timers, timercnt, ((W)w)->active); |
1938 | adjustheap (timers, timercnt, ev_active (w)); |
1938 | } |
1939 | } |
1939 | else |
1940 | else |
1940 | ev_timer_stop (EV_A_ w); |
1941 | ev_timer_stop (EV_A_ w); |
1941 | } |
1942 | } |
1942 | else if (w->repeat) |
1943 | else if (w->repeat) |
… | |
… | |
1967 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1968 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1968 | array_needsize (WT, periodics, periodicmax, periodiccnt + 1, EMPTY2); |
1969 | array_needsize (WT, periodics, periodicmax, periodiccnt + 1, EMPTY2); |
1969 | periodics [periodiccnt] = (WT)w; |
1970 | periodics [periodiccnt] = (WT)w; |
1970 | upheap (periodics, periodiccnt); |
1971 | upheap (periodics, periodiccnt); |
1971 | |
1972 | |
1972 | /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/ |
1973 | /*assert (("internal periodic heap corruption", periodics [ev_active (w)] == w));*/ |
1973 | } |
1974 | } |
1974 | |
1975 | |
1975 | void noinline |
1976 | void noinline |
1976 | ev_periodic_stop (EV_P_ ev_periodic *w) |
1977 | ev_periodic_stop (EV_P_ ev_periodic *w) |
1977 | { |
1978 | { |
1978 | clear_pending (EV_A_ (W)w); |
1979 | clear_pending (EV_A_ (W)w); |
1979 | if (expect_false (!ev_is_active (w))) |
1980 | if (expect_false (!ev_is_active (w))) |
1980 | return; |
1981 | return; |
1981 | |
1982 | |
1982 | assert (("internal periodic heap corruption", periodics [((W)w)->active] == (WT)w)); |
|
|
1983 | |
|
|
1984 | { |
1983 | { |
1985 | int active = ((W)w)->active; |
1984 | int active = ev_active (w); |
|
|
1985 | |
|
|
1986 | assert (("internal periodic heap corruption", periodics [active] == (WT)w)); |
1986 | |
1987 | |
1987 | if (expect_true (active < periodiccnt)) |
1988 | if (expect_true (active < periodiccnt)) |
1988 | { |
1989 | { |
1989 | periodics [active] = periodics [periodiccnt]; |
1990 | periodics [active] = periodics [periodiccnt]; |
1990 | adjustheap (periodics, periodiccnt, active); |
1991 | adjustheap (periodics, periodiccnt, active); |
… | |
… | |
2114 | if (w->wd < 0) |
2115 | if (w->wd < 0) |
2115 | { |
2116 | { |
2116 | ev_timer_start (EV_A_ &w->timer); /* this is not race-free, so we still need to recheck periodically */ |
2117 | ev_timer_start (EV_A_ &w->timer); /* this is not race-free, so we still need to recheck periodically */ |
2117 | |
2118 | |
2118 | /* monitor some parent directory for speedup hints */ |
2119 | /* monitor some parent directory for speedup hints */ |
|
|
2120 | /* note that exceeding the hardcoded limit is not a correctness issue, */ |
|
|
2121 | /* but an efficiency issue only */ |
2119 | if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096) |
2122 | if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096) |
2120 | { |
2123 | { |
2121 | char path [4096]; |
2124 | char path [4096]; |
2122 | strcpy (path, w->path); |
2125 | strcpy (path, w->path); |
2123 | |
2126 | |
… | |
… | |
2368 | clear_pending (EV_A_ (W)w); |
2371 | clear_pending (EV_A_ (W)w); |
2369 | if (expect_false (!ev_is_active (w))) |
2372 | if (expect_false (!ev_is_active (w))) |
2370 | return; |
2373 | return; |
2371 | |
2374 | |
2372 | { |
2375 | { |
2373 | int active = ((W)w)->active; |
2376 | int active = ev_active (w); |
2374 | |
2377 | |
2375 | idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]]; |
2378 | idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]]; |
2376 | ((W)idles [ABSPRI (w)][active - 1])->active = active; |
2379 | ev_active (idles [ABSPRI (w)][active - 1]) = active; |
2377 | |
2380 | |
2378 | ev_stop (EV_A_ (W)w); |
2381 | ev_stop (EV_A_ (W)w); |
2379 | --idleall; |
2382 | --idleall; |
2380 | } |
2383 | } |
2381 | } |
2384 | } |
… | |
… | |
2398 | clear_pending (EV_A_ (W)w); |
2401 | clear_pending (EV_A_ (W)w); |
2399 | if (expect_false (!ev_is_active (w))) |
2402 | if (expect_false (!ev_is_active (w))) |
2400 | return; |
2403 | return; |
2401 | |
2404 | |
2402 | { |
2405 | { |
2403 | int active = ((W)w)->active; |
2406 | int active = ev_active (w); |
|
|
2407 | |
2404 | prepares [active - 1] = prepares [--preparecnt]; |
2408 | prepares [active - 1] = prepares [--preparecnt]; |
2405 | ((W)prepares [active - 1])->active = active; |
2409 | ev_active (prepares [active - 1]) = active; |
2406 | } |
2410 | } |
2407 | |
2411 | |
2408 | ev_stop (EV_A_ (W)w); |
2412 | ev_stop (EV_A_ (W)w); |
2409 | } |
2413 | } |
2410 | |
2414 | |
… | |
… | |
2425 | clear_pending (EV_A_ (W)w); |
2429 | clear_pending (EV_A_ (W)w); |
2426 | if (expect_false (!ev_is_active (w))) |
2430 | if (expect_false (!ev_is_active (w))) |
2427 | return; |
2431 | return; |
2428 | |
2432 | |
2429 | { |
2433 | { |
2430 | int active = ((W)w)->active; |
2434 | int active = ev_active (w); |
|
|
2435 | |
2431 | checks [active - 1] = checks [--checkcnt]; |
2436 | checks [active - 1] = checks [--checkcnt]; |
2432 | ((W)checks [active - 1])->active = active; |
2437 | ev_active (checks [active - 1]) = active; |
2433 | } |
2438 | } |
2434 | |
2439 | |
2435 | ev_stop (EV_A_ (W)w); |
2440 | ev_stop (EV_A_ (W)w); |
2436 | } |
2441 | } |
2437 | |
2442 | |
… | |
… | |
2533 | clear_pending (EV_A_ (W)w); |
2538 | clear_pending (EV_A_ (W)w); |
2534 | if (expect_false (!ev_is_active (w))) |
2539 | if (expect_false (!ev_is_active (w))) |
2535 | return; |
2540 | return; |
2536 | |
2541 | |
2537 | { |
2542 | { |
2538 | int active = ((W)w)->active; |
2543 | int active = ev_active (w); |
|
|
2544 | |
2539 | forks [active - 1] = forks [--forkcnt]; |
2545 | forks [active - 1] = forks [--forkcnt]; |
2540 | ((W)forks [active - 1])->active = active; |
2546 | ev_active (forks [active - 1]) = active; |
2541 | } |
2547 | } |
2542 | |
2548 | |
2543 | ev_stop (EV_A_ (W)w); |
2549 | ev_stop (EV_A_ (W)w); |
2544 | } |
2550 | } |
2545 | #endif |
2551 | #endif |
… | |
… | |
2564 | clear_pending (EV_A_ (W)w); |
2570 | clear_pending (EV_A_ (W)w); |
2565 | if (expect_false (!ev_is_active (w))) |
2571 | if (expect_false (!ev_is_active (w))) |
2566 | return; |
2572 | return; |
2567 | |
2573 | |
2568 | { |
2574 | { |
2569 | int active = ((W)w)->active; |
2575 | int active = ev_active (w); |
|
|
2576 | |
2570 | asyncs [active - 1] = asyncs [--asynccnt]; |
2577 | asyncs [active - 1] = asyncs [--asynccnt]; |
2571 | ((W)asyncs [active - 1])->active = active; |
2578 | ev_active (asyncs [active - 1]) = active; |
2572 | } |
2579 | } |
2573 | |
2580 | |
2574 | ev_stop (EV_A_ (W)w); |
2581 | ev_stop (EV_A_ (W)w); |
2575 | } |
2582 | } |
2576 | |
2583 | |