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.320 by root, Fri Dec 4 20:25:06 2009 UTC

153 153
154#endif 154#endif
155 155
156#include <math.h> 156#include <math.h>
157#include <stdlib.h> 157#include <stdlib.h>
158#include <string.h>
158#include <fcntl.h> 159#include <fcntl.h>
159#include <stddef.h> 160#include <stddef.h>
160 161
161#include <stdio.h> 162#include <stdio.h>
162 163
450 */ 451 */
451#define TIME_EPSILON 0.0001220703125 /* 1/8192 */ 452#define TIME_EPSILON 0.0001220703125 /* 1/8192 */
452 453
453#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 454#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) */ 455#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 456
457#if __GNUC__ >= 4 457#if __GNUC__ >= 4
458# define expect(expr,value) __builtin_expect ((expr),(value)) 458# define expect(expr,value) __builtin_expect ((expr),(value))
459# define noinline __attribute__ ((noinline)) 459# define noinline __attribute__ ((noinline))
460#else 460#else
1659 } 1659 }
1660 } 1660 }
1661 1661
1662#if EV_USE_SIGNALFD 1662#if EV_USE_SIGNALFD
1663 if (ev_is_active (&sigfd_w)) 1663 if (ev_is_active (&sigfd_w))
1664 {
1665 /*ev_ref (EV_A);*/
1666 /*ev_io_stop (EV_A_ &sigfd_w);*/
1667
1668 close (sigfd); 1664 close (sigfd);
1669 }
1670#endif 1665#endif
1671 1666
1672#if EV_USE_INOTIFY 1667#if EV_USE_INOTIFY
1673 if (fs_fd >= 0) 1668 if (fs_fd >= 0)
1674 close (fs_fd); 1669 close (fs_fd);
2753# if EV_USE_SIGNALFD 2748# if EV_USE_SIGNALFD
2754 if (sigfd < 0) /*TODO*/ 2749 if (sigfd < 0) /*TODO*/
2755# endif 2750# endif
2756 { 2751 {
2757# if _WIN32 2752# if _WIN32
2753 evpipe_init (EV_A);
2754
2758 signal (w->signum, ev_sighandler); 2755 signal (w->signum, ev_sighandler);
2759# else 2756# else
2760 struct sigaction sa; 2757 struct sigaction sa;
2761 2758
2762 evpipe_init (EV_A); 2759 evpipe_init (EV_A);
2860static void noinline 2857static void noinline
2861infy_add (EV_P_ ev_stat *w) 2858infy_add (EV_P_ ev_stat *w)
2862{ 2859{
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); 2860 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 2861
2865 if (w->wd < 0) 2862 if (w->wd >= 0)
2863 {
2864 struct statfs sfs;
2865
2866 /* now local changes will be tracked by inotify, but remote changes won't */
2867 /* unless the filesystem is known to be local, we therefore still poll */
2868 /* also do poll on <2.6.25, but with normal frequency */
2869
2870 if (!fs_2625)
2871 w->timer.repeat = w->interval ? w->interval : DEF_STAT_INTERVAL;
2872 else if (!statfs (w->path, &sfs)
2873 && (sfs.f_type == 0x1373 /* devfs */
2874 || sfs.f_type == 0xEF53 /* ext2/3 */
2875 || sfs.f_type == 0x3153464a /* jfs */
2876 || sfs.f_type == 0x52654973 /* reiser3 */
2877 || sfs.f_type == 0x01021994 /* tempfs */
2878 || sfs.f_type == 0x58465342 /* xfs */))
2879 w->timer.repeat = 0.; /* filesystem is local, kernel new enough */
2880 else
2881 w->timer.repeat = w->interval ? w->interval : NFS_STAT_INTERVAL; /* remote, use reduced frequency */
2866 { 2882 }
2883 else
2884 {
2885 /* can't use inotify, continue to stat */
2867 w->timer.repeat = w->interval ? w->interval : DEF_STAT_INTERVAL; 2886 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 2887
2870 /* monitor some parent directory for speedup hints */ 2888 /* 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, */ 2889 /* note that exceeding the hardcoded path limit is not a correctness issue, */
2872 /* but an efficiency issue only */ 2890 /* but an efficiency issue only */
2873 if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096) 2891 if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096)
2874 { 2892 {
2875 char path [4096]; 2893 char path [4096];
2891 while (w->wd < 0 && (errno == ENOENT || errno == EACCES)); 2909 while (w->wd < 0 && (errno == ENOENT || errno == EACCES));
2892 } 2910 }
2893 } 2911 }
2894 2912
2895 if (w->wd >= 0) 2913 if (w->wd >= 0)
2896 {
2897 struct statfs sfs;
2898
2899 wlist_add (&fs_hash [w->wd & (EV_INOTIFY_HASHSIZE - 1)].head, (WL)w); 2914 wlist_add (&fs_hash [w->wd & (EV_INOTIFY_HASHSIZE - 1)].head, (WL)w);
2900 2915
2901 /* now local changes will be tracked by inotify, but remote changes won't */ 2916 /* now re-arm timer, if required */
2902 /* unless the filesystem it known to be local, we therefore still poll */ 2917 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); 2918 ev_timer_again (EV_A_ &w->timer);
2916 } 2919 if (ev_is_active (&w->timer)) ev_unref (EV_A);
2917} 2920}
2918 2921
2919static void noinline 2922static void noinline
2920infy_del (EV_P_ ev_stat *w) 2923infy_del (EV_P_ ev_stat *w)
2921{ 2924{
2997 return; 3000 return;
2998 3001
2999 fs_2625 = 1; 3002 fs_2625 = 1;
3000} 3003}
3001 3004
3005inline_size int
3006infy_newfd (void)
3007{
3008#if defined (IN_CLOEXEC) && defined (IN_NONBLOCK)
3009 int fd = inotify_init1 (IN_CLOEXEC | IN_NONBLOCK);
3010 if (fd >= 0)
3011 return fd;
3012#endif
3013 return inotify_init ();
3014}
3015
3002inline_size void 3016inline_size void
3003infy_init (EV_P) 3017infy_init (EV_P)
3004{ 3018{
3005 if (fs_fd != -2) 3019 if (fs_fd != -2)
3006 return; 3020 return;
3007 3021
3008 fs_fd = -1; 3022 fs_fd = -1;
3009 3023
3010 check_2625 (EV_A); 3024 check_2625 (EV_A);
3011 3025
3012 fs_fd = inotify_init (); 3026 fs_fd = infy_newfd ();
3013 3027
3014 if (fs_fd >= 0) 3028 if (fs_fd >= 0)
3015 { 3029 {
3030 fd_intern (fs_fd);
3016 ev_io_init (&fs_w, infy_cb, fs_fd, EV_READ); 3031 ev_io_init (&fs_w, infy_cb, fs_fd, EV_READ);
3017 ev_set_priority (&fs_w, EV_MAXPRI); 3032 ev_set_priority (&fs_w, EV_MAXPRI);
3018 ev_io_start (EV_A_ &fs_w); 3033 ev_io_start (EV_A_ &fs_w);
3034 ev_unref (EV_A);
3019 } 3035 }
3020} 3036}
3021 3037
3022inline_size void 3038inline_size void
3023infy_fork (EV_P) 3039infy_fork (EV_P)
3025 int slot; 3041 int slot;
3026 3042
3027 if (fs_fd < 0) 3043 if (fs_fd < 0)
3028 return; 3044 return;
3029 3045
3046 ev_ref (EV_A);
3047 ev_io_stop (EV_A_ &fs_w);
3030 close (fs_fd); 3048 close (fs_fd);
3031 fs_fd = inotify_init (); 3049 fs_fd = infy_newfd ();
3050
3051 if (fs_fd >= 0)
3052 {
3053 fd_intern (fs_fd);
3054 ev_io_set (&fs_w, fs_fd, EV_READ);
3055 ev_io_start (EV_A_ &fs_w);
3056 ev_unref (EV_A);
3057 }
3032 3058
3033 for (slot = 0; slot < EV_INOTIFY_HASHSIZE; ++slot) 3059 for (slot = 0; slot < EV_INOTIFY_HASHSIZE; ++slot)
3034 { 3060 {
3035 WL w_ = fs_hash [slot].head; 3061 WL w_ = fs_hash [slot].head;
3036 fs_hash [slot].head = 0; 3062 fs_hash [slot].head = 0;
3043 w->wd = -1; 3069 w->wd = -1;
3044 3070
3045 if (fs_fd >= 0) 3071 if (fs_fd >= 0)
3046 infy_add (EV_A_ w); /* re-add, no matter what */ 3072 infy_add (EV_A_ w); /* re-add, no matter what */
3047 else 3073 else
3074 {
3075 w->timer.repeat = w->interval ? w->interval : DEF_STAT_INTERVAL;
3076 if (ev_is_active (&w->timer)) ev_ref (EV_A);
3048 ev_timer_again (EV_A_ &w->timer); 3077 ev_timer_again (EV_A_ &w->timer);
3078 if (ev_is_active (&w->timer)) ev_unref (EV_A);
3079 }
3049 } 3080 }
3050 } 3081 }
3051} 3082}
3052 3083
3053#endif 3084#endif
3070static void noinline 3101static void noinline
3071stat_timer_cb (EV_P_ ev_timer *w_, int revents) 3102stat_timer_cb (EV_P_ ev_timer *w_, int revents)
3072{ 3103{
3073 ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer)); 3104 ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer));
3074 3105
3075 /* we copy this here each the time so that */ 3106 ev_statdata prev = w->attr;
3076 /* prev has the old value when the callback gets invoked */
3077 w->prev = w->attr;
3078 ev_stat_stat (EV_A_ w); 3107 ev_stat_stat (EV_A_ w);
3079 3108
3080 /* memcmp doesn't work on netbsd, they.... do stuff to their struct stat */ 3109 /* memcmp doesn't work on netbsd, they.... do stuff to their struct stat */
3081 if ( 3110 if (
3082 w->prev.st_dev != w->attr.st_dev 3111 prev.st_dev != w->attr.st_dev
3083 || w->prev.st_ino != w->attr.st_ino 3112 || prev.st_ino != w->attr.st_ino
3084 || w->prev.st_mode != w->attr.st_mode 3113 || prev.st_mode != w->attr.st_mode
3085 || w->prev.st_nlink != w->attr.st_nlink 3114 || prev.st_nlink != w->attr.st_nlink
3086 || w->prev.st_uid != w->attr.st_uid 3115 || prev.st_uid != w->attr.st_uid
3087 || w->prev.st_gid != w->attr.st_gid 3116 || prev.st_gid != w->attr.st_gid
3088 || w->prev.st_rdev != w->attr.st_rdev 3117 || prev.st_rdev != w->attr.st_rdev
3089 || w->prev.st_size != w->attr.st_size 3118 || prev.st_size != w->attr.st_size
3090 || w->prev.st_atime != w->attr.st_atime 3119 || prev.st_atime != w->attr.st_atime
3091 || w->prev.st_mtime != w->attr.st_mtime 3120 || prev.st_mtime != w->attr.st_mtime
3092 || w->prev.st_ctime != w->attr.st_ctime 3121 || prev.st_ctime != w->attr.st_ctime
3093 ) { 3122 ) {
3123 /* we only update w->prev on actual differences */
3124 /* in case we test more often than invoke the callback, */
3125 /* to ensure that prev is always different to attr */
3126 w->prev = prev;
3127
3094 #if EV_USE_INOTIFY 3128 #if EV_USE_INOTIFY
3095 if (fs_fd >= 0) 3129 if (fs_fd >= 0)
3096 { 3130 {
3097 infy_del (EV_A_ w); 3131 infy_del (EV_A_ w);
3098 infy_add (EV_A_ w); 3132 infy_add (EV_A_ w);
3123 3157
3124 if (fs_fd >= 0) 3158 if (fs_fd >= 0)
3125 infy_add (EV_A_ w); 3159 infy_add (EV_A_ w);
3126 else 3160 else
3127#endif 3161#endif
3162 {
3128 ev_timer_again (EV_A_ &w->timer); 3163 ev_timer_again (EV_A_ &w->timer);
3164 ev_unref (EV_A);
3165 }
3129 3166
3130 ev_start (EV_A_ (W)w, 1); 3167 ev_start (EV_A_ (W)w, 1);
3131 3168
3132 EV_FREQUENT_CHECK; 3169 EV_FREQUENT_CHECK;
3133} 3170}
3142 EV_FREQUENT_CHECK; 3179 EV_FREQUENT_CHECK;
3143 3180
3144#if EV_USE_INOTIFY 3181#if EV_USE_INOTIFY
3145 infy_del (EV_A_ w); 3182 infy_del (EV_A_ w);
3146#endif 3183#endif
3184
3185 if (ev_is_active (&w->timer))
3186 {
3187 ev_ref (EV_A);
3147 ev_timer_stop (EV_A_ &w->timer); 3188 ev_timer_stop (EV_A_ &w->timer);
3189 }
3148 3190
3149 ev_stop (EV_A_ (W)w); 3191 ev_stop (EV_A_ (W)w);
3150 3192
3151 EV_FREQUENT_CHECK; 3193 EV_FREQUENT_CHECK;
3152} 3194}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines