mirror of
https://github.com/isar/libmdbx.git
synced 2025-01-20 05:18:21 +08:00
mdbx: drop mdbx_get_errno_checked().
Change-Id: Ifa829b465083f17eba911e19947bdae854f9759c
This commit is contained in:
parent
151d4540de
commit
10b0197026
@ -129,7 +129,7 @@ static __inline BOOL funlock(mdbx_filehandle_t fd, uint64_t offset,
|
||||
int mdbx_txn_lock(MDBX_env *env) {
|
||||
if (flock(env->me_fd, LCK_EXCLUSIVE | LCK_WAITFOR, LCK_BODY))
|
||||
return MDBX_SUCCESS;
|
||||
return mdbx_get_errno_checked();
|
||||
return GetLastError();
|
||||
}
|
||||
|
||||
void mdbx_txn_unlock(MDBX_env *env) {
|
||||
@ -155,7 +155,7 @@ int mdbx_rdt_lock(MDBX_env *env) {
|
||||
/* transite from S-? (used) to S-E (locked), e.g. exlcusive lock upper-part */
|
||||
if (flock(env->me_lfd, LCK_EXCLUSIVE | LCK_WAITFOR, LCK_UPPER))
|
||||
return MDBX_SUCCESS;
|
||||
return mdbx_get_errno_checked();
|
||||
return GetLastError();
|
||||
}
|
||||
|
||||
void mdbx_rdt_unlock(MDBX_env *env) {
|
||||
@ -196,7 +196,7 @@ static int internal_seize_lck(HANDLE lfd) {
|
||||
/* 1) now on ?-? (free), get ?-E (middle) */
|
||||
mdbx_jitter4testing(false);
|
||||
if (!flock(lfd, LCK_EXCLUSIVE | LCK_WAITFOR, LCK_UPPER)) {
|
||||
rc = mdbx_get_errno_checked() /* 2) something went wrong, give up */;
|
||||
rc = GetLastError() /* 2) something went wrong, give up */;
|
||||
mdbx_error("%s(%s) failed: errcode %u", mdbx_func_,
|
||||
"?-?(free) >> ?-E(middle)", rc);
|
||||
return rc;
|
||||
@ -208,7 +208,7 @@ static int internal_seize_lck(HANDLE lfd) {
|
||||
return MDBX_RESULT_TRUE; /* 4) got E-E (exclusive), done */
|
||||
|
||||
/* 5) still on ?-E (middle) */
|
||||
rc = mdbx_get_errno_checked();
|
||||
rc = GetLastError();
|
||||
mdbx_jitter4testing(false);
|
||||
if (rc != ERROR_SHARING_VIOLATION && rc != ERROR_LOCK_VIOLATION) {
|
||||
/* 6) something went wrong, give up */
|
||||
@ -220,9 +220,8 @@ static int internal_seize_lck(HANDLE lfd) {
|
||||
|
||||
/* 7) still on ?-E (middle), try S-E (locked) */
|
||||
mdbx_jitter4testing(false);
|
||||
rc = flock(lfd, LCK_SHARED | LCK_DONTWAIT, LCK_LOWER)
|
||||
? MDBX_RESULT_FALSE
|
||||
: mdbx_get_errno_checked();
|
||||
rc = flock(lfd, LCK_SHARED | LCK_DONTWAIT, LCK_LOWER) ? MDBX_RESULT_FALSE
|
||||
: GetLastError();
|
||||
|
||||
mdbx_jitter4testing(false);
|
||||
if (rc != MDBX_RESULT_FALSE)
|
||||
@ -247,7 +246,7 @@ int mdbx_lck_seize(MDBX_env *env) {
|
||||
/* LY: without-lck mode (e.g. on read-only filesystem) */
|
||||
mdbx_jitter4testing(false);
|
||||
if (!flock(env->me_fd, LCK_SHARED | LCK_DONTWAIT, LCK_WHOLE)) {
|
||||
rc = mdbx_get_errno_checked();
|
||||
rc = GetLastError();
|
||||
mdbx_error("%s(%s) failed: errcode %u", mdbx_func_, "without-lck", rc);
|
||||
return rc;
|
||||
}
|
||||
@ -264,7 +263,7 @@ int mdbx_lck_seize(MDBX_env *env) {
|
||||
* - we can't lock meta-pages, otherwise other process could get an error
|
||||
* while opening db in valid (non-conflict) mode. */
|
||||
if (!flock(env->me_fd, LCK_EXCLUSIVE | LCK_DONTWAIT, LCK_BODY)) {
|
||||
rc = mdbx_get_errno_checked();
|
||||
rc = GetLastError();
|
||||
mdbx_error("%s(%s) failed: errcode %u", mdbx_func_,
|
||||
"lock-against-without-lck", rc);
|
||||
mdbx_jitter4testing(false);
|
||||
@ -292,7 +291,7 @@ int mdbx_lck_downgrade(MDBX_env *env) {
|
||||
|
||||
/* 2) now at ?-E (middle), transite to S-E (locked) */
|
||||
if (!flock(env->me_lfd, LCK_SHARED | LCK_DONTWAIT, LCK_LOWER)) {
|
||||
int rc = mdbx_get_errno_checked() /* 3) something went wrong, give up */;
|
||||
int rc = GetLastError() /* 3) something went wrong, give up */;
|
||||
return rc;
|
||||
}
|
||||
|
||||
@ -320,7 +319,7 @@ int mdbx_lck_upgrade(MDBX_env *env) {
|
||||
return MDBX_RESULT_TRUE; /* 4) got E-E (exclusive), done */
|
||||
|
||||
/* 5) still on ?-E (middle) */
|
||||
int rc = mdbx_get_errno_checked();
|
||||
int rc = GetLastError();
|
||||
mdbx_jitter4testing(false);
|
||||
if (rc != ERROR_SHARING_VIOLATION && rc != ERROR_LOCK_VIOLATION) {
|
||||
/* 6) something went wrong, report but continue */
|
||||
@ -332,7 +331,7 @@ int mdbx_lck_upgrade(MDBX_env *env) {
|
||||
mdbx_jitter4testing(false);
|
||||
rc = flock(env->me_lfd, LCK_SHARED | LCK_DONTWAIT, LCK_LOWER)
|
||||
? MDBX_RESULT_FALSE
|
||||
: mdbx_get_errno_checked();
|
||||
: GetLastError();
|
||||
|
||||
mdbx_jitter4testing(false);
|
||||
if (rc != MDBX_RESULT_FALSE) {
|
||||
@ -352,14 +351,14 @@ void mdbx_lck_destroy(MDBX_env *env) {
|
||||
/* double `unlock` for robustly remove overlapped shared/exclusive locks */
|
||||
while (funlock(env->me_lfd, LCK_LOWER))
|
||||
;
|
||||
rc = mdbx_get_errno_checked();
|
||||
rc = GetLastError();
|
||||
assert(rc == ERROR_NOT_LOCKED);
|
||||
(void)rc;
|
||||
SetLastError(ERROR_SUCCESS);
|
||||
|
||||
while (funlock(env->me_lfd, LCK_UPPER))
|
||||
;
|
||||
rc = mdbx_get_errno_checked();
|
||||
rc = GetLastError();
|
||||
assert(rc == ERROR_NOT_LOCKED);
|
||||
(void)rc;
|
||||
SetLastError(ERROR_SUCCESS);
|
||||
@ -370,21 +369,21 @@ void mdbx_lck_destroy(MDBX_env *env) {
|
||||
* releases such locks via deferred queues) */
|
||||
while (funlock(env->me_fd, LCK_BODY))
|
||||
;
|
||||
rc = mdbx_get_errno_checked();
|
||||
rc = GetLastError();
|
||||
assert(rc == ERROR_NOT_LOCKED);
|
||||
(void)rc;
|
||||
SetLastError(ERROR_SUCCESS);
|
||||
|
||||
while (funlock(env->me_fd, LCK_META))
|
||||
;
|
||||
rc = mdbx_get_errno_checked();
|
||||
rc = GetLastError();
|
||||
assert(rc == ERROR_NOT_LOCKED);
|
||||
(void)rc;
|
||||
SetLastError(ERROR_SUCCESS);
|
||||
|
||||
while (funlock(env->me_fd, LCK_WHOLE))
|
||||
;
|
||||
rc = mdbx_get_errno_checked();
|
||||
rc = GetLastError();
|
||||
assert(rc == ERROR_NOT_LOCKED);
|
||||
(void)rc;
|
||||
SetLastError(ERROR_SUCCESS);
|
||||
@ -418,7 +417,7 @@ int mdbx_rpid_check(MDBX_env *env, mdbx_pid_t pid) {
|
||||
rc = WaitForSingleObject(hProcess, 0);
|
||||
CloseHandle(hProcess);
|
||||
} else {
|
||||
rc = mdbx_get_errno_checked();
|
||||
rc = GetLastError();
|
||||
}
|
||||
|
||||
switch (rc) {
|
||||
|
44
src/osal.c
44
src/osal.c
@ -22,7 +22,7 @@ static int waitstatus2errcode(DWORD result) {
|
||||
case WAIT_OBJECT_0:
|
||||
return MDBX_SUCCESS;
|
||||
case WAIT_FAILED:
|
||||
return mdbx_get_errno_checked();
|
||||
return GetLastError();
|
||||
case WAIT_ABANDONED:
|
||||
return ERROR_ABANDONED_WAIT_0;
|
||||
case WAIT_IO_COMPLETION:
|
||||
@ -202,11 +202,11 @@ int mdbx_condmutex_init(mdbx_condmutex_t *condmutex) {
|
||||
condmutex->event = NULL;
|
||||
condmutex->mutex = CreateMutex(NULL, FALSE, NULL);
|
||||
if (!condmutex->mutex)
|
||||
return mdbx_get_errno_checked();
|
||||
return GetLastError();
|
||||
|
||||
condmutex->event = CreateEvent(NULL, FALSE, FALSE, NULL);
|
||||
if (!condmutex->event) {
|
||||
rc = mdbx_get_errno_checked();
|
||||
rc = GetLastError();
|
||||
(void)CloseHandle(condmutex->mutex);
|
||||
condmutex->mutex = NULL;
|
||||
}
|
||||
@ -235,14 +235,12 @@ int mdbx_condmutex_destroy(mdbx_condmutex_t *condmutex) {
|
||||
int rc = MDBX_EINVAL;
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
if (condmutex->event) {
|
||||
rc =
|
||||
CloseHandle(condmutex->event) ? MDBX_SUCCESS : mdbx_get_errno_checked();
|
||||
rc = CloseHandle(condmutex->event) ? MDBX_SUCCESS : GetLastError();
|
||||
if (rc == MDBX_SUCCESS)
|
||||
condmutex->event = NULL;
|
||||
}
|
||||
if (condmutex->mutex) {
|
||||
rc =
|
||||
CloseHandle(condmutex->mutex) ? MDBX_SUCCESS : mdbx_get_errno_checked();
|
||||
rc = CloseHandle(condmutex->mutex) ? MDBX_SUCCESS : GetLastError();
|
||||
if (rc == MDBX_SUCCESS)
|
||||
condmutex->mutex = NULL;
|
||||
}
|
||||
@ -272,8 +270,7 @@ int mdbx_condmutex_lock(mdbx_condmutex_t *condmutex) {
|
||||
|
||||
int mdbx_condmutex_unlock(mdbx_condmutex_t *condmutex) {
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
return ReleaseMutex(condmutex->mutex) ? MDBX_SUCCESS
|
||||
: mdbx_get_errno_checked();
|
||||
return ReleaseMutex(condmutex->mutex) ? MDBX_SUCCESS : GetLastError();
|
||||
#else
|
||||
return pthread_mutex_unlock(&condmutex->mutex);
|
||||
#endif
|
||||
@ -281,7 +278,7 @@ int mdbx_condmutex_unlock(mdbx_condmutex_t *condmutex) {
|
||||
|
||||
int mdbx_condmutex_signal(mdbx_condmutex_t *condmutex) {
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
return SetEvent(condmutex->event) ? MDBX_SUCCESS : mdbx_get_errno_checked();
|
||||
return SetEvent(condmutex->event) ? MDBX_SUCCESS : GetLastError();
|
||||
#else
|
||||
return pthread_cond_signal(&condmutex->cond);
|
||||
#endif
|
||||
@ -385,14 +382,14 @@ int mdbx_openfile(const char *pathname, int flags, mode_t mode,
|
||||
CreationDisposition, FlagsAndAttributes, NULL);
|
||||
|
||||
if (*fd == INVALID_HANDLE_VALUE)
|
||||
return mdbx_get_errno_checked();
|
||||
if ((flags & O_CREAT) && mdbx_get_errno_checked() != ERROR_ALREADY_EXISTS) {
|
||||
return GetLastError();
|
||||
if ((flags & O_CREAT) && GetLastError() != ERROR_ALREADY_EXISTS) {
|
||||
/* set FILE_ATTRIBUTE_NOT_CONTENT_INDEXED for new file */
|
||||
DWORD FileAttributes = GetFileAttributesA(pathname);
|
||||
if (FileAttributes == INVALID_FILE_ATTRIBUTES ||
|
||||
!SetFileAttributesA(pathname, FileAttributes |
|
||||
FILE_ATTRIBUTE_NOT_CONTENT_INDEXED)) {
|
||||
int rc = mdbx_get_errno_checked();
|
||||
int rc = GetLastError();
|
||||
CloseHandle(*fd);
|
||||
*fd = INVALID_HANDLE_VALUE;
|
||||
return rc;
|
||||
@ -417,7 +414,7 @@ int mdbx_openfile(const char *pathname, int flags, mode_t mode,
|
||||
|
||||
int mdbx_closefile(mdbx_filehandle_t fd) {
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
return CloseHandle(fd) ? MDBX_SUCCESS : mdbx_get_errno_checked();
|
||||
return CloseHandle(fd) ? MDBX_SUCCESS : GetLastError();
|
||||
#else
|
||||
return (close(fd) == 0) ? MDBX_SUCCESS : errno;
|
||||
#endif
|
||||
@ -435,7 +432,7 @@ int mdbx_pread(mdbx_filehandle_t fd, void *buf, size_t bytes, uint64_t offset) {
|
||||
|
||||
DWORD read = 0;
|
||||
if (unlikely(!ReadFile(fd, buf, (DWORD)bytes, &read, &ov))) {
|
||||
int rc = mdbx_get_errno_checked();
|
||||
int rc = GetLastError();
|
||||
return (rc == MDBX_SUCCESS) ? /* paranoia */ ERROR_READ_FAULT : rc;
|
||||
}
|
||||
#else
|
||||
@ -464,7 +461,7 @@ int mdbx_pwrite(mdbx_filehandle_t fd, const void *buf, size_t bytes,
|
||||
DWORD written;
|
||||
if (likely(WriteFile(fd, buf, (DWORD)bytes, &written, &ov)))
|
||||
return (bytes == written) ? MDBX_SUCCESS : MDBX_EIO /* ERROR_WRITE_FAULT */;
|
||||
return mdbx_get_errno_checked();
|
||||
return GetLastError();
|
||||
#else
|
||||
int rc;
|
||||
ssize_t written;
|
||||
@ -524,7 +521,7 @@ int mdbx_write(mdbx_filehandle_t fd, const void *buf, size_t bytes) {
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
DWORD written;
|
||||
if (unlikely(!WriteFile(fd, ptr, (DWORD)chunk, &written, NULL)))
|
||||
return mdbx_get_errno_checked();
|
||||
return GetLastError();
|
||||
#else
|
||||
ssize_t written = write(fd, ptr, chunk);
|
||||
if (written < 0) {
|
||||
@ -557,7 +554,7 @@ int mdbx_write(mdbx_filehandle_t fd, const void *buf, size_t bytes) {
|
||||
int mdbx_filesync(mdbx_filehandle_t fd, bool fullsync) {
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
(void)fullsync;
|
||||
return FlushFileBuffers(fd) ? MDBX_SUCCESS : mdbx_get_errno_checked();
|
||||
return FlushFileBuffers(fd) ? MDBX_SUCCESS : GetLastError();
|
||||
#elif __GLIBC_PREREQ(2, 16) || _BSD_SOURCE || _XOPEN_SOURCE || \
|
||||
(__GLIBC_PREREQ(2, 8) && _POSIX_C_SOURCE >= 200112L)
|
||||
for (;;) {
|
||||
@ -583,7 +580,7 @@ int mdbx_filesize(mdbx_filehandle_t fd, uint64_t *length) {
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
BY_HANDLE_FILE_INFORMATION info;
|
||||
if (!GetFileInformationByHandle(fd, &info))
|
||||
return mdbx_get_errno_checked();
|
||||
return GetLastError();
|
||||
*length = info.nFileSizeLow | (uint64_t)info.nFileSizeHigh << 32;
|
||||
#else
|
||||
struct stat st;
|
||||
@ -604,7 +601,7 @@ int mdbx_ftruncate(mdbx_filehandle_t fd, uint64_t length) {
|
||||
li.QuadPart = length;
|
||||
return (SetFilePointerEx(fd, li, NULL, FILE_BEGIN) && SetEndOfFile(fd))
|
||||
? MDBX_SUCCESS
|
||||
: mdbx_get_errno_checked();
|
||||
: GetLastError();
|
||||
#else
|
||||
STATIC_ASSERT_MSG(sizeof(off_t) >= sizeof(size_t),
|
||||
"libmdbx requires 64-bit file I/O on 64-bit systems");
|
||||
@ -617,7 +614,7 @@ int mdbx_ftruncate(mdbx_filehandle_t fd, uint64_t length) {
|
||||
int mdbx_thread_key_create(mdbx_thread_key_t *key) {
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
*key = TlsAlloc();
|
||||
return (*key != TLS_OUT_OF_INDEXES) ? MDBX_SUCCESS : mdbx_get_errno_checked();
|
||||
return (*key != TLS_OUT_OF_INDEXES) ? MDBX_SUCCESS : GetLastError();
|
||||
#else
|
||||
return pthread_key_create(key, mdbx_rthc_dtor);
|
||||
#endif
|
||||
@ -652,7 +649,7 @@ int mdbx_thread_create(mdbx_thread_t *thread,
|
||||
void *arg) {
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
*thread = CreateThread(NULL, 0, start_routine, arg, 0, NULL);
|
||||
return *thread ? MDBX_SUCCESS : mdbx_get_errno_checked();
|
||||
return *thread ? MDBX_SUCCESS : GetLastError();
|
||||
#else
|
||||
return pthread_create(thread, NULL, start_routine, arg);
|
||||
#endif
|
||||
@ -674,8 +671,7 @@ int mdbx_msync(void *addr, size_t length, int async) {
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
if (async)
|
||||
return MDBX_SUCCESS;
|
||||
return FlushViewOfFile(addr, length) ? MDBX_SUCCESS
|
||||
: mdbx_get_errno_checked();
|
||||
return FlushViewOfFile(addr, length) ? MDBX_SUCCESS : GetLastError();
|
||||
#else
|
||||
const int mode = async ? MS_ASYNC : MS_SYNC;
|
||||
return (msync(addr, length, mode) == 0) ? MDBX_SUCCESS : errno;
|
||||
|
15
src/osal.h
15
src/osal.h
@ -396,21 +396,6 @@ static __inline int mdbx_get_errno(void) {
|
||||
return rc;
|
||||
}
|
||||
|
||||
static __inline int __mdbx_get_errno_checked(const char *file, unsigned line) {
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
DWORD rc = GetLastError();
|
||||
if (unlikely(rc == MDBX_EINVAL))
|
||||
mdbx_assert_fail(nullptr, "unexpected ERROR_INVALID_PARAMETER", file, line);
|
||||
#else
|
||||
int rc = errno;
|
||||
if (unlikely(rc == MDBX_EINVAL))
|
||||
mdbx_assert_fail(nullptr, "unexpected EINVAL", file, line);
|
||||
#endif
|
||||
return rc;
|
||||
}
|
||||
|
||||
#define mdbx_get_errno_checked() __mdbx_get_errno_checked(__FILE__, __LINE__)
|
||||
|
||||
int mdbx_memalign_alloc(size_t alignment, size_t bytes, void **result);
|
||||
void mdbx_memalign_free(void *ptr);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user