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

Comparing libev/ev.c (file contents):
Revision 1.314 by root, Wed Aug 26 17:31:20 2009 UTC vs.
Revision 1.318 by root, Tue Nov 17 00:22:28 2009 UTC

450 */ 450 */
451#define TIME_EPSILON 0.0001220703125 /* 1/8192 */ 451#define TIME_EPSILON 0.0001220703125 /* 1/8192 */
452 452
453#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) */
454#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) */
455/*#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds, TODO */
456 455
457#if __GNUC__ >= 4 456#if __GNUC__ >= 4
458# define expect(expr,value) __builtin_expect ((expr),(value)) 457# define expect(expr,value) __builtin_expect ((expr),(value))
459# define noinline __attribute__ ((noinline)) 458# define noinline __attribute__ ((noinline))
460#else 459#else
1659 } 1658 }
1660 } 1659 }
1661 1660
1662#if EV_USE_SIGNALFD 1661#if EV_USE_SIGNALFD
1663 if (ev_is_active (&sigfd_w)) 1662 if (ev_is_active (&sigfd_w))
1664 {
1665 /*ev_ref (EV_A);*/
1666 /*ev_io_stop (EV_A_ &sigfd_w);*/
1667
1668 close (sigfd); 1663 close (sigfd);
1669 }
1670#endif 1664#endif
1671 1665
1672#if EV_USE_INOTIFY 1666#if EV_USE_INOTIFY
1673 if (fs_fd >= 0) 1667 if (fs_fd >= 0)
1674 close (fs_fd); 1668 close (fs_fd);
2753# if EV_USE_SIGNALFD 2747# if EV_USE_SIGNALFD
2754 if (sigfd < 0) /*TODO*/ 2748 if (sigfd < 0) /*TODO*/
2755# endif 2749# endif
2756 { 2750 {
2757# if _WIN32 2751# if _WIN32
2752 evpipe_init (EV_A);
2753
2758 signal (w->signum, ev_sighandler); 2754 signal (w->signum, ev_sighandler);
2759# else 2755# else
2760 struct sigaction sa; 2756 struct sigaction sa;
2761 2757
2762 evpipe_init (EV_A); 2758 evpipe_init (EV_A);
2860static void noinline 2856static void noinline
2861infy_add (EV_P_ ev_stat *w) 2857infy_add (EV_P_ ev_stat *w)
2862{ 2858{
2863 w->wd = inotify_add_watch (fs_fd, w->path, IN_ATTRIB | IN_DELETE_SELF | IN_MOVE_SELF | IN_MODIFY | IN_DONT_FOLLOW | IN_MASK_ADD); 2859 w->wd = inotify_add_watch (fs_fd, w->path, IN_ATTRIB | IN_DELETE_SELF | IN_MOVE_SELF | IN_MODIFY | IN_DONT_FOLLOW | IN_MASK_ADD);
2864 2860
2865 if (w->wd < 0) 2861 if (w->wd >= 0)
2862 {
2863 struct statfs sfs;
2864
2865 /* now local changes will be tracked by inotify, but remote changes won't */
2866 /* unless the filesystem is known to be local, we therefore still poll */
2867 /* also do poll on <2.6.25, but with normal frequency */
2868
2869 if (!fs_2625)
2870 w->timer.repeat = w->interval ? w->interval : DEF_STAT_INTERVAL;
2871 else if (!statfs (w->path, &sfs)
2872 && (sfs.f_type == 0x1373 /* devfs */
2873 || sfs.f_type == 0xEF53 /* ext2/3 */
2874 || sfs.f_type == 0x3153464a /* jfs */
2875 || sfs.f_type == 0x52654973 /* reiser3 */
2876 || sfs.f_type == 0x01021994 /* tempfs */
2877 || sfs.f_type == 0x58465342 /* xfs */))
2878 w->timer.repeat = 0.; /* filesystem is local, kernel new enough */
2879 else
2880 w->timer.repeat = w->interval ? w->interval : NFS_STAT_INTERVAL; /* remote, use reduced frequency */
2866 { 2881 }
2882 else
2883 {
2884 /* can't use inotify, continue to stat */
2867 w->timer.repeat = w->interval ? w->interval : DEF_STAT_INTERVAL; 2885 w->timer.repeat = w->interval ? w->interval : DEF_STAT_INTERVAL;
2868 ev_timer_again (EV_A_ &w->timer); /* this is not race-free, so we still need to recheck periodically */
2869 2886
2870 /* monitor some parent directory for speedup hints */ 2887 /* if path is not there, monitor some parent directory for speedup hints */
2871 /* note that exceeding the hardcoded path limit is not a correctness issue, */ 2888 /* note that exceeding the hardcoded path limit is not a correctness issue, */
2872 /* but an efficiency issue only */ 2889 /* but an efficiency issue only */
2873 if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096) 2890 if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096)
2874 { 2891 {
2875 char path [4096]; 2892 char path [4096];
2891 while (w->wd < 0 && (errno == ENOENT || errno == EACCES)); 2908 while (w->wd < 0 && (errno == ENOENT || errno == EACCES));
2892 } 2909 }
2893 } 2910 }
2894 2911
2895 if (w->wd >= 0) 2912 if (w->wd >= 0)
2896 {
2897 struct statfs sfs;
2898
2899 wlist_add (&fs_hash [w->wd & (EV_INOTIFY_HASHSIZE - 1)].head, (WL)w); 2913 wlist_add (&fs_hash [w->wd & (EV_INOTIFY_HASHSIZE - 1)].head, (WL)w);
2900 2914
2901 /* now local changes will be tracked by inotify, but remote changes won't */ 2915 /* now re-arm timer, if required */
2902 /* unless the filesystem it known to be local, we therefore still poll */ 2916 if (ev_is_active (&w->timer)) ev_ref (EV_A);
2903 /* also do poll on <2.6.25, but with normal frequency */
2904
2905 if (fs_2625 && !statfs (w->path, &sfs))
2906 if (sfs.f_type == 0x1373 /* devfs */
2907 || sfs.f_type == 0xEF53 /* ext2/3 */
2908 || sfs.f_type == 0x3153464a /* jfs */
2909 || sfs.f_type == 0x52654973 /* reiser3 */
2910 || sfs.f_type == 0x01021994 /* tempfs */
2911 || sfs.f_type == 0x58465342 /* xfs */)
2912 return;
2913
2914 w->timer.repeat = w->interval ? w->interval : fs_2625 ? NFS_STAT_INTERVAL : DEF_STAT_INTERVAL;
2915 ev_timer_again (EV_A_ &w->timer); 2917 ev_timer_again (EV_A_ &w->timer);
2916 } 2918 if (ev_is_active (&w->timer)) ev_unref (EV_A);
2917} 2919}
2918 2920
2919static void noinline 2921static void noinline
2920infy_del (EV_P_ ev_stat *w) 2922infy_del (EV_P_ ev_stat *w)
2921{ 2923{
2997 return; 2999 return;
2998 3000
2999 fs_2625 = 1; 3001 fs_2625 = 1;
3000} 3002}
3001 3003
3004inline_size int
3005infy_newfd (void)
3006{
3007#if defined (IN_CLOEXEC) && defined (IN_NONBLOCK)
3008 int fd = inotify_init1 (IN_CLOEXEC | IN_NONBLOCK);
3009 if (fd >= 0)
3010 return fd;
3011#endif
3012 return inotify_init ();
3013}
3014
3002inline_size void 3015inline_size void
3003infy_init (EV_P) 3016infy_init (EV_P)
3004{ 3017{
3005 if (fs_fd != -2) 3018 if (fs_fd != -2)
3006 return; 3019 return;
3007 3020
3008 fs_fd = -1; 3021 fs_fd = -1;
3009 3022
3010 check_2625 (EV_A); 3023 check_2625 (EV_A);
3011 3024
3012 fs_fd = inotify_init (); 3025 fs_fd = infy_newfd ();
3013 3026
3014 if (fs_fd >= 0) 3027 if (fs_fd >= 0)
3015 { 3028 {
3029 fd_intern (fs_fd);
3016 ev_io_init (&fs_w, infy_cb, fs_fd, EV_READ); 3030 ev_io_init (&fs_w, infy_cb, fs_fd, EV_READ);
3017 ev_set_priority (&fs_w, EV_MAXPRI); 3031 ev_set_priority (&fs_w, EV_MAXPRI);
3018 ev_io_start (EV_A_ &fs_w); 3032 ev_io_start (EV_A_ &fs_w);
3033 ev_unref (EV_A);
3019 } 3034 }
3020} 3035}
3021 3036
3022inline_size void 3037inline_size void
3023infy_fork (EV_P) 3038infy_fork (EV_P)
3025 int slot; 3040 int slot;
3026 3041
3027 if (fs_fd < 0) 3042 if (fs_fd < 0)
3028 return; 3043 return;
3029 3044
3045 ev_ref (EV_A);
3046 ev_io_stop (EV_A_ &fs_w);
3030 close (fs_fd); 3047 close (fs_fd);
3031 fs_fd = inotify_init (); 3048 fs_fd = infy_newfd ();
3049
3050 if (fs_fd >= 0)
3051 {
3052 fd_intern (fs_fd);
3053 ev_io_set (&fs_w, fs_fd, EV_READ);
3054 ev_io_start (EV_A_ &fs_w);
3055 ev_unref (EV_A);
3056 }
3032 3057
3033 for (slot = 0; slot < EV_INOTIFY_HASHSIZE; ++slot) 3058 for (slot = 0; slot < EV_INOTIFY_HASHSIZE; ++slot)
3034 { 3059 {
3035 WL w_ = fs_hash [slot].head; 3060 WL w_ = fs_hash [slot].head;
3036 fs_hash [slot].head = 0; 3061 fs_hash [slot].head = 0;
3043 w->wd = -1; 3068 w->wd = -1;
3044 3069
3045 if (fs_fd >= 0) 3070 if (fs_fd >= 0)
3046 infy_add (EV_A_ w); /* re-add, no matter what */ 3071 infy_add (EV_A_ w); /* re-add, no matter what */
3047 else 3072 else
3073 {
3074 w->timer.repeat = w->interval ? w->interval : DEF_STAT_INTERVAL;
3075 if (ev_is_active (&w->timer)) ev_ref (EV_A);
3048 ev_timer_again (EV_A_ &w->timer); 3076 ev_timer_again (EV_A_ &w->timer);
3077 if (ev_is_active (&w->timer)) ev_unref (EV_A);
3078 }
3049 } 3079 }
3050 } 3080 }
3051} 3081}
3052 3082
3053#endif 3083#endif
3123 3153
3124 if (fs_fd >= 0) 3154 if (fs_fd >= 0)
3125 infy_add (EV_A_ w); 3155 infy_add (EV_A_ w);
3126 else 3156 else
3127#endif 3157#endif
3158 {
3128 ev_timer_again (EV_A_ &w->timer); 3159 ev_timer_again (EV_A_ &w->timer);
3160 ev_unref (EV_A);
3161 }
3129 3162
3130 ev_start (EV_A_ (W)w, 1); 3163 ev_start (EV_A_ (W)w, 1);
3131 3164
3132 EV_FREQUENT_CHECK; 3165 EV_FREQUENT_CHECK;
3133} 3166}
3142 EV_FREQUENT_CHECK; 3175 EV_FREQUENT_CHECK;
3143 3176
3144#if EV_USE_INOTIFY 3177#if EV_USE_INOTIFY
3145 infy_del (EV_A_ w); 3178 infy_del (EV_A_ w);
3146#endif 3179#endif
3180
3181 if (ev_is_active (&w->timer))
3182 {
3183 ev_ref (EV_A);
3147 ev_timer_stop (EV_A_ &w->timer); 3184 ev_timer_stop (EV_A_ &w->timer);
3185 }
3148 3186
3149 ev_stop (EV_A_ (W)w); 3187 ev_stop (EV_A_ (W)w);
3150 3188
3151 EV_FREQUENT_CHECK; 3189 EV_FREQUENT_CHECK;
3152} 3190}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines