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

Comparing libev/ev.c (file contents):
Revision 1.306 by root, Sun Jul 19 06:35:25 2009 UTC vs.
Revision 1.313 by root, Wed Aug 19 23:44:51 2009 UTC

187#endif 187#endif
188 188
189/* this block tries to deduce configuration from header-defined symbols and defaults */ 189/* this block tries to deduce configuration from header-defined symbols and defaults */
190 190
191/* try to deduce the maximum number of signals on this platform */ 191/* try to deduce the maximum number of signals on this platform */
192/* one some platforms, NSIG is one too large. we do not bother */
193#if defined (EV_NSIG) 192#if defined (EV_NSIG)
194/* use what's provided */ 193/* use what's provided */
195#elif defined (NSIG) 194#elif defined (NSIG)
196# define EV_NSIG (NSIG) 195# define EV_NSIG (NSIG)
197#elif defined(_NSIG) 196#elif defined(_NSIG)
387# include <stdint.h> 386# include <stdint.h>
388# ifndef EFD_NONBLOCK 387# ifndef EFD_NONBLOCK
389# define EFD_NONBLOCK O_NONBLOCK 388# define EFD_NONBLOCK O_NONBLOCK
390# endif 389# endif
391# ifndef EFD_CLOEXEC 390# ifndef EFD_CLOEXEC
391# ifdef O_CLOEXEC
392# define EFD_CLOEXEC O_CLOEXEC 392# define EFD_CLOEXEC O_CLOEXEC
393# else
394# define EFD_CLOEXEC 02000000
395# endif
393# endif 396# endif
394# ifdef __cplusplus 397# ifdef __cplusplus
395extern "C" { 398extern "C" {
396# endif 399# endif
397int eventfd (unsigned int initval, int flags); 400int eventfd (unsigned int initval, int flags);
473 476
474#if EV_USE_MONOTONIC 477#if EV_USE_MONOTONIC
475static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 478static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
476#endif 479#endif
477 480
481#ifndef EV_FD_TO_WIN32_HANDLE
482# define EV_FD_TO_WIN32_HANDLE(fd) _get_osfhandle (fd)
483#endif
484#ifndef EV_WIN32_HANDLE_TO_FD
485# define EV_WIN32_HANDLE_TO_FD(handle) _open_osfhandle (fd, 0)
486#endif
487#ifndef EV_WIN32_CLOSE_FD
488# define EV_WIN32_CLOSE_FD(fd) close (fd)
489#endif
490
478#ifdef _WIN32 491#ifdef _WIN32
479# include "ev_win32.c" 492# include "ev_win32.c"
480#endif 493#endif
481 494
482/*****************************************************************************/ 495/*****************************************************************************/
873 886
874#if EV_SELECT_IS_WINSOCKET 887#if EV_SELECT_IS_WINSOCKET
875 if (events) 888 if (events)
876 { 889 {
877 unsigned long arg; 890 unsigned long arg;
878 #ifdef EV_FD_TO_WIN32_HANDLE
879 anfd->handle = EV_FD_TO_WIN32_HANDLE (fd); 891 anfd->handle = EV_FD_TO_WIN32_HANDLE (fd);
880 #else
881 anfd->handle = _get_osfhandle (fd);
882 #endif
883 assert (("libev: only socket fds supported in this configuration", ioctlsocket (anfd->handle, FIONREAD, &arg) == 0)); 892 assert (("libev: only socket fds supported in this configuration", ioctlsocket (anfd->handle, FIONREAD, &arg) == 0));
884 } 893 }
885#endif 894#endif
886 895
887 { 896 {
958 967
959 for (fd = anfdmax; fd--; ) 968 for (fd = anfdmax; fd--; )
960 if (anfds [fd].events) 969 if (anfds [fd].events)
961 { 970 {
962 fd_kill (EV_A_ fd); 971 fd_kill (EV_A_ fd);
963 return; 972 break;
964 } 973 }
965} 974}
966 975
967/* usually called after fork if backend needs to re-arm all fds from scratch */ 976/* usually called after fork if backend needs to re-arm all fds from scratch */
968static void noinline 977static void noinline
1058 1067
1059 for (;;) 1068 for (;;)
1060 { 1069 {
1061 int c = k << 1; 1070 int c = k << 1;
1062 1071
1063 if (c > N + HEAP0 - 1) 1072 if (c >= N + HEAP0)
1064 break; 1073 break;
1065 1074
1066 c += c + 1 < N + HEAP0 && ANHE_at (heap [c]) > ANHE_at (heap [c + 1]) 1075 c += c + 1 < N + HEAP0 && ANHE_at (heap [c]) > ANHE_at (heap [c + 1])
1067 ? 1 : 0; 1076 ? 1 : 0;
1068 1077
1104 1113
1105/* move an element suitably so it is in a correct place */ 1114/* move an element suitably so it is in a correct place */
1106inline_size void 1115inline_size void
1107adjustheap (ANHE *heap, int N, int k) 1116adjustheap (ANHE *heap, int N, int k)
1108{ 1117{
1109 if (k > HEAP0 && ANHE_at (heap [HPARENT (k)]) >= ANHE_at (heap [k])) 1118 if (k > HEAP0 && ANHE_at (heap [k]) <= ANHE_at (heap [HPARENT (k)]))
1110 upheap (heap, k); 1119 upheap (heap, k);
1111 else 1120 else
1112 downheap (heap, N, k); 1121 downheap (heap, N, k);
1113} 1122}
1114 1123
1127/*****************************************************************************/ 1136/*****************************************************************************/
1128 1137
1129/* associate signal watchers to a signal signal */ 1138/* associate signal watchers to a signal signal */
1130typedef struct 1139typedef struct
1131{ 1140{
1141 EV_ATOMIC_T pending;
1132#if EV_MULTIPLICITY 1142#if EV_MULTIPLICITY
1133 EV_P; 1143 EV_P;
1134#endif 1144#endif
1135 WL head; 1145 WL head;
1136 EV_ATOMIC_T gotsig;
1137} ANSIG; 1146} ANSIG;
1138 1147
1139static ANSIG signals [EV_NSIG - 1]; 1148static ANSIG signals [EV_NSIG - 1];
1140static EV_ATOMIC_T gotsig;
1141 1149
1142/*****************************************************************************/ 1150/*****************************************************************************/
1143 1151
1144/* used to prepare libev internal fd's */ 1152/* used to prepare libev internal fd's */
1145/* this is not fork-safe */ 1153/* this is not fork-safe */
1213/* called whenever the libev signal pipe */ 1221/* called whenever the libev signal pipe */
1214/* got some events (signal, async) */ 1222/* got some events (signal, async) */
1215static void 1223static void
1216pipecb (EV_P_ ev_io *iow, int revents) 1224pipecb (EV_P_ ev_io *iow, int revents)
1217{ 1225{
1226 int i;
1227
1218#if EV_USE_EVENTFD 1228#if EV_USE_EVENTFD
1219 if (evfd >= 0) 1229 if (evfd >= 0)
1220 { 1230 {
1221 uint64_t counter; 1231 uint64_t counter;
1222 read (evfd, &counter, sizeof (uint64_t)); 1232 read (evfd, &counter, sizeof (uint64_t));
1226 { 1236 {
1227 char dummy; 1237 char dummy;
1228 read (evpipe [0], &dummy, 1); 1238 read (evpipe [0], &dummy, 1);
1229 } 1239 }
1230 1240
1231 if (gotsig && ev_is_default_loop (EV_A)) 1241 if (sig_pending)
1232 { 1242 {
1233 int signum; 1243 sig_pending = 0;
1234 gotsig = 0;
1235 1244
1236 for (signum = EV_NSIG - 1; signum--; ) 1245 for (i = EV_NSIG - 1; i--; )
1237 if (signals [signum].gotsig) 1246 if (expect_false (signals [i].pending))
1238 ev_feed_signal_event (EV_A_ signum + 1); 1247 ev_feed_signal_event (EV_A_ i + 1);
1239 } 1248 }
1240 1249
1241#if EV_ASYNC_ENABLE 1250#if EV_ASYNC_ENABLE
1242 if (gotasync) 1251 if (async_pending)
1243 { 1252 {
1244 int i; 1253 async_pending = 0;
1245 gotasync = 0;
1246 1254
1247 for (i = asynccnt; i--; ) 1255 for (i = asynccnt; i--; )
1248 if (asyncs [i]->sent) 1256 if (asyncs [i]->sent)
1249 { 1257 {
1250 asyncs [i]->sent = 0; 1258 asyncs [i]->sent = 0;
1265 1273
1266#if _WIN32 1274#if _WIN32
1267 signal (signum, ev_sighandler); 1275 signal (signum, ev_sighandler);
1268#endif 1276#endif
1269 1277
1270 signals [signum - 1].gotsig = 1; 1278 signals [signum - 1].pending = 1;
1271 evpipe_write (EV_A_ &gotsig); 1279 evpipe_write (EV_A_ &sig_pending);
1272} 1280}
1273 1281
1274void noinline 1282void noinline
1275ev_feed_signal_event (EV_P_ int signum) 1283ev_feed_signal_event (EV_P_ int signum)
1276{ 1284{
1277 WL w; 1285 WL w;
1278 1286
1287 if (expect_false (signum <= 0 || signum > EV_NSIG))
1288 return;
1289
1290 --signum;
1291
1279#if EV_MULTIPLICITY 1292#if EV_MULTIPLICITY
1280 assert (("libev: feeding signal events is only supported in the default loop", loop == ev_default_loop_ptr)); 1293 /* it is permissible to try to feed a signal to the wrong loop */
1281#endif 1294 /* or, likely more useful, feeding a signal nobody is waiting for */
1282 1295
1283 if (signum <= 0 || signum > EV_NSIG) 1296 if (expect_false (signals [signum].loop != EV_A))
1284 return; 1297 return;
1298#endif
1285 1299
1286 --signum;
1287
1288 signals [signum].gotsig = 0; 1300 signals [signum].pending = 0;
1289 1301
1290 for (w = signals [signum].head; w; w = w->next) 1302 for (w = signals [signum].head; w; w = w->next)
1291 ev_feed_event (EV_A_ (W)w, EV_SIGNAL); 1303 ev_feed_event (EV_A_ (W)w, EV_SIGNAL);
1292} 1304}
1293 1305
1560 1572
1561 io_blocktime = 0.; 1573 io_blocktime = 0.;
1562 timeout_blocktime = 0.; 1574 timeout_blocktime = 0.;
1563 backend = 0; 1575 backend = 0;
1564 backend_fd = -1; 1576 backend_fd = -1;
1565 gotasync = 0; 1577 sig_pending = 0;
1578#if EV_ASYNC_ENABLE
1579 async_pending = 0;
1580#endif
1566#if EV_USE_INOTIFY 1581#if EV_USE_INOTIFY
1567 fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2; 1582 fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2;
1568#endif 1583#endif
1569#if EV_USE_SIGNALFD 1584#if EV_USE_SIGNALFD
1570 sigfd = flags & EVFLAG_NOSIGFD ? -1 : -2; 1585 sigfd = flags & EVFLAG_NOSIGFD ? -1 : -2;
1612 close (evfd); 1627 close (evfd);
1613#endif 1628#endif
1614 1629
1615 if (evpipe [0] >= 0) 1630 if (evpipe [0] >= 0)
1616 { 1631 {
1617 close (evpipe [0]); 1632 EV_WIN32_CLOSE_FD (evpipe [0]);
1618 close (evpipe [1]); 1633 EV_WIN32_CLOSE_FD (evpipe [1]);
1619 } 1634 }
1620 } 1635 }
1621 1636
1622#if EV_USE_SIGNALFD 1637#if EV_USE_SIGNALFD
1623 if (ev_is_active (&sigfd_w)) 1638 if (ev_is_active (&sigfd_w))
1704 1719
1705 if (ev_is_active (&pipe_w)) 1720 if (ev_is_active (&pipe_w))
1706 { 1721 {
1707 /* this "locks" the handlers against writing to the pipe */ 1722 /* this "locks" the handlers against writing to the pipe */
1708 /* while we modify the fd vars */ 1723 /* while we modify the fd vars */
1709 gotsig = 1; 1724 sig_pending = 1;
1710#if EV_ASYNC_ENABLE 1725#if EV_ASYNC_ENABLE
1711 gotasync = 1; 1726 async_pending = 1;
1712#endif 1727#endif
1713 1728
1714 ev_ref (EV_A); 1729 ev_ref (EV_A);
1715 ev_io_stop (EV_A_ &pipe_w); 1730 ev_io_stop (EV_A_ &pipe_w);
1716 1731
1719 close (evfd); 1734 close (evfd);
1720#endif 1735#endif
1721 1736
1722 if (evpipe [0] >= 0) 1737 if (evpipe [0] >= 0)
1723 { 1738 {
1724 close (evpipe [0]); 1739 EV_WIN32_CLOSE_FD (evpipe [0]);
1725 close (evpipe [1]); 1740 EV_WIN32_CLOSE_FD (evpipe [1]);
1726 } 1741 }
1727 1742
1728 evpipe_init (EV_A); 1743 evpipe_init (EV_A);
1729 /* now iterate over everything, in case we missed something */ 1744 /* now iterate over everything, in case we missed something */
1730 pipecb (EV_A_ &pipe_w, EV_READ); 1745 pipecb (EV_A_ &pipe_w, EV_READ);
1856 assert (checkmax >= checkcnt); 1871 assert (checkmax >= checkcnt);
1857 array_verify (EV_A_ (W *)checks, checkcnt); 1872 array_verify (EV_A_ (W *)checks, checkcnt);
1858 1873
1859# if 0 1874# if 0
1860 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) 1875 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next)
1861 for (signum = EV_NSIG; signum--; ) if (signals [signum].gotsig) 1876 for (signum = EV_NSIG; signum--; ) if (signals [signum].pending)
1862# endif 1877# endif
1863#endif 1878#endif
1864} 1879}
1865#endif 1880#endif
1866 1881
2393inline_size void 2408inline_size void
2394wlist_del (WL *head, WL elem) 2409wlist_del (WL *head, WL elem)
2395{ 2410{
2396 while (*head) 2411 while (*head)
2397 { 2412 {
2398 if (*head == elem) 2413 if (expect_true (*head == elem))
2399 { 2414 {
2400 *head = elem->next; 2415 *head = elem->next;
2401 return; 2416 break;
2402 } 2417 }
2403 2418
2404 head = &(*head)->next; 2419 head = &(*head)->next;
2405 } 2420 }
2406} 2421}
2666 return; 2681 return;
2667 2682
2668 assert (("libev: ev_signal_start called with illegal signal number", w->signum > 0 && w->signum < EV_NSIG)); 2683 assert (("libev: ev_signal_start called with illegal signal number", w->signum > 0 && w->signum < EV_NSIG));
2669 2684
2670#if EV_MULTIPLICITY 2685#if EV_MULTIPLICITY
2671 assert (("libev: tried to attach to a signal from two different loops", 2686 assert (("libev: a signal must not be attached to two different loops",
2672 !signals [w->signum - 1].loop || signals [w->signum - 1].loop == loop)); 2687 !signals [w->signum - 1].loop || signals [w->signum - 1].loop == loop));
2673 2688
2674 signals [w->signum - 1].loop = EV_A; 2689 signals [w->signum - 1].loop = EV_A;
2675#endif 2690#endif
2676 2691
2747 wlist_del (&signals [w->signum - 1].head, (WL)w); 2762 wlist_del (&signals [w->signum - 1].head, (WL)w);
2748 ev_stop (EV_A_ (W)w); 2763 ev_stop (EV_A_ (W)w);
2749 2764
2750 if (!signals [w->signum - 1].head) 2765 if (!signals [w->signum - 1].head)
2751 { 2766 {
2752 #if EV_MULTIPLICITY 2767#if EV_MULTIPLICITY
2753 signals [w->signum - 1].loop = 0; /* unattach from signal */ 2768 signals [w->signum - 1].loop = 0; /* unattach from signal */
2754 #endif 2769#endif
2755 #if EV_USE_SIGNALFD 2770#if EV_USE_SIGNALFD
2756 if (sigfd >= 0) 2771 if (sigfd >= 0)
2757 { 2772 {
2758 sigprocmask (SIG_UNBLOCK, &sigfd_set, 0);//D 2773 sigprocmask (SIG_UNBLOCK, &sigfd_set, 0);//D
2759 sigdelset (&sigfd_set, w->signum); 2774 sigdelset (&sigfd_set, w->signum);
2760 signalfd (sigfd, &sigfd_set, 0); 2775 signalfd (sigfd, &sigfd_set, 0);
2761 sigprocmask (SIG_BLOCK, &sigfd_set, 0);//D 2776 sigprocmask (SIG_BLOCK, &sigfd_set, 0);//D
2762 /*TODO: maybe unblock signal? */ 2777 /*TODO: maybe unblock signal? */
2763 } 2778 }
2764 else 2779 else
2765 #endif 2780#endif
2766 signal (w->signum, SIG_DFL); 2781 signal (w->signum, SIG_DFL);
2767 } 2782 }
2768 2783
2769 EV_FREQUENT_CHECK; 2784 EV_FREQUENT_CHECK;
2770} 2785}
2852 } 2867 }
2853 } 2868 }
2854 2869
2855 if (w->wd >= 0) 2870 if (w->wd >= 0)
2856 { 2871 {
2872 struct statfs sfs;
2873
2857 wlist_add (&fs_hash [w->wd & (EV_INOTIFY_HASHSIZE - 1)].head, (WL)w); 2874 wlist_add (&fs_hash [w->wd & (EV_INOTIFY_HASHSIZE - 1)].head, (WL)w);
2858 2875
2859 /* now local changes will be tracked by inotify, but remote changes won't */ 2876 /* now local changes will be tracked by inotify, but remote changes won't */
2860 /* unless the filesystem it known to be local, we therefore still poll */ 2877 /* unless the filesystem it known to be local, we therefore still poll */
2861 /* also do poll on <2.6.25, but with normal frequency */ 2878 /* also do poll on <2.6.25, but with normal frequency */
2862 struct statfs sfs;
2863 2879
2864 if (fs_2625 && !statfs (w->path, &sfs)) 2880 if (fs_2625 && !statfs (w->path, &sfs))
2865 if (sfs.f_type == 0x1373 /* devfs */ 2881 if (sfs.f_type == 0x1373 /* devfs */
2866 || sfs.f_type == 0xEF53 /* ext2/3 */ 2882 || sfs.f_type == 0xEF53 /* ext2/3 */
2867 || sfs.f_type == 0x3153464a /* jfs */ 2883 || sfs.f_type == 0x3153464a /* jfs */
3416 3432
3417void 3433void
3418ev_async_send (EV_P_ ev_async *w) 3434ev_async_send (EV_P_ ev_async *w)
3419{ 3435{
3420 w->sent = 1; 3436 w->sent = 1;
3421 evpipe_write (EV_A_ &gotasync); 3437 evpipe_write (EV_A_ &async_pending);
3422} 3438}
3423#endif 3439#endif
3424 3440
3425/*****************************************************************************/ 3441/*****************************************************************************/
3426 3442

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines