mdbx-posix: rework LCK seize/destroy.

This commit is contained in:
Leonid Yuriev 2019-08-25 15:56:43 +03:00
parent 2536dc47ac
commit 5eb3b77efe

View File

@ -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);
}