… | |
… | |
4 | * Copyright (c) 2007,2008,2009,2010,2011 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 | * |
10 | * 1. Redistributions of source code must retain the above copyright notice, |
10 | * 1. Redistributions of source code must retain the above copyright notice, |
11 | * this list of conditions and the following disclaimer. |
11 | * this list of conditions and the following disclaimer. |
12 | * |
12 | * |
13 | * 2. Redistributions in binary form must reproduce the above copyright |
13 | * 2. Redistributions in binary form must reproduce the above copyright |
14 | * notice, this list of conditions and the following disclaimer in the |
14 | * notice, this list of conditions and the following disclaimer in the |
15 | * documentation and/or other materials provided with the distribution. |
15 | * documentation and/or other materials provided with the distribution. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
18 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- |
18 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- |
19 | * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
19 | * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
20 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- |
20 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- |
21 | * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
21 | * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
… | |
… | |
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 |
… | |
… | |
815 | select (0, 0, 0, 0, &tv); |
816 | select (0, 0, 0, 0, &tv); |
816 | #endif |
817 | #endif |
817 | } |
818 | } |
818 | } |
819 | } |
819 | |
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 | |
820 | /*****************************************************************************/ |
829 | /*****************************************************************************/ |
821 | |
830 | |
822 | #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 */ |
823 | |
832 | |
824 | /* find a suitable new size for the given array, */ |
833 | /* find a suitable new size for the given array, */ |
… | |
… | |
965 | inline_size void |
974 | inline_size void |
966 | fd_reify (EV_P) |
975 | fd_reify (EV_P) |
967 | { |
976 | { |
968 | int i; |
977 | int i; |
969 | |
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 | |
970 | for (i = 0; i < fdchangecnt; ++i) |
1004 | for (i = 0; i < fdchangecnt; ++i) |
971 | { |
1005 | { |
972 | int fd = fdchanges [i]; |
1006 | int fd = fdchanges [i]; |
973 | ANFD *anfd = anfds + fd; |
1007 | ANFD *anfd = anfds + fd; |
974 | ev_io *w; |
1008 | ev_io *w; |
975 | |
1009 | |
976 | unsigned char o_events = anfd->events; |
1010 | unsigned char o_events = anfd->events; |
977 | unsigned char o_reify = anfd->reify; |
1011 | unsigned char o_reify = anfd->reify; |
978 | |
1012 | |
979 | anfd->reify = 0; |
1013 | anfd->reify = 0; |
980 | |
|
|
981 | #if EV_SELECT_IS_WINSOCKET || EV_USE_IOCP |
|
|
982 | if (o_reify & EV__IOFDSET) |
|
|
983 | { |
|
|
984 | unsigned long arg; |
|
|
985 | anfd->handle = EV_FD_TO_WIN32_HANDLE (fd); |
|
|
986 | assert (("libev: only socket fds supported in this configuration", ioctlsocket (anfd->handle, FIONREAD, &arg) == 0)); |
|
|
987 | printf ("oi %d %x\n", fd, anfd->handle);//D |
|
|
988 | } |
|
|
989 | #endif |
|
|
990 | |
1014 | |
991 | /*if (expect_true (o_reify & EV_ANFD_REIFY)) probably a deoptimisation */ |
1015 | /*if (expect_true (o_reify & EV_ANFD_REIFY)) probably a deoptimisation */ |
992 | { |
1016 | { |
993 | anfd->events = 0; |
1017 | anfd->events = 0; |
994 | |
1018 | |
… | |
… | |
1343 | char dummy; |
1367 | char dummy; |
1344 | /* 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 */ |
1345 | read (evpipe [0], &dummy, 1); |
1369 | read (evpipe [0], &dummy, 1); |
1346 | } |
1370 | } |
1347 | |
1371 | |
|
|
1372 | #if EV_SIGNAL_ENABLE |
1348 | if (sig_pending) |
1373 | if (sig_pending) |
1349 | { |
1374 | { |
1350 | sig_pending = 0; |
1375 | sig_pending = 0; |
1351 | |
1376 | |
1352 | for (i = EV_NSIG - 1; i--; ) |
1377 | for (i = EV_NSIG - 1; i--; ) |
1353 | if (expect_false (signals [i].pending)) |
1378 | if (expect_false (signals [i].pending)) |
1354 | ev_feed_signal_event (EV_A_ i + 1); |
1379 | ev_feed_signal_event (EV_A_ i + 1); |
1355 | } |
1380 | } |
|
|
1381 | #endif |
1356 | |
1382 | |
1357 | #if EV_ASYNC_ENABLE |
1383 | #if EV_ASYNC_ENABLE |
1358 | if (async_pending) |
1384 | if (async_pending) |
1359 | { |
1385 | { |
1360 | async_pending = 0; |
1386 | async_pending = 0; |
… | |
… | |
2181 | feed_reverse_done (EV_A_ EV_TIMER); |
2207 | feed_reverse_done (EV_A_ EV_TIMER); |
2182 | } |
2208 | } |
2183 | } |
2209 | } |
2184 | |
2210 | |
2185 | #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 | |
2186 | /* make periodics pending */ |
2221 | /* make periodics pending */ |
2187 | inline_size void |
2222 | inline_size void |
2188 | periodics_reify (EV_P) |
2223 | periodics_reify (EV_P) |
2189 | { |
2224 | { |
2190 | EV_FREQUENT_CHECK; |
2225 | EV_FREQUENT_CHECK; |
… | |
… | |
2209 | ANHE_at_cache (periodics [HEAP0]); |
2244 | ANHE_at_cache (periodics [HEAP0]); |
2210 | downheap (periodics, periodiccnt, HEAP0); |
2245 | downheap (periodics, periodiccnt, HEAP0); |
2211 | } |
2246 | } |
2212 | else if (w->interval) |
2247 | else if (w->interval) |
2213 | { |
2248 | { |
2214 | ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; |
2249 | periodic_recalc (EV_A_ w); |
|
|
2250 | |
2215 | /* 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 */ |
2216 | /* this might happen because of floating point inexactness */ |
2252 | /* this might happen because of floating point inexactness */ |
2217 | if (ev_at (w) - ev_rt_now < TIME_EPSILON) |
2253 | if (ev_at (w) - ev_rt_now < TIME_EPSILON) |
2218 | { |
2254 | { |
2219 | ev_at (w) += w->interval; |
2255 | ev_at (w) += w->interval; |
… | |
… | |
2253 | ev_periodic *w = (ev_periodic *)ANHE_w (periodics [i]); |
2289 | ev_periodic *w = (ev_periodic *)ANHE_w (periodics [i]); |
2254 | |
2290 | |
2255 | if (w->reschedule_cb) |
2291 | if (w->reschedule_cb) |
2256 | ev_at (w) = w->reschedule_cb (w, ev_rt_now); |
2292 | ev_at (w) = w->reschedule_cb (w, ev_rt_now); |
2257 | else if (w->interval) |
2293 | else if (w->interval) |
2258 | ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; |
2294 | periodic_recalc (EV_A_ w); |
2259 | |
2295 | |
2260 | ANHE_at_cache (periodics [i]); |
2296 | ANHE_at_cache (periodics [i]); |
2261 | } |
2297 | } |
2262 | |
2298 | |
2263 | reheap (periodics, periodiccnt); |
2299 | reheap (periodics, periodiccnt); |
… | |
… | |
2755 | if (w->reschedule_cb) |
2791 | if (w->reschedule_cb) |
2756 | ev_at (w) = w->reschedule_cb (w, ev_rt_now); |
2792 | ev_at (w) = w->reschedule_cb (w, ev_rt_now); |
2757 | else if (w->interval) |
2793 | else if (w->interval) |
2758 | { |
2794 | { |
2759 | 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.)); |
2760 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
2796 | periodic_recalc (EV_A_ w); |
2761 | ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; |
|
|
2762 | } |
2797 | } |
2763 | else |
2798 | else |
2764 | ev_at (w) = w->offset; |
2799 | ev_at (w) = w->offset; |
2765 | |
2800 | |
2766 | EV_FREQUENT_CHECK; |
2801 | EV_FREQUENT_CHECK; |
… | |
… | |
3043 | if (!pend || pend == path) |
3078 | if (!pend || pend == path) |
3044 | break; |
3079 | break; |
3045 | |
3080 | |
3046 | *pend = 0; |
3081 | *pend = 0; |
3047 | w->wd = inotify_add_watch (fs_fd, path, mask); |
3082 | w->wd = inotify_add_watch (fs_fd, path, mask); |
3048 | } |
3083 | } |
3049 | while (w->wd < 0 && (errno == ENOENT || errno == EACCES)); |
3084 | while (w->wd < 0 && (errno == ENOENT || errno == EACCES)); |
3050 | } |
3085 | } |
3051 | } |
3086 | } |
3052 | |
3087 | |
3053 | if (w->wd >= 0) |
3088 | if (w->wd >= 0) |