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

Comparing libev/ev.c (file contents):
Revision 1.310 by root, Sun Jul 26 04:43:03 2009 UTC vs.
Revision 1.316 by root, Fri Sep 18 21:02:12 2009 UTC

302# define EV_USE_EVENTFD 0 302# define EV_USE_EVENTFD 0
303# endif 303# endif
304#endif 304#endif
305 305
306#ifndef EV_USE_SIGNALFD 306#ifndef EV_USE_SIGNALFD
307# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 9)) 307# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7))
308# define EV_USE_SIGNALFD 1 308# define EV_USE_SIGNALFD 1
309# else 309# else
310# define EV_USE_SIGNALFD 0 310# define EV_USE_SIGNALFD 0
311# endif 311# endif
312#endif 312#endif
386# include <stdint.h> 386# include <stdint.h>
387# ifndef EFD_NONBLOCK 387# ifndef EFD_NONBLOCK
388# define EFD_NONBLOCK O_NONBLOCK 388# define EFD_NONBLOCK O_NONBLOCK
389# endif 389# endif
390# ifndef EFD_CLOEXEC 390# ifndef EFD_CLOEXEC
391# ifdef O_CLOEXEC
391# define EFD_CLOEXEC O_CLOEXEC 392# define EFD_CLOEXEC O_CLOEXEC
393# else
394# define EFD_CLOEXEC 02000000
395# endif
392# endif 396# endif
393# ifdef __cplusplus 397# ifdef __cplusplus
394extern "C" { 398extern "C" {
395# endif 399# endif
396int eventfd (unsigned int initval, int flags); 400int eventfd (unsigned int initval, int flags);
398} 402}
399# endif 403# endif
400#endif 404#endif
401 405
402#if EV_USE_SIGNALFD 406#if EV_USE_SIGNALFD
403# include <sys/signalfd.h> 407/* our minimum requirement is glibc 2.7 which has the stub, but not the header */
408# include <stdint.h>
409# ifndef SFD_NONBLOCK
410# define SFD_NONBLOCK O_NONBLOCK
404#endif 411# endif
412# ifndef SFD_CLOEXEC
413# ifdef O_CLOEXEC
414# define SFD_CLOEXEC O_CLOEXEC
415# else
416# define SFD_CLOEXEC 02000000
417# endif
418# endif
419# ifdef __cplusplus
420extern "C" {
421# endif
422int signalfd (int fd, const sigset_t *mask, int flags);
423
424struct signalfd_siginfo
425{
426 uint32_t ssi_signo;
427 char pad[128 - sizeof (uint32_t)];
428};
429# ifdef __cplusplus
430}
431# endif
432#endif
433
405 434
406/**/ 435/**/
407 436
408#if EV_VERIFY >= 3 437#if EV_VERIFY >= 3
409# define EV_FREQUENT_CHECK ev_loop_verify (EV_A) 438# define EV_FREQUENT_CHECK ev_loop_verify (EV_A)
421 */ 450 */
422#define TIME_EPSILON 0.0001220703125 /* 1/8192 */ 451#define TIME_EPSILON 0.0001220703125 /* 1/8192 */
423 452
424#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 453#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
425#define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */ 454#define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */
426/*#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds, TODO */
427 455
428#if __GNUC__ >= 4 456#if __GNUC__ >= 4
429# define expect(expr,value) __builtin_expect ((expr),(value)) 457# define expect(expr,value) __builtin_expect ((expr),(value))
430# define noinline __attribute__ ((noinline)) 458# define noinline __attribute__ ((noinline))
431#else 459#else
470static EV_ATOMIC_T have_realtime; /* did clock_gettime (CLOCK_REALTIME) work? */ 498static EV_ATOMIC_T have_realtime; /* did clock_gettime (CLOCK_REALTIME) work? */
471#endif 499#endif
472 500
473#if EV_USE_MONOTONIC 501#if EV_USE_MONOTONIC
474static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 502static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
503#endif
504
505#ifndef EV_FD_TO_WIN32_HANDLE
506# define EV_FD_TO_WIN32_HANDLE(fd) _get_osfhandle (fd)
507#endif
508#ifndef EV_WIN32_HANDLE_TO_FD
509# define EV_WIN32_HANDLE_TO_FD(handle) _open_osfhandle (fd, 0)
510#endif
511#ifndef EV_WIN32_CLOSE_FD
512# define EV_WIN32_CLOSE_FD(fd) close (fd)
475#endif 513#endif
476 514
477#ifdef _WIN32 515#ifdef _WIN32
478# include "ev_win32.c" 516# include "ev_win32.c"
479#endif 517#endif
872 910
873#if EV_SELECT_IS_WINSOCKET 911#if EV_SELECT_IS_WINSOCKET
874 if (events) 912 if (events)
875 { 913 {
876 unsigned long arg; 914 unsigned long arg;
877 #ifdef EV_FD_TO_WIN32_HANDLE
878 anfd->handle = EV_FD_TO_WIN32_HANDLE (fd); 915 anfd->handle = EV_FD_TO_WIN32_HANDLE (fd);
879 #else
880 anfd->handle = _get_osfhandle (fd);
881 #endif
882 assert (("libev: only socket fds supported in this configuration", ioctlsocket (anfd->handle, FIONREAD, &arg) == 0)); 916 assert (("libev: only socket fds supported in this configuration", ioctlsocket (anfd->handle, FIONREAD, &arg) == 0));
883 } 917 }
884#endif 918#endif
885 919
886 { 920 {
1617 close (evfd); 1651 close (evfd);
1618#endif 1652#endif
1619 1653
1620 if (evpipe [0] >= 0) 1654 if (evpipe [0] >= 0)
1621 { 1655 {
1622 close (evpipe [0]); 1656 EV_WIN32_CLOSE_FD (evpipe [0]);
1623 close (evpipe [1]); 1657 EV_WIN32_CLOSE_FD (evpipe [1]);
1624 } 1658 }
1625 } 1659 }
1626 1660
1627#if EV_USE_SIGNALFD 1661#if EV_USE_SIGNALFD
1628 if (ev_is_active (&sigfd_w)) 1662 if (ev_is_active (&sigfd_w))
1724 close (evfd); 1758 close (evfd);
1725#endif 1759#endif
1726 1760
1727 if (evpipe [0] >= 0) 1761 if (evpipe [0] >= 0)
1728 { 1762 {
1729 close (evpipe [0]); 1763 EV_WIN32_CLOSE_FD (evpipe [0]);
1730 close (evpipe [1]); 1764 EV_WIN32_CLOSE_FD (evpipe [1]);
1731 } 1765 }
1732 1766
1733 evpipe_init (EV_A); 1767 evpipe_init (EV_A);
1734 /* now iterate over everything, in case we missed something */ 1768 /* now iterate over everything, in case we missed something */
1735 pipecb (EV_A_ &pipe_w, EV_READ); 1769 pipecb (EV_A_ &pipe_w, EV_READ);
2857 } 2891 }
2858 } 2892 }
2859 2893
2860 if (w->wd >= 0) 2894 if (w->wd >= 0)
2861 { 2895 {
2896 struct statfs sfs;
2897
2862 wlist_add (&fs_hash [w->wd & (EV_INOTIFY_HASHSIZE - 1)].head, (WL)w); 2898 wlist_add (&fs_hash [w->wd & (EV_INOTIFY_HASHSIZE - 1)].head, (WL)w);
2863 2899
2864 /* now local changes will be tracked by inotify, but remote changes won't */ 2900 /* now local changes will be tracked by inotify, but remote changes won't */
2865 /* unless the filesystem it known to be local, we therefore still poll */ 2901 /* unless the filesystem it known to be local, we therefore still poll */
2866 /* also do poll on <2.6.25, but with normal frequency */ 2902 /* also do poll on <2.6.25, but with normal frequency */
2867 struct statfs sfs;
2868 2903
2869 if (fs_2625 && !statfs (w->path, &sfs)) 2904 if (fs_2625 && !statfs (w->path, &sfs))
2870 if (sfs.f_type == 0x1373 /* devfs */ 2905 if (sfs.f_type == 0x1373 /* devfs */
2871 || sfs.f_type == 0xEF53 /* ext2/3 */ 2906 || sfs.f_type == 0xEF53 /* ext2/3 */
2872 || sfs.f_type == 0x3153464a /* jfs */ 2907 || sfs.f_type == 0x3153464a /* jfs */
2961 return; 2996 return;
2962 2997
2963 fs_2625 = 1; 2998 fs_2625 = 1;
2964} 2999}
2965 3000
3001inline_size int
3002infy_newfd (void)
3003{
3004#if defined (IN_CLOEXEC) && defined (IN_NONBLOCK)
3005 int fd = inotify_init1 (IN_CLOEXEC | IN_NONBLOCK);
3006 if (fd >= 0)
3007 return fd;
3008#endif
3009 return inotify_init ();
3010}
3011
2966inline_size void 3012inline_size void
2967infy_init (EV_P) 3013infy_init (EV_P)
2968{ 3014{
2969 if (fs_fd != -2) 3015 if (fs_fd != -2)
2970 return; 3016 return;
2971 3017
2972 fs_fd = -1; 3018 fs_fd = -1;
2973 3019
2974 check_2625 (EV_A); 3020 check_2625 (EV_A);
2975 3021
2976 fs_fd = inotify_init (); 3022 fs_fd = infy_newfd ();
2977 3023
2978 if (fs_fd >= 0) 3024 if (fs_fd >= 0)
2979 { 3025 {
3026 fd_intern (fs_fd);
2980 ev_io_init (&fs_w, infy_cb, fs_fd, EV_READ); 3027 ev_io_init (&fs_w, infy_cb, fs_fd, EV_READ);
2981 ev_set_priority (&fs_w, EV_MAXPRI); 3028 ev_set_priority (&fs_w, EV_MAXPRI);
2982 ev_io_start (EV_A_ &fs_w); 3029 ev_io_start (EV_A_ &fs_w);
2983 } 3030 }
2984} 3031}
2989 int slot; 3036 int slot;
2990 3037
2991 if (fs_fd < 0) 3038 if (fs_fd < 0)
2992 return; 3039 return;
2993 3040
3041 ev_io_stop (EV_A_ &fs_w);
2994 close (fs_fd); 3042 close (fs_fd);
2995 fs_fd = inotify_init (); 3043 fs_fd = infy_newfd ();
3044
3045 if (fs_fd >= 0)
3046 {
3047 fd_intern (fs_fd);
3048 ev_io_set (&fs_w, fs_fd, EV_READ);
3049 ev_io_start (EV_A_ &fs_w);
3050 }
2996 3051
2997 for (slot = 0; slot < EV_INOTIFY_HASHSIZE; ++slot) 3052 for (slot = 0; slot < EV_INOTIFY_HASHSIZE; ++slot)
2998 { 3053 {
2999 WL w_ = fs_hash [slot].head; 3054 WL w_ = fs_hash [slot].head;
3000 fs_hash [slot].head = 0; 3055 fs_hash [slot].head = 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines