Optimize code holding reader mutex

vmware
Hallvard Furuseth 11 years ago
parent 3d67838a59
commit 3a1d73daff
  1. 29
      libraries/liblmdb/mdb.c

@ -2119,8 +2119,9 @@ static int
mdb_txn_renew0(MDB_txn *txn) mdb_txn_renew0(MDB_txn *txn)
{ {
MDB_env *env = txn->mt_env; MDB_env *env = txn->mt_env;
MDB_txninfo *ti = env->me_txns;
MDB_meta *meta; MDB_meta *meta;
unsigned int i; unsigned int i, nr;
uint16_t x; uint16_t x;
int rc, new_notls = 0; int rc, new_notls = 0;
@ -2129,7 +2130,7 @@ mdb_txn_renew0(MDB_txn *txn)
txn->mt_dbxs = env->me_dbxs; /* mostly static anyway */ txn->mt_dbxs = env->me_dbxs; /* mostly static anyway */
if (txn->mt_flags & MDB_TXN_RDONLY) { if (txn->mt_flags & MDB_TXN_RDONLY) {
if (!env->me_txns) { if (!ti) {
meta = env->me_metas[ mdb_env_pick_meta(env) ]; meta = env->me_metas[ mdb_env_pick_meta(env) ];
txn->mt_txnid = meta->mm_txnid; txn->mt_txnid = meta->mm_txnid;
txn->mt_u.reader = NULL; txn->mt_u.reader = NULL;
@ -2153,36 +2154,38 @@ mdb_txn_renew0(MDB_txn *txn)
} }
LOCK_MUTEX_R(env); LOCK_MUTEX_R(env);
for (i=0; i<env->me_txns->mti_numreaders; i++) nr = ti->mti_numreaders;
if (env->me_txns->mti_readers[i].mr_pid == 0) for (i=0; i<nr; i++)
if (ti->mti_readers[i].mr_pid == 0)
break; break;
if (i == env->me_maxreaders) { if (i == env->me_maxreaders) {
UNLOCK_MUTEX_R(env); UNLOCK_MUTEX_R(env);
return MDB_READERS_FULL; return MDB_READERS_FULL;
} }
env->me_txns->mti_readers[i].mr_pid = pid; ti->mti_readers[i].mr_pid = pid;
env->me_txns->mti_readers[i].mr_tid = tid; ti->mti_readers[i].mr_tid = tid;
if (i >= env->me_txns->mti_numreaders) if (i == nr)
env->me_txns->mti_numreaders = i+1; ti->mti_numreaders = ++nr;
/* Save numreaders for un-mutexed mdb_env_close() */ /* Save numreaders for un-mutexed mdb_env_close() */
env->me_numreaders = env->me_txns->mti_numreaders; env->me_numreaders = nr;
UNLOCK_MUTEX_R(env); UNLOCK_MUTEX_R(env);
r = &env->me_txns->mti_readers[i];
r = &ti->mti_readers[i];
new_notls = (env->me_flags & MDB_NOTLS); new_notls = (env->me_flags & MDB_NOTLS);
if (!new_notls && (rc=pthread_setspecific(env->me_txkey, r))) { if (!new_notls && (rc=pthread_setspecific(env->me_txkey, r))) {
r->mr_pid = 0; r->mr_pid = 0;
return rc; return rc;
} }
} }
txn->mt_txnid = r->mr_txnid = env->me_txns->mti_txnid; txn->mt_txnid = r->mr_txnid = ti->mti_txnid;
txn->mt_u.reader = r; txn->mt_u.reader = r;
meta = env->me_metas[txn->mt_txnid & 1]; meta = env->me_metas[txn->mt_txnid & 1];
} }
} else { } else {
if (env->me_txns) { if (ti) {
LOCK_MUTEX_W(env); LOCK_MUTEX_W(env);
txn->mt_txnid = env->me_txns->mti_txnid; txn->mt_txnid = ti->mti_txnid;
meta = env->me_metas[txn->mt_txnid & 1]; meta = env->me_metas[txn->mt_txnid & 1];
} else { } else {
meta = env->me_metas[ mdb_env_pick_meta(env) ]; meta = env->me_metas[ mdb_env_pick_meta(env) ];

Loading…
Cancel
Save