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 |
… | |
… | |
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 |
… | |
… | |
1736 | void |
1771 | void |
1737 | ev_loop_destroy (EV_P) |
1772 | ev_loop_destroy (EV_P) |
1738 | { |
1773 | { |
1739 | int i; |
1774 | int i; |
1740 | |
1775 | |
|
|
1776 | #if EV_MULTIPLICITY |
1741 | /* mimic free (0) */ |
1777 | /* mimic free (0) */ |
1742 | if (!EV_A) |
1778 | if (!EV_A) |
1743 | return; |
1779 | return; |
|
|
1780 | #endif |
1744 | |
1781 | |
1745 | #if EV_CLEANUP_ENABLE |
1782 | #if EV_CLEANUP_ENABLE |
1746 | /* queue cleanup watchers (and execute them) */ |
1783 | /* queue cleanup watchers (and execute them) */ |
1747 | if (expect_false (cleanupcnt)) |
1784 | if (expect_false (cleanupcnt)) |
1748 | { |
1785 | { |
… | |
… | |
2100 | for (pri = NUMPRI; pri--; ) |
2137 | for (pri = NUMPRI; pri--; ) |
2101 | while (pendingcnt [pri]) |
2138 | while (pendingcnt [pri]) |
2102 | { |
2139 | { |
2103 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
2140 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
2104 | |
2141 | |
2105 | /*assert (("libev: non-pending watcher on pending list", p->w->pending));*/ |
|
|
2106 | /* ^ this is no longer true, as pending_w could be here */ |
|
|
2107 | |
|
|
2108 | p->w->pending = 0; |
2142 | p->w->pending = 0; |
2109 | EV_CB_INVOKE (p->w, p->events); |
2143 | EV_CB_INVOKE (p->w, p->events); |
2110 | EV_FREQUENT_CHECK; |
2144 | EV_FREQUENT_CHECK; |
2111 | } |
2145 | } |
2112 | } |
2146 | } |
… | |
… | |
2173 | feed_reverse_done (EV_A_ EV_TIMER); |
2207 | feed_reverse_done (EV_A_ EV_TIMER); |
2174 | } |
2208 | } |
2175 | } |
2209 | } |
2176 | |
2210 | |
2177 | #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 | |
2178 | /* make periodics pending */ |
2221 | /* make periodics pending */ |
2179 | inline_size void |
2222 | inline_size void |
2180 | periodics_reify (EV_P) |
2223 | periodics_reify (EV_P) |
2181 | { |
2224 | { |
2182 | EV_FREQUENT_CHECK; |
2225 | EV_FREQUENT_CHECK; |
… | |
… | |
2201 | ANHE_at_cache (periodics [HEAP0]); |
2244 | ANHE_at_cache (periodics [HEAP0]); |
2202 | downheap (periodics, periodiccnt, HEAP0); |
2245 | downheap (periodics, periodiccnt, HEAP0); |
2203 | } |
2246 | } |
2204 | else if (w->interval) |
2247 | else if (w->interval) |
2205 | { |
2248 | { |
2206 | ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; |
2249 | periodic_recalc (EV_A_ w); |
|
|
2250 | |
2207 | /* 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 */ |
2208 | /* this might happen because of floating point inexactness */ |
2252 | /* this might happen because of floating point inexactness */ |
2209 | if (ev_at (w) - ev_rt_now < TIME_EPSILON) |
2253 | if (ev_at (w) - ev_rt_now < TIME_EPSILON) |
2210 | { |
2254 | { |
2211 | ev_at (w) += w->interval; |
2255 | ev_at (w) += w->interval; |
… | |
… | |
2245 | ev_periodic *w = (ev_periodic *)ANHE_w (periodics [i]); |
2289 | ev_periodic *w = (ev_periodic *)ANHE_w (periodics [i]); |
2246 | |
2290 | |
2247 | if (w->reschedule_cb) |
2291 | if (w->reschedule_cb) |
2248 | ev_at (w) = w->reschedule_cb (w, ev_rt_now); |
2292 | ev_at (w) = w->reschedule_cb (w, ev_rt_now); |
2249 | else if (w->interval) |
2293 | else if (w->interval) |
2250 | ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; |
2294 | periodic_recalc (EV_A_ w); |
2251 | |
2295 | |
2252 | ANHE_at_cache (periodics [i]); |
2296 | ANHE_at_cache (periodics [i]); |
2253 | } |
2297 | } |
2254 | |
2298 | |
2255 | reheap (periodics, periodiccnt); |
2299 | reheap (periodics, periodiccnt); |
… | |
… | |
2747 | if (w->reschedule_cb) |
2791 | if (w->reschedule_cb) |
2748 | ev_at (w) = w->reschedule_cb (w, ev_rt_now); |
2792 | ev_at (w) = w->reschedule_cb (w, ev_rt_now); |
2749 | else if (w->interval) |
2793 | else if (w->interval) |
2750 | { |
2794 | { |
2751 | 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.)); |
2752 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
2796 | periodic_recalc (EV_A_ w); |
2753 | ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; |
|
|
2754 | } |
2797 | } |
2755 | else |
2798 | else |
2756 | ev_at (w) = w->offset; |
2799 | ev_at (w) = w->offset; |
2757 | |
2800 | |
2758 | EV_FREQUENT_CHECK; |
2801 | EV_FREQUENT_CHECK; |
… | |
… | |
2879 | sa.sa_handler = ev_sighandler; |
2922 | sa.sa_handler = ev_sighandler; |
2880 | sigfillset (&sa.sa_mask); |
2923 | sigfillset (&sa.sa_mask); |
2881 | 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 */ |
2882 | sigaction (w->signum, &sa, 0); |
2925 | sigaction (w->signum, &sa, 0); |
2883 | |
2926 | |
|
|
2927 | if (origflags & EVFLAG_NOSIGMASK) |
|
|
2928 | { |
2884 | sigemptyset (&sa.sa_mask); |
2929 | sigemptyset (&sa.sa_mask); |
2885 | sigaddset (&sa.sa_mask, w->signum); |
2930 | sigaddset (&sa.sa_mask, w->signum); |
2886 | sigprocmask (SIG_UNBLOCK, &sa.sa_mask, 0); |
2931 | sigprocmask (SIG_UNBLOCK, &sa.sa_mask, 0); |
|
|
2932 | } |
2887 | #endif |
2933 | #endif |
2888 | } |
2934 | } |
2889 | |
2935 | |
2890 | EV_FREQUENT_CHECK; |
2936 | EV_FREQUENT_CHECK; |
2891 | } |
2937 | } |