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 | * |
… | |
… | |
376 | # undef EV_USE_INOTIFY |
376 | # undef EV_USE_INOTIFY |
377 | # define EV_USE_INOTIFY 0 |
377 | # define EV_USE_INOTIFY 0 |
378 | #endif |
378 | #endif |
379 | |
379 | |
380 | #if !EV_USE_NANOSLEEP |
380 | #if !EV_USE_NANOSLEEP |
381 | # ifndef _WIN32 |
381 | /* hp-ux has it in sys/time.h, which we unconditionally include above */ |
|
|
382 | # if !defined(_WIN32) && !defined(__hpux) |
382 | # include <sys/select.h> |
383 | # include <sys/select.h> |
383 | # endif |
384 | # endif |
384 | #endif |
385 | #endif |
385 | |
386 | |
386 | #if EV_USE_INOTIFY |
387 | #if EV_USE_INOTIFY |
… | |
… | |
530 | |
531 | |
531 | static unsigned int noinline |
532 | static unsigned int noinline |
532 | ev_linux_version (void) |
533 | ev_linux_version (void) |
533 | { |
534 | { |
534 | #ifdef __linux |
535 | #ifdef __linux |
|
|
536 | unsigned int v = 0; |
535 | struct utsname buf; |
537 | struct utsname buf; |
536 | unsigned int v; |
|
|
537 | int i; |
538 | int i; |
538 | char *p = buf.release; |
539 | char *p = buf.release; |
539 | |
540 | |
540 | if (uname (&buf)) |
541 | if (uname (&buf)) |
541 | return 0; |
542 | return 0; |
… | |
… | |
591 | if (syserr_cb) |
592 | if (syserr_cb) |
592 | syserr_cb (msg); |
593 | syserr_cb (msg); |
593 | else |
594 | else |
594 | { |
595 | { |
595 | #if EV_AVOID_STDIO |
596 | #if EV_AVOID_STDIO |
596 | const char *err = strerror (errno); |
|
|
597 | |
|
|
598 | ev_printerr (msg); |
597 | ev_printerr (msg); |
599 | ev_printerr (": "); |
598 | ev_printerr (": "); |
600 | ev_printerr (err); |
599 | ev_printerr (strerror (errno)); |
601 | ev_printerr ("\n"); |
600 | ev_printerr ("\n"); |
602 | #else |
601 | #else |
603 | perror (msg); |
602 | perror (msg); |
604 | #endif |
603 | #endif |
605 | abort (); |
604 | abort (); |
… | |
… | |
639 | ptr = alloc (ptr, size); |
638 | ptr = alloc (ptr, size); |
640 | |
639 | |
641 | if (!ptr && size) |
640 | if (!ptr && size) |
642 | { |
641 | { |
643 | #if EV_AVOID_STDIO |
642 | #if EV_AVOID_STDIO |
644 | ev_printerr ("libev: memory allocation failed, aborting.\n"); |
643 | ev_printerr ("(libev) memory allocation failed, aborting.\n"); |
645 | #else |
644 | #else |
646 | fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size); |
645 | fprintf (stderr, "(libev) cannot allocate %ld bytes, aborting.", size); |
647 | #endif |
646 | #endif |
648 | abort (); |
647 | abort (); |
649 | } |
648 | } |
650 | |
649 | |
651 | return ptr; |
650 | return ptr; |
… | |
… | |
817 | select (0, 0, 0, 0, &tv); |
816 | select (0, 0, 0, 0, &tv); |
818 | #endif |
817 | #endif |
819 | } |
818 | } |
820 | } |
819 | } |
821 | |
820 | |
|
|
821 | inline_speed int |
|
|
822 | ev_timeout_to_ms (ev_tstamp timeout) |
|
|
823 | { |
|
|
824 | int ms = timeout * 1000. + .999999; |
|
|
825 | |
|
|
826 | return expect_true (ms) ? ms : timeout < 1e-6 ? 0 : 1; |
|
|
827 | } |
|
|
828 | |
822 | /*****************************************************************************/ |
829 | /*****************************************************************************/ |
823 | |
830 | |
824 | #define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */ |
831 | #define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */ |
825 | |
832 | |
826 | /* find a suitable new size for the given array, */ |
833 | /* find a suitable new size for the given array, */ |
… | |
… | |
967 | inline_size void |
974 | inline_size void |
968 | fd_reify (EV_P) |
975 | fd_reify (EV_P) |
969 | { |
976 | { |
970 | int i; |
977 | int i; |
971 | |
978 | |
|
|
979 | #if EV_SELECT_IS_WINSOCKET || EV_USE_IOCP |
|
|
980 | for (i = 0; i < fdchangecnt; ++i) |
|
|
981 | { |
|
|
982 | int fd = fdchanges [i]; |
|
|
983 | ANFD *anfd = anfds + fd; |
|
|
984 | |
|
|
985 | if (anfd->reify & EV__IOFDSET) |
|
|
986 | { |
|
|
987 | SOCKET handle = EV_FD_TO_WIN32_HANDLE (fd); |
|
|
988 | |
|
|
989 | if (handle != anfd->handle) |
|
|
990 | { |
|
|
991 | unsigned long arg; |
|
|
992 | |
|
|
993 | assert (("libev: only socket fds supported in this configuration", ioctlsocket (handle, FIONREAD, &arg) == 0)); |
|
|
994 | |
|
|
995 | /* handle changed, but fd didn't - we need to do it in two steps */ |
|
|
996 | backend_modify (EV_A_ fd, anfd->events, 0); |
|
|
997 | anfd->events = 0; |
|
|
998 | anfd->handle = handle; |
|
|
999 | } |
|
|
1000 | } |
|
|
1001 | } |
|
|
1002 | #endif |
|
|
1003 | |
972 | for (i = 0; i < fdchangecnt; ++i) |
1004 | for (i = 0; i < fdchangecnt; ++i) |
973 | { |
1005 | { |
974 | int fd = fdchanges [i]; |
1006 | int fd = fdchanges [i]; |
975 | ANFD *anfd = anfds + fd; |
1007 | ANFD *anfd = anfds + fd; |
976 | ev_io *w; |
1008 | ev_io *w; |
977 | |
1009 | |
978 | unsigned char o_events = anfd->events; |
1010 | unsigned char o_events = anfd->events; |
979 | unsigned char o_reify = anfd->reify; |
1011 | unsigned char o_reify = anfd->reify; |
980 | |
1012 | |
981 | anfd->reify = 0; |
1013 | anfd->reify = 0; |
982 | |
|
|
983 | #if EV_SELECT_IS_WINSOCKET || EV_USE_IOCP |
|
|
984 | if (o_reify & EV__IOFDSET) |
|
|
985 | { |
|
|
986 | unsigned long arg; |
|
|
987 | anfd->handle = EV_FD_TO_WIN32_HANDLE (fd); |
|
|
988 | assert (("libev: only socket fds supported in this configuration", ioctlsocket (anfd->handle, FIONREAD, &arg) == 0)); |
|
|
989 | printf ("oi %d %x\n", fd, anfd->handle);//D |
|
|
990 | } |
|
|
991 | #endif |
|
|
992 | |
1014 | |
993 | /*if (expect_true (o_reify & EV_ANFD_REIFY)) probably a deoptimisation */ |
1015 | /*if (expect_true (o_reify & EV_ANFD_REIFY)) probably a deoptimisation */ |
994 | { |
1016 | { |
995 | anfd->events = 0; |
1017 | anfd->events = 0; |
996 | |
1018 | |
… | |
… | |
1345 | char dummy; |
1367 | char dummy; |
1346 | /* see discussion in evpipe_write when you think this read should be recv in win32 */ |
1368 | /* see discussion in evpipe_write when you think this read should be recv in win32 */ |
1347 | read (evpipe [0], &dummy, 1); |
1369 | read (evpipe [0], &dummy, 1); |
1348 | } |
1370 | } |
1349 | |
1371 | |
|
|
1372 | #if EV_SIGNAL_ENABLE |
1350 | if (sig_pending) |
1373 | if (sig_pending) |
1351 | { |
1374 | { |
1352 | sig_pending = 0; |
1375 | sig_pending = 0; |
1353 | |
1376 | |
1354 | for (i = EV_NSIG - 1; i--; ) |
1377 | for (i = EV_NSIG - 1; i--; ) |
1355 | if (expect_false (signals [i].pending)) |
1378 | if (expect_false (signals [i].pending)) |
1356 | ev_feed_signal_event (EV_A_ i + 1); |
1379 | ev_feed_signal_event (EV_A_ i + 1); |
1357 | } |
1380 | } |
|
|
1381 | #endif |
1358 | |
1382 | |
1359 | #if EV_ASYNC_ENABLE |
1383 | #if EV_ASYNC_ENABLE |
1360 | if (async_pending) |
1384 | if (async_pending) |
1361 | { |
1385 | { |
1362 | async_pending = 0; |
1386 | async_pending = 0; |
… | |
… | |
1371 | #endif |
1395 | #endif |
1372 | } |
1396 | } |
1373 | |
1397 | |
1374 | /*****************************************************************************/ |
1398 | /*****************************************************************************/ |
1375 | |
1399 | |
|
|
1400 | void |
|
|
1401 | ev_feed_signal (int signum) |
|
|
1402 | { |
|
|
1403 | #if EV_MULTIPLICITY |
|
|
1404 | EV_P = signals [signum - 1].loop; |
|
|
1405 | |
|
|
1406 | if (!EV_A) |
|
|
1407 | return; |
|
|
1408 | #endif |
|
|
1409 | |
|
|
1410 | signals [signum - 1].pending = 1; |
|
|
1411 | evpipe_write (EV_A_ &sig_pending); |
|
|
1412 | } |
|
|
1413 | |
1376 | static void |
1414 | static void |
1377 | ev_sighandler (int signum) |
1415 | ev_sighandler (int signum) |
1378 | { |
1416 | { |
1379 | #if EV_MULTIPLICITY |
|
|
1380 | EV_P = signals [signum - 1].loop; |
|
|
1381 | #endif |
|
|
1382 | |
|
|
1383 | #ifdef _WIN32 |
1417 | #ifdef _WIN32 |
1384 | signal (signum, ev_sighandler); |
1418 | signal (signum, ev_sighandler); |
1385 | #endif |
1419 | #endif |
1386 | |
1420 | |
1387 | signals [signum - 1].pending = 1; |
1421 | ev_feed_signal (signum); |
1388 | evpipe_write (EV_A_ &sig_pending); |
|
|
1389 | } |
1422 | } |
1390 | |
1423 | |
1391 | void noinline |
1424 | void noinline |
1392 | ev_feed_signal_event (EV_P_ int signum) |
1425 | ev_feed_signal_event (EV_P_ int signum) |
1393 | { |
1426 | { |
… | |
… | |
1645 | static void noinline |
1678 | static void noinline |
1646 | loop_init (EV_P_ unsigned int flags) |
1679 | loop_init (EV_P_ unsigned int flags) |
1647 | { |
1680 | { |
1648 | if (!backend) |
1681 | if (!backend) |
1649 | { |
1682 | { |
|
|
1683 | origflags = flags; |
|
|
1684 | |
1650 | #if EV_USE_REALTIME |
1685 | #if EV_USE_REALTIME |
1651 | if (!have_realtime) |
1686 | if (!have_realtime) |
1652 | { |
1687 | { |
1653 | struct timespec ts; |
1688 | struct timespec ts; |
1654 | |
1689 | |
… | |
… | |
1699 | #endif |
1734 | #endif |
1700 | #if EV_USE_SIGNALFD |
1735 | #if EV_USE_SIGNALFD |
1701 | sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1; |
1736 | sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1; |
1702 | #endif |
1737 | #endif |
1703 | |
1738 | |
1704 | if (!(flags & 0x0000ffffU)) |
1739 | if (!(flags & EVBACKEND_MASK)) |
1705 | flags |= ev_recommended_backends (); |
1740 | flags |= ev_recommended_backends (); |
1706 | |
1741 | |
1707 | #if EV_USE_IOCP |
1742 | #if EV_USE_IOCP |
1708 | if (!backend && (flags & EVBACKEND_IOCP )) backend = iocp_init (EV_A_ flags); |
1743 | if (!backend && (flags & EVBACKEND_IOCP )) backend = iocp_init (EV_A_ flags); |
1709 | #endif |
1744 | #endif |
… | |
… | |
1731 | #endif |
1766 | #endif |
1732 | } |
1767 | } |
1733 | } |
1768 | } |
1734 | |
1769 | |
1735 | /* free up a loop structure */ |
1770 | /* free up a loop structure */ |
1736 | static void noinline |
1771 | void |
1737 | loop_destroy (EV_P) |
1772 | ev_loop_destroy (EV_P) |
1738 | { |
1773 | { |
1739 | int i; |
1774 | int i; |
|
|
1775 | |
|
|
1776 | #if EV_MULTIPLICITY |
|
|
1777 | /* mimic free (0) */ |
|
|
1778 | if (!EV_A) |
|
|
1779 | return; |
|
|
1780 | #endif |
|
|
1781 | |
|
|
1782 | #if EV_CLEANUP_ENABLE |
|
|
1783 | /* queue cleanup watchers (and execute them) */ |
|
|
1784 | if (expect_false (cleanupcnt)) |
|
|
1785 | { |
|
|
1786 | queue_events (EV_A_ (W *)cleanups, cleanupcnt, EV_CLEANUP); |
|
|
1787 | EV_INVOKE_PENDING; |
|
|
1788 | } |
|
|
1789 | #endif |
|
|
1790 | |
|
|
1791 | #if EV_CHILD_ENABLE |
|
|
1792 | if (ev_is_active (&childev)) |
|
|
1793 | { |
|
|
1794 | ev_ref (EV_A); /* child watcher */ |
|
|
1795 | ev_signal_stop (EV_A_ &childev); |
|
|
1796 | } |
|
|
1797 | #endif |
1740 | |
1798 | |
1741 | if (ev_is_active (&pipe_w)) |
1799 | if (ev_is_active (&pipe_w)) |
1742 | { |
1800 | { |
1743 | /*ev_ref (EV_A);*/ |
1801 | /*ev_ref (EV_A);*/ |
1744 | /*ev_io_stop (EV_A_ &pipe_w);*/ |
1802 | /*ev_io_stop (EV_A_ &pipe_w);*/ |
… | |
… | |
1805 | array_free (periodic, EMPTY); |
1863 | array_free (periodic, EMPTY); |
1806 | #endif |
1864 | #endif |
1807 | #if EV_FORK_ENABLE |
1865 | #if EV_FORK_ENABLE |
1808 | array_free (fork, EMPTY); |
1866 | array_free (fork, EMPTY); |
1809 | #endif |
1867 | #endif |
|
|
1868 | #if EV_CLEANUP_ENABLE |
|
|
1869 | array_free (cleanup, EMPTY); |
|
|
1870 | #endif |
1810 | array_free (prepare, EMPTY); |
1871 | array_free (prepare, EMPTY); |
1811 | array_free (check, EMPTY); |
1872 | array_free (check, EMPTY); |
1812 | #if EV_ASYNC_ENABLE |
1873 | #if EV_ASYNC_ENABLE |
1813 | array_free (async, EMPTY); |
1874 | array_free (async, EMPTY); |
1814 | #endif |
1875 | #endif |
1815 | |
1876 | |
1816 | backend = 0; |
1877 | backend = 0; |
|
|
1878 | |
|
|
1879 | #if EV_MULTIPLICITY |
|
|
1880 | if (ev_is_default_loop (EV_A)) |
|
|
1881 | #endif |
|
|
1882 | ev_default_loop_ptr = 0; |
|
|
1883 | #if EV_MULTIPLICITY |
|
|
1884 | else |
|
|
1885 | ev_free (EV_A); |
|
|
1886 | #endif |
1817 | } |
1887 | } |
1818 | |
1888 | |
1819 | #if EV_USE_INOTIFY |
1889 | #if EV_USE_INOTIFY |
1820 | inline_size void infy_fork (EV_P); |
1890 | inline_size void infy_fork (EV_P); |
1821 | #endif |
1891 | #endif |
… | |
… | |
1880 | loop_init (EV_A_ flags); |
1950 | loop_init (EV_A_ flags); |
1881 | |
1951 | |
1882 | if (ev_backend (EV_A)) |
1952 | if (ev_backend (EV_A)) |
1883 | return EV_A; |
1953 | return EV_A; |
1884 | |
1954 | |
|
|
1955 | ev_free (EV_A); |
1885 | return 0; |
1956 | return 0; |
1886 | } |
1957 | } |
1887 | |
1958 | |
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 */ |
1959 | #endif /* multiplicity */ |
1901 | |
1960 | |
1902 | #if EV_VERIFY |
1961 | #if EV_VERIFY |
1903 | static void noinline |
1962 | static void noinline |
1904 | verify_watcher (EV_P_ W w) |
1963 | verify_watcher (EV_P_ W w) |
… | |
… | |
1979 | #if EV_FORK_ENABLE |
2038 | #if EV_FORK_ENABLE |
1980 | assert (forkmax >= forkcnt); |
2039 | assert (forkmax >= forkcnt); |
1981 | array_verify (EV_A_ (W *)forks, forkcnt); |
2040 | array_verify (EV_A_ (W *)forks, forkcnt); |
1982 | #endif |
2041 | #endif |
1983 | |
2042 | |
|
|
2043 | #if EV_CLEANUP_ENABLE |
|
|
2044 | assert (cleanupmax >= cleanupcnt); |
|
|
2045 | array_verify (EV_A_ (W *)cleanups, cleanupcnt); |
|
|
2046 | #endif |
|
|
2047 | |
1984 | #if EV_ASYNC_ENABLE |
2048 | #if EV_ASYNC_ENABLE |
1985 | assert (asyncmax >= asynccnt); |
2049 | assert (asyncmax >= asynccnt); |
1986 | array_verify (EV_A_ (W *)asyncs, asynccnt); |
2050 | array_verify (EV_A_ (W *)asyncs, asynccnt); |
1987 | #endif |
2051 | #endif |
1988 | |
2052 | |
… | |
… | |
2006 | } |
2070 | } |
2007 | #endif |
2071 | #endif |
2008 | |
2072 | |
2009 | #if EV_MULTIPLICITY |
2073 | #if EV_MULTIPLICITY |
2010 | struct ev_loop * |
2074 | struct ev_loop * |
2011 | ev_default_loop_init (unsigned int flags) |
|
|
2012 | #else |
2075 | #else |
2013 | int |
2076 | int |
|
|
2077 | #endif |
2014 | ev_default_loop (unsigned int flags) |
2078 | ev_default_loop (unsigned int flags) |
2015 | #endif |
|
|
2016 | { |
2079 | { |
2017 | if (!ev_default_loop_ptr) |
2080 | if (!ev_default_loop_ptr) |
2018 | { |
2081 | { |
2019 | #if EV_MULTIPLICITY |
2082 | #if EV_MULTIPLICITY |
2020 | EV_P = ev_default_loop_ptr = &default_loop_struct; |
2083 | EV_P = ev_default_loop_ptr = &default_loop_struct; |
… | |
… | |
2039 | |
2102 | |
2040 | return ev_default_loop_ptr; |
2103 | return ev_default_loop_ptr; |
2041 | } |
2104 | } |
2042 | |
2105 | |
2043 | void |
2106 | void |
2044 | ev_default_destroy (void) |
2107 | ev_loop_fork (EV_P) |
2045 | { |
2108 | { |
2046 | #if EV_MULTIPLICITY |
|
|
2047 | EV_P = ev_default_loop_ptr; |
|
|
2048 | #endif |
|
|
2049 | |
|
|
2050 | ev_default_loop_ptr = 0; |
|
|
2051 | |
|
|
2052 | #if EV_CHILD_ENABLE |
|
|
2053 | ev_ref (EV_A); /* child watcher */ |
|
|
2054 | ev_signal_stop (EV_A_ &childev); |
|
|
2055 | #endif |
|
|
2056 | |
|
|
2057 | loop_destroy (EV_A); |
|
|
2058 | } |
|
|
2059 | |
|
|
2060 | void |
|
|
2061 | ev_default_fork (void) |
|
|
2062 | { |
|
|
2063 | #if EV_MULTIPLICITY |
|
|
2064 | EV_P = ev_default_loop_ptr; |
|
|
2065 | #endif |
|
|
2066 | |
|
|
2067 | postfork = 1; /* must be in line with ev_loop_fork */ |
2109 | postfork = 1; /* must be in line with ev_default_fork */ |
2068 | } |
2110 | } |
2069 | |
2111 | |
2070 | /*****************************************************************************/ |
2112 | /*****************************************************************************/ |
2071 | |
2113 | |
2072 | void |
2114 | void |
… | |
… | |
2094 | |
2136 | |
2095 | for (pri = NUMPRI; pri--; ) |
2137 | for (pri = NUMPRI; pri--; ) |
2096 | while (pendingcnt [pri]) |
2138 | while (pendingcnt [pri]) |
2097 | { |
2139 | { |
2098 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
2140 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
2099 | |
|
|
2100 | /*assert (("libev: non-pending watcher on pending list", p->w->pending));*/ |
|
|
2101 | /* ^ this is no longer true, as pending_w could be here */ |
|
|
2102 | |
2141 | |
2103 | p->w->pending = 0; |
2142 | p->w->pending = 0; |
2104 | EV_CB_INVOKE (p->w, p->events); |
2143 | EV_CB_INVOKE (p->w, p->events); |
2105 | EV_FREQUENT_CHECK; |
2144 | EV_FREQUENT_CHECK; |
2106 | } |
2145 | } |
… | |
… | |
2168 | feed_reverse_done (EV_A_ EV_TIMER); |
2207 | feed_reverse_done (EV_A_ EV_TIMER); |
2169 | } |
2208 | } |
2170 | } |
2209 | } |
2171 | |
2210 | |
2172 | #if EV_PERIODIC_ENABLE |
2211 | #if EV_PERIODIC_ENABLE |
|
|
2212 | |
|
|
2213 | inline_speed void |
|
|
2214 | periodic_recalc (EV_P_ ev_periodic *w) |
|
|
2215 | { |
|
|
2216 | /* TODO: use slow but potentially more correct incremental algo, */ |
|
|
2217 | /* also do not rely on ceil */ |
|
|
2218 | ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; |
|
|
2219 | } |
|
|
2220 | |
2173 | /* make periodics pending */ |
2221 | /* make periodics pending */ |
2174 | inline_size void |
2222 | inline_size void |
2175 | periodics_reify (EV_P) |
2223 | periodics_reify (EV_P) |
2176 | { |
2224 | { |
2177 | EV_FREQUENT_CHECK; |
2225 | EV_FREQUENT_CHECK; |
… | |
… | |
2196 | ANHE_at_cache (periodics [HEAP0]); |
2244 | ANHE_at_cache (periodics [HEAP0]); |
2197 | downheap (periodics, periodiccnt, HEAP0); |
2245 | downheap (periodics, periodiccnt, HEAP0); |
2198 | } |
2246 | } |
2199 | else if (w->interval) |
2247 | else if (w->interval) |
2200 | { |
2248 | { |
2201 | ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; |
2249 | periodic_recalc (EV_A_ w); |
|
|
2250 | |
2202 | /* if next trigger time is not sufficiently in the future, put it there */ |
2251 | /* if next trigger time is not sufficiently in the future, put it there */ |
2203 | /* this might happen because of floating point inexactness */ |
2252 | /* this might happen because of floating point inexactness */ |
2204 | if (ev_at (w) - ev_rt_now < TIME_EPSILON) |
2253 | if (ev_at (w) - ev_rt_now < TIME_EPSILON) |
2205 | { |
2254 | { |
2206 | ev_at (w) += w->interval; |
2255 | ev_at (w) += w->interval; |
… | |
… | |
2240 | ev_periodic *w = (ev_periodic *)ANHE_w (periodics [i]); |
2289 | ev_periodic *w = (ev_periodic *)ANHE_w (periodics [i]); |
2241 | |
2290 | |
2242 | if (w->reschedule_cb) |
2291 | if (w->reschedule_cb) |
2243 | ev_at (w) = w->reschedule_cb (w, ev_rt_now); |
2292 | ev_at (w) = w->reschedule_cb (w, ev_rt_now); |
2244 | else if (w->interval) |
2293 | else if (w->interval) |
2245 | ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; |
2294 | periodic_recalc (EV_A_ w); |
2246 | |
2295 | |
2247 | ANHE_at_cache (periodics [i]); |
2296 | ANHE_at_cache (periodics [i]); |
2248 | } |
2297 | } |
2249 | |
2298 | |
2250 | reheap (periodics, periodiccnt); |
2299 | reheap (periodics, periodiccnt); |
… | |
… | |
2742 | if (w->reschedule_cb) |
2791 | if (w->reschedule_cb) |
2743 | ev_at (w) = w->reschedule_cb (w, ev_rt_now); |
2792 | ev_at (w) = w->reschedule_cb (w, ev_rt_now); |
2744 | else if (w->interval) |
2793 | else if (w->interval) |
2745 | { |
2794 | { |
2746 | assert (("libev: ev_periodic_start called with negative interval value", w->interval >= 0.)); |
2795 | assert (("libev: ev_periodic_start called with negative interval value", w->interval >= 0.)); |
2747 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
2796 | periodic_recalc (EV_A_ w); |
2748 | ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; |
|
|
2749 | } |
2797 | } |
2750 | else |
2798 | else |
2751 | ev_at (w) = w->offset; |
2799 | ev_at (w) = w->offset; |
2752 | |
2800 | |
2753 | EV_FREQUENT_CHECK; |
2801 | EV_FREQUENT_CHECK; |
… | |
… | |
2874 | sa.sa_handler = ev_sighandler; |
2922 | sa.sa_handler = ev_sighandler; |
2875 | sigfillset (&sa.sa_mask); |
2923 | sigfillset (&sa.sa_mask); |
2876 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
2924 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
2877 | sigaction (w->signum, &sa, 0); |
2925 | sigaction (w->signum, &sa, 0); |
2878 | |
2926 | |
|
|
2927 | if (origflags & EVFLAG_NOSIGMASK) |
|
|
2928 | { |
2879 | sigemptyset (&sa.sa_mask); |
2929 | sigemptyset (&sa.sa_mask); |
2880 | sigaddset (&sa.sa_mask, w->signum); |
2930 | sigaddset (&sa.sa_mask, w->signum); |
2881 | sigprocmask (SIG_UNBLOCK, &sa.sa_mask, 0); |
2931 | sigprocmask (SIG_UNBLOCK, &sa.sa_mask, 0); |
|
|
2932 | } |
2882 | #endif |
2933 | #endif |
2883 | } |
2934 | } |
2884 | |
2935 | |
2885 | EV_FREQUENT_CHECK; |
2936 | EV_FREQUENT_CHECK; |
2886 | } |
2937 | } |
… | |
… | |
3578 | |
3629 | |
3579 | EV_FREQUENT_CHECK; |
3630 | EV_FREQUENT_CHECK; |
3580 | } |
3631 | } |
3581 | #endif |
3632 | #endif |
3582 | |
3633 | |
|
|
3634 | #if EV_CLEANUP_ENABLE |
|
|
3635 | void |
|
|
3636 | ev_cleanup_start (EV_P_ ev_cleanup *w) |
|
|
3637 | { |
|
|
3638 | if (expect_false (ev_is_active (w))) |
|
|
3639 | return; |
|
|
3640 | |
|
|
3641 | EV_FREQUENT_CHECK; |
|
|
3642 | |
|
|
3643 | ev_start (EV_A_ (W)w, ++cleanupcnt); |
|
|
3644 | array_needsize (ev_cleanup *, cleanups, cleanupmax, cleanupcnt, EMPTY2); |
|
|
3645 | cleanups [cleanupcnt - 1] = w; |
|
|
3646 | |
|
|
3647 | /* cleanup watchers should never keep a refcount on the loop */ |
|
|
3648 | ev_unref (EV_A); |
|
|
3649 | EV_FREQUENT_CHECK; |
|
|
3650 | } |
|
|
3651 | |
|
|
3652 | void |
|
|
3653 | ev_cleanup_stop (EV_P_ ev_cleanup *w) |
|
|
3654 | { |
|
|
3655 | clear_pending (EV_A_ (W)w); |
|
|
3656 | if (expect_false (!ev_is_active (w))) |
|
|
3657 | return; |
|
|
3658 | |
|
|
3659 | EV_FREQUENT_CHECK; |
|
|
3660 | ev_ref (EV_A); |
|
|
3661 | |
|
|
3662 | { |
|
|
3663 | int active = ev_active (w); |
|
|
3664 | |
|
|
3665 | cleanups [active - 1] = cleanups [--cleanupcnt]; |
|
|
3666 | ev_active (cleanups [active - 1]) = active; |
|
|
3667 | } |
|
|
3668 | |
|
|
3669 | ev_stop (EV_A_ (W)w); |
|
|
3670 | |
|
|
3671 | EV_FREQUENT_CHECK; |
|
|
3672 | } |
|
|
3673 | #endif |
|
|
3674 | |
3583 | #if EV_ASYNC_ENABLE |
3675 | #if EV_ASYNC_ENABLE |
3584 | void |
3676 | void |
3585 | ev_async_start (EV_P_ ev_async *w) |
3677 | ev_async_start (EV_P_ ev_async *w) |
3586 | { |
3678 | { |
3587 | if (expect_false (ev_is_active (w))) |
3679 | if (expect_false (ev_is_active (w))) |