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

Comparing libeio/eio.c (file contents):
Revision 1.86 by root, Thu Jul 14 18:30:10 2011 UTC vs.
Revision 1.93 by root, Mon Jul 18 01:27:03 2011 UTC

58#include <sys/stat.h> 58#include <sys/stat.h>
59#include <limits.h> 59#include <limits.h>
60#include <fcntl.h> 60#include <fcntl.h>
61#include <assert.h> 61#include <assert.h>
62 62
63#include <sys/statvfs.h>
64/* intptr_t comes from unistd.h, says POSIX/UNIX/tradition */ 63/* intptr_t comes from unistd.h, says POSIX/UNIX/tradition */
65/* intptr_t only comes from stdint.h, says idiot openbsd coder */ 64/* intptr_t only comes from stdint.h, says idiot openbsd coder */
66#if HAVE_STDINT_H 65#if HAVE_STDINT_H
67# include <stdint.h> 66# include <stdint.h>
68#endif 67#endif
69 68
70#ifndef ECANCELED 69#ifndef ECANCELED
71# define ECANCELED EDOM 70# define ECANCELED EDOM
72#endif 71#endif
72#ifndef ELOOP
73# define ELOOP EDOM
74#endif
73 75
74static void eio_destroy (eio_req *req); 76static void eio_destroy (eio_req *req);
75 77
76#ifndef EIO_FINISH 78#ifndef EIO_FINISH
77# define EIO_FINISH(req) ((req)->finish) && !EIO_CANCELLED (req) ? (req)->finish (req) : 0 79# define EIO_FINISH(req) ((req)->finish) && !EIO_CANCELLED (req) ? (req)->finish (req) : 0
96 98
97#define EIO_ENOSYS() EIO_ERRNO (ENOSYS, -1) 99#define EIO_ENOSYS() EIO_ERRNO (ENOSYS, -1)
98 100
99#ifdef _WIN32 101#ifdef _WIN32
100 102
103 #undef PAGESIZE
101 #define PAGESIZE 4096 /* GetSystemInfo? */ 104 #define PAGESIZE 4096 /* GetSystemInfo? */
102 105
106 #ifdef EIO_STRUCT_STATI64
103 #define stat(path,buf) _stati64 (path,buf) 107 #define stat(path,buf) _stati64 (path,buf)
108 #define fstat(fd,buf) _fstati64 (path,buf)
109 #endif
104 #define lstat(path,buf) stat (path,buf) 110 #define lstat(path,buf) stat (path,buf)
105 #define fstat(fd,buf) _fstati64 (path,buf)
106 #define fsync(fd) (FlushFileBuffers (EIO_FD_TO_WIN32_HANDLE (fd)) ? 0 : EIO_ERRNO (EBADF, -1)) 111 #define fsync(fd) (FlushFileBuffers ((HANDLE)EIO_FD_TO_WIN32_HANDLE (fd)) ? 0 : EIO_ERRNO (EBADF, -1))
107 #define mkdir(path,mode) _mkdir (path) 112 #define mkdir(path,mode) _mkdir (path)
108 #define link(old,neu) (CreateHardLink (neu, old, 0) ? 0 : EIO_ERRNO (ENOENT, -1)) 113 #define link(old,neu) (CreateHardLink (neu, old, 0) ? 0 : EIO_ERRNO (ENOENT, -1))
109 114
115 #define chmod(path,mode) _chmod (path, mode)
116 #define dup(fd) _dup (fd)
117 #define dup2(fd1,fd2) _dup2 (fd1, fd2)
118
119 #define fchmod(fd,mode) EIO_ENOSYS ()
110 #define chown(path,uid,gid) EIO_ENOSYS () 120 #define chown(path,uid,gid) EIO_ENOSYS ()
111 #define fchown(fd,uid,gid) EIO_ENOSYS () 121 #define fchown(fd,uid,gid) EIO_ENOSYS ()
112 #define truncate(path,offs) EIO_ENOSYS () /* far-miss: SetEndOfFile */ 122 #define truncate(path,offs) EIO_ENOSYS () /* far-miss: SetEndOfFile */
113 #define ftruncate(fd,offs) EIO_ENOSYS () /* near-miss: SetEndOfFile */ 123 #define ftruncate(fd,offs) EIO_ENOSYS () /* near-miss: SetEndOfFile */
114 #define mknod(path,mode,dev) EIO_ENOSYS () 124 #define mknod(path,mode,dev) EIO_ENOSYS ()
115 #define sync() EIO_ENOSYS () 125 #define sync() EIO_ENOSYS ()
126 #define readlink(path,buf,s) EIO_ENOSYS ()
127 #define statvfs(path,buf) EIO_ENOSYS ()
128 #define fstatvfs(fd,buf) EIO_ENOSYS ()
116 129
117 /* we could even stat and see if it exists */ 130 /* we could even stat and see if it exists */
118 static int 131 static int
119 symlink (const char *old, const char *neu) 132 symlink (const char *old, const char *neu)
120 { 133 {
134 #if WINVER >= 0x0600
121 if (CreateSymbolicLink (neu, old, 1)) 135 if (CreateSymbolicLink (neu, old, 1))
122 return 0; 136 return 0;
123 137
124 if (CreateSymbolicLink (neu, old, 0)) 138 if (CreateSymbolicLink (neu, old, 0))
125 return 0; 139 return 0;
140 #endif
126 141
127 return EIO_ERRNO (ENOENT, -1); 142 return EIO_ERRNO (ENOENT, -1);
128 } 143 }
144
145 /* POSIX API only */
146 #define CreateHardLink(neu,old,flags) 0
147 #define CreateSymbolicLink(neu,old,flags) 0
148
149 struct statvfs
150 {
151 int dummy;
152 };
153
154 #define DT_DIR EIO_DT_DIR
155 #define DT_REG EIO_DT_REG
156 #define D_NAME(entp) entp.cFileName
157 #define D_TYPE(entp) (entp.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ? DT_DIR : DT_REG)
129 158
130#else 159#else
131 160
132 #include <sys/time.h> 161 #include <sys/time.h>
133 #include <sys/select.h> 162 #include <sys/select.h>
138 #include <dirent.h> 167 #include <dirent.h>
139 168
140 #if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES 169 #if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES
141 #include <sys/mman.h> 170 #include <sys/mman.h>
142 #endif 171 #endif
172
173 #define D_NAME(entp) entp->d_name
143 174
144 /* POSIX_SOURCE is useless on bsd's, and XOPEN_SOURCE is unreliable there, too */ 175 /* POSIX_SOURCE is useless on bsd's, and XOPEN_SOURCE is unreliable there, too */
145 #if __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ 176 #if __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__
146 #define _DIRENT_HAVE_D_TYPE /* sigh */ 177 #define _DIRENT_HAVE_D_TYPE /* sigh */
147 #define D_INO(de) (de)->d_fileno 178 #define D_INO(de) (de)->d_fileno
185#endif 216#endif
186#ifndef D_INO 217#ifndef D_INO
187# define D_INO(de) 0 218# define D_INO(de) 0
188#endif 219#endif
189#ifndef D_NAMLEN 220#ifndef D_NAMLEN
190# define D_NAMLEN(de) strlen ((de)->d_name) 221# define D_NAMLEN(entp) strlen (D_NAME (entp))
191#endif 222#endif
192 223
193/* used for struct dirent, AIX doesn't provide it */ 224/* used for struct dirent, AIX doesn't provide it */
194#ifndef NAME_MAX 225#ifndef NAME_MAX
195# define NAME_MAX 4096 226# define NAME_MAX 4096
229#define ETP_WORKER_CLEAR(req) \ 260#define ETP_WORKER_CLEAR(req) \
230 if (wrk->dbuf) \ 261 if (wrk->dbuf) \
231 { \ 262 { \
232 free (wrk->dbuf); \ 263 free (wrk->dbuf); \
233 wrk->dbuf = 0; \ 264 wrk->dbuf = 0; \
234 } \
235 \
236 if (wrk->dirp) \
237 { \
238 closedir (wrk->dirp); \
239 wrk->dirp = 0; \
240 } 265 }
241 266
242#define ETP_WORKER_COMMON \ 267#define ETP_WORKER_COMMON \
243 void *dbuf; \ 268 void *dbuf;
244 DIR *dirp;
245 269
246/*****************************************************************************/ 270/*****************************************************************************/
247 271
248#define ETP_NUM_PRI (ETP_PRI_MAX - ETP_PRI_MIN + 1) 272#define ETP_NUM_PRI (ETP_PRI_MAX - ETP_PRI_MIN + 1)
249 273
278/* 302/*
279 * make our pread/pwrite emulation safe against themselves, but not against 303 * make our pread/pwrite emulation safe against themselves, but not against
280 * normal read/write by using a mutex. slows down execution a lot, 304 * normal read/write by using a mutex. slows down execution a lot,
281 * but that's your problem, not mine. 305 * but that's your problem, not mine.
282 */ 306 */
283static xmutex_t preadwritelock = X_MUTEX_INIT; 307static xmutex_t preadwritelock;
284#endif 308#endif
285 309
286typedef struct etp_worker 310typedef struct etp_worker
287{ 311{
288 /* locked by wrklock */ 312 /* locked by wrklock */
294 ETP_REQ *req; /* currently processed request */ 318 ETP_REQ *req; /* currently processed request */
295 319
296 ETP_WORKER_COMMON 320 ETP_WORKER_COMMON
297} etp_worker; 321} etp_worker;
298 322
299static etp_worker wrk_first = { &wrk_first, &wrk_first, 0 }; /* NOT etp */ 323static etp_worker wrk_first; /* NOT etp */
300 324
301#define ETP_WORKER_LOCK(wrk) X_LOCK (wrklock) 325#define ETP_WORKER_LOCK(wrk) X_LOCK (wrklock)
302#define ETP_WORKER_UNLOCK(wrk) X_UNLOCK (wrklock) 326#define ETP_WORKER_UNLOCK(wrk) X_UNLOCK (wrklock)
303 327
304/* worker threads management */ 328/* worker threads management */
375} etp_reqq; 399} etp_reqq;
376 400
377static etp_reqq req_queue; 401static etp_reqq req_queue;
378static etp_reqq res_queue; 402static etp_reqq res_queue;
379 403
404static void ecb_noinline ecb_cold
405reqq_init (etp_reqq *q)
406{
407 int pri;
408
409 for (pri = 0; pri < ETP_NUM_PRI; ++pri)
410 q->qs[pri] = q->qe[pri] = 0;
411
412 q->size = 0;
413}
414
380static int ecb_noinline 415static int ecb_noinline
381reqq_push (etp_reqq *q, ETP_REQ *req) 416reqq_push (etp_reqq *q, ETP_REQ *req)
382{ 417{
383 int pri = req->pri; 418 int pri = req->pri;
384 req->next = 0; 419 req->next = 0;
418 } 453 }
419 454
420 abort (); 455 abort ();
421} 456}
422 457
423static void ecb_cold 458static int ecb_cold
424etp_thread_init (void) 459etp_init (void (*want_poll)(void), void (*done_poll)(void))
425{ 460{
426#if !HAVE_PREADWRITE
427 X_MUTEX_CREATE (preadwritelock);
428#endif
429 X_MUTEX_CREATE (wrklock); 461 X_MUTEX_CREATE (wrklock);
430 X_MUTEX_CREATE (reslock); 462 X_MUTEX_CREATE (reslock);
431 X_MUTEX_CREATE (reqlock); 463 X_MUTEX_CREATE (reqlock);
432 X_COND_CREATE (reqwait); 464 X_COND_CREATE (reqwait);
433}
434 465
435static void ecb_cold 466 reqq_init (&req_queue);
436etp_atfork_prepare (void) 467 reqq_init (&res_queue);
437{
438}
439 468
440static void ecb_cold 469 wrk_first.next =
441etp_atfork_parent (void) 470 wrk_first.prev = &wrk_first;
442{
443}
444
445static void ecb_cold
446etp_atfork_child (void)
447{
448 ETP_REQ *prv;
449
450 while ((prv = reqq_shift (&req_queue)))
451 ETP_DESTROY (prv);
452
453 while ((prv = reqq_shift (&res_queue)))
454 ETP_DESTROY (prv);
455
456 while (wrk_first.next != &wrk_first)
457 {
458 etp_worker *wrk = wrk_first.next;
459
460 if (wrk->req)
461 ETP_DESTROY (wrk->req);
462
463 etp_worker_clear (wrk);
464 etp_worker_free (wrk);
465 }
466 471
467 started = 0; 472 started = 0;
468 idle = 0; 473 idle = 0;
469 nreqs = 0; 474 nreqs = 0;
470 nready = 0; 475 nready = 0;
471 npending = 0; 476 npending = 0;
472
473 etp_thread_init ();
474}
475
476static void ecb_cold
477etp_once_init (void)
478{
479 etp_thread_init ();
480 X_THREAD_ATFORK (etp_atfork_prepare, etp_atfork_parent, etp_atfork_child);
481}
482
483static int ecb_cold
484etp_init (void (*want_poll)(void), void (*done_poll)(void))
485{
486 static pthread_once_t doinit = PTHREAD_ONCE_INIT;
487
488 pthread_once (&doinit, etp_once_init);
489 477
490 want_poll_cb = want_poll; 478 want_poll_cb = want_poll;
491 done_poll_cb = done_poll; 479 done_poll_cb = done_poll;
492 480
493 return 0; 481 return 0;
876# undef pread 864# undef pread
877# undef pwrite 865# undef pwrite
878# define pread eio__pread 866# define pread eio__pread
879# define pwrite eio__pwrite 867# define pwrite eio__pwrite
880 868
881static ssize_t 869static eio_ssize_t
882eio__pread (int fd, void *buf, size_t count, off_t offset) 870eio__pread (int fd, void *buf, size_t count, off_t offset)
883{ 871{
884 ssize_t res; 872 eio_ssize_t res;
885 off_t ooffset; 873 off_t ooffset;
886 874
887 X_LOCK (preadwritelock); 875 X_LOCK (preadwritelock);
888 ooffset = lseek (fd, 0, SEEK_CUR); 876 ooffset = lseek (fd, 0, SEEK_CUR);
889 lseek (fd, offset, SEEK_SET); 877 lseek (fd, offset, SEEK_SET);
892 X_UNLOCK (preadwritelock); 880 X_UNLOCK (preadwritelock);
893 881
894 return res; 882 return res;
895} 883}
896 884
897static ssize_t 885static eio_ssize_t
898eio__pwrite (int fd, void *buf, size_t count, off_t offset) 886eio__pwrite (int fd, void *buf, size_t count, off_t offset)
899{ 887{
900 ssize_t res; 888 eio_ssize_t res;
901 off_t ooffset; 889 off_t ooffset;
902 890
903 X_LOCK (preadwritelock); 891 X_LOCK (preadwritelock);
904 ooffset = lseek (fd, 0, SEEK_CUR); 892 ooffset = lseek (fd, 0, SEEK_CUR);
905 lseek (fd, offset, SEEK_SET); 893 lseek (fd, offset, SEEK_SET);
994 982
995#if !HAVE_READAHEAD 983#if !HAVE_READAHEAD
996# undef readahead 984# undef readahead
997# define readahead(fd,offset,count) eio__readahead (fd, offset, count, self) 985# define readahead(fd,offset,count) eio__readahead (fd, offset, count, self)
998 986
999static ssize_t 987static eio_ssize_t
1000eio__readahead (int fd, off_t offset, size_t count, etp_worker *self) 988eio__readahead (int fd, off_t offset, size_t count, etp_worker *self)
1001{ 989{
1002 size_t todo = count; 990 size_t todo = count;
1003 dBUF; 991 dBUF;
1004 992
1016} 1004}
1017 1005
1018#endif 1006#endif
1019 1007
1020/* sendfile always needs emulation */ 1008/* sendfile always needs emulation */
1021static ssize_t 1009static eio_ssize_t
1022eio__sendfile (int ofd, int ifd, off_t offset, size_t count, etp_worker *self) 1010eio__sendfile (int ofd, int ifd, off_t offset, size_t count, etp_worker *self)
1023{ 1011{
1024 ssize_t written = 0; 1012 eio_ssize_t written = 0;
1025 ssize_t res; 1013 eio_ssize_t res;
1026 1014
1027 if (!count) 1015 if (!count)
1028 return 0; 1016 return 0;
1029 1017
1030 for (;;) 1018 for (;;)
1125 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK 1113 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK
1126 /* BSDs */ 1114 /* BSDs */
1127#ifdef ENOTSUP /* sigh, if the steenking pile called openbsd would only try to at least compile posix code... */ 1115#ifdef ENOTSUP /* sigh, if the steenking pile called openbsd would only try to at least compile posix code... */
1128 || errno == ENOTSUP 1116 || errno == ENOTSUP
1129#endif 1117#endif
1118#ifdef EOPNOTSUPP /* windows */
1130 || errno == EOPNOTSUPP /* BSDs */ 1119 || errno == EOPNOTSUPP /* BSDs */
1120#endif
1131#if __solaris 1121#if __solaris
1132 || errno == EAFNOSUPPORT || errno == EPROTOTYPE 1122 || errno == EAFNOSUPPORT || errno == EPROTOTYPE
1133#endif 1123#endif
1134 ) 1124 )
1135 ) 1125 )
1139 1129
1140 res = 0; 1130 res = 0;
1141 1131
1142 while (count) 1132 while (count)
1143 { 1133 {
1144 ssize_t cnt; 1134 eio_ssize_t cnt;
1145 1135
1146 cnt = pread (ifd, eio_buf, count > EIO_BUFSIZE ? EIO_BUFSIZE : count, offset); 1136 cnt = pread (ifd, eio_buf, count > EIO_BUFSIZE ? EIO_BUFSIZE : count, offset);
1147 1137
1148 if (cnt <= 0) 1138 if (cnt <= 0)
1149 { 1139 {
1197 /* round up length */ 1187 /* round up length */
1198 *length = (*length + mask) & ~mask; 1188 *length = (*length + mask) & ~mask;
1199} 1189}
1200 1190
1201#if !_POSIX_MEMLOCK 1191#if !_POSIX_MEMLOCK
1202# define eio__mlockall(a) eio_nosyscall() 1192# define eio__mlockall(a) EIO_ENOSYS ()
1203#else 1193#else
1204 1194
1205static int 1195static int
1206eio__mlockall (int flags) 1196eio__mlockall (int flags)
1207{ 1197{
1357 res += strlen (res); 1347 res += strlen (res);
1358 } 1348 }
1359 1349
1360 while (*rel) 1350 while (*rel)
1361 { 1351 {
1362 ssize_t len, linklen; 1352 eio_ssize_t len, linklen;
1363 char *beg = rel; 1353 char *beg = rel;
1364 1354
1365 while (*rel && *rel != '/') 1355 while (*rel && *rel != '/')
1366 ++rel; 1356 ++rel;
1367 1357
1460 1450
1461#define EIO_SORT_CUTOFF 30 /* quite high, but performs well on many filesystems */ 1451#define EIO_SORT_CUTOFF 30 /* quite high, but performs well on many filesystems */
1462#define EIO_SORT_FAST 60 /* when to only use insertion sort */ 1452#define EIO_SORT_FAST 60 /* when to only use insertion sort */
1463 1453
1464static void 1454static void
1465eio_dent_radix_sort (eio_dirent *dents, int size, signed char score_bits, ino_t inode_bits) 1455eio_dent_radix_sort (eio_dirent *dents, int size, signed char score_bits, eio_ino_t inode_bits)
1466{ 1456{
1467 unsigned char bits [9 + sizeof (ino_t) * 8]; 1457 unsigned char bits [9 + sizeof (eio_ino_t) * 8];
1468 unsigned char *bit = bits; 1458 unsigned char *bit = bits;
1469 1459
1470 assert (CHAR_BIT == 8); 1460 assert (CHAR_BIT == 8);
1471 assert (sizeof (eio_dirent) * 8 < 256); 1461 assert (sizeof (eio_dirent) * 8 < 256);
1472 assert (offsetof (eio_dirent, inode)); /* we use bit #0 as sentinel */ 1462 assert (offsetof (eio_dirent, inode)); /* we use bit #0 as sentinel */
1474 1464
1475 if (size <= EIO_SORT_FAST) 1465 if (size <= EIO_SORT_FAST)
1476 return; 1466 return;
1477 1467
1478 /* first prepare an array of bits to test in our radix sort */ 1468 /* first prepare an array of bits to test in our radix sort */
1479 /* try to take endianness into account, as well as differences in ino_t sizes */ 1469 /* try to take endianness into account, as well as differences in eio_ino_t sizes */
1480 /* inode_bits must contain all inodes ORed together */ 1470 /* inode_bits must contain all inodes ORed together */
1481 /* which is used to skip bits that are 0 everywhere, which is very common */ 1471 /* which is used to skip bits that are 0 everywhere, which is very common */
1482 { 1472 {
1483 ino_t endianness; 1473 eio_ino_t endianness;
1484 int i, j; 1474 int i, j;
1485 1475
1486 /* we store the byte offset of byte n into byte n of "endianness" */ 1476 /* we store the byte offset of byte n into byte n of "endianness" */
1487 for (i = 0; i < sizeof (ino_t); ++i) 1477 for (i = 0; i < sizeof (eio_ino_t); ++i)
1488 ((unsigned char *)&endianness)[i] = i; 1478 ((unsigned char *)&endianness)[i] = i;
1489 1479
1490 *bit++ = 0; 1480 *bit++ = 0;
1491 1481
1492 for (i = 0; i < sizeof (ino_t); ++i) 1482 for (i = 0; i < sizeof (eio_ino_t); ++i)
1493 { 1483 {
1494 /* shifting off the byte offsets out of "endianness" */ 1484 /* shifting off the byte offsets out of "endianness" */
1495 int offs = (offsetof (eio_dirent, inode) + (endianness & 0xff)) * 8; 1485 int offs = (offsetof (eio_dirent, inode) + (endianness & 0xff)) * 8;
1496 endianness >>= 8; 1486 endianness >>= 8;
1497 1487
1498 for (j = 0; j < 8; ++j) 1488 for (j = 0; j < 8; ++j)
1499 if (inode_bits & (((ino_t)1) << (i * 8 + j))) 1489 if (inode_bits & (((eio_ino_t)1) << (i * 8 + j)))
1500 *bit++ = offs + j; 1490 *bit++ = offs + j;
1501 } 1491 }
1502 1492
1503 for (j = 0; j < 8; ++j) 1493 for (j = 0; j < 8; ++j)
1504 if (score_bits & (1 << j)) 1494 if (score_bits & (1 << j))
1505 *bit++ = offsetof (eio_dirent, score) * 8 + j; 1495 *bit++ = offsetof (eio_dirent, score) * 8 + j;
1506 } 1496 }
1507 1497
1508 /* now actually do the sorting (a variant of MSD radix sort) */ 1498 /* now actually do the sorting (a variant of MSD radix sort) */
1509 { 1499 {
1510 eio_dirent *base_stk [9 + sizeof (ino_t) * 8], *base; 1500 eio_dirent *base_stk [9 + sizeof (eio_ino_t) * 8], *base;
1511 eio_dirent *end_stk [9 + sizeof (ino_t) * 8], *end; 1501 eio_dirent *end_stk [9 + sizeof (eio_ino_t) * 8], *end;
1512 unsigned char *bit_stk [9 + sizeof (ino_t) * 8]; 1502 unsigned char *bit_stk [9 + sizeof (eio_ino_t) * 8];
1513 int stk_idx = 0; 1503 int stk_idx = 0;
1514 1504
1515 base_stk [stk_idx] = dents; 1505 base_stk [stk_idx] = dents;
1516 end_stk [stk_idx] = dents + size; 1506 end_stk [stk_idx] = dents + size;
1517 bit_stk [stk_idx] = bit - 1; 1507 bit_stk [stk_idx] = bit - 1;
1596 } 1586 }
1597 } 1587 }
1598} 1588}
1599 1589
1600static void 1590static void
1601eio_dent_sort (eio_dirent *dents, int size, signed char score_bits, ino_t inode_bits) 1591eio_dent_sort (eio_dirent *dents, int size, signed char score_bits, eio_ino_t inode_bits)
1602{ 1592{
1603 if (size <= 1) 1593 if (size <= 1)
1604 return; /* our insertion sort relies on size > 0 */ 1594 return; /* our insertion sort relies on size > 0 */
1605 1595
1606 /* first we use a radix sort, but only for dirs >= EIO_SORT_FAST */ 1596 /* first we use a radix sort, but only for dirs >= EIO_SORT_FAST */
1614 1604
1615/* read a full directory */ 1605/* read a full directory */
1616static void 1606static void
1617eio__scandir (eio_req *req, etp_worker *self) 1607eio__scandir (eio_req *req, etp_worker *self)
1618{ 1608{
1619 DIR *dirp;
1620 EIO_STRUCT_DIRENT *entp;
1621 char *name, *names; 1609 char *name, *names;
1622 int namesalloc = 4096; 1610 int namesalloc = 4096 - sizeof (void *) * 4;
1623 int namesoffs = 0; 1611 int namesoffs = 0;
1624 int flags = req->int1; 1612 int flags = req->int1;
1625 eio_dirent *dents = 0; 1613 eio_dirent *dents = 0;
1626 int dentalloc = 128; 1614 int dentalloc = 128;
1627 int dentoffs = 0; 1615 int dentoffs = 0;
1628 ino_t inode_bits = 0; 1616 eio_ino_t inode_bits = 0;
1617#ifdef _WIN32
1618 HANDLE dirp;
1619 WIN32_FIND_DATA entp;
1620#else
1621 DIR *dirp;
1622 EIO_STRUCT_DIRENT *entp;
1623#endif
1629 1624
1630 req->result = -1; 1625 req->result = -1;
1631 1626
1632 if (!(flags & EIO_READDIR_DENTS)) 1627 if (!(flags & EIO_READDIR_DENTS))
1633 flags &= ~(EIO_READDIR_DIRS_FIRST | EIO_READDIR_STAT_ORDER); 1628 flags &= ~(EIO_READDIR_DIRS_FIRST | EIO_READDIR_STAT_ORDER);
1634 1629
1635 X_LOCK (wrklock); 1630#ifdef _WIN32
1636 /* the corresponding closedir is in ETP_WORKER_CLEAR */ 1631 {
1632 int len = strlen ((const char *)req->ptr1);
1633 char *path = malloc (MAX_PATH);
1634 const char *fmt;
1635
1636 if (!len)
1637 fmt = "./*";
1638 else if (((const char *)req->ptr1)[len - 1] == '/' || ((const char *)req->ptr1)[len - 1] == '\\')
1639 fmt = "%s*";
1640 else
1641 fmt = "%s/*";
1642
1643 _snprintf (path, MAX_PATH, fmt, (const char *)req->ptr1);
1644 dirp = FindFirstFile (path, &entp);
1645 free (path);
1646
1647 if (dirp == INVALID_HANDLE_VALUE)
1648 {
1649 dirp = 0;
1650
1651 switch (GetLastError ())
1652 {
1653 case ERROR_FILE_NOT_FOUND:
1654 req->result = 0;
1655 break;
1656
1657 case ERROR_INVALID_NAME:
1658 case ERROR_PATH_NOT_FOUND:
1659 case ERROR_NO_MORE_FILES:
1660 errno = ENOENT;
1661 break;
1662
1663 case ERROR_NOT_ENOUGH_MEMORY:
1664 errno = ENOMEM;
1665 break;
1666
1667 default:
1668 errno = EINVAL;
1669 break;
1670 }
1671 }
1672 }
1673#else
1637 self->dirp = dirp = opendir (req->ptr1); 1674 dirp = opendir (req->ptr1);
1675#endif
1638 1676
1639 if (req->flags & EIO_FLAG_PTR1_FREE) 1677 if (req->flags & EIO_FLAG_PTR1_FREE)
1640 free (req->ptr1); 1678 free (req->ptr1);
1641 1679
1642 req->flags |= EIO_FLAG_PTR1_FREE | EIO_FLAG_PTR2_FREE; 1680 req->flags |= EIO_FLAG_PTR1_FREE | EIO_FLAG_PTR2_FREE;
1643 req->ptr1 = dents = flags ? malloc (dentalloc * sizeof (eio_dirent)) : 0; 1681 req->ptr1 = dents = flags ? malloc (dentalloc * sizeof (eio_dirent)) : 0;
1644 req->ptr2 = names = malloc (namesalloc); 1682 req->ptr2 = names = malloc (namesalloc);
1645 X_UNLOCK (wrklock);
1646 1683
1647 if (dirp && names && (!flags || dents)) 1684 if (dirp && names && (!flags || dents))
1648 for (;;) 1685 for (;;)
1649 { 1686 {
1687 int done;
1688
1689#ifdef _WIN32
1690 done = !dirp;
1691#else
1650 errno = 0; 1692 errno = 0;
1651 entp = readdir (dirp); 1693 entp = readdir (dirp);
1694 done = !entp;
1695#endif
1652 1696
1653 if (!entp) 1697 if (done)
1654 { 1698 {
1699#ifndef _WIN32
1700 int old_errno = errno;
1701 closedir (dirp);
1702 errno = old_errno;
1703
1655 if (errno) 1704 if (errno)
1656 break; 1705 break;
1706#endif
1657 1707
1658 /* sort etc. */ 1708 /* sort etc. */
1659 req->int1 = flags; 1709 req->int1 = flags;
1660 req->result = dentoffs; 1710 req->result = dentoffs;
1661 1711
1690 1740
1691 break; 1741 break;
1692 } 1742 }
1693 1743
1694 /* now add the entry to our list(s) */ 1744 /* now add the entry to our list(s) */
1695 name = entp->d_name; 1745 name = D_NAME (entp);
1696 1746
1697 /* skip . and .. entries */ 1747 /* skip . and .. entries */
1698 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) 1748 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2])))
1699 { 1749 {
1700 int len = D_NAMLEN (entp) + 1; 1750 int len = D_NAMLEN (entp) + 1;
1701 1751
1702 while (ecb_expect_false (namesoffs + len > namesalloc)) 1752 while (ecb_expect_false (namesoffs + len > namesalloc))
1703 { 1753 {
1704 namesalloc *= 2; 1754 namesalloc *= 2;
1705 X_LOCK (wrklock);
1706 req->ptr2 = names = realloc (names, namesalloc); 1755 req->ptr2 = names = realloc (names, namesalloc);
1707 X_UNLOCK (wrklock);
1708 1756
1709 if (!names) 1757 if (!names)
1710 break; 1758 break;
1711 } 1759 }
1712 1760
1717 struct eio_dirent *ent; 1765 struct eio_dirent *ent;
1718 1766
1719 if (ecb_expect_false (dentoffs == dentalloc)) 1767 if (ecb_expect_false (dentoffs == dentalloc))
1720 { 1768 {
1721 dentalloc *= 2; 1769 dentalloc *= 2;
1722 X_LOCK (wrklock);
1723 req->ptr1 = dents = realloc (dents, dentalloc * sizeof (eio_dirent)); 1770 req->ptr1 = dents = realloc (dents, dentalloc * sizeof (eio_dirent));
1724 X_UNLOCK (wrklock);
1725 1771
1726 if (!dents) 1772 if (!dents)
1727 break; 1773 break;
1728 } 1774 }
1729 1775
1809 if (EIO_CANCELLED (req)) 1855 if (EIO_CANCELLED (req))
1810 { 1856 {
1811 errno = ECANCELED; 1857 errno = ECANCELED;
1812 break; 1858 break;
1813 } 1859 }
1860
1861#ifdef _WIN32
1862 if (!FindNextFile (dirp, &entp))
1863 {
1864 FindClose (dirp);
1865 dirp = 0;
1866 }
1867#endif
1814 } 1868 }
1815} 1869}
1816 1870
1817/*****************************************************************************/ 1871/*****************************************************************************/
1818 1872
1834X_THREAD_PROC (etp_proc) 1888X_THREAD_PROC (etp_proc)
1835{ 1889{
1836 ETP_REQ *req; 1890 ETP_REQ *req;
1837 struct timespec ts; 1891 struct timespec ts;
1838 etp_worker *self = (etp_worker *)thr_arg; 1892 etp_worker *self = (etp_worker *)thr_arg;
1839 int timeout;
1840 1893
1841 /* try to distribute timeouts somewhat evenly */ 1894 /* try to distribute timeouts somewhat evenly */
1842 ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL); 1895 ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL);
1843 1896
1844 for (;;) 1897 for (;;)
1914/*****************************************************************************/ 1967/*****************************************************************************/
1915 1968
1916int ecb_cold 1969int ecb_cold
1917eio_init (void (*want_poll)(void), void (*done_poll)(void)) 1970eio_init (void (*want_poll)(void), void (*done_poll)(void))
1918{ 1971{
1972#if !HAVE_PREADWRITE
1973 X_MUTEX_CREATE (preadwritelock);
1974#endif
1975
1919 return etp_init (want_poll, done_poll); 1976 return etp_init (want_poll, done_poll);
1920} 1977}
1921 1978
1922ecb_inline void 1979ecb_inline void
1923eio_api_destroy (eio_req *req) 1980eio_api_destroy (eio_req *req)
2178eio_req *eio_fchmod (int fd, mode_t mode, int pri, eio_cb cb, void *data) 2235eio_req *eio_fchmod (int fd, mode_t mode, int pri, eio_cb cb, void *data)
2179{ 2236{
2180 REQ (EIO_FCHMOD); req->int1 = fd; req->int2 = (long)mode; SEND; 2237 REQ (EIO_FCHMOD); req->int1 = fd; req->int2 = (long)mode; SEND;
2181} 2238}
2182 2239
2183eio_req *eio_fchown (int fd, uid_t uid, gid_t gid, int pri, eio_cb cb, void *data) 2240eio_req *eio_fchown (int fd, eio_uid_t uid, eio_gid_t gid, int pri, eio_cb cb, void *data)
2184{ 2241{
2185 REQ (EIO_FCHOWN); req->int1 = fd; req->int2 = (long)uid; req->int3 = (long)gid; SEND; 2242 REQ (EIO_FCHOWN); req->int1 = fd; req->int2 = (long)uid; req->int3 = (long)gid; SEND;
2186} 2243}
2187 2244
2188eio_req *eio_dup2 (int fd, int fd2, int pri, eio_cb cb, void *data) 2245eio_req *eio_dup2 (int fd, int fd2, int pri, eio_cb cb, void *data)
2208eio_req *eio_truncate (const char *path, off_t offset, int pri, eio_cb cb, void *data) 2265eio_req *eio_truncate (const char *path, off_t offset, int pri, eio_cb cb, void *data)
2209{ 2266{
2210 REQ (EIO_TRUNCATE); PATH; req->offs = offset; SEND; 2267 REQ (EIO_TRUNCATE); PATH; req->offs = offset; SEND;
2211} 2268}
2212 2269
2213eio_req *eio_chown (const char *path, uid_t uid, gid_t gid, int pri, eio_cb cb, void *data) 2270eio_req *eio_chown (const char *path, eio_uid_t uid, eio_gid_t gid, int pri, eio_cb cb, void *data)
2214{ 2271{
2215 REQ (EIO_CHOWN); PATH; req->int2 = (long)uid; req->int3 = (long)gid; SEND; 2272 REQ (EIO_CHOWN); PATH; req->int2 = (long)uid; req->int3 = (long)gid; SEND;
2216} 2273}
2217 2274
2218eio_req *eio_chmod (const char *path, mode_t mode, int pri, eio_cb cb, void *data) 2275eio_req *eio_chmod (const char *path, mode_t mode, int pri, eio_cb cb, void *data)
2365} 2422}
2366 2423
2367/*****************************************************************************/ 2424/*****************************************************************************/
2368/* misc garbage */ 2425/* misc garbage */
2369 2426
2370ssize_t 2427eio_ssize_t
2371eio_sendfile_sync (int ofd, int ifd, off_t offset, size_t count) 2428eio_sendfile_sync (int ofd, int ifd, off_t offset, size_t count)
2372{ 2429{
2373 etp_worker wrk; 2430 etp_worker wrk;
2374 ssize_t ret; 2431 eio_ssize_t ret;
2375 2432
2376 wrk.dbuf = 0; 2433 wrk.dbuf = 0;
2377 2434
2378 ret = eio__sendfile (ofd, ifd, offset, count, &wrk); 2435 ret = eio__sendfile (ofd, ifd, offset, count, &wrk);
2379 2436

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines