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

Comparing libeio/eio.c (file contents):
Revision 1.96 by root, Tue Jul 19 05:20:36 2011 UTC vs.
Revision 1.101 by root, Wed Aug 3 15:25:38 2011 UTC

105#ifdef _WIN32 105#ifdef _WIN32
106 106
107 #undef PAGESIZE 107 #undef PAGESIZE
108 #define PAGESIZE 4096 /* GetSystemInfo? */ 108 #define PAGESIZE 4096 /* GetSystemInfo? */
109 109
110 /* TODO: look at how perl does stat (non-sloppy), unlink (ro-files), utime, link */
111
110 #ifdef EIO_STRUCT_STATI64 112 #ifdef EIO_STRUCT_STATI64
113 /* look at perl's non-sloppy stat */
111 #define stat(path,buf) _stati64 (path,buf) 114 #define stat(path,buf) _stati64 (path,buf)
112 #define fstat(fd,buf) _fstati64 (fd,buf) 115 #define fstat(fd,buf) _fstati64 (fd,buf)
113 #endif 116 #endif
114 #define lstat(path,buf) stat (path,buf) 117 #define lstat(path,buf) stat (path,buf)
115 #define fsync(fd) (FlushFileBuffers ((HANDLE)EIO_FD_TO_WIN32_HANDLE (fd)) ? 0 : EIO_ERRNO (EBADF, -1)) 118 #define fsync(fd) (FlushFileBuffers ((HANDLE)EIO_FD_TO_WIN32_HANDLE (fd)) ? 0 : EIO_ERRNO (EBADF, -1))
129 #define sync() EIO_ENOSYS () 132 #define sync() EIO_ENOSYS ()
130 #define readlink(path,buf,s) EIO_ENOSYS () 133 #define readlink(path,buf,s) EIO_ENOSYS ()
131 #define statvfs(path,buf) EIO_ENOSYS () 134 #define statvfs(path,buf) EIO_ENOSYS ()
132 #define fstatvfs(fd,buf) EIO_ENOSYS () 135 #define fstatvfs(fd,buf) EIO_ENOSYS ()
133 136
137 /* rename() uses MoveFile, which fails to overwrite */
138 #define rename(old,neu) eio__rename (old, neu)
139
140 static int
141 eio__rename (const char *old, const char *neu)
142 {
143 if (MoveFileEx (old, neu, MOVEFILE_REPLACE_EXISTING))
144 return 0;
145
146 /* should steal _dosmaperr */
147 switch (GetLastError ())
148 {
149 case ERROR_FILE_NOT_FOUND:
150 case ERROR_PATH_NOT_FOUND:
151 case ERROR_INVALID_DRIVE:
152 case ERROR_NO_MORE_FILES:
153 case ERROR_BAD_NETPATH:
154 case ERROR_BAD_NET_NAME:
155 case ERROR_BAD_PATHNAME:
156 case ERROR_FILENAME_EXCED_RANGE:
157 errno = ENOENT;
158 break;
159
160 default:
161 errno = EACCES;
162 break;
163 }
164
165 return -1;
166 }
167
134 /* we could even stat and see if it exists */ 168 /* we could even stat and see if it exists */
135 static int 169 static int
136 symlink (const char *old, const char *neu) 170 symlink (const char *old, const char *neu)
137 { 171 {
138 #if WINVER >= 0x0600 172 #if WINVER >= 0x0600
199 233
200#endif 234#endif
201 235
202#if HAVE_UTIMES 236#if HAVE_UTIMES
203# include <utime.h> 237# include <utime.h>
238#endif
239
240#if HAVE_SYS_SYSCALL_H
241# include <sys/syscall.h>
242#endif
243
244#if HAVE_SYS_PRCTL_H
245# include <sys/prctl.h>
204#endif 246#endif
205 247
206#if HAVE_SENDFILE 248#if HAVE_SENDFILE
207# if __linux 249# if __linux
208# include <sys/sendfile.h> 250# include <sys/sendfile.h>
282static void (*done_poll_cb) (void); 324static void (*done_poll_cb) (void);
283 325
284static unsigned int max_poll_time; /* reslock */ 326static unsigned int max_poll_time; /* reslock */
285static unsigned int max_poll_reqs; /* reslock */ 327static unsigned int max_poll_reqs; /* reslock */
286 328
287static volatile unsigned int nreqs; /* reqlock */ 329static unsigned int nreqs; /* reqlock */
288static volatile unsigned int nready; /* reqlock */ 330static unsigned int nready; /* reqlock */
289static volatile unsigned int npending; /* reqlock */ 331static unsigned int npending; /* reqlock */
290static volatile unsigned int max_idle = 4; /* maximum number of threads that can idle indefinitely */ 332static unsigned int max_idle = 4; /* maximum number of threads that can idle indefinitely */
291static volatile unsigned int idle_timeout = 10; /* number of seconds after which an idle threads exit */ 333static unsigned int idle_timeout = 10; /* number of seconds after which an idle threads exit */
292 334
293static xmutex_t wrklock; 335static xmutex_t wrklock;
294static xmutex_t reslock; 336static xmutex_t reslock;
295static xmutex_t reqlock; 337static xmutex_t reqlock;
296static xcond_t reqwait; 338static xcond_t reqwait;
936 978
937#if !HAVE_FDATASYNC 979#if !HAVE_FDATASYNC
938# undef fdatasync 980# undef fdatasync
939# define fdatasync(fd) fsync (fd) 981# define fdatasync(fd) fsync (fd)
940#endif 982#endif
983
984static int
985eio__syncfs (int fd)
986{
987 int res;
988
989#if HAVE_SYS_SYNCFS
990 res = (int)syscall (__NR_syncfs, (int)(fd));
991#else
992 res = -1;
993 errno = ENOSYS;
994#endif
995
996 if (res < 0 && errno == ENOSYS && fd >= 0)
997 sync ();
998
999 return res;
1000}
941 1001
942/* sync_file_range always needs emulation */ 1002/* sync_file_range always needs emulation */
943static int 1003static int
944eio__sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags) 1004eio__sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags)
945{ 1005{
1648 1708
1649 if (dirp == INVALID_HANDLE_VALUE) 1709 if (dirp == INVALID_HANDLE_VALUE)
1650 { 1710 {
1651 dirp = 0; 1711 dirp = 0;
1652 1712
1713 /* should steal _dosmaperr */
1653 switch (GetLastError ()) 1714 switch (GetLastError ())
1654 { 1715 {
1655 case ERROR_FILE_NOT_FOUND: 1716 case ERROR_FILE_NOT_FOUND:
1656 req->result = 0; 1717 req->result = 0;
1657 break; 1718 break;
1891{ 1952{
1892 ETP_REQ *req; 1953 ETP_REQ *req;
1893 struct timespec ts; 1954 struct timespec ts;
1894 etp_worker *self = (etp_worker *)thr_arg; 1955 etp_worker *self = (etp_worker *)thr_arg;
1895 1956
1957#if HAVE_PRCTL_SET_NAME
1958 prctl (PR_SET_NAME, (unsigned long)"eio_thread", 0, 0, 0);
1959#endif
1960
1896 /* try to distribute timeouts somewhat evenly */ 1961 /* try to distribute timeouts somewhat evenly */
1897 ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL); 1962 ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL);
1898 1963
1899 for (;;) 1964 for (;;)
1900 { 1965 {
2067 req->result = readlink (req->ptr1, req->ptr2, PATH_MAX); break; 2132 req->result = readlink (req->ptr1, req->ptr2, PATH_MAX); break;
2068 2133
2069 case EIO_SYNC: req->result = 0; sync (); break; 2134 case EIO_SYNC: req->result = 0; sync (); break;
2070 case EIO_FSYNC: req->result = fsync (req->int1); break; 2135 case EIO_FSYNC: req->result = fsync (req->int1); break;
2071 case EIO_FDATASYNC: req->result = fdatasync (req->int1); break; 2136 case EIO_FDATASYNC: req->result = fdatasync (req->int1); break;
2137 case EIO_SYNCFS: req->result = eio__syncfs (req->int1); break;
2138 case EIO_SYNC_FILE_RANGE: req->result = eio__sync_file_range (req->int1, req->offs, req->size, req->int2); break;
2072 case EIO_MSYNC: req->result = eio__msync (req->ptr2, req->size, req->int1); break; 2139 case EIO_MSYNC: req->result = eio__msync (req->ptr2, req->size, req->int1); break;
2073 case EIO_MTOUCH: req->result = eio__mtouch (req); break; 2140 case EIO_MTOUCH: req->result = eio__mtouch (req); break;
2074 case EIO_MLOCK: req->result = eio__mlock (req->ptr2, req->size); break; 2141 case EIO_MLOCK: req->result = eio__mlock (req->ptr2, req->size); break;
2075 case EIO_MLOCKALL: req->result = eio__mlockall (req->int1); break; 2142 case EIO_MLOCKALL: req->result = eio__mlockall (req->int1); break;
2076 case EIO_SYNC_FILE_RANGE: req->result = eio__sync_file_range (req->int1, req->offs, req->size, req->int2); break;
2077 case EIO_FALLOCATE: req->result = eio__fallocate (req->int1, req->int2, req->offs, req->size); break; 2143 case EIO_FALLOCATE: req->result = eio__fallocate (req->int1, req->int2, req->offs, req->size); break;
2078 2144
2079 case EIO_READDIR: eio__scandir (req, self); break; 2145 case EIO_READDIR: eio__scandir (req, self); break;
2080 2146
2081 case EIO_BUSY: 2147 case EIO_BUSY:
2162eio_req *eio_msync (void *addr, size_t length, int flags, int pri, eio_cb cb, void *data) 2228eio_req *eio_msync (void *addr, size_t length, int flags, int pri, eio_cb cb, void *data)
2163{ 2229{
2164 REQ (EIO_MSYNC); req->ptr2 = addr; req->size = length; req->int1 = flags; SEND; 2230 REQ (EIO_MSYNC); req->ptr2 = addr; req->size = length; req->int1 = flags; SEND;
2165} 2231}
2166 2232
2233eio_req *eio_fdatasync (int fd, int pri, eio_cb cb, void *data)
2234{
2235 REQ (EIO_FDATASYNC); req->int1 = fd; SEND;
2236}
2237
2238eio_req *eio_syncfs (int fd, int pri, eio_cb cb, void *data)
2239{
2240 REQ (EIO_SYNCFS); req->int1 = fd; SEND;
2241}
2242
2243eio_req *eio_sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags, int pri, eio_cb cb, void *data)
2244{
2245 REQ (EIO_SYNC_FILE_RANGE); req->int1 = fd; req->offs = offset; req->size = nbytes; req->int2 = flags; SEND;
2246}
2247
2167eio_req *eio_mtouch (void *addr, size_t length, int flags, int pri, eio_cb cb, void *data) 2248eio_req *eio_mtouch (void *addr, size_t length, int flags, int pri, eio_cb cb, void *data)
2168{ 2249{
2169 REQ (EIO_MTOUCH); req->ptr2 = addr; req->size = length; req->int1 = flags; SEND; 2250 REQ (EIO_MTOUCH); req->ptr2 = addr; req->size = length; req->int1 = flags; SEND;
2170} 2251}
2171 2252
2177eio_req *eio_mlockall (int flags, int pri, eio_cb cb, void *data) 2258eio_req *eio_mlockall (int flags, int pri, eio_cb cb, void *data)
2178{ 2259{
2179 REQ (EIO_MLOCKALL); req->int1 = flags; SEND; 2260 REQ (EIO_MLOCKALL); req->int1 = flags; SEND;
2180} 2261}
2181 2262
2182eio_req *eio_sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags, int pri, eio_cb cb, void *data)
2183{
2184 REQ (EIO_SYNC_FILE_RANGE); req->int1 = fd; req->offs = offset; req->size = nbytes; req->int2 = flags; SEND;
2185}
2186
2187eio_req *eio_fallocate (int fd, int mode, off_t offset, size_t len, int pri, eio_cb cb, void *data) 2263eio_req *eio_fallocate (int fd, int mode, off_t offset, size_t len, int pri, eio_cb cb, void *data)
2188{ 2264{
2189 REQ (EIO_FALLOCATE); req->int1 = fd; req->int2 = mode; req->offs = offset; req->size = len; SEND; 2265 REQ (EIO_FALLOCATE); req->int1 = fd; req->int2 = mode; req->offs = offset; req->size = len; SEND;
2190}
2191
2192eio_req *eio_fdatasync (int fd, int pri, eio_cb cb, void *data)
2193{
2194 REQ (EIO_FDATASYNC); req->int1 = fd; SEND;
2195} 2266}
2196 2267
2197eio_req *eio_close (int fd, int pri, eio_cb cb, void *data) 2268eio_req *eio_close (int fd, int pri, eio_cb cb, void *data)
2198{ 2269{
2199 REQ (EIO_CLOSE); req->int1 = fd; SEND; 2270 REQ (EIO_CLOSE); req->int1 = fd; SEND;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines