[FIX/WIP] Fix mutex issues around LMDB

Currently doing a test run on another project of mine to find out how
stable it is.

Next up(more long-termed): Faster JSON parsing than just plaintext!
This commit is contained in:
LDA 2024-08-10 10:33:50 +02:00
parent f6af2cd782
commit 20bb7a20ad

View file

@ -159,7 +159,6 @@ LMDBLock(Db *d, Array *k)
"%s: could not begin transaction: %s", "%s: could not begin transaction: %s",
__func__, mdb_strerror(code) __func__, mdb_strerror(code)
); );
pthread_mutex_unlock(&d->lock);
goto end; goto end;
} }
/* apparently you need to give it a dbi */ /* apparently you need to give it a dbi */
@ -169,9 +168,7 @@ LMDBLock(Db *d, Array *k)
"%s: could not get transaction dbi: %s", "%s: could not get transaction dbi: %s",
__func__, mdb_strerror(code) __func__, mdb_strerror(code)
); );
pthread_mutex_unlock(&d->lock);
goto end; goto end;
} }
empty_json.mv_size = 0; empty_json.mv_size = 0;
@ -180,10 +177,7 @@ LMDBLock(Db *d, Array *k)
code = mdb_get(transaction, dbi, &key, &empty_json); code = mdb_get(transaction, dbi, &key, &empty_json);
if (code == MDB_NOTFOUND) if (code == MDB_NOTFOUND)
{ {
Log(LOG_ERR, /* No use logging it, as that is just locking behaviour */
"%s: mdb_get failure: %s",
__func__, mdb_strerror(code)
);
mdb_txn_abort(transaction); mdb_txn_abort(transaction);
mdb_dbi_close(db->environ, dbi); mdb_dbi_close(db->environ, dbi);
goto end; goto end;
@ -215,6 +209,10 @@ LMDBLock(Db *d, Array *k)
ret->transaction = transaction; ret->transaction = transaction;
ret->dbi = dbi; ret->dbi = dbi;
end: end:
if (!ret)
{
pthread_mutex_unlock(&d->lock);
}
LMDBKillKey(key); LMDBKillKey(key);
return (DbRef *) ret; return (DbRef *) ret;
} }
@ -351,7 +349,10 @@ LMDBUnlock(Db *d, DbRef *r)
{ {
free(val.mv_data); free(val.mv_data);
} }
pthread_mutex_unlock(&d->lock); if (ret)
{
pthread_mutex_unlock(&d->lock);
}
return ret; return ret;
} }
static DbRef * static DbRef *
@ -379,7 +380,6 @@ LMDBCreate(Db *d, Array *k)
"%s: could not begin transaction: %s", "%s: could not begin transaction: %s",
__func__, mdb_strerror(code) __func__, mdb_strerror(code)
); );
pthread_mutex_unlock(&d->lock);
goto end; goto end;
} }
/* apparently you need to give it a dbi */ /* apparently you need to give it a dbi */
@ -389,7 +389,6 @@ LMDBCreate(Db *d, Array *k)
"%s: could not get transaction dbi: %s", "%s: could not get transaction dbi: %s",
__func__, mdb_strerror(code) __func__, mdb_strerror(code)
); );
pthread_mutex_unlock(&d->lock);
goto end; goto end;
} }
@ -435,6 +434,10 @@ LMDBCreate(Db *d, Array *k)
ret->transaction = transaction; ret->transaction = transaction;
ret->dbi = dbi; ret->dbi = dbi;
end: end:
if (!ret)
{
pthread_mutex_unlock(&d->lock);
}
LMDBKillKey(key); LMDBKillKey(key);
return (DbRef *) ret; return (DbRef *) ret;
} }