From 0a7a290d068a9e28bdb952736470767c2550658a Mon Sep 17 00:00:00 2001 From: Leo Yuriev Date: Thu, 26 Oct 2017 21:47:41 +0300 Subject: [PATCH] mdbx: drop mdbx_txn_trylock(). --- src/lck-posix.c | 12 +++--------- src/lck-windows.c | 19 ++++++------------- src/mdbx.c | 13 ++++++------- src/osal.h | 3 +-- 4 files changed, 16 insertions(+), 31 deletions(-) diff --git a/src/lck-posix.c b/src/lck-posix.c index 2aeed6f4..14ce3d50 100644 --- a/src/lck-posix.c +++ b/src/lck-posix.c @@ -213,16 +213,10 @@ void mdbx_rdt_unlock(MDBX_env *env) { mdbx_panic("%s() failed: errcode %d\n", mdbx_func_, rc); } -int mdbx_txn_lock(MDBX_env *env) { +int mdbx_txn_lock(MDBX_env *env, bool dontwait) { mdbx_trace(">>"); - int rc = mdbx_robust_lock(env, &env->me_lck->mti_wmutex); - mdbx_trace("<< rc %d", rc); - return MDBX_IS_ERROR(rc) ? rc : MDBX_SUCCESS; -} - -int mdbx_txn_trylock(MDBX_env *env) { - mdbx_trace(">>"); - int rc = mdbx_robust_trylock(env, &env->me_lck->mti_wmutex); + int rc = dontwait ? mdbx_robust_trylock(env, &env->me_lck->mti_wmutex) + : mdbx_robust_lock(env, &env->me_lck->mti_wmutex); mdbx_trace("<< rc %d", rc); return MDBX_IS_ERROR(rc) ? rc : MDBX_SUCCESS; } diff --git a/src/lck-windows.c b/src/lck-windows.c index d3340d58..672512a7 100644 --- a/src/lck-windows.c +++ b/src/lck-windows.c @@ -1,4 +1,4 @@ -/* +/* * Copyright 2015-2017 Leonid Yuriev * and other libmdbx authors: please see AUTHORS file. * All rights reserved. @@ -126,20 +126,13 @@ static __inline BOOL funlock(mdbx_filehandle_t fd, uint64_t offset, #define LCK_BODY LCK_BODY_OFFSET, LCK_BODY_LEN #define LCK_WHOLE 0, LCK_MAXLEN -int mdbx_txn_lock(MDBX_env *env) { - if (flock(env->me_fd, LCK_EXCLUSIVE | LCK_WAITFOR, LCK_BODY)) - return MDBX_SUCCESS; - return GetLastError(); -} - -int mdbx_txn_trylock(MDBX_env *env) { - if (flock(env->me_fd, LCK_EXCLUSIVE | LCK_DONTWAIT, LCK_BODY)) +int mdbx_txn_lock(MDBX_env *env, bool dontwait) { + if (flock(env->me_fd, dontwait ? (LCK_EXCLUSIVE | LCK_DONTWAIT) + : (LCK_EXCLUSIVE | LCK_WAITFOR), + LCK_BODY)) return MDBX_SUCCESS; int rc = GetLastError(); - if (rc == ERROR_LOCK_VIOLATION) { - rc = MDBX_BUSY; - } - return rc; + return (!dontwait || rc != ERROR_LOCK_VIOLATION) ? rc : MDBX_BUSY; } void mdbx_txn_unlock(MDBX_env *env) { diff --git a/src/mdbx.c b/src/mdbx.c index 7e480ce3..ace75f93 100644 --- a/src/mdbx.c +++ b/src/mdbx.c @@ -2288,7 +2288,7 @@ int mdbx_env_sync(MDBX_env *env, int force) { (!env->me_txn0 || env->me_txn0->mt_owner != mdbx_thread_self()); if (outside_txn) { - int rc = mdbx_txn_lock(env); + int rc = mdbx_txn_lock(env, false); if (unlikely(rc != MDBX_SUCCESS)) return rc; } @@ -2317,7 +2317,7 @@ int mdbx_env_sync(MDBX_env *env, int force) { if (unlikely(rc != MDBX_SUCCESS)) return rc; - rc = mdbx_txn_lock(env); + rc = mdbx_txn_lock(env, false); if (unlikely(rc != MDBX_SUCCESS)) return rc; @@ -2563,8 +2563,7 @@ static int mdbx_txn_renew0(MDBX_txn *txn, unsigned flags) { } else { /* Not yet touching txn == env->me_txn0, it may be active */ mdbx_jitter4testing(false); - rc = F_ISSET(flags, MDBX_TRYTXN) ? mdbx_txn_trylock(env) - : mdbx_txn_lock(env); + rc = mdbx_txn_lock(env, F_ISSET(flags, MDBX_TRYTXN)); if (unlikely(rc)) return rc; @@ -4444,7 +4443,7 @@ LIBMDBX_API int mdbx_env_set_geometry(MDBX_env *env, intptr_t size_lower, return MDBX_EACCESS; if (outside_txn) { - int err = mdbx_txn_lock(env); + int err = mdbx_txn_lock(env, false); if (unlikely(err != MDBX_SUCCESS)) return err; } @@ -9772,7 +9771,7 @@ static int __cold mdbx_env_copy_asis(MDBX_env *env, mdbx_filehandle_t fd) { goto bailout; /* FIXME: or just return? */ /* Temporarily block writers until we snapshot the meta pages */ - rc = mdbx_txn_lock(env); + rc = mdbx_txn_lock(env, false); if (unlikely(rc != MDBX_SUCCESS)) goto bailout; @@ -9857,7 +9856,7 @@ int __cold mdbx_env_set_flags(MDBX_env *env, unsigned flags, int onoff) { if (unlikely(flags & ~CHANGEABLE)) return MDBX_EINVAL; - int rc = mdbx_txn_lock(env); + int rc = mdbx_txn_lock(env, false); if (unlikely(rc)) return rc; diff --git a/src/osal.h b/src/osal.h index ad9fd275..871e52b0 100644 --- a/src/osal.h +++ b/src/osal.h @@ -505,8 +505,7 @@ void mdbx_lck_destroy(MDBX_env *env); int mdbx_rdt_lock(MDBX_env *env); void mdbx_rdt_unlock(MDBX_env *env); -int mdbx_txn_lock(MDBX_env *env); -int mdbx_txn_trylock(MDBX_env *env); +int mdbx_txn_lock(MDBX_env *env, bool dontwait); void mdbx_txn_unlock(MDBX_env *env); int mdbx_rpid_set(MDBX_env *env);