}
}
-void MDBEnv::incROTX()
-{
- auto threadId = std::this_thread::get_id();
- {
- std::shared_lock<std::shared_mutex> lock(d_countmutex);
- if (auto transactionsIt = d_ROtransactionsOut.find(threadId); transactionsIt != d_ROtransactionsOut.end()) {
- ++transactionsIt->second;
- return;
- }
- }
-
- {
- std::unique_lock<std::shared_mutex> lock(d_countmutex);
- auto [transactionsIt, inserted] = d_ROtransactionsOut.emplace(threadId, 1);
- if (!inserted) {
- ++transactionsIt->second;
- }
- }
-}
-
-void MDBEnv::decROTX()
-{
- auto threadId = std::this_thread::get_id();
- {
- std::shared_lock<std::shared_mutex> lock(d_countmutex);
- d_ROtransactionsOut.at(threadId)--;
- }
-}
-
void MDBEnv::incRWTX()
{
auto threadId = std::this_thread::get_id();
return 0;
}
-int MDBEnv::getROTX()
-{
- auto threadId = std::this_thread::get_id();
- {
- std::shared_lock<std::shared_mutex> lock(d_countmutex);
- if (auto transactionsIt = d_RWtransactionsOut.find(threadId); transactionsIt != d_RWtransactionsOut.end()) {
- return transactionsIt->second.load();
- }
- }
- return 0;
-}
-
std::shared_ptr<MDBEnv> getMDBEnv(const char* fname, int flags, int mode, uint64_t mapsizeMB)
{
throw std::runtime_error("Unable to start RO transaction: "+MDBError(retCode));
}
- env->incROTX();
-
return result;
}
closeROCursors();
// if d_txn is non-nullptr here, either the transaction object was invalidated earlier (e.g. by moving from it), or it is an RW transaction which has already cleaned up the d_txn pointer (with an abort).
if (d_txn) {
- d_parent->decROTX();
mdb_txn_abort(d_txn); // this appears to work better than abort for r/o database opening
d_txn = nullptr;
}
closeROCursors();
// if d_txn is non-nullptr here, either the transaction object was invalidated earlier (e.g. by moving from it), or it is an RW transaction which has already cleaned up the d_txn pointer (with an abort).
if (d_txn) {
- d_parent->decROTX();
mdb_txn_commit(d_txn); // this appears to work better than abort for r/o database opening
d_txn = nullptr;
}
int getRWTX();
void incRWTX();
void decRWTX();
- int getROTX();
- void incROTX();
- void decROTX();
private:
std::mutex d_openmut;
std::shared_mutex d_countmutex;
std::unordered_map<std::thread::id, std::atomic<int>> d_RWtransactionsOut;
- std::unordered_map<std::thread::id, std::atomic<int>> d_ROtransactionsOut;
};
std::shared_ptr<MDBEnv> getMDBEnv(const char* fname, int flags, int mode, uint64_t mapsizeMB);