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

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

184# define WIN32_LEAN_AND_MEAN 184# define WIN32_LEAN_AND_MEAN
185# include <windows.h> 185# include <windows.h>
186# ifndef EV_SELECT_IS_WINSOCKET 186# ifndef EV_SELECT_IS_WINSOCKET
187# define EV_SELECT_IS_WINSOCKET 1 187# define EV_SELECT_IS_WINSOCKET 1
188# endif 188# endif
189# undef EV_AVOID_STDIO
189#endif 190#endif
190 191
191/* this block tries to deduce configuration from header-defined symbols and defaults */ 192/* this block tries to deduce configuration from header-defined symbols and defaults */
192 193
193/* try to deduce the maximum number of signals on this platform */ 194/* try to deduce the maximum number of signals on this platform */
206#elif defined (MAXSIG) 207#elif defined (MAXSIG)
207# define EV_NSIG (MAXSIG+1) 208# define EV_NSIG (MAXSIG+1)
208#elif defined (MAX_SIG) 209#elif defined (MAX_SIG)
209# define EV_NSIG (MAX_SIG+1) 210# define EV_NSIG (MAX_SIG+1)
210#elif defined (SIGARRAYSIZE) 211#elif defined (SIGARRAYSIZE)
211# define EV_NSIG SIGARRAYSIZE /* Assume ary[SIGARRAYSIZE] */ 212# define EV_NSIG (SIGARRAYSIZE) /* Assume ary[SIGARRAYSIZE] */
212#elif defined (_sys_nsig) 213#elif defined (_sys_nsig)
213# define EV_NSIG (_sys_nsig) /* Solaris 2.5 */ 214# define EV_NSIG (_sys_nsig) /* Solaris 2.5 */
214#else 215#else
215# error "unable to find value for NSIG, please report" 216# error "unable to find value for NSIG, please report"
216/* 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! :) */
217# define EV_NSIG 65 219# define EV_NSIG 65
218#endif 220#endif
219 221
220#ifndef EV_USE_CLOCK_SYSCALL 222#ifndef EV_USE_CLOCK_SYSCALL
221# if __linux && __GLIBC__ >= 2 223# if __linux && __GLIBC__ >= 2
524# include "ev_win32.c" 526# include "ev_win32.c"
525#endif 527#endif
526 528
527/*****************************************************************************/ 529/*****************************************************************************/
528 530
531#if EV_AVOID_STDIO
532static void noinline
533ev_printerr (const char *msg)
534{
535 write (STDERR_FILENO, msg, strlen (msg));
536}
537#endif
538
529static void (*syserr_cb)(const char *msg); 539static void (*syserr_cb)(const char *msg);
530 540
531void 541void
532ev_set_syserr_cb (void (*cb)(const char *msg)) 542ev_set_syserr_cb (void (*cb)(const char *msg))
533{ 543{
543 if (syserr_cb) 553 if (syserr_cb)
544 syserr_cb (msg); 554 syserr_cb (msg);
545 else 555 else
546 { 556 {
547#if EV_AVOID_STDIO 557#if EV_AVOID_STDIO
548 write (STDERR_FILENO, msg, strlen (msg));
549 write (STDERR_FILENO, ": ", 2);
550 msg = strerror (errno); 558 const char *err = strerror (errno);
551 write (STDERR_FILENO, msg, strlen (msg)); 559
552 write (STDERR_FILENO, "\n", 1); 560 ev_printerr (msg);
561 ev_printerr (": ");
562 ev_printerr (err);
563 ev_printerr ("\n");
553#else 564#else
554 perror (msg); 565 perror (msg);
555#endif 566#endif
556 abort (); 567 abort ();
557 } 568 }
558} 569}
559 570
560static void * 571static void *
561ev_realloc_emul (void *ptr, long size) 572ev_realloc_emul (void *ptr, long size)
562{ 573{
574#if __GLIBC__
575 return realloc (ptr, size);
576#else
563 /* some systems, notably openbsd and darwin, fail to properly 577 /* some systems, notably openbsd and darwin, fail to properly
564 * 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
565 * the single unix specification, so work around them here. 579 * the single unix specification, so work around them here.
566 */ 580 */
567 581
568 if (size) 582 if (size)
569 return realloc (ptr, size); 583 return realloc (ptr, size);
570 584
571 free (ptr); 585 free (ptr);
572 return 0; 586 return 0;
587#endif
573} 588}
574 589
575static void *(*alloc)(void *ptr, long size) = ev_realloc_emul; 590static void *(*alloc)(void *ptr, long size) = ev_realloc_emul;
576 591
577void 592void
586 ptr = alloc (ptr, size); 601 ptr = alloc (ptr, size);
587 602
588 if (!ptr && size) 603 if (!ptr && size)
589 { 604 {
590#if EV_AVOID_STDIO 605#if EV_AVOID_STDIO
591 write (STDERR_FILENO, "libev: memory allocation failed, aborting.", 606 ev_printerr ("libev: memory allocation failed, aborting.\n");
592 sizeof ("libev: memory allocation failed, aborting.") - 1);
593#else 607#else
594 fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size); 608 fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size);
595#endif 609#endif
596 abort (); 610 abort ();
597 } 611 }
979 ev_io_stop (EV_A_ w); 993 ev_io_stop (EV_A_ w);
980 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);
981 } 995 }
982} 996}
983 997
984/* check whether the given fd is atcually valid, for error recovery */ 998/* check whether the given fd is actually valid, for error recovery */
985inline_size int 999inline_size int
986fd_valid (int fd) 1000fd_valid (int fd)
987{ 1001{
988#ifdef _WIN32 1002#ifdef _WIN32
989 return EV_FD_TO_WIN32_HANDLE (fd) != -1; 1003 return EV_FD_TO_WIN32_HANDLE (fd) != -1;
1029 { 1043 {
1030 anfds [fd].events = 0; 1044 anfds [fd].events = 0;
1031 anfds [fd].emask = 0; 1045 anfds [fd].emask = 0;
1032 fd_change (EV_A_ fd, EV__IOFDSET | EV_ANFD_REIFY); 1046 fd_change (EV_A_ fd, EV__IOFDSET | EV_ANFD_REIFY);
1033 } 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
1034} 1062}
1035 1063
1036/*****************************************************************************/ 1064/*****************************************************************************/
1037 1065
1038/* 1066/*
1192 1220
1193static ANSIG signals [EV_NSIG - 1]; 1221static ANSIG signals [EV_NSIG - 1];
1194 1222
1195/*****************************************************************************/ 1223/*****************************************************************************/
1196 1224
1197/* used to prepare libev internal fd's */ 1225#if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE
1198/* this is not fork-safe */
1199inline_speed void
1200fd_intern (int fd)
1201{
1202#ifdef _WIN32
1203 unsigned long arg = 1;
1204 ioctlsocket (EV_FD_TO_WIN32_HANDLE (fd), FIONBIO, &arg);
1205#else
1206 fcntl (fd, F_SETFD, FD_CLOEXEC);
1207 fcntl (fd, F_SETFL, O_NONBLOCK);
1208#endif
1209}
1210 1226
1211static void noinline 1227static void noinline
1212evpipe_init (EV_P) 1228evpipe_init (EV_P)
1213{ 1229{
1214 if (!ev_is_active (&pipe_w)) 1230 if (!ev_is_active (&pipe_w))
1215 { 1231 {
1216#if EV_USE_EVENTFD 1232# if EV_USE_EVENTFD
1217 evfd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC); 1233 evfd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC);
1218 if (evfd < 0 && errno == EINVAL) 1234 if (evfd < 0 && errno == EINVAL)
1219 evfd = eventfd (0, 0); 1235 evfd = eventfd (0, 0);
1220 1236
1221 if (evfd >= 0) 1237 if (evfd >= 0)
1223 evpipe [0] = -1; 1239 evpipe [0] = -1;
1224 fd_intern (evfd); /* doing it twice doesn't hurt */ 1240 fd_intern (evfd); /* doing it twice doesn't hurt */
1225 ev_io_set (&pipe_w, evfd, EV_READ); 1241 ev_io_set (&pipe_w, evfd, EV_READ);
1226 } 1242 }
1227 else 1243 else
1228#endif 1244# endif
1229 { 1245 {
1230 while (pipe (evpipe)) 1246 while (pipe (evpipe))
1231 ev_syserr ("(libev) error creating signal/async pipe"); 1247 ev_syserr ("(libev) error creating signal/async pipe");
1232 1248
1233 fd_intern (evpipe [0]); 1249 fd_intern (evpipe [0]);
1244evpipe_write (EV_P_ EV_ATOMIC_T *flag) 1260evpipe_write (EV_P_ EV_ATOMIC_T *flag)
1245{ 1261{
1246 if (!*flag) 1262 if (!*flag)
1247 { 1263 {
1248 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;
1249 1266
1250 *flag = 1; 1267 *flag = 1;
1251 1268
1252#if EV_USE_EVENTFD 1269#if EV_USE_EVENTFD
1253 if (evfd >= 0) 1270 if (evfd >= 0)
1255 uint64_t counter = 1; 1272 uint64_t counter = 1;
1256 write (evfd, &counter, sizeof (uint64_t)); 1273 write (evfd, &counter, sizeof (uint64_t));
1257 } 1274 }
1258 else 1275 else
1259#endif 1276#endif
1260 write (evpipe [1], &old_errno, 1); 1277 write (evpipe [1], &dummy, 1);
1261 1278
1262 errno = old_errno; 1279 errno = old_errno;
1263 } 1280 }
1264} 1281}
1265 1282
1366 break; 1383 break;
1367 } 1384 }
1368} 1385}
1369#endif 1386#endif
1370 1387
1388#endif
1389
1371/*****************************************************************************/ 1390/*****************************************************************************/
1372 1391
1392#if EV_CHILD_ENABLE
1373static WL childs [EV_PID_HASHSIZE]; 1393static WL childs [EV_PID_HASHSIZE];
1374
1375#ifndef _WIN32
1376 1394
1377static ev_signal childev; 1395static ev_signal childev;
1378 1396
1379#ifndef WIFCONTINUED 1397#ifndef WIFCONTINUED
1380# define WIFCONTINUED(status) 0 1398# define WIFCONTINUED(status) 0
1649 if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags); 1667 if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags);
1650#endif 1668#endif
1651 1669
1652 ev_prepare_init (&pending_w, pendingcb); 1670 ev_prepare_init (&pending_w, pendingcb);
1653 1671
1672#if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE
1654 ev_init (&pipe_w, pipecb); 1673 ev_init (&pipe_w, pipecb);
1655 ev_set_priority (&pipe_w, EV_MAXPRI); 1674 ev_set_priority (&pipe_w, EV_MAXPRI);
1675#endif
1656 } 1676 }
1657} 1677}
1658 1678
1659/* free up a loop structure */ 1679/* free up a loop structure */
1660static void noinline 1680static void noinline
1910 1930
1911 assert (checkmax >= checkcnt); 1931 assert (checkmax >= checkcnt);
1912 array_verify (EV_A_ (W *)checks, checkcnt); 1932 array_verify (EV_A_ (W *)checks, checkcnt);
1913 1933
1914# if 0 1934# if 0
1935#if EV_CHILD_ENABLE
1915 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)
1916 for (signum = EV_NSIG; signum--; ) if (signals [signum].pending) 1937 for (signum = EV_NSIG; signum--; ) if (signals [signum].pending)
1938#endif
1917# endif 1939# endif
1918#endif 1940#endif
1919} 1941}
1920#endif 1942#endif
1921 1943
1937 1959
1938 loop_init (EV_A_ flags); 1960 loop_init (EV_A_ flags);
1939 1961
1940 if (ev_backend (EV_A)) 1962 if (ev_backend (EV_A))
1941 { 1963 {
1942#ifndef _WIN32 1964#if EV_CHILD_ENABLE
1943 ev_signal_init (&childev, childcb, SIGCHLD); 1965 ev_signal_init (&childev, childcb, SIGCHLD);
1944 ev_set_priority (&childev, EV_MAXPRI); 1966 ev_set_priority (&childev, EV_MAXPRI);
1945 ev_signal_start (EV_A_ &childev); 1967 ev_signal_start (EV_A_ &childev);
1946 ev_unref (EV_A); /* child watcher should not keep loop alive */ 1968 ev_unref (EV_A); /* child watcher should not keep loop alive */
1947#endif 1969#endif
1960 EV_P = ev_default_loop_ptr; 1982 EV_P = ev_default_loop_ptr;
1961#endif 1983#endif
1962 1984
1963 ev_default_loop_ptr = 0; 1985 ev_default_loop_ptr = 0;
1964 1986
1965#ifndef _WIN32 1987#if EV_CHILD_ENABLE
1966 ev_ref (EV_A); /* child watcher */ 1988 ev_ref (EV_A); /* child watcher */
1967 ev_signal_stop (EV_A_ &childev); 1989 ev_signal_stop (EV_A_ &childev);
1968#endif 1990#endif
1969 1991
1970 loop_destroy (EV_A); 1992 loop_destroy (EV_A);
2712 2734
2713#ifndef SA_RESTART 2735#ifndef SA_RESTART
2714# define SA_RESTART 0 2736# define SA_RESTART 0
2715#endif 2737#endif
2716 2738
2739#if EV_SIGNAL_ENABLE
2740
2717void noinline 2741void noinline
2718ev_signal_start (EV_P_ ev_signal *w) 2742ev_signal_start (EV_P_ ev_signal *w)
2719{ 2743{
2720 if (expect_false (ev_is_active (w))) 2744 if (expect_false (ev_is_active (w)))
2721 return; 2745 return;
2827 } 2851 }
2828 2852
2829 EV_FREQUENT_CHECK; 2853 EV_FREQUENT_CHECK;
2830} 2854}
2831 2855
2856#endif
2857
2858#if EV_CHILD_ENABLE
2859
2832void 2860void
2833ev_child_start (EV_P_ ev_child *w) 2861ev_child_start (EV_P_ ev_child *w)
2834{ 2862{
2835#if EV_MULTIPLICITY 2863#if EV_MULTIPLICITY
2836 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));
2858 wlist_del (&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w); 2886 wlist_del (&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w);
2859 ev_stop (EV_A_ (W)w); 2887 ev_stop (EV_A_ (W)w);
2860 2888
2861 EV_FREQUENT_CHECK; 2889 EV_FREQUENT_CHECK;
2862} 2890}
2891
2892#endif
2863 2893
2864#if EV_STAT_ENABLE 2894#if EV_STAT_ENABLE
2865 2895
2866# ifdef _WIN32 2896# ifdef _WIN32
2867# undef lstat 2897# undef lstat

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines