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

Comparing libeio/eio.c (file contents):
Revision 1.62 by root, Tue Feb 15 03:15:16 2011 UTC vs.
Revision 1.67 by root, Sun Jun 5 20:25:00 2011 UTC

35 * and other provisions required by the GPL. If you do not delete the 35 * and other provisions required by the GPL. If you do not delete the
36 * provisions above, a recipient may use your version of this file under 36 * provisions above, a recipient may use your version of this file under
37 * either the BSD or the GPL. 37 * either the BSD or the GPL.
38 */ 38 */
39 39
40#ifndef _WIN32
41# include "config.h"
42#endif
43
40#include "eio.h" 44#include "eio.h"
41 45
42#ifdef EIO_STACKSIZE 46#ifdef EIO_STACKSIZE
43# define XTHREAD_STACKSIZE EIO_STACKSIZE 47# define XTHREAD_STACKSIZE EIO_STACKSIZE
44#endif 48#endif
54#include <sys/statvfs.h> 58#include <sys/statvfs.h>
55#include <limits.h> 59#include <limits.h>
56#include <fcntl.h> 60#include <fcntl.h>
57#include <assert.h> 61#include <assert.h>
58 62
63/* intptr_t comes from unistd.h, says POSIX/UNIX/tradition */
64/* intptr_t only comes form stdint.h, says idiot openbsd coder */
65#if HAVE_STDINT_H
66# include <stdint.h>
67#endif
68
59#ifndef EIO_FINISH 69#ifndef EIO_FINISH
60# define EIO_FINISH(req) ((req)->finish) && !EIO_CANCELLED (req) ? (req)->finish (req) : 0 70# define EIO_FINISH(req) ((req)->finish) && !EIO_CANCELLED (req) ? (req)->finish (req) : 0
61#endif 71#endif
62 72
63#ifndef EIO_DESTROY 73#ifndef EIO_DESTROY
71#ifdef _WIN32 81#ifdef _WIN32
72 82
73 /*doh*/ 83 /*doh*/
74#else 84#else
75 85
76# include "config.h"
77# include <sys/time.h> 86# include <sys/time.h>
78# include <sys/select.h> 87# include <sys/select.h>
79# include <unistd.h> 88# include <unistd.h>
80# include <utime.h> 89# include <utime.h>
81# include <signal.h> 90# include <signal.h>
1060} 1069}
1061 1070
1062static signed char 1071static signed char
1063eio_dent_cmp (const eio_dirent *a, const eio_dirent *b) 1072eio_dent_cmp (const eio_dirent *a, const eio_dirent *b)
1064{ 1073{
1065 return a->score - b->score ? a->score - b->score /* works because our signed char is always 0..100 */ 1074 return a->score - b->score ? a->score - b->score /* works because our signed char is always 0..100 */
1066 : a->inode < b->inode ? -1 : a->inode > b->inode ? 1 : 0; 1075 : a->inode < b->inode ? -1
1076 : a->inode > b->inode ? 1
1077 : 0;
1067} 1078}
1068 1079
1069#define EIO_DENT_CMP(i,op,j) eio_dent_cmp (&i, &j) op 0 1080#define EIO_DENT_CMP(i,op,j) eio_dent_cmp (&i, &j) op 0
1070 1081
1071#define EIO_SORT_CUTOFF 30 /* quite high, but performs well on many filesystems */ 1082#define EIO_SORT_CUTOFF 30 /* quite high, but performs well on many filesystems */
1077 unsigned char bits [9 + sizeof (ino_t) * 8]; 1088 unsigned char bits [9 + sizeof (ino_t) * 8];
1078 unsigned char *bit = bits; 1089 unsigned char *bit = bits;
1079 1090
1080 assert (CHAR_BIT == 8); 1091 assert (CHAR_BIT == 8);
1081 assert (sizeof (eio_dirent) * 8 < 256); 1092 assert (sizeof (eio_dirent) * 8 < 256);
1082 assert (offsetof (eio_dirent, inode)); /* we use 0 as sentinel */ 1093 assert (offsetof (eio_dirent, inode)); /* we use bit #0 as sentinel */
1083 assert (offsetof (eio_dirent, score)); /* we use 0 as sentinel */ 1094 assert (offsetof (eio_dirent, score)); /* we use bit #0 as sentinel */
1084 1095
1085 if (size <= EIO_SORT_FAST) 1096 if (size <= EIO_SORT_FAST)
1086 return; 1097 return;
1087 1098
1088 /* first prepare an array of bits to test in our radix sort */ 1099 /* first prepare an array of bits to test in our radix sort */
1243 flags &= ~(EIO_READDIR_DIRS_FIRST | EIO_READDIR_STAT_ORDER); 1254 flags &= ~(EIO_READDIR_DIRS_FIRST | EIO_READDIR_STAT_ORDER);
1244 1255
1245 X_LOCK (wrklock); 1256 X_LOCK (wrklock);
1246 /* the corresponding closedir is in ETP_WORKER_CLEAR */ 1257 /* the corresponding closedir is in ETP_WORKER_CLEAR */
1247 self->dirp = dirp = opendir (req->ptr1); 1258 self->dirp = dirp = opendir (req->ptr1);
1259
1248 req->flags |= EIO_FLAG_PTR1_FREE | EIO_FLAG_PTR2_FREE; 1260 req->flags |= EIO_FLAG_PTR1_FREE | EIO_FLAG_PTR2_FREE;
1249 req->ptr1 = dents = flags ? malloc (dentalloc * sizeof (eio_dirent)) : 0; 1261 req->ptr1 = dents = flags ? malloc (dentalloc * sizeof (eio_dirent)) : 0;
1250 req->ptr2 = names = malloc (namesalloc); 1262 req->ptr2 = names = malloc (namesalloc);
1251 X_UNLOCK (wrklock); 1263 X_UNLOCK (wrklock);
1252 1264
1264 /* sort etc. */ 1276 /* sort etc. */
1265 req->int1 = flags; 1277 req->int1 = flags;
1266 req->result = dentoffs; 1278 req->result = dentoffs;
1267 1279
1268 if (flags & EIO_READDIR_STAT_ORDER) 1280 if (flags & EIO_READDIR_STAT_ORDER)
1269 eio_dent_sort (dents, dentoffs, 0, inode_bits); /* sort by inode exclusively */ 1281 eio_dent_sort (dents, dentoffs, flags & EIO_READDIR_DIRS_FIRST ? 7 : 0, inode_bits);
1270 else if (flags & EIO_READDIR_DIRS_FIRST) 1282 else if (flags & EIO_READDIR_DIRS_FIRST)
1271 if (flags & EIO_READDIR_FOUND_UNKNOWN) 1283 if (flags & EIO_READDIR_FOUND_UNKNOWN)
1272 eio_dent_sort (dents, dentoffs, 7, inode_bits); /* sort by score and inode */ 1284 eio_dent_sort (dents, dentoffs, 7, inode_bits); /* sort by score and inode */
1273 else 1285 else
1274 { 1286 {
1276 eio_dirent *oth = dents + dentoffs; 1288 eio_dirent *oth = dents + dentoffs;
1277 eio_dirent *dir = dents; 1289 eio_dirent *dir = dents;
1278 1290
1279 /* now partition dirs to the front, and non-dirs to the back */ 1291 /* now partition dirs to the front, and non-dirs to the back */
1280 /* by walking from both sides and swapping if necessary */ 1292 /* by walking from both sides and swapping if necessary */
1281 /* also clear score, so it doesn't influence sorting */
1282 while (oth > dir) 1293 while (oth > dir)
1283 { 1294 {
1284 if (dir->type == EIO_DT_DIR) 1295 if (dir->type == EIO_DT_DIR)
1285 ++dir; 1296 ++dir;
1286 else if ((--oth)->type == EIO_DT_DIR) 1297 else if ((--oth)->type == EIO_DT_DIR)
1289 1300
1290 ++dir; 1301 ++dir;
1291 } 1302 }
1292 } 1303 }
1293 1304
1294 /* now sort the dirs only */ 1305 /* now sort the dirs only (dirs all have the same score) */
1295 eio_dent_sort (dents, dir - dents, 0, inode_bits); 1306 eio_dent_sort (dents, dir - dents, 0, inode_bits);
1296 } 1307 }
1297 1308
1298 break; 1309 break;
1299 } 1310 }
1512} 1523}
1513 1524
1514#endif 1525#endif
1515 1526
1516int 1527int
1517eio__mtouch (void *mem, size_t len, int flags) 1528eio__mtouch (eio_req *req)
1518{ 1529{
1530 void *mem = req->ptr2;
1531 size_t len = req->size;
1532 int flags = req->int1;
1533
1519 eio_page_align (&mem, &len); 1534 eio_page_align (&mem, &len);
1520 1535
1521 { 1536 {
1522 intptr_t addr = (intptr_t)mem; 1537 intptr_t addr = (intptr_t)mem;
1523 intptr_t end = addr + len; 1538 intptr_t end = addr + len;
1524 intptr_t page = eio_pagesize (); 1539 intptr_t page = eio_pagesize ();
1525 1540
1526 if (addr < end) 1541 if (addr < end)
1527 if (flags & EIO_MT_MODIFY) /* modify */ 1542 if (flags & EIO_MT_MODIFY) /* modify */
1528 do { *((volatile sig_atomic_t *)addr) |= 0; } while ((addr += page) < len); 1543 do { *((volatile sig_atomic_t *)addr) |= 0; } while ((addr += page) < len && !EIO_CANCELLED (req));
1529 else 1544 else
1530 do { *((volatile sig_atomic_t *)addr) ; } while ((addr += page) < len); 1545 do { *((volatile sig_atomic_t *)addr) ; } while ((addr += page) < len && !EIO_CANCELLED (req));
1531 } 1546 }
1532 1547
1533 return 0; 1548 return 0;
1534} 1549}
1535 1550
1709 1724
1710 case EIO_SYNC: req->result = 0; sync (); break; 1725 case EIO_SYNC: req->result = 0; sync (); break;
1711 case EIO_FSYNC: req->result = fsync (req->int1); break; 1726 case EIO_FSYNC: req->result = fsync (req->int1); break;
1712 case EIO_FDATASYNC: req->result = fdatasync (req->int1); break; 1727 case EIO_FDATASYNC: req->result = fdatasync (req->int1); break;
1713 case EIO_MSYNC: req->result = eio__msync (req->ptr2, req->size, req->int1); break; 1728 case EIO_MSYNC: req->result = eio__msync (req->ptr2, req->size, req->int1); break;
1714 case EIO_MTOUCH: req->result = eio__mtouch (req->ptr2, req->size, req->int1); break; 1729 case EIO_MTOUCH: req->result = eio__mtouch (req); break;
1715 case EIO_MLOCK: req->result = eio__mlock (req->ptr2, req->size); break; 1730 case EIO_MLOCK: req->result = eio__mlock (req->ptr2, req->size); break;
1716 case EIO_MLOCKALL: req->result = eio__mlockall (req->int1); break; 1731 case EIO_MLOCKALL: req->result = eio__mlockall (req->int1); break;
1717 case EIO_SYNC_FILE_RANGE: req->result = eio__sync_file_range (req->int1, req->offs, req->size, req->int2); break; 1732 case EIO_SYNC_FILE_RANGE: req->result = eio__sync_file_range (req->int1, req->offs, req->size, req->int2); break;
1718 1733
1719 case EIO_READDIR: eio__scandir (req, self); break; 1734 case EIO_READDIR: eio__scandir (req, self); break;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines