lmdb: make transaction admission reentrant

(cherry picked from commit 6ee665b2c228dde4bb576f0ed7edb8d1a1702673)
This commit is contained in:
selsta
2026-10-04 18:50:23 -04:00
committed by such-gitea
parent c9d51bd08a
commit 444eb4dd79
2 changed files with 12 additions and 6 deletions
+11 -6
View File
@@ -356,6 +356,7 @@ typedef struct outtx {
} outtx; } outtx;
std::atomic<uint64_t> mdb_txn_safe::num_active_txns{0}; std::atomic<uint64_t> mdb_txn_safe::num_active_txns{0};
thread_local uint64_t mdb_txn_safe::num_active_txns_per_thread = 0;
std::atomic_flag mdb_txn_safe::creation_gate = ATOMIC_FLAG_INIT; std::atomic_flag mdb_txn_safe::creation_gate = ATOMIC_FLAG_INIT;
mdb_threadinfo::~mdb_threadinfo() mdb_threadinfo::~mdb_threadinfo()
@@ -373,9 +374,12 @@ mdb_txn_safe::mdb_txn_safe(const bool check) : m_txn(NULL), m_tinfo(NULL), m_che
{ {
if (check) if (check)
{ {
while (creation_gate.test_and_set()); const bool nested = num_active_txns_per_thread != 0;
num_active_txns++; if (!nested)
creation_gate.clear(); while (creation_gate.test_and_set());
increment_txns(1);
if (!nested)
creation_gate.clear();
} }
} }
@@ -406,12 +410,12 @@ mdb_txn_safe::~mdb_txn_safe()
} }
mdb_txn_abort(m_txn); mdb_txn_abort(m_txn);
} }
num_active_txns--; increment_txns(-1);
} }
void mdb_txn_safe::uncheck() void mdb_txn_safe::uncheck()
{ {
num_active_txns--; increment_txns(-1);
m_check = false; m_check = false;
} }
@@ -466,7 +470,8 @@ void mdb_txn_safe::allow_new_txns()
void mdb_txn_safe::increment_txns(int i) void mdb_txn_safe::increment_txns(int i)
{ {
num_active_txns += i; num_active_txns_per_thread += i;
num_active_txns += i;
} }
#define TXN_PREFIX(flags); \ #define TXN_PREFIX(flags); \
+1
View File
@@ -161,6 +161,7 @@ struct mdb_txn_safe
bool m_batch_txn = false; bool m_batch_txn = false;
bool m_check; bool m_check;
static std::atomic<uint64_t> num_active_txns; static std::atomic<uint64_t> num_active_txns;
static thread_local uint64_t num_active_txns_per_thread;
// could use a mutex here, but this should be sufficient. // could use a mutex here, but this should be sufficient.
static std::atomic_flag creation_gate; static std::atomic_flag creation_gate;