diff options
-rw-r--r-- | src/wallet/bdb.cpp | 25 | ||||
-rw-r--r-- | src/wallet/bdb.h | 6 |
2 files changed, 24 insertions, 7 deletions
diff --git a/src/wallet/bdb.cpp b/src/wallet/bdb.cpp index 0989dc21c3..8ca4991f04 100644 --- a/src/wallet/bdb.cpp +++ b/src/wallet/bdb.cpp @@ -334,7 +334,7 @@ BerkeleyDatabase::~BerkeleyDatabase() } } -BerkeleyBatch::BerkeleyBatch(BerkeleyDatabase& database, const char* pszMode, bool fFlushOnCloseIn) : pdb(nullptr), activeTxn(nullptr), m_cursor(nullptr) +BerkeleyBatch::BerkeleyBatch(BerkeleyDatabase& database, const char* pszMode, bool fFlushOnCloseIn) : pdb(nullptr), activeTxn(nullptr), m_cursor(nullptr), m_database(database) { fReadOnly = (!strchr(pszMode, '+') && !strchr(pszMode, 'w')); fFlushOnClose = fFlushOnCloseIn; @@ -408,7 +408,7 @@ BerkeleyBatch::BerkeleyBatch(BerkeleyDatabase& database, const char* pszMode, bo fReadOnly = fTmp; } } - ++env->mapFileUseCount[strFilename]; + database.AddRef(); strFile = strFilename; } } @@ -446,11 +446,7 @@ void BerkeleyBatch::Close() if (fFlushOnClose) Flush(); - { - LOCK(cs_db); - --env->mapFileUseCount[strFile]; - } - env->m_db_in_use.notify_all(); + m_database.RemoveRef(); } void BerkeleyEnvironment::CloseDb(const std::string& strFile) @@ -846,6 +842,21 @@ bool BerkeleyBatch::HasKey(CDataStream&& key) return ret == 0; } +void BerkeleyDatabase::AddRef() +{ + LOCK(cs_db); + ++env->mapFileUseCount[strFile]; +} + +void BerkeleyDatabase::RemoveRef() +{ + { + LOCK(cs_db); + --env->mapFileUseCount[strFile]; + } + env->m_db_in_use.notify_all(); +} + std::unique_ptr<BerkeleyBatch> BerkeleyDatabase::MakeBatch(const char* mode, bool flush_on_close) { return MakeUnique<BerkeleyBatch>(*this, mode, flush_on_close); diff --git a/src/wallet/bdb.h b/src/wallet/bdb.h index 52cd9d92df..9c4d6afa87 100644 --- a/src/wallet/bdb.h +++ b/src/wallet/bdb.h @@ -121,6 +121,11 @@ public: */ bool Rewrite(const char* pszSkip=nullptr); + /** Indicate the a new database user has began using the database. */ + void AddRef(); + /** Indicate that database user has stopped using the database and that it could be flushed or closed. */ + void RemoveRef(); + /** Back up the entire database to a file. */ bool Backup(const std::string& strDest) const; @@ -212,6 +217,7 @@ protected: bool fReadOnly; bool fFlushOnClose; BerkeleyEnvironment *env; + BerkeleyDatabase& m_database; public: explicit BerkeleyBatch(BerkeleyDatabase& database, const char* pszMode = "r+", bool fFlushOnCloseIn=true); |