mdbx: refine lck-posix implementation.

This commit is contained in:
Leonid Yuriev 2019-08-11 19:13:29 +03:00
parent 036144eed4
commit 69f831af0b

View File

@ -167,11 +167,16 @@ int __cold mdbx_lck_seize(MDBX_env *env) {
(env->me_flags & MDBX_RDONLY) ? F_RDLCK : F_WRLCK, (env->me_flags & MDBX_RDONLY) ? F_RDLCK : F_WRLCK,
env->me_pid, 1); env->me_pid, 1);
if (rc == 0) { if (rc == 0) {
/* got dxb-shared-rw */ /* got dxb-shared, try lck-shared */
rc = mdbx_lck_op(env->me_lfd, OP_SETLKW, F_RDLCK, 0, 1);
if (rc == 0) {
/* got both dxb and lck shared lock */
return MDBX_RESULT_FALSE; return MDBX_RESULT_FALSE;
} }
mdbx_error("%s(%s) failed: errcode %u", mdbx_func_, mdbx_error("%s(%s) failed: errcode %u", mdbx_func_, "lck-shared", rc);
"lock-against-without-lck", rc); } else {
mdbx_error("%s(%s) failed: errcode %u", mdbx_func_, "dxb-shared", rc);
}
assert(MDBX_IS_ERROR(rc)); assert(MDBX_IS_ERROR(rc));
} }
@ -184,7 +189,9 @@ bailout:
int mdbx_lck_downgrade(MDBX_env *env, bool complete) { int mdbx_lck_downgrade(MDBX_env *env, bool complete) {
assert(env->me_lfd != INVALID_HANDLE_VALUE); assert(env->me_lfd != INVALID_HANDLE_VALUE);
int rc = mdbx_lck_op(env->me_lfd, OP_SETLK, F_UNLCK, 0, OFF_T_MAX); int rc = mdbx_lck_op(env->me_lfd, OP_SETLK, F_UNLCK, 1, OFF_T_MAX - 1);
if (rc == 0)
rc = mdbx_lck_op(env->me_lfd, OP_SETLKW, F_RDLCK, 0, 1);
if (unlikely(rc != 0)) { if (unlikely(rc != 0)) {
mdbx_error("%s(%s) failed: errcode %u", mdbx_func_, "lck", rc); mdbx_error("%s(%s) failed: errcode %u", mdbx_func_, "lck", rc);
goto bailout; goto bailout;
@ -251,22 +258,13 @@ bailout:
} }
void __cold mdbx_lck_destroy(MDBX_env *env) { void __cold mdbx_lck_destroy(MDBX_env *env) {
if (env->me_lfd != INVALID_HANDLE_VALUE && env->me_lck) { /* File locks would be released (by kernel) while the file-descriptors
/* try get exclusive access */ * will be closed. But to avoid false-positive EDEADLK from the kernel,
if (mdbx_lck_op(env->me_fd, OP_SETLK, * locks should be released here explicitly with properly order. */
(env->me_flags & MDBX_RDONLY) ? F_RDLCK : F_WRLCK, 0, if (env->me_lfd != INVALID_HANDLE_VALUE)
OFF_T_MAX) == 0 && (void)mdbx_lck_op(env->me_lfd, OP_SETLK, F_UNLCK, 0, OFF_T_MAX);
mdbx_lck_op(env->me_lfd, OP_SETLKW, F_WRLCK, 0, OFF_T_MAX) == 0) { assert(env->me_fd != INVALID_HANDLE_VALUE);
mdbx_info("%s: got exclusive, drown mutexes", mdbx_func_); (void)mdbx_lck_op(env->me_fd, OP_SETLK, F_UNLCK, 0, OFF_T_MAX);
int rc = pthread_mutex_destroy(&env->me_lck->mti_rmutex);
if (rc == 0)
rc = pthread_mutex_destroy(&env->me_lck->mti_wmutex);
assert(rc == 0);
(void)rc;
/* file locks would be released (by kernel)
* while the me_lfd will be closed */
}
}
} }
static int mdbx_robust_lock(MDBX_env *env, pthread_mutex_t *mutex) { static int mdbx_robust_lock(MDBX_env *env, pthread_mutex_t *mutex) {