From 5eb3b77efe4a596631da7bda3f3a947caee64f26 Mon Sep 17 00:00:00 2001 From: Leonid Yuriev Date: Sun, 25 Aug 2019 15:56:43 +0300 Subject: [PATCH] mdbx-posix: rework LCK seize/destroy. --- src/lck-posix.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/lck-posix.c b/src/lck-posix.c index afb3e79a..3b931579 100644 --- a/src/lck-posix.c +++ b/src/lck-posix.c @@ -152,7 +152,8 @@ int __cold mdbx_lck_seize(MDBX_env *env) { (env->me_flags & MDBX_RDONLY) ? F_RDLCK : F_WRLCK, 0, OFF_T_MAX); if (rc == 0) { - /* got dxb-exclusive, try lck-exclusive */ + continue_exclusive: + /* got dxb-exclusive, continue lck-exclusive */ rc = mdbx_lck_op(env->me_lfd, OP_SETLKW, F_WRLCK, 0, OFF_T_MAX); if (rc == 0) { /* got both exclusive */ @@ -169,7 +170,14 @@ int __cold mdbx_lck_seize(MDBX_env *env) { (env->me_flags & MDBX_RDONLY) ? F_RDLCK : F_WRLCK, env->me_pid, 1); if (rc == 0) { - /* got dxb-shared, try lck-shared */ + /* got dxb-shared, try again dxb-exclusive */ + rc = mdbx_lck_op(env->me_fd, OP_SETLK, + (env->me_flags & MDBX_RDONLY) ? F_RDLCK : F_WRLCK, 0, + OFF_T_MAX); + if (rc == 0) + goto continue_exclusive; + + /* continue 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 */ @@ -266,6 +274,9 @@ void __cold mdbx_lck_destroy(MDBX_env *env) { if (env->me_lfd != INVALID_HANDLE_VALUE) { /* try get exclusive access */ if (env->me_lck && + mdbx_lck_op(env->me_fd, OP_SETLK, + (env->me_flags & MDBX_RDONLY) ? F_RDLCK : F_WRLCK, 0, + OFF_T_MAX) == 0 && mdbx_lck_op(env->me_lfd, OP_SETLK, F_WRLCK, 0, OFF_T_MAX) == 0) { mdbx_info("%s: got exclusive, drown mutexes", mdbx_func_); int rc = pthread_mutex_destroy(&env->me_lck->mti_rmutex); @@ -273,6 +284,7 @@ void __cold mdbx_lck_destroy(MDBX_env *env) { rc = pthread_mutex_destroy(&env->me_lck->mti_wmutex); assert(rc == 0); (void)rc; + msync(env->me_lck, env->me_os_psize, MS_ASYNC); } (void)mdbx_lck_op(env->me_lfd, OP_SETLK, F_UNLCK, 0, OFF_T_MAX); }