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

Comparing libev/ev.c (file contents):
Revision 1.332 by root, Tue Mar 9 08:58:17 2010 UTC vs.
Revision 1.337 by root, Wed Mar 10 09:18:24 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 */
581
577 if (size) 582 if (size)
578 return realloc (ptr, size); 583 return realloc (ptr, size);
579 584
580 free (ptr); 585 free (ptr);
581 return 0; 586 return 0;
587#endif
582} 588}
583 589
584static void *(*alloc)(void *ptr, long size) = ev_realloc_emul; 590static void *(*alloc)(void *ptr, long size) = ev_realloc_emul;
585 591
586void 592void
885} 891}
886 892
887/*****************************************************************************/ 893/*****************************************************************************/
888 894
889inline_speed void 895inline_speed void
890fd_event_nc (EV_P_ int fd, int revents) 896fd_event_nocheck (EV_P_ int fd, int revents)
891{ 897{
892 ANFD *anfd = anfds + fd; 898 ANFD *anfd = anfds + fd;
893 ev_io *w; 899 ev_io *w;
894 900
895 for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next) 901 for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next)
907fd_event (EV_P_ int fd, int revents) 913fd_event (EV_P_ int fd, int revents)
908{ 914{
909 ANFD *anfd = anfds + fd; 915 ANFD *anfd = anfds + fd;
910 916
911 if (expect_true (!anfd->reify)) 917 if (expect_true (!anfd->reify))
912 fd_event_nc (EV_A_ fd, revents); 918 fd_event_nocheck (EV_A_ fd, revents);
913} 919}
914 920
915void 921void
916ev_feed_fd_event (EV_P_ int fd, int revents) 922ev_feed_fd_event (EV_P_ int fd, int revents)
917{ 923{
918 if (fd >= 0 && fd < anfdmax) 924 if (fd >= 0 && fd < anfdmax)
919 fd_event_nc (EV_A_ fd, revents); 925 fd_event_nocheck (EV_A_ fd, revents);
920} 926}
921 927
922/* make sure the external fd watch events are in-sync */ 928/* make sure the external fd watch events are in-sync */
923/* with the kernel/libev internal state */ 929/* with the kernel/libev internal state */
924inline_size void 930inline_size void
987 ev_io_stop (EV_A_ w); 993 ev_io_stop (EV_A_ w);
988 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);
989 } 995 }
990} 996}
991 997
992/* check whether the given fd is atcually valid, for error recovery */ 998/* check whether the given fd is actually valid, for error recovery */
993inline_size int 999inline_size int
994fd_valid (int fd) 1000fd_valid (int fd)
995{ 1001{
996#ifdef _WIN32 1002#ifdef _WIN32
997 return EV_FD_TO_WIN32_HANDLE (fd) != -1; 1003 return EV_FD_TO_WIN32_HANDLE (fd) != -1;
1037 { 1043 {
1038 anfds [fd].events = 0; 1044 anfds [fd].events = 0;
1039 anfds [fd].emask = 0; 1045 anfds [fd].emask = 0;
1040 fd_change (EV_A_ fd, EV__IOFDSET | EV_ANFD_REIFY); 1046 fd_change (EV_A_ fd, EV__IOFDSET | EV_ANFD_REIFY);
1041 } 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
1042} 1062}
1043 1063
1044/*****************************************************************************/ 1064/*****************************************************************************/
1045 1065
1046/* 1066/*
1200 1220
1201static ANSIG signals [EV_NSIG - 1]; 1221static ANSIG signals [EV_NSIG - 1];
1202 1222
1203/*****************************************************************************/ 1223/*****************************************************************************/
1204 1224
1205/* used to prepare libev internal fd's */ 1225#if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE
1206/* this is not fork-safe */
1207inline_speed void
1208fd_intern (int fd)
1209{
1210#ifdef _WIN32
1211 unsigned long arg = 1;
1212 ioctlsocket (EV_FD_TO_WIN32_HANDLE (fd), FIONBIO, &arg);
1213#else
1214 fcntl (fd, F_SETFD, FD_CLOEXEC);
1215 fcntl (fd, F_SETFL, O_NONBLOCK);
1216#endif
1217}
1218 1226
1219static void noinline 1227static void noinline
1220evpipe_init (EV_P) 1228evpipe_init (EV_P)
1221{ 1229{
1222 if (!ev_is_active (&pipe_w)) 1230 if (!ev_is_active (&pipe_w))
1223 { 1231 {
1224#if EV_USE_EVENTFD 1232# if EV_USE_EVENTFD
1225 evfd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC); 1233 evfd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC);
1226 if (evfd < 0 && errno == EINVAL) 1234 if (evfd < 0 && errno == EINVAL)
1227 evfd = eventfd (0, 0); 1235 evfd = eventfd (0, 0);
1228 1236
1229 if (evfd >= 0) 1237 if (evfd >= 0)
1231 evpipe [0] = -1; 1239 evpipe [0] = -1;
1232 fd_intern (evfd); /* doing it twice doesn't hurt */ 1240 fd_intern (evfd); /* doing it twice doesn't hurt */
1233 ev_io_set (&pipe_w, evfd, EV_READ); 1241 ev_io_set (&pipe_w, evfd, EV_READ);
1234 } 1242 }
1235 else 1243 else
1236#endif 1244# endif
1237 { 1245 {
1238 while (pipe (evpipe)) 1246 while (pipe (evpipe))
1239 ev_syserr ("(libev) error creating signal/async pipe"); 1247 ev_syserr ("(libev) error creating signal/async pipe");
1240 1248
1241 fd_intern (evpipe [0]); 1249 fd_intern (evpipe [0]);
1252evpipe_write (EV_P_ EV_ATOMIC_T *flag) 1260evpipe_write (EV_P_ EV_ATOMIC_T *flag)
1253{ 1261{
1254 if (!*flag) 1262 if (!*flag)
1255 { 1263 {
1256 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;
1257 1266
1258 *flag = 1; 1267 *flag = 1;
1259 1268
1260#if EV_USE_EVENTFD 1269#if EV_USE_EVENTFD
1261 if (evfd >= 0) 1270 if (evfd >= 0)
1263 uint64_t counter = 1; 1272 uint64_t counter = 1;
1264 write (evfd, &counter, sizeof (uint64_t)); 1273 write (evfd, &counter, sizeof (uint64_t));
1265 } 1274 }
1266 else 1275 else
1267#endif 1276#endif
1268 write (evpipe [1], &old_errno, 1); 1277 write (evpipe [1], &dummy, 1);
1269 1278
1270 errno = old_errno; 1279 errno = old_errno;
1271 } 1280 }
1272} 1281}
1273 1282
1374 break; 1383 break;
1375 } 1384 }
1376} 1385}
1377#endif 1386#endif
1378 1387
1388#endif
1389
1379/*****************************************************************************/ 1390/*****************************************************************************/
1380 1391
1392#if EV_CHILD_ENABLE
1381static WL childs [EV_PID_HASHSIZE]; 1393static WL childs [EV_PID_HASHSIZE];
1382
1383#ifndef _WIN32
1384 1394
1385static ev_signal childev; 1395static ev_signal childev;
1386 1396
1387#ifndef WIFCONTINUED 1397#ifndef WIFCONTINUED
1388# define WIFCONTINUED(status) 0 1398# define WIFCONTINUED(status) 0
1657 if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags); 1667 if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags);
1658#endif 1668#endif
1659 1669
1660 ev_prepare_init (&pending_w, pendingcb); 1670 ev_prepare_init (&pending_w, pendingcb);
1661 1671
1672#if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE
1662 ev_init (&pipe_w, pipecb); 1673 ev_init (&pipe_w, pipecb);
1663 ev_set_priority (&pipe_w, EV_MAXPRI); 1674 ev_set_priority (&pipe_w, EV_MAXPRI);
1675#endif
1664 } 1676 }
1665} 1677}
1666 1678
1667/* free up a loop structure */ 1679/* free up a loop structure */
1668static void noinline 1680static void noinline
1786 { 1798 {
1787 EV_WIN32_CLOSE_FD (evpipe [0]); 1799 EV_WIN32_CLOSE_FD (evpipe [0]);
1788 EV_WIN32_CLOSE_FD (evpipe [1]); 1800 EV_WIN32_CLOSE_FD (evpipe [1]);
1789 } 1801 }
1790 1802
1803#if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE
1791 evpipe_init (EV_A); 1804 evpipe_init (EV_A);
1792 /* now iterate over everything, in case we missed something */ 1805 /* now iterate over everything, in case we missed something */
1793 pipecb (EV_A_ &pipe_w, EV_READ); 1806 pipecb (EV_A_ &pipe_w, EV_READ);
1807#endif
1794 } 1808 }
1795 1809
1796 postfork = 0; 1810 postfork = 0;
1797} 1811}
1798 1812
1911#if EV_ASYNC_ENABLE 1925#if EV_ASYNC_ENABLE
1912 assert (asyncmax >= asynccnt); 1926 assert (asyncmax >= asynccnt);
1913 array_verify (EV_A_ (W *)asyncs, asynccnt); 1927 array_verify (EV_A_ (W *)asyncs, asynccnt);
1914#endif 1928#endif
1915 1929
1930#if EV_PREPARE_ENABLE
1916 assert (preparemax >= preparecnt); 1931 assert (preparemax >= preparecnt);
1917 array_verify (EV_A_ (W *)prepares, preparecnt); 1932 array_verify (EV_A_ (W *)prepares, preparecnt);
1933#endif
1918 1934
1935#if EV_CHECK_ENABLE
1919 assert (checkmax >= checkcnt); 1936 assert (checkmax >= checkcnt);
1920 array_verify (EV_A_ (W *)checks, checkcnt); 1937 array_verify (EV_A_ (W *)checks, checkcnt);
1938#endif
1921 1939
1922# if 0 1940# if 0
1941#if EV_CHILD_ENABLE
1923 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) 1942 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next)
1924 for (signum = EV_NSIG; signum--; ) if (signals [signum].pending) 1943 for (signum = EV_NSIG; signum--; ) if (signals [signum].pending)
1944#endif
1925# endif 1945# endif
1926#endif 1946#endif
1927} 1947}
1928#endif 1948#endif
1929 1949
1945 1965
1946 loop_init (EV_A_ flags); 1966 loop_init (EV_A_ flags);
1947 1967
1948 if (ev_backend (EV_A)) 1968 if (ev_backend (EV_A))
1949 { 1969 {
1950#ifndef _WIN32 1970#if EV_CHILD_ENABLE
1951 ev_signal_init (&childev, childcb, SIGCHLD); 1971 ev_signal_init (&childev, childcb, SIGCHLD);
1952 ev_set_priority (&childev, EV_MAXPRI); 1972 ev_set_priority (&childev, EV_MAXPRI);
1953 ev_signal_start (EV_A_ &childev); 1973 ev_signal_start (EV_A_ &childev);
1954 ev_unref (EV_A); /* child watcher should not keep loop alive */ 1974 ev_unref (EV_A); /* child watcher should not keep loop alive */
1955#endif 1975#endif
1968 EV_P = ev_default_loop_ptr; 1988 EV_P = ev_default_loop_ptr;
1969#endif 1989#endif
1970 1990
1971 ev_default_loop_ptr = 0; 1991 ev_default_loop_ptr = 0;
1972 1992
1973#ifndef _WIN32 1993#if EV_CHILD_ENABLE
1974 ev_ref (EV_A); /* child watcher */ 1994 ev_ref (EV_A); /* child watcher */
1975 ev_signal_stop (EV_A_ &childev); 1995 ev_signal_stop (EV_A_ &childev);
1976#endif 1996#endif
1977 1997
1978 loop_destroy (EV_A); 1998 loop_destroy (EV_A);
2290 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK); 2310 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK);
2291 EV_INVOKE_PENDING; 2311 EV_INVOKE_PENDING;
2292 } 2312 }
2293#endif 2313#endif
2294 2314
2315#if EV_PREPARE_ENABLE
2295 /* queue prepare watchers (and execute them) */ 2316 /* queue prepare watchers (and execute them) */
2296 if (expect_false (preparecnt)) 2317 if (expect_false (preparecnt))
2297 { 2318 {
2298 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 2319 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
2299 EV_INVOKE_PENDING; 2320 EV_INVOKE_PENDING;
2300 } 2321 }
2322#endif
2301 2323
2302 if (expect_false (loop_done)) 2324 if (expect_false (loop_done))
2303 break; 2325 break;
2304 2326
2305 /* we might have forked, so reify kernel state if necessary */ 2327 /* we might have forked, so reify kernel state if necessary */
2378#if EV_IDLE_ENABLE 2400#if EV_IDLE_ENABLE
2379 /* queue idle watchers unless other events are pending */ 2401 /* queue idle watchers unless other events are pending */
2380 idle_reify (EV_A); 2402 idle_reify (EV_A);
2381#endif 2403#endif
2382 2404
2405#if EV_CHECK_ENABLE
2383 /* queue check watchers, to be executed first */ 2406 /* queue check watchers, to be executed first */
2384 if (expect_false (checkcnt)) 2407 if (expect_false (checkcnt))
2385 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 2408 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
2409#endif
2386 2410
2387 EV_INVOKE_PENDING; 2411 EV_INVOKE_PENDING;
2388 } 2412 }
2389 while (expect_true ( 2413 while (expect_true (
2390 activecnt 2414 activecnt
2720 2744
2721#ifndef SA_RESTART 2745#ifndef SA_RESTART
2722# define SA_RESTART 0 2746# define SA_RESTART 0
2723#endif 2747#endif
2724 2748
2749#if EV_SIGNAL_ENABLE
2750
2725void noinline 2751void noinline
2726ev_signal_start (EV_P_ ev_signal *w) 2752ev_signal_start (EV_P_ ev_signal *w)
2727{ 2753{
2728 if (expect_false (ev_is_active (w))) 2754 if (expect_false (ev_is_active (w)))
2729 return; 2755 return;
2835 } 2861 }
2836 2862
2837 EV_FREQUENT_CHECK; 2863 EV_FREQUENT_CHECK;
2838} 2864}
2839 2865
2866#endif
2867
2868#if EV_CHILD_ENABLE
2869
2840void 2870void
2841ev_child_start (EV_P_ ev_child *w) 2871ev_child_start (EV_P_ ev_child *w)
2842{ 2872{
2843#if EV_MULTIPLICITY 2873#if EV_MULTIPLICITY
2844 assert (("libev: child watchers are only supported in the default loop", loop == ev_default_loop_ptr)); 2874 assert (("libev: child watchers are only supported in the default loop", loop == ev_default_loop_ptr));
2866 wlist_del (&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w); 2896 wlist_del (&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w);
2867 ev_stop (EV_A_ (W)w); 2897 ev_stop (EV_A_ (W)w);
2868 2898
2869 EV_FREQUENT_CHECK; 2899 EV_FREQUENT_CHECK;
2870} 2900}
2901
2902#endif
2871 2903
2872#if EV_STAT_ENABLE 2904#if EV_STAT_ENABLE
2873 2905
2874# ifdef _WIN32 2906# ifdef _WIN32
2875# undef lstat 2907# undef lstat
3296 3328
3297 EV_FREQUENT_CHECK; 3329 EV_FREQUENT_CHECK;
3298} 3330}
3299#endif 3331#endif
3300 3332
3333#if EV_PREPARE_ENABLE
3301void 3334void
3302ev_prepare_start (EV_P_ ev_prepare *w) 3335ev_prepare_start (EV_P_ ev_prepare *w)
3303{ 3336{
3304 if (expect_false (ev_is_active (w))) 3337 if (expect_false (ev_is_active (w)))
3305 return; 3338 return;
3331 3364
3332 ev_stop (EV_A_ (W)w); 3365 ev_stop (EV_A_ (W)w);
3333 3366
3334 EV_FREQUENT_CHECK; 3367 EV_FREQUENT_CHECK;
3335} 3368}
3369#endif
3336 3370
3371#if EV_CHECK_ENABLE
3337void 3372void
3338ev_check_start (EV_P_ ev_check *w) 3373ev_check_start (EV_P_ ev_check *w)
3339{ 3374{
3340 if (expect_false (ev_is_active (w))) 3375 if (expect_false (ev_is_active (w)))
3341 return; 3376 return;
3367 3402
3368 ev_stop (EV_A_ (W)w); 3403 ev_stop (EV_A_ (W)w);
3369 3404
3370 EV_FREQUENT_CHECK; 3405 EV_FREQUENT_CHECK;
3371} 3406}
3407#endif
3372 3408
3373#if EV_EMBED_ENABLE 3409#if EV_EMBED_ENABLE
3374void noinline 3410void noinline
3375ev_embed_sweep (EV_P_ ev_embed *w) 3411ev_embed_sweep (EV_P_ ev_embed *w)
3376{ 3412{
3705 if (types & EV_ASYNC) 3741 if (types & EV_ASYNC)
3706 for (i = asynccnt; i--; ) 3742 for (i = asynccnt; i--; )
3707 cb (EV_A_ EV_ASYNC, asyncs [i]); 3743 cb (EV_A_ EV_ASYNC, asyncs [i]);
3708#endif 3744#endif
3709 3745
3746#if EV_PREPARE_ENABLE
3710 if (types & EV_PREPARE) 3747 if (types & EV_PREPARE)
3711 for (i = preparecnt; i--; ) 3748 for (i = preparecnt; i--; )
3712#if EV_EMBED_ENABLE 3749# if EV_EMBED_ENABLE
3713 if (ev_cb (prepares [i]) != embed_prepare_cb) 3750 if (ev_cb (prepares [i]) != embed_prepare_cb)
3714#endif 3751# endif
3715 cb (EV_A_ EV_PREPARE, prepares [i]); 3752 cb (EV_A_ EV_PREPARE, prepares [i]);
3753#endif
3716 3754
3755#if EV_CHECK_ENABLE
3717 if (types & EV_CHECK) 3756 if (types & EV_CHECK)
3718 for (i = checkcnt; i--; ) 3757 for (i = checkcnt; i--; )
3719 cb (EV_A_ EV_CHECK, checks [i]); 3758 cb (EV_A_ EV_CHECK, checks [i]);
3759#endif
3720 3760
3761#if EV_SIGNAL_ENABLE
3721 if (types & EV_SIGNAL) 3762 if (types & EV_SIGNAL)
3722 for (i = 0; i < EV_NSIG - 1; ++i) 3763 for (i = 0; i < EV_NSIG - 1; ++i)
3723 for (wl = signals [i].head; wl; ) 3764 for (wl = signals [i].head; wl; )
3724 { 3765 {
3725 wn = wl->next; 3766 wn = wl->next;
3726 cb (EV_A_ EV_SIGNAL, wl); 3767 cb (EV_A_ EV_SIGNAL, wl);
3727 wl = wn; 3768 wl = wn;
3728 } 3769 }
3770#endif
3729 3771
3772#if EV_CHILD_ENABLE
3730 if (types & EV_CHILD) 3773 if (types & EV_CHILD)
3731 for (i = EV_PID_HASHSIZE; i--; ) 3774 for (i = EV_PID_HASHSIZE; i--; )
3732 for (wl = childs [i]; wl; ) 3775 for (wl = childs [i]; wl; )
3733 { 3776 {
3734 wn = wl->next; 3777 wn = wl->next;
3735 cb (EV_A_ EV_CHILD, wl); 3778 cb (EV_A_ EV_CHILD, wl);
3736 wl = wn; 3779 wl = wn;
3737 } 3780 }
3781#endif
3738/* EV_STAT 0x00001000 /* stat data changed */ 3782/* EV_STAT 0x00001000 /* stat data changed */
3739/* EV_EMBED 0x00010000 /* embedded event loop needs sweep */ 3783/* EV_EMBED 0x00010000 /* embedded event loop needs sweep */
3740} 3784}
3741#endif 3785#endif
3742 3786

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines