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.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 */
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
886} 891}
887 892
888/*****************************************************************************/ 893/*****************************************************************************/
889 894
890inline_speed void 895inline_speed void
891fd_event_nc (EV_P_ int fd, int revents) 896fd_event_nocheck (EV_P_ int fd, int revents)
892{ 897{
893 ANFD *anfd = anfds + fd; 898 ANFD *anfd = anfds + fd;
894 ev_io *w; 899 ev_io *w;
895 900
896 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)
908fd_event (EV_P_ int fd, int revents) 913fd_event (EV_P_ int fd, int revents)
909{ 914{
910 ANFD *anfd = anfds + fd; 915 ANFD *anfd = anfds + fd;
911 916
912 if (expect_true (!anfd->reify)) 917 if (expect_true (!anfd->reify))
913 fd_event_nc (EV_A_ fd, revents); 918 fd_event_nocheck (EV_A_ fd, revents);
914} 919}
915 920
916void 921void
917ev_feed_fd_event (EV_P_ int fd, int revents) 922ev_feed_fd_event (EV_P_ int fd, int revents)
918{ 923{
919 if (fd >= 0 && fd < anfdmax) 924 if (fd >= 0 && fd < anfdmax)
920 fd_event_nc (EV_A_ fd, revents); 925 fd_event_nocheck (EV_A_ fd, revents);
921} 926}
922 927
923/* make sure the external fd watch events are in-sync */ 928/* make sure the external fd watch events are in-sync */
924/* with the kernel/libev internal state */ 929/* with the kernel/libev internal state */
925inline_size void 930inline_size void
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
1787 { 1798 {
1788 EV_WIN32_CLOSE_FD (evpipe [0]); 1799 EV_WIN32_CLOSE_FD (evpipe [0]);
1789 EV_WIN32_CLOSE_FD (evpipe [1]); 1800 EV_WIN32_CLOSE_FD (evpipe [1]);
1790 } 1801 }
1791 1802
1803#if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE
1792 evpipe_init (EV_A); 1804 evpipe_init (EV_A);
1793 /* now iterate over everything, in case we missed something */ 1805 /* now iterate over everything, in case we missed something */
1794 pipecb (EV_A_ &pipe_w, EV_READ); 1806 pipecb (EV_A_ &pipe_w, EV_READ);
1807#endif
1795 } 1808 }
1796 1809
1797 postfork = 0; 1810 postfork = 0;
1798} 1811}
1799 1812
1912#if EV_ASYNC_ENABLE 1925#if EV_ASYNC_ENABLE
1913 assert (asyncmax >= asynccnt); 1926 assert (asyncmax >= asynccnt);
1914 array_verify (EV_A_ (W *)asyncs, asynccnt); 1927 array_verify (EV_A_ (W *)asyncs, asynccnt);
1915#endif 1928#endif
1916 1929
1930#if EV_PREPARE_ENABLE
1917 assert (preparemax >= preparecnt); 1931 assert (preparemax >= preparecnt);
1918 array_verify (EV_A_ (W *)prepares, preparecnt); 1932 array_verify (EV_A_ (W *)prepares, preparecnt);
1933#endif
1919 1934
1935#if EV_CHECK_ENABLE
1920 assert (checkmax >= checkcnt); 1936 assert (checkmax >= checkcnt);
1921 array_verify (EV_A_ (W *)checks, checkcnt); 1937 array_verify (EV_A_ (W *)checks, checkcnt);
1938#endif
1922 1939
1923# if 0 1940# if 0
1941#if EV_CHILD_ENABLE
1924 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)
1925 for (signum = EV_NSIG; signum--; ) if (signals [signum].pending) 1943 for (signum = EV_NSIG; signum--; ) if (signals [signum].pending)
1944#endif
1926# endif 1945# endif
1927#endif 1946#endif
1928} 1947}
1929#endif 1948#endif
1930 1949
1946 1965
1947 loop_init (EV_A_ flags); 1966 loop_init (EV_A_ flags);
1948 1967
1949 if (ev_backend (EV_A)) 1968 if (ev_backend (EV_A))
1950 { 1969 {
1951#ifndef _WIN32 1970#if EV_CHILD_ENABLE
1952 ev_signal_init (&childev, childcb, SIGCHLD); 1971 ev_signal_init (&childev, childcb, SIGCHLD);
1953 ev_set_priority (&childev, EV_MAXPRI); 1972 ev_set_priority (&childev, EV_MAXPRI);
1954 ev_signal_start (EV_A_ &childev); 1973 ev_signal_start (EV_A_ &childev);
1955 ev_unref (EV_A); /* child watcher should not keep loop alive */ 1974 ev_unref (EV_A); /* child watcher should not keep loop alive */
1956#endif 1975#endif
1969 EV_P = ev_default_loop_ptr; 1988 EV_P = ev_default_loop_ptr;
1970#endif 1989#endif
1971 1990
1972 ev_default_loop_ptr = 0; 1991 ev_default_loop_ptr = 0;
1973 1992
1974#ifndef _WIN32 1993#if EV_CHILD_ENABLE
1975 ev_ref (EV_A); /* child watcher */ 1994 ev_ref (EV_A); /* child watcher */
1976 ev_signal_stop (EV_A_ &childev); 1995 ev_signal_stop (EV_A_ &childev);
1977#endif 1996#endif
1978 1997
1979 loop_destroy (EV_A); 1998 loop_destroy (EV_A);
2291 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK); 2310 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK);
2292 EV_INVOKE_PENDING; 2311 EV_INVOKE_PENDING;
2293 } 2312 }
2294#endif 2313#endif
2295 2314
2315#if EV_PREPARE_ENABLE
2296 /* queue prepare watchers (and execute them) */ 2316 /* queue prepare watchers (and execute them) */
2297 if (expect_false (preparecnt)) 2317 if (expect_false (preparecnt))
2298 { 2318 {
2299 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 2319 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
2300 EV_INVOKE_PENDING; 2320 EV_INVOKE_PENDING;
2301 } 2321 }
2322#endif
2302 2323
2303 if (expect_false (loop_done)) 2324 if (expect_false (loop_done))
2304 break; 2325 break;
2305 2326
2306 /* we might have forked, so reify kernel state if necessary */ 2327 /* we might have forked, so reify kernel state if necessary */
2379#if EV_IDLE_ENABLE 2400#if EV_IDLE_ENABLE
2380 /* queue idle watchers unless other events are pending */ 2401 /* queue idle watchers unless other events are pending */
2381 idle_reify (EV_A); 2402 idle_reify (EV_A);
2382#endif 2403#endif
2383 2404
2405#if EV_CHECK_ENABLE
2384 /* queue check watchers, to be executed first */ 2406 /* queue check watchers, to be executed first */
2385 if (expect_false (checkcnt)) 2407 if (expect_false (checkcnt))
2386 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 2408 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
2409#endif
2387 2410
2388 EV_INVOKE_PENDING; 2411 EV_INVOKE_PENDING;
2389 } 2412 }
2390 while (expect_true ( 2413 while (expect_true (
2391 activecnt 2414 activecnt
2721 2744
2722#ifndef SA_RESTART 2745#ifndef SA_RESTART
2723# define SA_RESTART 0 2746# define SA_RESTART 0
2724#endif 2747#endif
2725 2748
2749#if EV_SIGNAL_ENABLE
2750
2726void noinline 2751void noinline
2727ev_signal_start (EV_P_ ev_signal *w) 2752ev_signal_start (EV_P_ ev_signal *w)
2728{ 2753{
2729 if (expect_false (ev_is_active (w))) 2754 if (expect_false (ev_is_active (w)))
2730 return; 2755 return;
2836 } 2861 }
2837 2862
2838 EV_FREQUENT_CHECK; 2863 EV_FREQUENT_CHECK;
2839} 2864}
2840 2865
2866#endif
2867
2868#if EV_CHILD_ENABLE
2869
2841void 2870void
2842ev_child_start (EV_P_ ev_child *w) 2871ev_child_start (EV_P_ ev_child *w)
2843{ 2872{
2844#if EV_MULTIPLICITY 2873#if EV_MULTIPLICITY
2845 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));
2867 wlist_del (&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w); 2896 wlist_del (&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w);
2868 ev_stop (EV_A_ (W)w); 2897 ev_stop (EV_A_ (W)w);
2869 2898
2870 EV_FREQUENT_CHECK; 2899 EV_FREQUENT_CHECK;
2871} 2900}
2901
2902#endif
2872 2903
2873#if EV_STAT_ENABLE 2904#if EV_STAT_ENABLE
2874 2905
2875# ifdef _WIN32 2906# ifdef _WIN32
2876# undef lstat 2907# undef lstat
3297 3328
3298 EV_FREQUENT_CHECK; 3329 EV_FREQUENT_CHECK;
3299} 3330}
3300#endif 3331#endif
3301 3332
3333#if EV_PREPARE_ENABLE
3302void 3334void
3303ev_prepare_start (EV_P_ ev_prepare *w) 3335ev_prepare_start (EV_P_ ev_prepare *w)
3304{ 3336{
3305 if (expect_false (ev_is_active (w))) 3337 if (expect_false (ev_is_active (w)))
3306 return; 3338 return;
3332 3364
3333 ev_stop (EV_A_ (W)w); 3365 ev_stop (EV_A_ (W)w);
3334 3366
3335 EV_FREQUENT_CHECK; 3367 EV_FREQUENT_CHECK;
3336} 3368}
3369#endif
3337 3370
3371#if EV_CHECK_ENABLE
3338void 3372void
3339ev_check_start (EV_P_ ev_check *w) 3373ev_check_start (EV_P_ ev_check *w)
3340{ 3374{
3341 if (expect_false (ev_is_active (w))) 3375 if (expect_false (ev_is_active (w)))
3342 return; 3376 return;
3368 3402
3369 ev_stop (EV_A_ (W)w); 3403 ev_stop (EV_A_ (W)w);
3370 3404
3371 EV_FREQUENT_CHECK; 3405 EV_FREQUENT_CHECK;
3372} 3406}
3407#endif
3373 3408
3374#if EV_EMBED_ENABLE 3409#if EV_EMBED_ENABLE
3375void noinline 3410void noinline
3376ev_embed_sweep (EV_P_ ev_embed *w) 3411ev_embed_sweep (EV_P_ ev_embed *w)
3377{ 3412{
3706 if (types & EV_ASYNC) 3741 if (types & EV_ASYNC)
3707 for (i = asynccnt; i--; ) 3742 for (i = asynccnt; i--; )
3708 cb (EV_A_ EV_ASYNC, asyncs [i]); 3743 cb (EV_A_ EV_ASYNC, asyncs [i]);
3709#endif 3744#endif
3710 3745
3746#if EV_PREPARE_ENABLE
3711 if (types & EV_PREPARE) 3747 if (types & EV_PREPARE)
3712 for (i = preparecnt; i--; ) 3748 for (i = preparecnt; i--; )
3713#if EV_EMBED_ENABLE 3749# if EV_EMBED_ENABLE
3714 if (ev_cb (prepares [i]) != embed_prepare_cb) 3750 if (ev_cb (prepares [i]) != embed_prepare_cb)
3715#endif 3751# endif
3716 cb (EV_A_ EV_PREPARE, prepares [i]); 3752 cb (EV_A_ EV_PREPARE, prepares [i]);
3753#endif
3717 3754
3755#if EV_CHECK_ENABLE
3718 if (types & EV_CHECK) 3756 if (types & EV_CHECK)
3719 for (i = checkcnt; i--; ) 3757 for (i = checkcnt; i--; )
3720 cb (EV_A_ EV_CHECK, checks [i]); 3758 cb (EV_A_ EV_CHECK, checks [i]);
3759#endif
3721 3760
3761#if EV_SIGNAL_ENABLE
3722 if (types & EV_SIGNAL) 3762 if (types & EV_SIGNAL)
3723 for (i = 0; i < EV_NSIG - 1; ++i) 3763 for (i = 0; i < EV_NSIG - 1; ++i)
3724 for (wl = signals [i].head; wl; ) 3764 for (wl = signals [i].head; wl; )
3725 { 3765 {
3726 wn = wl->next; 3766 wn = wl->next;
3727 cb (EV_A_ EV_SIGNAL, wl); 3767 cb (EV_A_ EV_SIGNAL, wl);
3728 wl = wn; 3768 wl = wn;
3729 } 3769 }
3770#endif
3730 3771
3772#if EV_CHILD_ENABLE
3731 if (types & EV_CHILD) 3773 if (types & EV_CHILD)
3732 for (i = EV_PID_HASHSIZE; i--; ) 3774 for (i = EV_PID_HASHSIZE; i--; )
3733 for (wl = childs [i]; wl; ) 3775 for (wl = childs [i]; wl; )
3734 { 3776 {
3735 wn = wl->next; 3777 wn = wl->next;
3736 cb (EV_A_ EV_CHILD, wl); 3778 cb (EV_A_ EV_CHILD, wl);
3737 wl = wn; 3779 wl = wn;
3738 } 3780 }
3781#endif
3739/* EV_STAT 0x00001000 /* stat data changed */ 3782/* EV_STAT 0x00001000 /* stat data changed */
3740/* EV_EMBED 0x00010000 /* embedded event loop needs sweep */ 3783/* EV_EMBED 0x00010000 /* embedded event loop needs sweep */
3741} 3784}
3742#endif 3785#endif
3743 3786

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines