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

Comparing libev/ev.c (file contents):
Revision 1.333 by root, Tue Mar 9 08:58:22 2010 UTC vs.
Revision 1.336 by root, Wed Mar 10 08:19:38 2010 UTC

207#elif defined (MAXSIG) 207#elif defined (MAXSIG)
208# define EV_NSIG (MAXSIG+1) 208# define EV_NSIG (MAXSIG+1)
209#elif defined (MAX_SIG) 209#elif defined (MAX_SIG)
210# define EV_NSIG (MAX_SIG+1) 210# define EV_NSIG (MAX_SIG+1)
211#elif defined (SIGARRAYSIZE) 211#elif defined (SIGARRAYSIZE)
212# define EV_NSIG SIGARRAYSIZE /* Assume ary[SIGARRAYSIZE] */ 212# define EV_NSIG (SIGARRAYSIZE) /* Assume ary[SIGARRAYSIZE] */
213#elif defined (_sys_nsig) 213#elif defined (_sys_nsig)
214# define EV_NSIG (_sys_nsig) /* Solaris 2.5 */ 214# define EV_NSIG (_sys_nsig) /* Solaris 2.5 */
215#else 215#else
216# error "unable to find value for NSIG, please report" 216# error "unable to find value for NSIG, please report"
217/* to make it compile regardless, just remove the above line */ 217/* to make it compile regardless, just remove the above line, */
218/* but consider reporting it, too! :) */
218# define EV_NSIG 65 219# define EV_NSIG 65
219#endif 220#endif
220 221
221#ifndef EV_USE_CLOCK_SYSCALL 222#ifndef EV_USE_CLOCK_SYSCALL
222# if __linux && __GLIBC__ >= 2 223# if __linux && __GLIBC__ >= 2
568} 569}
569 570
570static void * 571static void *
571ev_realloc_emul (void *ptr, long size) 572ev_realloc_emul (void *ptr, long size)
572{ 573{
574#if __GLIBC__
575 return realloc (ptr, size);
576#else
573 /* some systems, notably openbsd and darwin, fail to properly 577 /* some systems, notably openbsd and darwin, fail to properly
574 * implement realloc (x, 0) (as required by both ansi c-98 and 578 * implement realloc (x, 0) (as required by both ansi c-89 and
575 * the single unix specification, so work around them here. 579 * the single unix specification, so work around them here.
576 */ 580 */
577 581
578 if (size) 582 if (size)
579 return realloc (ptr, size); 583 return realloc (ptr, size);
580 584
581 free (ptr); 585 free (ptr);
582 return 0; 586 return 0;
587#endif
583} 588}
584 589
585static void *(*alloc)(void *ptr, long size) = ev_realloc_emul; 590static void *(*alloc)(void *ptr, long size) = ev_realloc_emul;
586 591
587void 592void
988 ev_io_stop (EV_A_ w); 993 ev_io_stop (EV_A_ w);
989 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); 994 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
990 } 995 }
991} 996}
992 997
993/* check whether the given fd is atcually valid, for error recovery */ 998/* check whether the given fd is actually valid, for error recovery */
994inline_size int 999inline_size int
995fd_valid (int fd) 1000fd_valid (int fd)
996{ 1001{
997#ifdef _WIN32 1002#ifdef _WIN32
998 return EV_FD_TO_WIN32_HANDLE (fd) != -1; 1003 return EV_FD_TO_WIN32_HANDLE (fd) != -1;
1038 { 1043 {
1039 anfds [fd].events = 0; 1044 anfds [fd].events = 0;
1040 anfds [fd].emask = 0; 1045 anfds [fd].emask = 0;
1041 fd_change (EV_A_ fd, EV__IOFDSET | EV_ANFD_REIFY); 1046 fd_change (EV_A_ fd, EV__IOFDSET | EV_ANFD_REIFY);
1042 } 1047 }
1048}
1049
1050/* used to prepare libev internal fd's */
1051/* this is not fork-safe */
1052inline_speed void
1053fd_intern (int fd)
1054{
1055#ifdef _WIN32
1056 unsigned long arg = 1;
1057 ioctlsocket (EV_FD_TO_WIN32_HANDLE (fd), FIONBIO, &arg);
1058#else
1059 fcntl (fd, F_SETFD, FD_CLOEXEC);
1060 fcntl (fd, F_SETFL, O_NONBLOCK);
1061#endif
1043} 1062}
1044 1063
1045/*****************************************************************************/ 1064/*****************************************************************************/
1046 1065
1047/* 1066/*
1201 1220
1202static ANSIG signals [EV_NSIG - 1]; 1221static ANSIG signals [EV_NSIG - 1];
1203 1222
1204/*****************************************************************************/ 1223/*****************************************************************************/
1205 1224
1206/* used to prepare libev internal fd's */ 1225#if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE
1207/* this is not fork-safe */
1208inline_speed void
1209fd_intern (int fd)
1210{
1211#ifdef _WIN32
1212 unsigned long arg = 1;
1213 ioctlsocket (EV_FD_TO_WIN32_HANDLE (fd), FIONBIO, &arg);
1214#else
1215 fcntl (fd, F_SETFD, FD_CLOEXEC);
1216 fcntl (fd, F_SETFL, O_NONBLOCK);
1217#endif
1218}
1219 1226
1220static void noinline 1227static void noinline
1221evpipe_init (EV_P) 1228evpipe_init (EV_P)
1222{ 1229{
1223 if (!ev_is_active (&pipe_w)) 1230 if (!ev_is_active (&pipe_w))
1224 { 1231 {
1225#if EV_USE_EVENTFD 1232# if EV_USE_EVENTFD
1226 evfd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC); 1233 evfd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC);
1227 if (evfd < 0 && errno == EINVAL) 1234 if (evfd < 0 && errno == EINVAL)
1228 evfd = eventfd (0, 0); 1235 evfd = eventfd (0, 0);
1229 1236
1230 if (evfd >= 0) 1237 if (evfd >= 0)
1232 evpipe [0] = -1; 1239 evpipe [0] = -1;
1233 fd_intern (evfd); /* doing it twice doesn't hurt */ 1240 fd_intern (evfd); /* doing it twice doesn't hurt */
1234 ev_io_set (&pipe_w, evfd, EV_READ); 1241 ev_io_set (&pipe_w, evfd, EV_READ);
1235 } 1242 }
1236 else 1243 else
1237#endif 1244# endif
1238 { 1245 {
1239 while (pipe (evpipe)) 1246 while (pipe (evpipe))
1240 ev_syserr ("(libev) error creating signal/async pipe"); 1247 ev_syserr ("(libev) error creating signal/async pipe");
1241 1248
1242 fd_intern (evpipe [0]); 1249 fd_intern (evpipe [0]);
1253evpipe_write (EV_P_ EV_ATOMIC_T *flag) 1260evpipe_write (EV_P_ EV_ATOMIC_T *flag)
1254{ 1261{
1255 if (!*flag) 1262 if (!*flag)
1256 { 1263 {
1257 int old_errno = errno; /* save errno because write might clobber it */ 1264 int old_errno = errno; /* save errno because write might clobber it */
1265 char dummy;
1258 1266
1259 *flag = 1; 1267 *flag = 1;
1260 1268
1261#if EV_USE_EVENTFD 1269#if EV_USE_EVENTFD
1262 if (evfd >= 0) 1270 if (evfd >= 0)
1264 uint64_t counter = 1; 1272 uint64_t counter = 1;
1265 write (evfd, &counter, sizeof (uint64_t)); 1273 write (evfd, &counter, sizeof (uint64_t));
1266 } 1274 }
1267 else 1275 else
1268#endif 1276#endif
1269 write (evpipe [1], &old_errno, 1); 1277 write (evpipe [1], &dummy, 1);
1270 1278
1271 errno = old_errno; 1279 errno = old_errno;
1272 } 1280 }
1273} 1281}
1274 1282
1375 break; 1383 break;
1376 } 1384 }
1377} 1385}
1378#endif 1386#endif
1379 1387
1388#endif
1389
1380/*****************************************************************************/ 1390/*****************************************************************************/
1381 1391
1392#if EV_CHILD_ENABLE
1382static WL childs [EV_PID_HASHSIZE]; 1393static WL childs [EV_PID_HASHSIZE];
1383
1384#ifndef _WIN32
1385 1394
1386static ev_signal childev; 1395static ev_signal childev;
1387 1396
1388#ifndef WIFCONTINUED 1397#ifndef WIFCONTINUED
1389# define WIFCONTINUED(status) 0 1398# define WIFCONTINUED(status) 0
1658 if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags); 1667 if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags);
1659#endif 1668#endif
1660 1669
1661 ev_prepare_init (&pending_w, pendingcb); 1670 ev_prepare_init (&pending_w, pendingcb);
1662 1671
1672#if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE
1663 ev_init (&pipe_w, pipecb); 1673 ev_init (&pipe_w, pipecb);
1664 ev_set_priority (&pipe_w, EV_MAXPRI); 1674 ev_set_priority (&pipe_w, EV_MAXPRI);
1675#endif
1665 } 1676 }
1666} 1677}
1667 1678
1668/* free up a loop structure */ 1679/* free up a loop structure */
1669static void noinline 1680static void noinline
1919 1930
1920 assert (checkmax >= checkcnt); 1931 assert (checkmax >= checkcnt);
1921 array_verify (EV_A_ (W *)checks, checkcnt); 1932 array_verify (EV_A_ (W *)checks, checkcnt);
1922 1933
1923# if 0 1934# if 0
1935#if EV_CHILD_ENABLE
1924 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) 1936 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next)
1925 for (signum = EV_NSIG; signum--; ) if (signals [signum].pending) 1937 for (signum = EV_NSIG; signum--; ) if (signals [signum].pending)
1938#endif
1926# endif 1939# endif
1927#endif 1940#endif
1928} 1941}
1929#endif 1942#endif
1930 1943
1946 1959
1947 loop_init (EV_A_ flags); 1960 loop_init (EV_A_ flags);
1948 1961
1949 if (ev_backend (EV_A)) 1962 if (ev_backend (EV_A))
1950 { 1963 {
1951#ifndef _WIN32 1964#if EV_CHILD_ENABLE
1952 ev_signal_init (&childev, childcb, SIGCHLD); 1965 ev_signal_init (&childev, childcb, SIGCHLD);
1953 ev_set_priority (&childev, EV_MAXPRI); 1966 ev_set_priority (&childev, EV_MAXPRI);
1954 ev_signal_start (EV_A_ &childev); 1967 ev_signal_start (EV_A_ &childev);
1955 ev_unref (EV_A); /* child watcher should not keep loop alive */ 1968 ev_unref (EV_A); /* child watcher should not keep loop alive */
1956#endif 1969#endif
1969 EV_P = ev_default_loop_ptr; 1982 EV_P = ev_default_loop_ptr;
1970#endif 1983#endif
1971 1984
1972 ev_default_loop_ptr = 0; 1985 ev_default_loop_ptr = 0;
1973 1986
1974#ifndef _WIN32 1987#if EV_CHILD_ENABLE
1975 ev_ref (EV_A); /* child watcher */ 1988 ev_ref (EV_A); /* child watcher */
1976 ev_signal_stop (EV_A_ &childev); 1989 ev_signal_stop (EV_A_ &childev);
1977#endif 1990#endif
1978 1991
1979 loop_destroy (EV_A); 1992 loop_destroy (EV_A);
2721 2734
2722#ifndef SA_RESTART 2735#ifndef SA_RESTART
2723# define SA_RESTART 0 2736# define SA_RESTART 0
2724#endif 2737#endif
2725 2738
2739#if EV_SIGNAL_ENABLE
2740
2726void noinline 2741void noinline
2727ev_signal_start (EV_P_ ev_signal *w) 2742ev_signal_start (EV_P_ ev_signal *w)
2728{ 2743{
2729 if (expect_false (ev_is_active (w))) 2744 if (expect_false (ev_is_active (w)))
2730 return; 2745 return;
2836 } 2851 }
2837 2852
2838 EV_FREQUENT_CHECK; 2853 EV_FREQUENT_CHECK;
2839} 2854}
2840 2855
2856#endif
2857
2858#if EV_CHILD_ENABLE
2859
2841void 2860void
2842ev_child_start (EV_P_ ev_child *w) 2861ev_child_start (EV_P_ ev_child *w)
2843{ 2862{
2844#if EV_MULTIPLICITY 2863#if EV_MULTIPLICITY
2845 assert (("libev: child watchers are only supported in the default loop", loop == ev_default_loop_ptr)); 2864 assert (("libev: child watchers are only supported in the default loop", loop == ev_default_loop_ptr));
2867 wlist_del (&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w); 2886 wlist_del (&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w);
2868 ev_stop (EV_A_ (W)w); 2887 ev_stop (EV_A_ (W)w);
2869 2888
2870 EV_FREQUENT_CHECK; 2889 EV_FREQUENT_CHECK;
2871} 2890}
2891
2892#endif
2872 2893
2873#if EV_STAT_ENABLE 2894#if EV_STAT_ENABLE
2874 2895
2875# ifdef _WIN32 2896# ifdef _WIN32
2876# undef lstat 2897# undef lstat

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines