Commit 2873a79f authored by Yoni Fogel's avatar Yoni Fogel

Closes #1321

pread/pwrite now threadsafe and do not modify file pointer

git-svn-id: file:///svn/toku/tokudb.1032b@8369 c7de825b-a66e-492c-adef-691d508d4ae1
parent f682c6a3
...@@ -4,27 +4,36 @@ ...@@ -4,27 +4,36 @@
#include <unistd.h> #include <unistd.h>
#include <windows.h> #include <windows.h>
// Note: pread and pwrite are not thread safe on the same fildes as they
// rely on the file offset
int64_t int64_t
pread(int fildes, void *buf, size_t nbyte, int64_t offset) { pread(int fildes, void *buf, size_t nbyte, int64_t offset) {
int64_t r = _lseeki64(fildes, offset, SEEK_SET); HANDLE handle;
if (r>=0) { OVERLAPPED offset = {0};
assert(r==offset); handle = _get_osfhandle(fildes);
r = read(fildes, buf, nbyte); offset.Offset = offset % (1LL<<32LL);
} offset.OffsetHigh = offset / (1LL<<32LL);
size_t bytes_read;
int64_t r = ReadFile(handle, buf, nbyte, &bytes_read, &offset);
if (!r) r = GetLastError();
else r = bytes_read;
// printf("%s: %d %p %u %I64d %I64d\n", __FUNCTION__, fildes, buf, nbyte, offset, r); fflush(stdout); // printf("%s: %d %p %u %I64d %I64d\n", __FUNCTION__, fildes, buf, nbyte, offset, r); fflush(stdout);
return r; return r;
} }
int64_t int64_t
pwrite(int fildes, const void *buf, size_t nbyte, int64_t offset) { pwrite(int fildes, const void *buf, size_t nbyte, int64_t offset) {
int64_t r = _lseeki64(fildes, offset, SEEK_SET); HANDLE handle;
if (r>=0) { OVERLAPPED offset = {0};
assert(r==offset); handle = _get_osfhandle(fildes);
r = write(fildes, buf, nbyte); offset.Offset = offset % (1LL<<32LL);
} offset.OffsetHigh = offset / (1LL<<32LL);
size_t bytes_written;
int64_t r = ReadFile(handle, buf, nbyte, &bytes_written, &offset);
if (!r) r = GetLastError();
else r = bytes_written;
// printf("%s: %d %p %u %I64d %I64d\n", __FUNCTION__, fildes, buf, nbyte, offset, r); fflush(stdout); // printf("%s: %d %p %u %I64d %I64d\n", __FUNCTION__, fildes, buf, nbyte, offset, r); fflush(stdout);
return r; return r;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment