Skip to content

Commit 00f36ea

Browse files
TheBlueMattfurszy
authored andcommitted
Use CScheduler for wallet flushing, remove ThreadFlushWalletDB
1 parent 8ac7333 commit 00f36ea

File tree

5 files changed

+27
-31
lines changed

5 files changed

+27
-31
lines changed

src/init.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1908,7 +1908,7 @@ bool AppInitMain()
19081908

19091909
#ifdef ENABLE_WALLET
19101910
if (pwalletMain) {
1911-
pwalletMain->postInitProcess(threadGroup);
1911+
pwalletMain->postInitProcess(scheduler);
19121912

19131913
// StakeMiner thread disabled by default on regtest
19141914
if (gArgs.GetBoolArg("-staking", !Params().IsRegTestNet() && DEFAULT_STAKING)) {

src/wallet/wallet.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "policy/policy.h"
1616
#include "sapling/key_io_sapling.h"
1717
#include "script/sign.h"
18+
#include "scheduler.h"
1819
#include "spork.h"
1920
#include "util.h"
2021
#include "utilmoneystr.h"
@@ -4128,16 +4129,16 @@ bool CWallet::InitLoadWallet()
41284129
return true;
41294130
}
41304131

4131-
std::atomic<bool> CWallet::fFlushThreadRunning(false);
4132+
std::atomic<bool> CWallet::fFlushScheduled(false);
41324133

4133-
void CWallet::postInitProcess(boost::thread_group& threadGroup)
4134+
void CWallet::postInitProcess(CScheduler& scheduler)
41344135
{
41354136
// Add wallet transactions that aren't already in a block to mapTransactions
41364137
ReacceptWalletTransactions(/*fFirstLoad*/true);
41374138

41384139
// Run a thread to flush wallet periodically
4139-
if (!CWallet::fFlushThreadRunning.exchange(true)) {
4140-
threadGroup.create_thread(ThreadFlushWalletDB);
4140+
if (!CWallet::fFlushScheduled.exchange(true)) {
4141+
scheduler.scheduleEvery(MaybeFlushWalletDB, 500);
41414142
}
41424143
}
41434144

src/wallet/wallet.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@
4444
#include <utility>
4545
#include <vector>
4646

47-
#include <boost/thread/thread.hpp>
48-
4947
extern CWallet* pwalletMain;
5048

5149
/**
@@ -94,6 +92,7 @@ class COutput;
9492
class CStakeableOutput;
9593
class CReserveKey;
9694
class CScript;
95+
class CScheduler;
9796
class CWalletTx;
9897
class ScriptPubKeyMan;
9998
class SaplingScriptPubKeyMan;
@@ -265,7 +264,7 @@ typedef std::map<SaplingOutPoint, SaplingNoteData> mapSaplingNoteData_t;
265264
class CWallet : public CCryptoKeyStore, public CValidationInterface
266265
{
267266
private:
268-
static std::atomic<bool> fFlushThreadRunning;
267+
static std::atomic<bool> fFlushScheduled;
269268

270269
//! keeps track of whether Unlock has run a thorough check before
271270
bool fDecryptionThoroughlyChecked{false};
@@ -786,7 +785,7 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
786785
* Wallet post-init setup
787786
* Gives the wallet a chance to register repetitive tasks and complete post-init tasks
788787
*/
789-
void postInitProcess(boost::thread_group& threadGroup);
788+
void postInitProcess(CScheduler& scheduler);
790789

791790
/**
792791
* Address book entry changed.

src/wallet/walletdb.cpp

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -891,35 +891,31 @@ DBErrors CWalletDB::ZapWalletTx(CWallet* pwallet, std::vector<CWalletTx>& vWtx)
891891
return DB_LOAD_OK;
892892
}
893893

894-
void ThreadFlushWalletDB()
894+
void MaybeFlushWalletDB()
895895
{
896-
// Make this thread recognisable as the wallet flushing thread
897-
util::ThreadRename("pivx-wallet");
898-
899-
static bool fOneThread;
900-
if (fOneThread)
896+
static std::atomic<bool> fOneThread;
897+
if (fOneThread.exchange(true)) {
901898
return;
902-
fOneThread = true;
903-
if (!gArgs.GetBoolArg("-flushwallet", DEFAULT_FLUSHWALLET))
899+
}
900+
if (!gArgs.GetBoolArg("-flushwallet", DEFAULT_FLUSHWALLET)) {
904901
return;
902+
}
905903

906-
unsigned int nLastSeen = CWalletDB::GetUpdateCounter();
907-
unsigned int nLastFlushed = CWalletDB::GetUpdateCounter();
908-
int64_t nLastWalletUpdate = GetTime();
909-
while (true) {
910-
MilliSleep(500);
904+
static unsigned int nLastSeen = CWalletDB::GetUpdateCounter();
905+
static unsigned int nLastFlushed = CWalletDB::GetUpdateCounter();
906+
static int64_t nLastWalletUpdate = GetTime();
911907

912-
if (nLastSeen != CWalletDB::GetUpdateCounter()) {
913-
nLastSeen = CWalletDB::GetUpdateCounter();
914-
nLastWalletUpdate = GetTime();
915-
}
908+
if (nLastSeen != CWalletDB::GetUpdateCounter()) {
909+
nLastSeen = CWalletDB::GetUpdateCounter();
910+
nLastWalletUpdate = GetTime();
911+
}
916912

917-
if (nLastFlushed != CWalletDB::GetUpdateCounter() && GetTime() - nLastWalletUpdate >= 2) {
918-
if (CDB::PeriodicFlush(pwalletMain->GetDBHandle())) {
919-
nLastFlushed = CWalletDB::GetUpdateCounter();
920-
}
913+
if (nLastFlushed != CWalletDB::GetUpdateCounter() && GetTime() - nLastWalletUpdate >= 2) {
914+
if (CDB::PeriodicFlush(pwalletMain->GetDBHandle())) {
915+
nLastFlushed = CWalletDB::GetUpdateCounter();
921916
}
922917
}
918+
fOneThread = false;
923919
}
924920

925921
void NotifyBacked(const CWallet& wallet, bool fSuccess, std::string strMessage)

src/wallet/walletdb.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,6 @@ void NotifyBacked(const CWallet& wallet, bool fSuccess, std::string strMessage);
230230
bool BackupWallet(const CWallet& wallet, const fs::path& strDest);
231231
bool AttemptBackupWallet(const CWallet& wallet, const fs::path& pathSrc, const fs::path& pathDest);
232232

233-
void ThreadFlushWalletDB();
233+
void MaybeFlushWalletDB();
234234

235235
#endif // BITCOIN_WALLETDB_H

0 commit comments

Comments
 (0)