1 | /* |
1 | /* |
2 | * libev event processing core, watcher management |
2 | * libev event processing core, watcher management |
3 | * |
3 | * |
4 | * Copyright (c) 2007,2008,2009,2010 Marc Alexander Lehmann <libev@schmorp.de> |
4 | * Copyright (c) 2007,2008,2009,2010,2011 Marc Alexander Lehmann <libev@schmorp.de> |
5 | * All rights reserved. |
5 | * All rights reserved. |
6 | * |
6 | * |
7 | * Redistribution and use in source and binary forms, with or without modifica- |
7 | * Redistribution and use in source and binary forms, with or without modifica- |
8 | * tion, are permitted provided that the following conditions are met: |
8 | * tion, are permitted provided that the following conditions are met: |
9 | * |
9 | * |
… | |
… | |
530 | |
530 | |
531 | static unsigned int noinline |
531 | static unsigned int noinline |
532 | ev_linux_version (void) |
532 | ev_linux_version (void) |
533 | { |
533 | { |
534 | #ifdef __linux |
534 | #ifdef __linux |
|
|
535 | unsigned int v = 0; |
535 | struct utsname buf; |
536 | struct utsname buf; |
536 | unsigned int v; |
|
|
537 | int i; |
537 | int i; |
538 | char *p = buf.release; |
538 | char *p = buf.release; |
539 | |
539 | |
540 | if (uname (&buf)) |
540 | if (uname (&buf)) |
541 | return 0; |
541 | return 0; |
… | |
… | |
591 | if (syserr_cb) |
591 | if (syserr_cb) |
592 | syserr_cb (msg); |
592 | syserr_cb (msg); |
593 | else |
593 | else |
594 | { |
594 | { |
595 | #if EV_AVOID_STDIO |
595 | #if EV_AVOID_STDIO |
596 | const char *err = strerror (errno); |
|
|
597 | |
|
|
598 | ev_printerr (msg); |
596 | ev_printerr (msg); |
599 | ev_printerr (": "); |
597 | ev_printerr (": "); |
600 | ev_printerr (err); |
598 | ev_printerr (strerror (errno)); |
601 | ev_printerr ("\n"); |
599 | ev_printerr ("\n"); |
602 | #else |
600 | #else |
603 | perror (msg); |
601 | perror (msg); |
604 | #endif |
602 | #endif |
605 | abort (); |
603 | abort (); |
… | |
… | |
639 | ptr = alloc (ptr, size); |
637 | ptr = alloc (ptr, size); |
640 | |
638 | |
641 | if (!ptr && size) |
639 | if (!ptr && size) |
642 | { |
640 | { |
643 | #if EV_AVOID_STDIO |
641 | #if EV_AVOID_STDIO |
644 | ev_printerr ("libev: memory allocation failed, aborting.\n"); |
642 | ev_printerr ("(libev) memory allocation failed, aborting.\n"); |
645 | #else |
643 | #else |
646 | fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size); |
644 | fprintf (stderr, "(libev) cannot allocate %ld bytes, aborting.", size); |
647 | #endif |
645 | #endif |
648 | abort (); |
646 | abort (); |
649 | } |
647 | } |
650 | |
648 | |
651 | return ptr; |
649 | return ptr; |
… | |
… | |
815 | /* by older ones */ |
813 | /* by older ones */ |
816 | EV_TV_SET (tv, delay); |
814 | EV_TV_SET (tv, delay); |
817 | select (0, 0, 0, 0, &tv); |
815 | select (0, 0, 0, 0, &tv); |
818 | #endif |
816 | #endif |
819 | } |
817 | } |
|
|
818 | } |
|
|
819 | |
|
|
820 | inline_speed int |
|
|
821 | ev_timeout_to_ms (ev_tstamp timeout) |
|
|
822 | { |
|
|
823 | int ms = timeout * 1000. + .999999; |
|
|
824 | |
|
|
825 | return expect_true (ms) ? ms : timeout < 1e-6 ? 0 : 1; |
820 | } |
826 | } |
821 | |
827 | |
822 | /*****************************************************************************/ |
828 | /*****************************************************************************/ |
823 | |
829 | |
824 | #define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */ |
830 | #define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */ |
… | |
… | |
1345 | char dummy; |
1351 | char dummy; |
1346 | /* see discussion in evpipe_write when you think this read should be recv in win32 */ |
1352 | /* see discussion in evpipe_write when you think this read should be recv in win32 */ |
1347 | read (evpipe [0], &dummy, 1); |
1353 | read (evpipe [0], &dummy, 1); |
1348 | } |
1354 | } |
1349 | |
1355 | |
|
|
1356 | #if EV_SIGNAL_ENABLE |
1350 | if (sig_pending) |
1357 | if (sig_pending) |
1351 | { |
1358 | { |
1352 | sig_pending = 0; |
1359 | sig_pending = 0; |
1353 | |
1360 | |
1354 | for (i = EV_NSIG - 1; i--; ) |
1361 | for (i = EV_NSIG - 1; i--; ) |
1355 | if (expect_false (signals [i].pending)) |
1362 | if (expect_false (signals [i].pending)) |
1356 | ev_feed_signal_event (EV_A_ i + 1); |
1363 | ev_feed_signal_event (EV_A_ i + 1); |
1357 | } |
1364 | } |
|
|
1365 | #endif |
1358 | |
1366 | |
1359 | #if EV_ASYNC_ENABLE |
1367 | #if EV_ASYNC_ENABLE |
1360 | if (async_pending) |
1368 | if (async_pending) |
1361 | { |
1369 | { |
1362 | async_pending = 0; |
1370 | async_pending = 0; |
… | |
… | |
1371 | #endif |
1379 | #endif |
1372 | } |
1380 | } |
1373 | |
1381 | |
1374 | /*****************************************************************************/ |
1382 | /*****************************************************************************/ |
1375 | |
1383 | |
|
|
1384 | void |
|
|
1385 | ev_feed_signal (int signum) |
|
|
1386 | { |
|
|
1387 | #if EV_MULTIPLICITY |
|
|
1388 | EV_P = signals [signum - 1].loop; |
|
|
1389 | |
|
|
1390 | if (!EV_A) |
|
|
1391 | return; |
|
|
1392 | #endif |
|
|
1393 | |
|
|
1394 | signals [signum - 1].pending = 1; |
|
|
1395 | evpipe_write (EV_A_ &sig_pending); |
|
|
1396 | } |
|
|
1397 | |
1376 | static void |
1398 | static void |
1377 | ev_sighandler (int signum) |
1399 | ev_sighandler (int signum) |
1378 | { |
1400 | { |
1379 | #if EV_MULTIPLICITY |
|
|
1380 | EV_P = signals [signum - 1].loop; |
|
|
1381 | #endif |
|
|
1382 | |
|
|
1383 | #ifdef _WIN32 |
1401 | #ifdef _WIN32 |
1384 | signal (signum, ev_sighandler); |
1402 | signal (signum, ev_sighandler); |
1385 | #endif |
1403 | #endif |
1386 | |
1404 | |
1387 | signals [signum - 1].pending = 1; |
1405 | ev_feed_signal (signum); |
1388 | evpipe_write (EV_A_ &sig_pending); |
|
|
1389 | } |
1406 | } |
1390 | |
1407 | |
1391 | void noinline |
1408 | void noinline |
1392 | ev_feed_signal_event (EV_P_ int signum) |
1409 | ev_feed_signal_event (EV_P_ int signum) |
1393 | { |
1410 | { |
… | |
… | |
1645 | static void noinline |
1662 | static void noinline |
1646 | loop_init (EV_P_ unsigned int flags) |
1663 | loop_init (EV_P_ unsigned int flags) |
1647 | { |
1664 | { |
1648 | if (!backend) |
1665 | if (!backend) |
1649 | { |
1666 | { |
|
|
1667 | origflags = flags; |
|
|
1668 | |
1650 | #if EV_USE_REALTIME |
1669 | #if EV_USE_REALTIME |
1651 | if (!have_realtime) |
1670 | if (!have_realtime) |
1652 | { |
1671 | { |
1653 | struct timespec ts; |
1672 | struct timespec ts; |
1654 | |
1673 | |
… | |
… | |
1699 | #endif |
1718 | #endif |
1700 | #if EV_USE_SIGNALFD |
1719 | #if EV_USE_SIGNALFD |
1701 | sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1; |
1720 | sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1; |
1702 | #endif |
1721 | #endif |
1703 | |
1722 | |
1704 | if (!(flags & 0x0000ffffU)) |
1723 | if (!(flags & EVBACKEND_MASK)) |
1705 | flags |= ev_recommended_backends (); |
1724 | flags |= ev_recommended_backends (); |
1706 | |
1725 | |
1707 | #if EV_USE_IOCP |
1726 | #if EV_USE_IOCP |
1708 | if (!backend && (flags & EVBACKEND_IOCP )) backend = iocp_init (EV_A_ flags); |
1727 | if (!backend && (flags & EVBACKEND_IOCP )) backend = iocp_init (EV_A_ flags); |
1709 | #endif |
1728 | #endif |
… | |
… | |
1731 | #endif |
1750 | #endif |
1732 | } |
1751 | } |
1733 | } |
1752 | } |
1734 | |
1753 | |
1735 | /* free up a loop structure */ |
1754 | /* free up a loop structure */ |
1736 | static void noinline |
1755 | void |
1737 | loop_destroy (EV_P) |
1756 | ev_loop_destroy (EV_P) |
1738 | { |
1757 | { |
1739 | int i; |
1758 | int i; |
|
|
1759 | |
|
|
1760 | #if EV_MULTIPLICITY |
|
|
1761 | /* mimic free (0) */ |
|
|
1762 | if (!EV_A) |
|
|
1763 | return; |
|
|
1764 | #endif |
|
|
1765 | |
|
|
1766 | #if EV_CLEANUP_ENABLE |
|
|
1767 | /* queue cleanup watchers (and execute them) */ |
|
|
1768 | if (expect_false (cleanupcnt)) |
|
|
1769 | { |
|
|
1770 | queue_events (EV_A_ (W *)cleanups, cleanupcnt, EV_CLEANUP); |
|
|
1771 | EV_INVOKE_PENDING; |
|
|
1772 | } |
|
|
1773 | #endif |
|
|
1774 | |
|
|
1775 | #if EV_CHILD_ENABLE |
|
|
1776 | if (ev_is_active (&childev)) |
|
|
1777 | { |
|
|
1778 | ev_ref (EV_A); /* child watcher */ |
|
|
1779 | ev_signal_stop (EV_A_ &childev); |
|
|
1780 | } |
|
|
1781 | #endif |
1740 | |
1782 | |
1741 | if (ev_is_active (&pipe_w)) |
1783 | if (ev_is_active (&pipe_w)) |
1742 | { |
1784 | { |
1743 | /*ev_ref (EV_A);*/ |
1785 | /*ev_ref (EV_A);*/ |
1744 | /*ev_io_stop (EV_A_ &pipe_w);*/ |
1786 | /*ev_io_stop (EV_A_ &pipe_w);*/ |
… | |
… | |
1805 | array_free (periodic, EMPTY); |
1847 | array_free (periodic, EMPTY); |
1806 | #endif |
1848 | #endif |
1807 | #if EV_FORK_ENABLE |
1849 | #if EV_FORK_ENABLE |
1808 | array_free (fork, EMPTY); |
1850 | array_free (fork, EMPTY); |
1809 | #endif |
1851 | #endif |
|
|
1852 | #if EV_CLEANUP_ENABLE |
|
|
1853 | array_free (cleanup, EMPTY); |
|
|
1854 | #endif |
1810 | array_free (prepare, EMPTY); |
1855 | array_free (prepare, EMPTY); |
1811 | array_free (check, EMPTY); |
1856 | array_free (check, EMPTY); |
1812 | #if EV_ASYNC_ENABLE |
1857 | #if EV_ASYNC_ENABLE |
1813 | array_free (async, EMPTY); |
1858 | array_free (async, EMPTY); |
1814 | #endif |
1859 | #endif |
1815 | |
1860 | |
1816 | backend = 0; |
1861 | backend = 0; |
|
|
1862 | |
|
|
1863 | #if EV_MULTIPLICITY |
|
|
1864 | if (ev_is_default_loop (EV_A)) |
|
|
1865 | #endif |
|
|
1866 | ev_default_loop_ptr = 0; |
|
|
1867 | #if EV_MULTIPLICITY |
|
|
1868 | else |
|
|
1869 | ev_free (EV_A); |
|
|
1870 | #endif |
1817 | } |
1871 | } |
1818 | |
1872 | |
1819 | #if EV_USE_INOTIFY |
1873 | #if EV_USE_INOTIFY |
1820 | inline_size void infy_fork (EV_P); |
1874 | inline_size void infy_fork (EV_P); |
1821 | #endif |
1875 | #endif |
… | |
… | |
1880 | loop_init (EV_A_ flags); |
1934 | loop_init (EV_A_ flags); |
1881 | |
1935 | |
1882 | if (ev_backend (EV_A)) |
1936 | if (ev_backend (EV_A)) |
1883 | return EV_A; |
1937 | return EV_A; |
1884 | |
1938 | |
|
|
1939 | ev_free (EV_A); |
1885 | return 0; |
1940 | return 0; |
1886 | } |
1941 | } |
1887 | |
1942 | |
1888 | void |
|
|
1889 | ev_loop_destroy (EV_P) |
|
|
1890 | { |
|
|
1891 | loop_destroy (EV_A); |
|
|
1892 | ev_free (loop); |
|
|
1893 | } |
|
|
1894 | |
|
|
1895 | void |
|
|
1896 | ev_loop_fork (EV_P) |
|
|
1897 | { |
|
|
1898 | postfork = 1; /* must be in line with ev_default_fork */ |
|
|
1899 | } |
|
|
1900 | #endif /* multiplicity */ |
1943 | #endif /* multiplicity */ |
1901 | |
1944 | |
1902 | #if EV_VERIFY |
1945 | #if EV_VERIFY |
1903 | static void noinline |
1946 | static void noinline |
1904 | verify_watcher (EV_P_ W w) |
1947 | verify_watcher (EV_P_ W w) |
… | |
… | |
1979 | #if EV_FORK_ENABLE |
2022 | #if EV_FORK_ENABLE |
1980 | assert (forkmax >= forkcnt); |
2023 | assert (forkmax >= forkcnt); |
1981 | array_verify (EV_A_ (W *)forks, forkcnt); |
2024 | array_verify (EV_A_ (W *)forks, forkcnt); |
1982 | #endif |
2025 | #endif |
1983 | |
2026 | |
|
|
2027 | #if EV_CLEANUP_ENABLE |
|
|
2028 | assert (cleanupmax >= cleanupcnt); |
|
|
2029 | array_verify (EV_A_ (W *)cleanups, cleanupcnt); |
|
|
2030 | #endif |
|
|
2031 | |
1984 | #if EV_ASYNC_ENABLE |
2032 | #if EV_ASYNC_ENABLE |
1985 | assert (asyncmax >= asynccnt); |
2033 | assert (asyncmax >= asynccnt); |
1986 | array_verify (EV_A_ (W *)asyncs, asynccnt); |
2034 | array_verify (EV_A_ (W *)asyncs, asynccnt); |
1987 | #endif |
2035 | #endif |
1988 | |
2036 | |
… | |
… | |
2038 | |
2086 | |
2039 | return ev_default_loop_ptr; |
2087 | return ev_default_loop_ptr; |
2040 | } |
2088 | } |
2041 | |
2089 | |
2042 | void |
2090 | void |
2043 | ev_default_destroy (void) |
2091 | ev_loop_fork (EV_P) |
2044 | { |
2092 | { |
2045 | #if EV_MULTIPLICITY |
|
|
2046 | EV_P = ev_default_loop_ptr; |
|
|
2047 | #endif |
|
|
2048 | |
|
|
2049 | ev_default_loop_ptr = 0; |
|
|
2050 | |
|
|
2051 | #if EV_CHILD_ENABLE |
|
|
2052 | ev_ref (EV_A); /* child watcher */ |
|
|
2053 | ev_signal_stop (EV_A_ &childev); |
|
|
2054 | #endif |
|
|
2055 | |
|
|
2056 | loop_destroy (EV_A); |
|
|
2057 | } |
|
|
2058 | |
|
|
2059 | void |
|
|
2060 | ev_default_fork (void) |
|
|
2061 | { |
|
|
2062 | #if EV_MULTIPLICITY |
|
|
2063 | EV_P = ev_default_loop_ptr; |
|
|
2064 | #endif |
|
|
2065 | |
|
|
2066 | postfork = 1; /* must be in line with ev_loop_fork */ |
2093 | postfork = 1; /* must be in line with ev_default_fork */ |
2067 | } |
2094 | } |
2068 | |
2095 | |
2069 | /*****************************************************************************/ |
2096 | /*****************************************************************************/ |
2070 | |
2097 | |
2071 | void |
2098 | void |
… | |
… | |
2093 | |
2120 | |
2094 | for (pri = NUMPRI; pri--; ) |
2121 | for (pri = NUMPRI; pri--; ) |
2095 | while (pendingcnt [pri]) |
2122 | while (pendingcnt [pri]) |
2096 | { |
2123 | { |
2097 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
2124 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
2098 | |
|
|
2099 | /*assert (("libev: non-pending watcher on pending list", p->w->pending));*/ |
|
|
2100 | /* ^ this is no longer true, as pending_w could be here */ |
|
|
2101 | |
2125 | |
2102 | p->w->pending = 0; |
2126 | p->w->pending = 0; |
2103 | EV_CB_INVOKE (p->w, p->events); |
2127 | EV_CB_INVOKE (p->w, p->events); |
2104 | EV_FREQUENT_CHECK; |
2128 | EV_FREQUENT_CHECK; |
2105 | } |
2129 | } |
… | |
… | |
2873 | sa.sa_handler = ev_sighandler; |
2897 | sa.sa_handler = ev_sighandler; |
2874 | sigfillset (&sa.sa_mask); |
2898 | sigfillset (&sa.sa_mask); |
2875 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
2899 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
2876 | sigaction (w->signum, &sa, 0); |
2900 | sigaction (w->signum, &sa, 0); |
2877 | |
2901 | |
|
|
2902 | if (origflags & EVFLAG_NOSIGMASK) |
|
|
2903 | { |
2878 | sigemptyset (&sa.sa_mask); |
2904 | sigemptyset (&sa.sa_mask); |
2879 | sigaddset (&sa.sa_mask, w->signum); |
2905 | sigaddset (&sa.sa_mask, w->signum); |
2880 | sigprocmask (SIG_UNBLOCK, &sa.sa_mask, 0); |
2906 | sigprocmask (SIG_UNBLOCK, &sa.sa_mask, 0); |
|
|
2907 | } |
2881 | #endif |
2908 | #endif |
2882 | } |
2909 | } |
2883 | |
2910 | |
2884 | EV_FREQUENT_CHECK; |
2911 | EV_FREQUENT_CHECK; |
2885 | } |
2912 | } |
… | |
… | |
3577 | |
3604 | |
3578 | EV_FREQUENT_CHECK; |
3605 | EV_FREQUENT_CHECK; |
3579 | } |
3606 | } |
3580 | #endif |
3607 | #endif |
3581 | |
3608 | |
|
|
3609 | #if EV_CLEANUP_ENABLE |
|
|
3610 | void |
|
|
3611 | ev_cleanup_start (EV_P_ ev_cleanup *w) |
|
|
3612 | { |
|
|
3613 | if (expect_false (ev_is_active (w))) |
|
|
3614 | return; |
|
|
3615 | |
|
|
3616 | EV_FREQUENT_CHECK; |
|
|
3617 | |
|
|
3618 | ev_start (EV_A_ (W)w, ++cleanupcnt); |
|
|
3619 | array_needsize (ev_cleanup *, cleanups, cleanupmax, cleanupcnt, EMPTY2); |
|
|
3620 | cleanups [cleanupcnt - 1] = w; |
|
|
3621 | |
|
|
3622 | /* cleanup watchers should never keep a refcount on the loop */ |
|
|
3623 | ev_unref (EV_A); |
|
|
3624 | EV_FREQUENT_CHECK; |
|
|
3625 | } |
|
|
3626 | |
|
|
3627 | void |
|
|
3628 | ev_cleanup_stop (EV_P_ ev_cleanup *w) |
|
|
3629 | { |
|
|
3630 | clear_pending (EV_A_ (W)w); |
|
|
3631 | if (expect_false (!ev_is_active (w))) |
|
|
3632 | return; |
|
|
3633 | |
|
|
3634 | EV_FREQUENT_CHECK; |
|
|
3635 | ev_ref (EV_A); |
|
|
3636 | |
|
|
3637 | { |
|
|
3638 | int active = ev_active (w); |
|
|
3639 | |
|
|
3640 | cleanups [active - 1] = cleanups [--cleanupcnt]; |
|
|
3641 | ev_active (cleanups [active - 1]) = active; |
|
|
3642 | } |
|
|
3643 | |
|
|
3644 | ev_stop (EV_A_ (W)w); |
|
|
3645 | |
|
|
3646 | EV_FREQUENT_CHECK; |
|
|
3647 | } |
|
|
3648 | #endif |
|
|
3649 | |
3582 | #if EV_ASYNC_ENABLE |
3650 | #if EV_ASYNC_ENABLE |
3583 | void |
3651 | void |
3584 | ev_async_start (EV_P_ ev_async *w) |
3652 | ev_async_start (EV_P_ ev_async *w) |
3585 | { |
3653 | { |
3586 | if (expect_false (ev_is_active (w))) |
3654 | if (expect_false (ev_is_active (w))) |