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

Comparing libev/ev.c (file contents):
Revision 1.315 by root, Wed Aug 26 17:46:22 2009 UTC vs.
Revision 1.321 by root, Thu Dec 31 06:50:17 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
1605#endif 1605#endif
1606#if EV_USE_INOTIFY 1606#if EV_USE_INOTIFY
1607 fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2; 1607 fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2;
1608#endif 1608#endif
1609#if EV_USE_SIGNALFD 1609#if EV_USE_SIGNALFD
1610 sigfd = flags & EVFLAG_NOSIGFD ? -1 : -2; 1610 sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1;
1611#endif 1611#endif
1612 1612
1613 if (!(flags & 0x0000ffffU)) 1613 if (!(flags & 0x0000ffffU))
1614 flags |= ev_recommended_backends (); 1614 flags |= ev_recommended_backends ();
1615 1615
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);
2793 signals [w->signum - 1].loop = 0; /* unattach from signal */ 2790 signals [w->signum - 1].loop = 0; /* unattach from signal */
2794#endif 2791#endif
2795#if EV_USE_SIGNALFD 2792#if EV_USE_SIGNALFD
2796 if (sigfd >= 0) 2793 if (sigfd >= 0)
2797 { 2794 {
2798 sigprocmask (SIG_UNBLOCK, &sigfd_set, 0);//D 2795 sigset_t ss;
2796
2797 sigemptyset (&ss);
2798 sigaddset (&ss, w->signum);
2799 sigdelset (&sigfd_set, w->signum); 2799 sigdelset (&sigfd_set, w->signum);
2800
2800 signalfd (sigfd, &sigfd_set, 0); 2801 signalfd (sigfd, &sigfd_set, 0);
2801 sigprocmask (SIG_BLOCK, &sigfd_set, 0);//D 2802 sigprocmask (SIG_UNBLOCK, &ss, 0);
2802 /*TODO: maybe unblock signal? */
2803 } 2803 }
2804 else 2804 else
2805#endif 2805#endif
2806 signal (w->signum, SIG_DFL); 2806 signal (w->signum, SIG_DFL);
2807 } 2807 }
2860static void noinline 2860static void noinline
2861infy_add (EV_P_ ev_stat *w) 2861infy_add (EV_P_ ev_stat *w)
2862{ 2862{
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); 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);
2864 2864
2865 if (w->wd < 0) 2865 if (w->wd >= 0)
2866 {
2867 struct statfs sfs;
2868
2869 /* now local changes will be tracked by inotify, but remote changes won't */
2870 /* unless the filesystem is known to be local, we therefore still poll */
2871 /* also do poll on <2.6.25, but with normal frequency */
2872
2873 if (!fs_2625)
2874 w->timer.repeat = w->interval ? w->interval : DEF_STAT_INTERVAL;
2875 else if (!statfs (w->path, &sfs)
2876 && (sfs.f_type == 0x1373 /* devfs */
2877 || sfs.f_type == 0xEF53 /* ext2/3 */
2878 || sfs.f_type == 0x3153464a /* jfs */
2879 || sfs.f_type == 0x52654973 /* reiser3 */
2880 || sfs.f_type == 0x01021994 /* tempfs */
2881 || sfs.f_type == 0x58465342 /* xfs */))
2882 w->timer.repeat = 0.; /* filesystem is local, kernel new enough */
2883 else
2884 w->timer.repeat = w->interval ? w->interval : NFS_STAT_INTERVAL; /* remote, use reduced frequency */
2866 { 2885 }
2886 else
2887 {
2888 /* can't use inotify, continue to stat */
2867 w->timer.repeat = w->interval ? w->interval : DEF_STAT_INTERVAL; 2889 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 2890
2870 /* monitor some parent directory for speedup hints */ 2891 /* 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, */ 2892 /* note that exceeding the hardcoded path limit is not a correctness issue, */
2872 /* but an efficiency issue only */ 2893 /* but an efficiency issue only */
2873 if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096) 2894 if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096)
2874 { 2895 {
2875 char path [4096]; 2896 char path [4096];
2891 while (w->wd < 0 && (errno == ENOENT || errno == EACCES)); 2912 while (w->wd < 0 && (errno == ENOENT || errno == EACCES));
2892 } 2913 }
2893 } 2914 }
2894 2915
2895 if (w->wd >= 0) 2916 if (w->wd >= 0)
2896 {
2897 struct statfs sfs;
2898
2899 wlist_add (&fs_hash [w->wd & (EV_INOTIFY_HASHSIZE - 1)].head, (WL)w); 2917 wlist_add (&fs_hash [w->wd & (EV_INOTIFY_HASHSIZE - 1)].head, (WL)w);
2900 2918
2901 /* now local changes will be tracked by inotify, but remote changes won't */ 2919 /* now re-arm timer, if required */
2902 /* unless the filesystem it known to be local, we therefore still poll */ 2920 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); 2921 ev_timer_again (EV_A_ &w->timer);
2916 } 2922 if (ev_is_active (&w->timer)) ev_unref (EV_A);
2917} 2923}
2918 2924
2919static void noinline 2925static void noinline
2920infy_del (EV_P_ ev_stat *w) 2926infy_del (EV_P_ ev_stat *w)
2921{ 2927{
3026 { 3032 {
3027 fd_intern (fs_fd); 3033 fd_intern (fs_fd);
3028 ev_io_init (&fs_w, infy_cb, fs_fd, EV_READ); 3034 ev_io_init (&fs_w, infy_cb, fs_fd, EV_READ);
3029 ev_set_priority (&fs_w, EV_MAXPRI); 3035 ev_set_priority (&fs_w, EV_MAXPRI);
3030 ev_io_start (EV_A_ &fs_w); 3036 ev_io_start (EV_A_ &fs_w);
3037 ev_unref (EV_A);
3031 } 3038 }
3032} 3039}
3033 3040
3034inline_size void 3041inline_size void
3035infy_fork (EV_P) 3042infy_fork (EV_P)
3037 int slot; 3044 int slot;
3038 3045
3039 if (fs_fd < 0) 3046 if (fs_fd < 0)
3040 return; 3047 return;
3041 3048
3049 ev_ref (EV_A);
3042 ev_io_stop (EV_A_ &fs_w); 3050 ev_io_stop (EV_A_ &fs_w);
3043 close (fs_fd); 3051 close (fs_fd);
3044 fs_fd = infy_newfd (); 3052 fs_fd = infy_newfd ();
3045 3053
3046 if (fs_fd >= 0) 3054 if (fs_fd >= 0)
3047 { 3055 {
3048 fd_intern (fs_fd); 3056 fd_intern (fs_fd);
3049 ev_io_set (&fs_w, fs_fd, EV_READ); 3057 ev_io_set (&fs_w, fs_fd, EV_READ);
3050 ev_io_start (EV_A_ &fs_w); 3058 ev_io_start (EV_A_ &fs_w);
3059 ev_unref (EV_A);
3051 } 3060 }
3052 3061
3053 for (slot = 0; slot < EV_INOTIFY_HASHSIZE; ++slot) 3062 for (slot = 0; slot < EV_INOTIFY_HASHSIZE; ++slot)
3054 { 3063 {
3055 WL w_ = fs_hash [slot].head; 3064 WL w_ = fs_hash [slot].head;
3063 w->wd = -1; 3072 w->wd = -1;
3064 3073
3065 if (fs_fd >= 0) 3074 if (fs_fd >= 0)
3066 infy_add (EV_A_ w); /* re-add, no matter what */ 3075 infy_add (EV_A_ w); /* re-add, no matter what */
3067 else 3076 else
3077 {
3078 w->timer.repeat = w->interval ? w->interval : DEF_STAT_INTERVAL;
3079 if (ev_is_active (&w->timer)) ev_ref (EV_A);
3068 ev_timer_again (EV_A_ &w->timer); 3080 ev_timer_again (EV_A_ &w->timer);
3081 if (ev_is_active (&w->timer)) ev_unref (EV_A);
3082 }
3069 } 3083 }
3070 } 3084 }
3071} 3085}
3072 3086
3073#endif 3087#endif
3090static void noinline 3104static void noinline
3091stat_timer_cb (EV_P_ ev_timer *w_, int revents) 3105stat_timer_cb (EV_P_ ev_timer *w_, int revents)
3092{ 3106{
3093 ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer)); 3107 ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer));
3094 3108
3095 /* we copy this here each the time so that */ 3109 ev_statdata prev = w->attr;
3096 /* prev has the old value when the callback gets invoked */
3097 w->prev = w->attr;
3098 ev_stat_stat (EV_A_ w); 3110 ev_stat_stat (EV_A_ w);
3099 3111
3100 /* memcmp doesn't work on netbsd, they.... do stuff to their struct stat */ 3112 /* memcmp doesn't work on netbsd, they.... do stuff to their struct stat */
3101 if ( 3113 if (
3102 w->prev.st_dev != w->attr.st_dev 3114 prev.st_dev != w->attr.st_dev
3103 || w->prev.st_ino != w->attr.st_ino 3115 || prev.st_ino != w->attr.st_ino
3104 || w->prev.st_mode != w->attr.st_mode 3116 || prev.st_mode != w->attr.st_mode
3105 || w->prev.st_nlink != w->attr.st_nlink 3117 || prev.st_nlink != w->attr.st_nlink
3106 || w->prev.st_uid != w->attr.st_uid 3118 || prev.st_uid != w->attr.st_uid
3107 || w->prev.st_gid != w->attr.st_gid 3119 || prev.st_gid != w->attr.st_gid
3108 || w->prev.st_rdev != w->attr.st_rdev 3120 || prev.st_rdev != w->attr.st_rdev
3109 || w->prev.st_size != w->attr.st_size 3121 || prev.st_size != w->attr.st_size
3110 || w->prev.st_atime != w->attr.st_atime 3122 || prev.st_atime != w->attr.st_atime
3111 || w->prev.st_mtime != w->attr.st_mtime 3123 || prev.st_mtime != w->attr.st_mtime
3112 || w->prev.st_ctime != w->attr.st_ctime 3124 || prev.st_ctime != w->attr.st_ctime
3113 ) { 3125 ) {
3126 /* we only update w->prev on actual differences */
3127 /* in case we test more often than invoke the callback, */
3128 /* to ensure that prev is always different to attr */
3129 w->prev = prev;
3130
3114 #if EV_USE_INOTIFY 3131 #if EV_USE_INOTIFY
3115 if (fs_fd >= 0) 3132 if (fs_fd >= 0)
3116 { 3133 {
3117 infy_del (EV_A_ w); 3134 infy_del (EV_A_ w);
3118 infy_add (EV_A_ w); 3135 infy_add (EV_A_ w);
3143 3160
3144 if (fs_fd >= 0) 3161 if (fs_fd >= 0)
3145 infy_add (EV_A_ w); 3162 infy_add (EV_A_ w);
3146 else 3163 else
3147#endif 3164#endif
3165 {
3148 ev_timer_again (EV_A_ &w->timer); 3166 ev_timer_again (EV_A_ &w->timer);
3167 ev_unref (EV_A);
3168 }
3149 3169
3150 ev_start (EV_A_ (W)w, 1); 3170 ev_start (EV_A_ (W)w, 1);
3151 3171
3152 EV_FREQUENT_CHECK; 3172 EV_FREQUENT_CHECK;
3153} 3173}
3162 EV_FREQUENT_CHECK; 3182 EV_FREQUENT_CHECK;
3163 3183
3164#if EV_USE_INOTIFY 3184#if EV_USE_INOTIFY
3165 infy_del (EV_A_ w); 3185 infy_del (EV_A_ w);
3166#endif 3186#endif
3187
3188 if (ev_is_active (&w->timer))
3189 {
3190 ev_ref (EV_A);
3167 ev_timer_stop (EV_A_ &w->timer); 3191 ev_timer_stop (EV_A_ &w->timer);
3192 }
3168 3193
3169 ev_stop (EV_A_ (W)w); 3194 ev_stop (EV_A_ (W)w);
3170 3195
3171 EV_FREQUENT_CHECK; 3196 EV_FREQUENT_CHECK;
3172} 3197}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines