ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev.c
(Generate patch)

Comparing libev/ev.c (file contents):
Revision 1.365 by root, Sun Oct 31 22:01:20 2010 UTC vs.
Revision 1.372 by root, Wed Feb 16 08:02:50 2011 UTC

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 *
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
821inline_speed int
822ev_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, */
965inline_size void 974inline_size void
966fd_reify (EV_P) 975fd_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;
1369#endif 1395#endif
1370} 1396}
1371 1397
1372/*****************************************************************************/ 1398/*****************************************************************************/
1373 1399
1400void
1401ev_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
1374static void 1414static void
1375ev_sighandler (int signum) 1415ev_sighandler (int signum)
1376{ 1416{
1377#if EV_MULTIPLICITY
1378 EV_P = signals [signum - 1].loop;
1379#endif
1380
1381#ifdef _WIN32 1417#ifdef _WIN32
1382 signal (signum, ev_sighandler); 1418 signal (signum, ev_sighandler);
1383#endif 1419#endif
1384 1420
1385 signals [signum - 1].pending = 1; 1421 ev_feed_signal (signum);
1386 evpipe_write (EV_A_ &sig_pending);
1387} 1422}
1388 1423
1389void noinline 1424void noinline
1390ev_feed_signal_event (EV_P_ int signum) 1425ev_feed_signal_event (EV_P_ int signum)
1391{ 1426{
1643static void noinline 1678static void noinline
1644loop_init (EV_P_ unsigned int flags) 1679loop_init (EV_P_ unsigned int flags)
1645{ 1680{
1646 if (!backend) 1681 if (!backend)
1647 { 1682 {
1683 origflags = flags;
1684
1648#if EV_USE_REALTIME 1685#if EV_USE_REALTIME
1649 if (!have_realtime) 1686 if (!have_realtime)
1650 { 1687 {
1651 struct timespec ts; 1688 struct timespec ts;
1652 1689
1697#endif 1734#endif
1698#if EV_USE_SIGNALFD 1735#if EV_USE_SIGNALFD
1699 sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1; 1736 sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1;
1700#endif 1737#endif
1701 1738
1702 if (!(flags & 0x0000ffffU)) 1739 if (!(flags & EVBACKEND_MASK))
1703 flags |= ev_recommended_backends (); 1740 flags |= ev_recommended_backends ();
1704 1741
1705#if EV_USE_IOCP 1742#if EV_USE_IOCP
1706 if (!backend && (flags & EVBACKEND_IOCP )) backend = iocp_init (EV_A_ flags); 1743 if (!backend && (flags & EVBACKEND_IOCP )) backend = iocp_init (EV_A_ flags);
1707#endif 1744#endif
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
2213inline_speed void
2214periodic_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 */
2179inline_size void 2222inline_size void
2180periodics_reify (EV_P) 2223periodics_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}
3032 if (!pend || pend == path) 3078 if (!pend || pend == path)
3033 break; 3079 break;
3034 3080
3035 *pend = 0; 3081 *pend = 0;
3036 w->wd = inotify_add_watch (fs_fd, path, mask); 3082 w->wd = inotify_add_watch (fs_fd, path, mask);
3037 } 3083 }
3038 while (w->wd < 0 && (errno == ENOENT || errno == EACCES)); 3084 while (w->wd < 0 && (errno == ENOENT || errno == EACCES));
3039 } 3085 }
3040 } 3086 }
3041 3087
3042 if (w->wd >= 0) 3088 if (w->wd >= 0)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines