mdbx: more cleanup for Windows.

This commit is contained in:
Leo Yuriev 2017-04-24 19:37:01 +03:00
parent 66d842c23b
commit 993730d2f1
5 changed files with 38 additions and 33 deletions

View File

@ -88,19 +88,22 @@
#include <stdint.h>
#if defined(_WIN32) || defined(_WIN64)
#include <windows.h>
#include <winnt.h>
typedef unsigned mode_t;
typedef HANDLE mdbx_filehandle_t;
typedef DWORD mdbx_pid_t;
typedef DWORD mdbx_tid_t;
#define MDBX_ENODATA ERROR_HANDLE_EOF
#define MDBX_EINVAL ERROR_INVALID_PARAMETER
#define MDBX_EACCESS ERROR_ACCESS_DENIED
#define MDBX_ENOMEM ERROR_OUTOFMEMORY
#define MDBX_EROFS ERROR_FILE_READ_ONLY
#define MDBX_ENOSYS ERROR_NOT_SUPPORTED
#else
#include <errno.h> /* for error codes */
#include <pthread.h> /* for pthread_t */
#include <sys/types.h> /* for pid_t */
@ -109,11 +112,12 @@ typedef DWORD mdbx_tid_t;
typedef int mdbx_filehandle_t;
typedef pid_t mdbx_pid_t;
typedef pthread_t mdbx_tid_t;
#define MDBX_ENODATA ENODATA
#define MDBX_EINVAL EINVAL
#define MDBX_EACCESS EACCES
#define MDBX_ENOMEM ENOMEM
#define MDBX_EROFS EROFS
#define MDBX_ENOSYS ENOSYS
#endif

View File

@ -2962,12 +2962,9 @@ static int mdbx_page_flush(MDB_txn *txn, int keep) {
/* Write up to MDB_COMMIT_PAGES dirty pages at a time. */
if (pos != next_pos || n == MDB_COMMIT_PAGES || wsize + size > MAX_WRITE) {
if (n) {
retry:
/* Write previous page(s) */
rc = mdbx_pwritev(env->me_fd, iov, n, wpos, wsize);
if (unlikely(rc != MDB_SUCCESS)) {
if (rc == EINTR)
goto retry;
mdbx_debug("Write error: %s", strerror(rc));
return rc;
}
@ -3395,12 +3392,11 @@ static int mdbx_env_sync0(MDB_env *env, unsigned flags, MDB_meta *pending) {
if (flags & MDB_WRITEMAP) {
rc = mdbx_msync(env->me_map, used_size, flags & MDB_MAPASYNC);
if (unlikely(rc != MDB_SUCCESS))
/* LY: mdbx_msync() should never return EINTR */
goto fail;
if ((flags & MDB_MAPASYNC) == 0)
env->me_sync_pending = 0;
} else {
bool syncmeta = false;
bool fullsync = false;
if (unlikely(prev_mapsize != pending->mm_mapsize)) {
/* LY: It is no reason to use fdatasync() here, even in case
* no such bug in a kernel. Because "no-bug" mean that a kernel
@ -3412,13 +3408,11 @@ static int mdbx_env_sync0(MDB_env *env, unsigned flags, MDB_meta *pending) {
*
* For more info about of a corresponding fdatasync() bug
* see http://www.spinics.net/lists/linux-ext4/msg33714.html */
syncmeta = true;
fullsync = true;
}
while (
unlikely((rc = mdbx_filesync(env->me_fd, syncmeta)) != MDB_SUCCESS)) {
if (rc != EINTR)
rc = mdbx_filesync(env->me_fd, fullsync);
if (unlikely(rc != MDB_SUCCESS))
goto fail;
}
env->me_sync_pending = 0;
}
}
@ -3498,12 +3492,11 @@ static int mdbx_env_sync0(MDB_env *env, unsigned flags, MDB_meta *pending) {
if (unlikely(rc != MDB_SUCCESS))
goto fail;
} else {
while (unlikely((rc = mdbx_filesync(env->me_fd, false)) != MDB_SUCCESS)) {
if (rc != EINTR)
rc = mdbx_filesync(env->me_fd, false);
if (rc != MDB_SUCCESS)
goto undo;
}
}
}
/* LY: currently this can't happen, but... */
if (unlikely(pending->mm_mapsize < prev_mapsize)) {
@ -3803,7 +3796,7 @@ static int __cold mdbx_setup_locks(MDB_env *env, char *lck_pathname, int mode) {
int err = mdbx_openfile(lck_pathname, O_RDWR | O_CREAT, mode, &env->me_lfd);
if (err != MDB_SUCCESS) {
if (err != EROFS || (env->me_flags & MDB_RDONLY) == 0)
if (err != MDBX_EROFS || (env->me_flags & MDB_RDONLY) == 0)
return err;
/* LY: without-lck mode (e.g. on read-only filesystem) */
env->me_lfd = INVALID_HANDLE_VALUE;
@ -8547,7 +8540,7 @@ int __cold mdbx_env_set_assert(MDB_env *env, MDB_assert_func *func) {
return MDB_SUCCESS;
#else
(void)func;
return ENOSYS;
return MDBX_ENOSYS;
#endif
}

View File

@ -114,7 +114,8 @@ int mdbx_asprintf(char **strp, const char *fmt, ...) {
*strp = malloc(needed + 1);
if (unlikely(*strp == NULL)) {
va_end(ones);
return -ENOMEM;
SetLastError(MDBX_ENOMEM);
return -1;
}
#if defined(vsnprintf) || defined(_BSD_SOURCE) || _XOPEN_SOURCE >= 500 || \
@ -451,19 +452,26 @@ int mdbx_write(mdbx_filehandle_t fd, const void *buf, size_t bytes) {
}
}
int mdbx_filesync(mdbx_filehandle_t fd, bool syncmeta) {
int mdbx_filesync(mdbx_filehandle_t fd, bool fullsync) {
#if defined(_WIN32) || defined(_WIN64)
(void)syncmeta;
return FlushFileBuffers(fd) ? 0 : -1;
(void)fullsync;
return FlushFileBuffers(fd) ? MDB_SUCCESS : GetLastError();
#elif __GLIBC_PREREQ(2, 16) || _BSD_SOURCE || _XOPEN_SOURCE || \
(__GLIBC_PREREQ(2, 8) && _POSIX_C_SOURCE >= 200112L)
for (;;) {
#if _POSIX_C_SOURCE >= 199309L || _XOPEN_SOURCE >= 500 || \
defined(_POSIX_SYNCHRONIZED_IO)
if (!syncmeta)
return (fdatasync(fd) == 0) ? MDB_SUCCESS : errno;
if (!fullsync && fdatasync(fd) == 0)
return MDB_SUCCESS;
#else
(void)fullsync;
#endif
(void)syncmeta;
return (fsync(fd) == 0) ? MDB_SUCCESS : errno;
if (fsync(fd) == 0)
return MDB_SUCCESS;
int rc = errno;
if (rc != EINTR)
return rc;
}
#else
#error FIXME
#endif
@ -568,7 +576,7 @@ int mdbx_msync(void *addr, size_t length, int async) {
#if defined(_WIN32) || defined(_WIN64)
if (async)
return MDB_SUCCESS;
return FlushViewOfFile(addr, length) ? 0 : GetLastError();
return FlushViewOfFile(addr, length) ? MDB_SUCCESS : GetLastError();
#else
return (msync(addr, length, async ? MS_ASYNC : MS_SYNC) == 0) ? MDB_SUCCESS
: errno;
@ -580,7 +588,7 @@ int mdbx_mremap_size(void **address, size_t old_size, size_t new_size) {
*address = MAP_FAILED;
(void)old_size;
(void)new_size;
return ERROR_NOT_SUPPORTED;
return ERROR_CALL_NOT_IMPLEMENTED;
#else
*address = mremap(*address, old_size, new_size, 0, address);
return (*address != MAP_FAILED) ? MDB_SUCCESS : errno;

View File

@ -363,7 +363,7 @@ void mdbx_thread_key_delete(mdbx_thread_key_t key);
void *mdbx_thread_rthc_get(mdbx_thread_key_t key);
void mdbx_thread_rthc_set(mdbx_thread_key_t key, const void *value);
int mdbx_filesync(mdbx_filehandle_t fd, bool syncmeta);
int mdbx_filesync(mdbx_filehandle_t fd, bool fullsync);
int mdbx_ftruncate(mdbx_filehandle_t fd, off_t length);
int mdbx_filesize(mdbx_filehandle_t fd, off_t *length);
int mdbx_openfile(const char *pathname, int flags, mode_t mode,

View File

@ -94,8 +94,8 @@ time now_motonic() {
if (reciprocal == 0) {
if (!QueryPerformanceFrequency(&Frequency))
failure_perror("QueryPerformanceFrequency()", GetLastError());
reciprocal =
((UINT64_C(1) << 32) + Frequency.QuadPart / 2) / Frequency.QuadPart;
reciprocal = (uint32_t)(((UINT64_C(1) << 32) + Frequency.QuadPart / 2) /
Frequency.QuadPart);
assert(reciprocal);
}
@ -104,7 +104,7 @@ time now_motonic() {
failure_perror("QueryPerformanceCounter()", GetLastError());
time result;
result.integer = Counter.QuadPart / Frequency.QuadPart;
result.integer = (uint32_t)(Counter.QuadPart / Frequency.QuadPart);
uint64_t mod = Counter.QuadPart % Frequency.QuadPart;
assert(mod < UINT32_MAX);
result.fractional = UInt32x32To64((uint32_t)mod, reciprocal);