From b1dc72b6b54e81ff8db8a7666df47db317cfcc4f Mon Sep 17 00:00:00 2001 From: Howard Chu Date: Fri, 19 Jul 2013 09:55:10 -0700 Subject: [PATCH 1/2] Get pid lock outside of rmutex Avoid holding rmutex for longer than necessary. --- libraries/liblmdb/mdb.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/libraries/liblmdb/mdb.c b/libraries/liblmdb/mdb.c index a4f6238e00..1e26d16ed4 100644 --- a/libraries/liblmdb/mdb.c +++ b/libraries/liblmdb/mdb.c @@ -2078,14 +2078,6 @@ mdb_txn_renew0(MDB_txn *txn) pid_t pid = env->me_pid; pthread_t tid = pthread_self(); - LOCK_MUTEX_R(env); - for (i=0; ime_txns->mti_numreaders; i++) - if (env->me_txns->mti_readers[i].mr_pid == 0) - break; - if (i == env->me_maxreaders) { - UNLOCK_MUTEX_R(env); - return MDB_READERS_FULL; - } if (!(env->me_flags & MDB_LIVE_READER)) { rc = mdb_reader_pid(env, Pidset, pid); if (rc) { @@ -2094,6 +2086,15 @@ mdb_txn_renew0(MDB_txn *txn) } env->me_flags |= MDB_LIVE_READER; } + + LOCK_MUTEX_R(env); + for (i=0; ime_txns->mti_numreaders; i++) + if (env->me_txns->mti_readers[i].mr_pid == 0) + break; + if (i == env->me_maxreaders) { + UNLOCK_MUTEX_R(env); + return MDB_READERS_FULL; + } env->me_txns->mti_readers[i].mr_pid = pid; env->me_txns->mti_readers[i].mr_tid = tid; if (i >= env->me_txns->mti_numreaders) From de72c08660fca1597be0bb0b6536c122ae324645 Mon Sep 17 00:00:00 2001 From: Howard Chu Date: Fri, 19 Jul 2013 09:57:33 -0700 Subject: [PATCH 2/2] Tweak reader_pid check Check again after acquiring rmutex. Avoids potential issue with a duplicate pid coming in between initial check and rmutex. --- libraries/liblmdb/mdb.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/libraries/liblmdb/mdb.c b/libraries/liblmdb/mdb.c index 1e26d16ed4..4d686007ba 100644 --- a/libraries/liblmdb/mdb.c +++ b/libraries/liblmdb/mdb.c @@ -8092,11 +8092,13 @@ int mdb_reader_check(MDB_env *env, int *dead) if (mdb_pid_insert(pids, pid) == 0) { if (mdb_reader_pid(env, Pidcheck, pid)) { LOCK_MUTEX_R(env); - for (j=i; j