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

Comparing libeio/eio.c (file contents):
Revision 1.72 by root, Fri Jun 10 12:45:20 2011 UTC vs.
Revision 1.87 by root, Thu Jul 14 19:19:22 2011 UTC

54#include <stdlib.h> 54#include <stdlib.h>
55#include <string.h> 55#include <string.h>
56#include <errno.h> 56#include <errno.h>
57#include <sys/types.h> 57#include <sys/types.h>
58#include <sys/stat.h> 58#include <sys/stat.h>
59#include <sys/statvfs.h>
60#include <limits.h> 59#include <limits.h>
61#include <fcntl.h> 60#include <fcntl.h>
62#include <assert.h> 61#include <assert.h>
63 62
63#include <sys/statvfs.h>
64/* intptr_t comes from unistd.h, says POSIX/UNIX/tradition */ 64/* intptr_t comes from unistd.h, says POSIX/UNIX/tradition */
65/* intptr_t only comes form stdint.h, says idiot openbsd coder */ 65/* intptr_t only comes from stdint.h, says idiot openbsd coder */
66#if HAVE_STDINT_H 66#if HAVE_STDINT_H
67# include <stdint.h> 67# include <stdint.h>
68#endif 68#endif
69 69
70#ifndef ECANCELED
71# define ECANCELED EDOM
72#endif
73
74static void eio_destroy (eio_req *req);
75
70#ifndef EIO_FINISH 76#ifndef EIO_FINISH
71# define EIO_FINISH(req) ((req)->finish) && !EIO_CANCELLED (req) ? (req)->finish (req) : 0 77# define EIO_FINISH(req) ((req)->finish) && !EIO_CANCELLED (req) ? (req)->finish (req) : 0
72#endif 78#endif
73 79
74#ifndef EIO_DESTROY 80#ifndef EIO_DESTROY
77 83
78#ifndef EIO_FEED 84#ifndef EIO_FEED
79# define EIO_FEED(req) do { if ((req)->feed ) (req)->feed (req); } while (0) 85# define EIO_FEED(req) do { if ((req)->feed ) (req)->feed (req); } while (0)
80#endif 86#endif
81 87
88#ifndef EIO_FD_TO_WIN32_HANDLE
89# define EIO_FD_TO_WIN32_HANDLE(fd) _get_osfhandle (fd)
90#endif
91#ifndef EIO_WIN32_HANDLE_TO_FD
92# define EIO_WIN32_HANDLE_TO_FD(handle) _open_osfhandle (handle, 0)
93#endif
94
95#define EIO_ERRNO(errval,retval) ((errno = errval), retval)
96
97#define EIO_ENOSYS() EIO_ERRNO (ENOSYS, -1)
98
82#ifdef _WIN32 99#ifdef _WIN32
83 100
84 /*doh*/ 101 #define PAGESIZE 4096 /* GetSystemInfo? */
102
103 #ifdef EIO_STRUCT_STATI64
104 #define stat(path,buf) _stati64 (path,buf)
105 #define fstat(fd,buf) _fstati64 (path,buf)
106 #endif
107 #define lstat(path,buf) stat (path,buf)
108 #define fsync(fd) (FlushFileBuffers (EIO_FD_TO_WIN32_HANDLE (fd)) ? 0 : EIO_ERRNO (EBADF, -1))
109 #define mkdir(path,mode) _mkdir (path)
110 #define link(old,neu) (CreateHardLink (neu, old, 0) ? 0 : EIO_ERRNO (ENOENT, -1))
111
112 #define chown(path,uid,gid) EIO_ENOSYS ()
113 #define fchown(fd,uid,gid) EIO_ENOSYS ()
114 #define truncate(path,offs) EIO_ENOSYS () /* far-miss: SetEndOfFile */
115 #define ftruncate(fd,offs) EIO_ENOSYS () /* near-miss: SetEndOfFile */
116 #define mknod(path,mode,dev) EIO_ENOSYS ()
117 #define sync() EIO_ENOSYS ()
118
119 /* we could even stat and see if it exists */
120 static int
121 symlink (const char *old, const char *neu)
122 {
123 if (CreateSymbolicLink (neu, old, 1))
124 return 0;
125
126 if (CreateSymbolicLink (neu, old, 0))
127 return 0;
128
129 return EIO_ERRNO (ENOENT, -1);
130 }
131
85#else 132#else
86 133
87# include <sys/time.h> 134 #include <sys/time.h>
88# include <sys/select.h> 135 #include <sys/select.h>
136 #include <sys/statvfs.h>
89# include <unistd.h> 137 #include <unistd.h>
90# include <utime.h> 138 #include <utime.h>
91# include <signal.h> 139 #include <signal.h>
92# include <dirent.h> 140 #include <dirent.h>
93 141
94#if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES 142 #if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES
95# include <sys/mman.h> 143 #include <sys/mman.h>
96#endif 144 #endif
97 145
98/* POSIX_SOURCE is useless on bsd's, and XOPEN_SOURCE is unreliable there, too */ 146 /* POSIX_SOURCE is useless on bsd's, and XOPEN_SOURCE is unreliable there, too */
99# if __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ 147 #if __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__
100# define _DIRENT_HAVE_D_TYPE /* sigh */ 148 #define _DIRENT_HAVE_D_TYPE /* sigh */
101# define D_INO(de) (de)->d_fileno 149 #define D_INO(de) (de)->d_fileno
102# define D_NAMLEN(de) (de)->d_namlen 150 #define D_NAMLEN(de) (de)->d_namlen
103# elif __linux || defined d_ino || _XOPEN_SOURCE >= 600 151 #elif __linux || defined d_ino || _XOPEN_SOURCE >= 600
104# define D_INO(de) (de)->d_ino 152 #define D_INO(de) (de)->d_ino
105# endif 153 #endif
106 154
107#ifdef _D_EXACT_NAMLEN 155 #ifdef _D_EXACT_NAMLEN
108# undef D_NAMLEN 156 #undef D_NAMLEN
109# define D_NAMLEN(de) _D_EXACT_NAMLEN (de) 157 #define D_NAMLEN(de) _D_EXACT_NAMLEN (de)
110#endif 158 #endif
111 159
112# ifdef _DIRENT_HAVE_D_TYPE 160 #ifdef _DIRENT_HAVE_D_TYPE
113# define D_TYPE(de) (de)->d_type 161 #define D_TYPE(de) (de)->d_type
114# endif 162 #endif
115 163
116# ifndef EIO_STRUCT_DIRENT 164 #ifndef EIO_STRUCT_DIRENT
117# define EIO_STRUCT_DIRENT struct dirent 165 #define EIO_STRUCT_DIRENT struct dirent
118# endif 166 #endif
119 167
120#endif 168#endif
121 169
122#if HAVE_SENDFILE 170#if HAVE_SENDFILE
123# if __linux 171# if __linux
200/*****************************************************************************/ 248/*****************************************************************************/
201 249
202#define ETP_NUM_PRI (ETP_PRI_MAX - ETP_PRI_MIN + 1) 250#define ETP_NUM_PRI (ETP_PRI_MAX - ETP_PRI_MIN + 1)
203 251
204/* calculate time difference in ~1/EIO_TICKS of a second */ 252/* calculate time difference in ~1/EIO_TICKS of a second */
205ECB_INLINE int 253ecb_inline int
206tvdiff (struct timeval *tv1, struct timeval *tv2) 254tvdiff (struct timeval *tv1, struct timeval *tv2)
207{ 255{
208 return (tv2->tv_sec - tv1->tv_sec ) * EIO_TICKS 256 return (tv2->tv_sec - tv1->tv_sec ) * EIO_TICKS
209 + ((tv2->tv_usec - tv1->tv_usec) >> 10); 257 + ((tv2->tv_usec - tv1->tv_usec) >> 10);
210} 258}
375} 423}
376 424
377static void ecb_cold 425static void ecb_cold
378etp_thread_init (void) 426etp_thread_init (void)
379{ 427{
428#if !HAVE_PREADWRITE
429 X_MUTEX_CREATE (preadwritelock);
430#endif
380 X_MUTEX_CREATE (wrklock); 431 X_MUTEX_CREATE (wrklock);
381 X_MUTEX_CREATE (reslock); 432 X_MUTEX_CREATE (reslock);
382 X_MUTEX_CREATE (reqlock); 433 X_MUTEX_CREATE (reqlock);
383 X_COND_CREATE (reqwait); 434 X_COND_CREATE (reqwait);
384} 435}
385 436
386static void ecb_cold 437static void ecb_cold
387etp_atfork_prepare (void) 438etp_atfork_prepare (void)
388{ 439{
389 X_LOCK (wrklock);
390 X_LOCK (reqlock);
391 X_LOCK (reslock);
392#if !HAVE_PREADWRITE
393 X_LOCK (preadwritelock);
394#endif
395} 440}
396 441
397static void ecb_cold 442static void ecb_cold
398etp_atfork_parent (void) 443etp_atfork_parent (void)
399{ 444{
400#if !HAVE_PREADWRITE
401 X_UNLOCK (preadwritelock);
402#endif
403 X_UNLOCK (reslock);
404 X_UNLOCK (reqlock);
405 X_UNLOCK (wrklock);
406} 445}
407 446
408static void ecb_cold 447static void ecb_cold
409etp_atfork_child (void) 448etp_atfork_child (void)
410{ 449{
583} 622}
584 623
585static void 624static void
586etp_cancel (ETP_REQ *req) 625etp_cancel (ETP_REQ *req)
587{ 626{
588 X_LOCK (wrklock); 627 req->cancelled = 1;
589 req->flags |= EIO_FLAG_CANCELLED;
590 X_UNLOCK (wrklock);
591 628
592 eio_grp_cancel (req); 629 eio_grp_cancel (req);
593} 630}
594 631
595static void 632static void
711 return eio_finish (grp); 748 return eio_finish (grp);
712 else 749 else
713 return 0; 750 return 0;
714} 751}
715 752
716void 753static void
717eio_destroy (eio_req *req) 754eio_destroy (eio_req *req)
718{ 755{
719 if ((req)->flags & EIO_FLAG_PTR1_FREE) free (req->ptr1); 756 if ((req)->flags & EIO_FLAG_PTR1_FREE) free (req->ptr1);
720 if ((req)->flags & EIO_FLAG_PTR2_FREE) free (req->ptr2); 757 if ((req)->flags & EIO_FLAG_PTR2_FREE) free (req->ptr2);
721 758
739 if (grp->grp_first == req) 776 if (grp->grp_first == req)
740 grp->grp_first = req->grp_next; 777 grp->grp_first = req->grp_next;
741 778
742 res2 = grp_dec (grp); 779 res2 = grp_dec (grp);
743 780
744 if (!res && res2) 781 if (!res)
745 res = res2; 782 res = res2;
746 } 783 }
747 784
748 eio_destroy (req); 785 eio_destroy (req);
749 786
944 /* even though we could play tricks with the flags, it's better to always 981 /* even though we could play tricks with the flags, it's better to always
945 * call fdatasync, as that matches the expectation of its users best */ 982 * call fdatasync, as that matches the expectation of its users best */
946 return fdatasync (fd); 983 return fdatasync (fd);
947} 984}
948 985
986static int
987eio__fallocate (int fd, int mode, off_t offset, size_t len)
988{
989#if HAVE_FALLOCATE
990 return fallocate (fd, mode, offset, len);
991#else
992 errno = ENOSYS;
993 return -1;
994#endif
995}
996
949#if !HAVE_READAHEAD 997#if !HAVE_READAHEAD
950# undef readahead 998# undef readahead
951# define readahead(fd,offset,count) eio__readahead (fd, offset, count, self) 999# define readahead(fd,offset,count) eio__readahead (fd, offset, count, self)
952 1000
953static ssize_t 1001static ssize_t
981 if (!count) 1029 if (!count)
982 return 0; 1030 return 0;
983 1031
984 for (;;) 1032 for (;;)
985 { 1033 {
1034#ifdef __APPLE__
1035# undef HAVE_SENDFILE /* broken, as everything on os x */
1036#endif
986#if HAVE_SENDFILE 1037#if HAVE_SENDFILE
987# if __linux 1038# if __linux
988 off_t soffset = offset; 1039 off_t soffset = offset;
989 res = sendfile (ofd, ifd, &soffset, count); 1040 res = sendfile (ofd, ifd, &soffset, count);
990 1041
1033 if (res < 0 && sbytes) 1084 if (res < 0 && sbytes)
1034 res = sbytes; 1085 res = sbytes;
1035 1086
1036# endif 1087# endif
1037 1088
1038#elif defined (_WIN32) 1089#elif defined (_WIN32) && 0
1039 /* does not work, just for documentation of what would need to be done */ 1090 /* does not work, just for documentation of what would need to be done */
1040 /* actually, cannot be done like this, as TransmitFile changes the file offset, */ 1091 /* actually, cannot be done like this, as TransmitFile changes the file offset, */
1041 /* libeio guarantees that the file offset does not change, and windows */ 1092 /* libeio guarantees that the file offset does not change, and windows */
1042 /* has no way to get an independent handle to the same file description */ 1093 /* has no way to get an independent handle to the same file description */
1043 HANDLE h = TO_SOCKET (ifd); 1094 HANDLE h = TO_SOCKET (ifd);
1115 count -= cnt; 1166 count -= cnt;
1116 } 1167 }
1117 } 1168 }
1118 1169
1119 return res; 1170 return res;
1171}
1172
1173#ifdef PAGESIZE
1174# define eio_pagesize() PAGESIZE
1175#else
1176static intptr_t
1177eio_pagesize (void)
1178{
1179 static intptr_t page;
1180
1181 if (!page)
1182 page = sysconf (_SC_PAGESIZE);
1183
1184 return page;
1185}
1186#endif
1187
1188static void
1189eio_page_align (void **addr, size_t *length)
1190{
1191 intptr_t mask = eio_pagesize () - 1;
1192
1193 /* round down addr */
1194 intptr_t adj = mask & (intptr_t)*addr;
1195
1196 *addr = (void *)((intptr_t)*addr - adj);
1197 *length += adj;
1198
1199 /* round up length */
1200 *length = (*length + mask) & ~mask;
1201}
1202
1203#if !_POSIX_MEMLOCK
1204# define eio__mlockall(a) eio_nosyscall()
1205#else
1206
1207static int
1208eio__mlockall (int flags)
1209{
1210 #if __GLIBC__ == 2 && __GLIBC_MINOR__ <= 7
1211 extern int mallopt (int, int);
1212 mallopt (-6, 238); /* http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=473812 */
1213 #endif
1214
1215 if (EIO_MCL_CURRENT != MCL_CURRENT
1216 || EIO_MCL_FUTURE != MCL_FUTURE)
1217 {
1218 flags = 0
1219 | (flags & EIO_MCL_CURRENT ? MCL_CURRENT : 0)
1220 | (flags & EIO_MCL_FUTURE ? MCL_FUTURE : 0);
1221 }
1222
1223 return mlockall (flags);
1224}
1225#endif
1226
1227#if !_POSIX_MEMLOCK_RANGE
1228# define eio__mlock(a,b) EIO_ENOSYS ()
1229#else
1230
1231static int
1232eio__mlock (void *addr, size_t length)
1233{
1234 eio_page_align (&addr, &length);
1235
1236 return mlock (addr, length);
1237}
1238
1239#endif
1240
1241#if !(_POSIX_MAPPED_FILES && _POSIX_SYNCHRONIZED_IO)
1242# define eio__msync(a,b,c) EIO_ENOSYS ()
1243#else
1244
1245static int
1246eio__msync (void *mem, size_t len, int flags)
1247{
1248 eio_page_align (&mem, &len);
1249
1250 if (EIO_MS_ASYNC != MS_SYNC
1251 || EIO_MS_INVALIDATE != MS_INVALIDATE
1252 || EIO_MS_SYNC != MS_SYNC)
1253 {
1254 flags = 0
1255 | (flags & EIO_MS_ASYNC ? MS_ASYNC : 0)
1256 | (flags & EIO_MS_INVALIDATE ? MS_INVALIDATE : 0)
1257 | (flags & EIO_MS_SYNC ? MS_SYNC : 0);
1258 }
1259
1260 return msync (mem, len, flags);
1261}
1262
1263#endif
1264
1265static int
1266eio__mtouch (eio_req *req)
1267{
1268 void *mem = req->ptr2;
1269 size_t len = req->size;
1270 int flags = req->int1;
1271
1272 eio_page_align (&mem, &len);
1273
1274 {
1275 intptr_t addr = (intptr_t)mem;
1276 intptr_t end = addr + len;
1277 intptr_t page = eio_pagesize ();
1278
1279 if (addr < end)
1280 if (flags & EIO_MT_MODIFY) /* modify */
1281 do { *((volatile sig_atomic_t *)addr) |= 0; } while ((addr += page) < len && !EIO_CANCELLED (req));
1282 else
1283 do { *((volatile sig_atomic_t *)addr) ; } while ((addr += page) < len && !EIO_CANCELLED (req));
1284 }
1285
1286 return 0;
1287}
1288
1289/*****************************************************************************/
1290/* requests implemented outside eio_execute, because they are so large */
1291
1292static void
1293eio__realpath (eio_req *req, etp_worker *self)
1294{
1295 char *rel = req->ptr1;
1296 char *res;
1297 char *tmp1, *tmp2;
1298#if SYMLOOP_MAX > 32
1299 int symlinks = SYMLOOP_MAX;
1300#else
1301 int symlinks = 32;
1302#endif
1303
1304 req->result = -1;
1305
1306 errno = EINVAL;
1307 if (!rel)
1308 return;
1309
1310 errno = ENOENT;
1311 if (!*rel)
1312 return;
1313
1314 if (!req->ptr2)
1315 {
1316 X_LOCK (wrklock);
1317 req->flags |= EIO_FLAG_PTR2_FREE;
1318 X_UNLOCK (wrklock);
1319 req->ptr2 = malloc (PATH_MAX * 3);
1320
1321 errno = ENOMEM;
1322 if (!req->ptr2)
1323 return;
1324 }
1325
1326 res = req->ptr2;
1327 tmp1 = res + PATH_MAX;
1328 tmp2 = tmp1 + PATH_MAX;
1329
1330#if 0 /* disabled, the musl way to do things is just too racy */
1331#if __linux && defined(O_NONBLOCK) && defined(O_NOATIME)
1332 /* on linux we may be able to ask the kernel */
1333 {
1334 int fd = open (rel, O_RDONLY | O_NONBLOCK | O_NOCTTY | O_NOATIME);
1335
1336 if (fd >= 0)
1337 {
1338 sprintf (tmp1, "/proc/self/fd/%d", fd);
1339 req->result = readlink (tmp1, res, PATH_MAX);
1340 close (fd);
1341
1342 /* here we should probably stat the open file and the disk file, to make sure they still match */
1343
1344 if (req->result > 0)
1345 goto done;
1346 }
1347 else if (errno == ELOOP || errno == ENAMETOOLONG || errno == ENOENT || errno == ENOTDIR || errno == EIO)
1348 return;
1349 }
1350#endif
1351#endif
1352
1353 if (*rel != '/')
1354 {
1355 if (!getcwd (res, PATH_MAX))
1356 return;
1357
1358 if (res [1]) /* only use if not / */
1359 res += strlen (res);
1360 }
1361
1362 while (*rel)
1363 {
1364 ssize_t len, linklen;
1365 char *beg = rel;
1366
1367 while (*rel && *rel != '/')
1368 ++rel;
1369
1370 len = rel - beg;
1371
1372 if (!len) /* skip slashes */
1373 {
1374 ++rel;
1375 continue;
1376 }
1377
1378 if (beg [0] == '.')
1379 {
1380 if (len == 1)
1381 continue; /* . - nop */
1382
1383 if (beg [1] == '.' && len == 2)
1384 {
1385 /* .. - back up one component, if possible */
1386
1387 while (res != req->ptr2)
1388 if (*--res == '/')
1389 break;
1390
1391 continue;
1392 }
1393 }
1394
1395 errno = ENAMETOOLONG;
1396 if (res + 1 + len + 1 >= tmp1)
1397 return;
1398
1399 /* copy one component */
1400 *res = '/';
1401 memcpy (res + 1, beg, len);
1402
1403 /* zero-terminate, for readlink */
1404 res [len + 1] = 0;
1405
1406 /* now check if it's a symlink */
1407 linklen = readlink (req->ptr2, tmp1, PATH_MAX);
1408
1409 if (linklen < 0)
1410 {
1411 if (errno != EINVAL)
1412 return;
1413
1414 /* it's a normal directory. hopefully */
1415 res += len + 1;
1416 }
1417 else
1418 {
1419 /* yay, it was a symlink - build new path in tmp2 */
1420 int rellen = strlen (rel);
1421
1422 errno = ENAMETOOLONG;
1423 if (linklen + 1 + rellen >= PATH_MAX)
1424 return;
1425
1426 errno = ELOOP;
1427 if (!--symlinks)
1428 return;
1429
1430 if (*tmp1 == '/')
1431 res = req->ptr2; /* symlink resolves to an absolute path */
1432
1433 /* we need to be careful, as rel might point into tmp2 already */
1434 memmove (tmp2 + linklen + 1, rel, rellen + 1);
1435 tmp2 [linklen] = '/';
1436 memcpy (tmp2, tmp1, linklen);
1437
1438 rel = tmp2;
1439 }
1440 }
1441
1442 /* special case for the lone root path */
1443 if (res == req->ptr2)
1444 *res++ = '/';
1445
1446 req->result = res - (char *)req->ptr2;
1447
1448done:
1449 req->ptr2 = realloc (req->ptr2, req->result); /* trade time for space savings */
1120} 1450}
1121 1451
1122static signed char 1452static signed char
1123eio_dent_cmp (const eio_dirent *a, const eio_dirent *b) 1453eio_dent_cmp (const eio_dirent *a, const eio_dirent *b)
1124{ 1454{
1305 flags &= ~(EIO_READDIR_DIRS_FIRST | EIO_READDIR_STAT_ORDER); 1635 flags &= ~(EIO_READDIR_DIRS_FIRST | EIO_READDIR_STAT_ORDER);
1306 1636
1307 X_LOCK (wrklock); 1637 X_LOCK (wrklock);
1308 /* the corresponding closedir is in ETP_WORKER_CLEAR */ 1638 /* the corresponding closedir is in ETP_WORKER_CLEAR */
1309 self->dirp = dirp = opendir (req->ptr1); 1639 self->dirp = dirp = opendir (req->ptr1);
1640
1641 if (req->flags & EIO_FLAG_PTR1_FREE)
1642 free (req->ptr1);
1310 1643
1311 req->flags |= EIO_FLAG_PTR1_FREE | EIO_FLAG_PTR2_FREE; 1644 req->flags |= EIO_FLAG_PTR1_FREE | EIO_FLAG_PTR2_FREE;
1312 req->ptr1 = dents = flags ? malloc (dentalloc * sizeof (eio_dirent)) : 0; 1645 req->ptr1 = dents = flags ? malloc (dentalloc * sizeof (eio_dirent)) : 0;
1313 req->ptr2 = names = malloc (namesalloc); 1646 req->ptr2 = names = malloc (namesalloc);
1314 X_UNLOCK (wrklock); 1647 X_UNLOCK (wrklock);
1481 break; 1814 break;
1482 } 1815 }
1483 } 1816 }
1484} 1817}
1485 1818
1486#ifdef PAGESIZE
1487# define eio_pagesize() PAGESIZE
1488#else
1489static intptr_t
1490eio_pagesize (void)
1491{
1492 static intptr_t page;
1493
1494 if (!page)
1495 page = sysconf (_SC_PAGESIZE);
1496
1497 return page;
1498}
1499#endif
1500
1501static void
1502eio_page_align (void **addr, size_t *length)
1503{
1504 intptr_t mask = eio_pagesize () - 1;
1505
1506 /* round down addr */
1507 intptr_t adj = mask & (intptr_t)*addr;
1508
1509 *addr = (void *)((intptr_t)*addr - adj);
1510 *length += adj;
1511
1512 /* round up length */
1513 *length = (*length + mask) & ~mask;
1514}
1515
1516#if !_POSIX_MEMLOCK
1517# define eio__mlockall(a) ((errno = ENOSYS), -1)
1518#else
1519
1520static int
1521eio__mlockall (int flags)
1522{
1523 #if __GLIBC__ == 2 && __GLIBC_MINOR__ <= 7
1524 extern int mallopt (int, int);
1525 mallopt (-6, 238); /* http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=473812 */
1526 #endif
1527
1528 if (EIO_MCL_CURRENT != MCL_CURRENT
1529 || EIO_MCL_FUTURE != MCL_FUTURE)
1530 {
1531 flags = 0
1532 | (flags & EIO_MCL_CURRENT ? MCL_CURRENT : 0)
1533 | (flags & EIO_MCL_FUTURE ? MCL_FUTURE : 0);
1534 }
1535
1536 return mlockall (flags);
1537}
1538#endif
1539
1540#if !_POSIX_MEMLOCK_RANGE
1541# define eio__mlock(a,b) ((errno = ENOSYS), -1)
1542#else
1543
1544static int
1545eio__mlock (void *addr, size_t length)
1546{
1547 eio_page_align (&addr, &length);
1548
1549 return mlock (addr, length);
1550}
1551
1552#endif
1553
1554#if !(_POSIX_MAPPED_FILES && _POSIX_SYNCHRONIZED_IO)
1555# define eio__msync(a,b,c) ((errno = ENOSYS), -1)
1556#else
1557
1558static int
1559eio__msync (void *mem, size_t len, int flags)
1560{
1561 eio_page_align (&mem, &len);
1562
1563 if (EIO_MS_ASYNC != MS_SYNC
1564 || EIO_MS_INVALIDATE != MS_INVALIDATE
1565 || EIO_MS_SYNC != MS_SYNC)
1566 {
1567 flags = 0
1568 | (flags & EIO_MS_ASYNC ? MS_ASYNC : 0)
1569 | (flags & EIO_MS_INVALIDATE ? MS_INVALIDATE : 0)
1570 | (flags & EIO_MS_SYNC ? MS_SYNC : 0);
1571 }
1572
1573 return msync (mem, len, flags);
1574}
1575
1576#endif
1577
1578static int
1579eio__mtouch (eio_req *req)
1580{
1581 void *mem = req->ptr2;
1582 size_t len = req->size;
1583 int flags = req->int1;
1584
1585 eio_page_align (&mem, &len);
1586
1587 {
1588 intptr_t addr = (intptr_t)mem;
1589 intptr_t end = addr + len;
1590 intptr_t page = eio_pagesize ();
1591
1592 if (addr < end)
1593 if (flags & EIO_MT_MODIFY) /* modify */
1594 do { *((volatile sig_atomic_t *)addr) |= 0; } while ((addr += page) < len && !EIO_CANCELLED (req));
1595 else
1596 do { *((volatile sig_atomic_t *)addr) ; } while ((addr += page) < len && !EIO_CANCELLED (req));
1597 }
1598
1599 return 0;
1600}
1601
1602/*****************************************************************************/ 1819/*****************************************************************************/
1603 1820
1604#define ALLOC(len) \ 1821#define ALLOC(len) \
1605 if (!req->ptr2) \ 1822 if (!req->ptr2) \
1606 { \ 1823 { \
1619X_THREAD_PROC (etp_proc) 1836X_THREAD_PROC (etp_proc)
1620{ 1837{
1621 ETP_REQ *req; 1838 ETP_REQ *req;
1622 struct timespec ts; 1839 struct timespec ts;
1623 etp_worker *self = (etp_worker *)thr_arg; 1840 etp_worker *self = (etp_worker *)thr_arg;
1841 int timeout;
1624 1842
1625 /* try to distribute timeouts somewhat randomly */ 1843 /* try to distribute timeouts somewhat evenly */
1626 ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL); 1844 ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL);
1627 1845
1628 for (;;) 1846 for (;;)
1629 { 1847 {
1848 ts.tv_sec = 0;
1849
1630 X_LOCK (reqlock); 1850 X_LOCK (reqlock);
1631 1851
1632 for (;;) 1852 for (;;)
1633 { 1853 {
1634 self->req = req = reqq_shift (&req_queue); 1854 self->req = req = reqq_shift (&req_queue);
1635 1855
1636 if (req) 1856 if (req)
1637 break; 1857 break;
1638 1858
1859 if (ts.tv_sec == 1) /* no request, but timeout detected, let's quit */
1860 {
1861 X_UNLOCK (reqlock);
1862 X_LOCK (wrklock);
1863 --started;
1864 X_UNLOCK (wrklock);
1865 goto quit;
1866 }
1867
1639 ++idle; 1868 ++idle;
1640 1869
1641 ts.tv_sec = time (0) + idle_timeout; 1870 if (idle <= max_idle)
1642 if (X_COND_TIMEDWAIT (reqwait, reqlock, ts) == ETIMEDOUT) 1871 /* we are allowed to idle, so do so without any timeout */
1872 X_COND_WAIT (reqwait, reqlock);
1873 else
1643 { 1874 {
1644 if (idle > max_idle) 1875 /* initialise timeout once */
1645 { 1876 if (!ts.tv_sec)
1646 --idle; 1877 ts.tv_sec = time (0) + idle_timeout;
1647 X_UNLOCK (reqlock);
1648 X_LOCK (wrklock);
1649 --started;
1650 X_UNLOCK (wrklock);
1651 goto quit;
1652 }
1653 1878
1654 /* we are allowed to idle, so do so without any timeout */
1655 X_COND_WAIT (reqwait, reqlock); 1879 if (X_COND_TIMEDWAIT (reqwait, reqlock, ts) == ETIMEDOUT)
1880 ts.tv_sec = 1; /* assuming this is not a value computed above.,.. */
1656 } 1881 }
1657 1882
1658 --idle; 1883 --idle;
1659 } 1884 }
1660 1885
1663 X_UNLOCK (reqlock); 1888 X_UNLOCK (reqlock);
1664 1889
1665 if (req->type < 0) 1890 if (req->type < 0)
1666 goto quit; 1891 goto quit;
1667 1892
1668 if (!EIO_CANCELLED (req))
1669 ETP_EXECUTE (self, req); 1893 ETP_EXECUTE (self, req);
1670 1894
1671 X_LOCK (reslock); 1895 X_LOCK (reslock);
1672 1896
1673 ++npending; 1897 ++npending;
1674 1898
1695eio_init (void (*want_poll)(void), void (*done_poll)(void)) 1919eio_init (void (*want_poll)(void), void (*done_poll)(void))
1696{ 1920{
1697 return etp_init (want_poll, done_poll); 1921 return etp_init (want_poll, done_poll);
1698} 1922}
1699 1923
1700ECB_INLINE void 1924ecb_inline void
1701eio_api_destroy (eio_req *req) 1925eio_api_destroy (eio_req *req)
1702{ 1926{
1703 free (req); 1927 free (req);
1704} 1928}
1705 1929
1728 } 1952 }
1729 1953
1730static void 1954static void
1731eio_execute (etp_worker *self, eio_req *req) 1955eio_execute (etp_worker *self, eio_req *req)
1732{ 1956{
1957 if (ecb_expect_false (EIO_CANCELLED (req)))
1958 {
1959 req->result = -1;
1960 req->errorno = ECANCELED;
1961 return;
1962 }
1963
1733 switch (req->type) 1964 switch (req->type)
1734 { 1965 {
1735 case EIO_READ: ALLOC (req->size); 1966 case EIO_READ: ALLOC (req->size);
1736 req->result = req->offs >= 0 1967 req->result = req->offs >= 0
1737 ? pread (req->int1, req->ptr2, req->size, req->offs) 1968 ? pread (req->int1, req->ptr2, req->size, req->offs)
1771 case EIO_RENAME: req->result = rename (req->ptr1, req->ptr2); break; 2002 case EIO_RENAME: req->result = rename (req->ptr1, req->ptr2); break;
1772 case EIO_LINK: req->result = link (req->ptr1, req->ptr2); break; 2003 case EIO_LINK: req->result = link (req->ptr1, req->ptr2); break;
1773 case EIO_SYMLINK: req->result = symlink (req->ptr1, req->ptr2); break; 2004 case EIO_SYMLINK: req->result = symlink (req->ptr1, req->ptr2); break;
1774 case EIO_MKNOD: req->result = mknod (req->ptr1, (mode_t)req->int2, (dev_t)req->offs); break; 2005 case EIO_MKNOD: req->result = mknod (req->ptr1, (mode_t)req->int2, (dev_t)req->offs); break;
1775 2006
2007 case EIO_REALPATH: eio__realpath (req, self); break;
2008
1776 case EIO_READLINK: ALLOC (PATH_MAX); 2009 case EIO_READLINK: ALLOC (PATH_MAX);
1777 req->result = readlink (req->ptr1, req->ptr2, PATH_MAX); break; 2010 req->result = readlink (req->ptr1, req->ptr2, PATH_MAX); break;
1778 2011
1779 case EIO_SYNC: req->result = 0; sync (); break; 2012 case EIO_SYNC: req->result = 0; sync (); break;
1780 case EIO_FSYNC: req->result = fsync (req->int1); break; 2013 case EIO_FSYNC: req->result = fsync (req->int1); break;
1782 case EIO_MSYNC: req->result = eio__msync (req->ptr2, req->size, req->int1); break; 2015 case EIO_MSYNC: req->result = eio__msync (req->ptr2, req->size, req->int1); break;
1783 case EIO_MTOUCH: req->result = eio__mtouch (req); break; 2016 case EIO_MTOUCH: req->result = eio__mtouch (req); break;
1784 case EIO_MLOCK: req->result = eio__mlock (req->ptr2, req->size); break; 2017 case EIO_MLOCK: req->result = eio__mlock (req->ptr2, req->size); break;
1785 case EIO_MLOCKALL: req->result = eio__mlockall (req->int1); break; 2018 case EIO_MLOCKALL: req->result = eio__mlockall (req->int1); break;
1786 case EIO_SYNC_FILE_RANGE: req->result = eio__sync_file_range (req->int1, req->offs, req->size, req->int2); break; 2019 case EIO_SYNC_FILE_RANGE: req->result = eio__sync_file_range (req->int1, req->offs, req->size, req->int2); break;
2020 case EIO_FALLOCATE: req->result = eio__fallocate (req->int1, req->int2, req->offs, req->size); break;
1787 2021
1788 case EIO_READDIR: eio__scandir (req, self); break; 2022 case EIO_READDIR: eio__scandir (req, self); break;
1789 2023
1790 case EIO_BUSY: 2024 case EIO_BUSY:
1791#ifdef _WIN32 2025#ifdef _WIN32
1891eio_req *eio_sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags, int pri, eio_cb cb, void *data) 2125eio_req *eio_sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags, int pri, eio_cb cb, void *data)
1892{ 2126{
1893 REQ (EIO_SYNC_FILE_RANGE); req->int1 = fd; req->offs = offset; req->size = nbytes; req->int2 = flags; SEND; 2127 REQ (EIO_SYNC_FILE_RANGE); req->int1 = fd; req->offs = offset; req->size = nbytes; req->int2 = flags; SEND;
1894} 2128}
1895 2129
2130eio_req *eio_fallocate (int fd, int mode, off_t offset, size_t len, int pri, eio_cb cb, void *data)
2131{
2132 REQ (EIO_FALLOCATE); req->int1 = fd; req->int2 = mode; req->offs = offset; req->size = len; SEND;
2133}
2134
1896eio_req *eio_fdatasync (int fd, int pri, eio_cb cb, void *data) 2135eio_req *eio_fdatasync (int fd, int pri, eio_cb cb, void *data)
1897{ 2136{
1898 REQ (EIO_FDATASYNC); req->int1 = fd; SEND; 2137 REQ (EIO_FDATASYNC); req->int1 = fd; SEND;
1899} 2138}
1900 2139
1995} 2234}
1996 2235
1997eio_req *eio_readlink (const char *path, int pri, eio_cb cb, void *data) 2236eio_req *eio_readlink (const char *path, int pri, eio_cb cb, void *data)
1998{ 2237{
1999 return eio__1path (EIO_READLINK, path, pri, cb, data); 2238 return eio__1path (EIO_READLINK, path, pri, cb, data);
2239}
2240
2241eio_req *eio_realpath (const char *path, int pri, eio_cb cb, void *data)
2242{
2243 return eio__1path (EIO_REALPATH, path, pri, cb, data);
2000} 2244}
2001 2245
2002eio_req *eio_stat (const char *path, int pri, eio_cb cb, void *data) 2246eio_req *eio_stat (const char *path, int pri, eio_cb cb, void *data)
2003{ 2247{
2004 return eio__1path (EIO_STAT, path, pri, cb, data); 2248 return eio__1path (EIO_STAT, path, pri, cb, data);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines