Skip to content

Commit 735d9b5

Browse files
committed
Use CScheduler for wallet flushing, remove ThreadFlushWalletDB
1 parent 73296f5 commit 735d9b5

File tree

5 files changed

+30
-34
lines changed

5 files changed

+30
-34
lines changed

src/init.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1639,7 +1639,7 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
16391639

16401640
#ifdef ENABLE_WALLET
16411641
if (pwalletMain)
1642-
pwalletMain->postInitProcess(threadGroup);
1642+
pwalletMain->postInitProcess(scheduler);
16431643
#endif
16441644

16451645
return !fRequestShutdown;

src/wallet/wallet.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "primitives/transaction.h"
2121
#include "script/script.h"
2222
#include "script/sign.h"
23+
#include "scheduler.h"
2324
#include "timedata.h"
2425
#include "txmempool.h"
2526
#include "util.h"
@@ -3754,17 +3755,17 @@ bool CWallet::InitLoadWallet()
37543755
return true;
37553756
}
37563757

3757-
std::atomic<bool> CWallet::fFlushThreadRunning(false);
3758+
std::atomic<bool> CWallet::fFlushScheduled(false);
37583759

3759-
void CWallet::postInitProcess(boost::thread_group& threadGroup)
3760+
void CWallet::postInitProcess(CScheduler& scheduler)
37603761
{
37613762
// Add wallet transactions that aren't already in a block to mempool
37623763
// Do this here as mempool requires genesis block to be loaded
37633764
ReacceptWalletTransactions();
37643765

37653766
// Run a thread to flush wallet periodically
3766-
if (!CWallet::fFlushThreadRunning.exchange(true)) {
3767-
threadGroup.create_thread(ThreadFlushWalletDB);
3767+
if (!CWallet::fFlushScheduled.exchange(true)) {
3768+
scheduler.scheduleEvery(MaybeFlushWalletDB, 500);
37683769
}
37693770
}
37703771

src/wallet/wallet.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
#include <vector>
3030

3131
#include <boost/shared_ptr.hpp>
32-
#include <boost/thread.hpp>
3332

3433
extern CWallet* pwalletMain;
3534

@@ -79,6 +78,7 @@ class CCoinControl;
7978
class COutput;
8079
class CReserveKey;
8180
class CScript;
81+
class CScheduler;
8282
class CTxMemPool;
8383
class CWalletTx;
8484

@@ -593,7 +593,7 @@ class CAccountingEntry
593593
class CWallet : public CCryptoKeyStore, public CValidationInterface
594594
{
595595
private:
596-
static std::atomic<bool> fFlushThreadRunning;
596+
static std::atomic<bool> fFlushScheduled;
597597

598598
/**
599599
* Select a set of coins such that nValueRet >= nTargetValue and at least
@@ -1001,7 +1001,7 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
10011001
* Wallet post-init setup
10021002
* Gives the wallet a chance to register repetitive tasks and complete post-init tasks
10031003
*/
1004-
void postInitProcess(boost::thread_group& threadGroup);
1004+
void postInitProcess(CScheduler& scheduler);
10051005

10061006
/* Wallets parameter interaction */
10071007
static bool ParameterInteraction();

src/wallet/walletdb.cpp

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -777,38 +777,33 @@ DBErrors CWalletDB::ZapWalletTx(vector<CWalletTx>& vWtx)
777777
return DB_LOAD_OK;
778778
}
779779

780-
void ThreadFlushWalletDB()
780+
void MaybeFlushWalletDB()
781781
{
782-
// Make this thread recognisable as the wallet flushing thread
783-
RenameThread("bitcoin-wallet");
784-
785-
static bool fOneThread;
786-
if (fOneThread)
782+
static std::atomic<bool> fOneThread;
783+
if (fOneThread.exchange(true)) {
787784
return;
788-
fOneThread = true;
789-
if (!GetBoolArg("-flushwallet", DEFAULT_FLUSHWALLET))
785+
}
786+
if (!GetBoolArg("-flushwallet", DEFAULT_FLUSHWALLET)) {
790787
return;
788+
}
791789

792-
unsigned int nLastSeen = CWalletDB::GetUpdateCounter();
793-
unsigned int nLastFlushed = CWalletDB::GetUpdateCounter();
794-
int64_t nLastWalletUpdate = GetTime();
795-
while (true)
796-
{
797-
MilliSleep(500);
790+
static unsigned int nLastSeen = CWalletDB::GetUpdateCounter();
791+
static unsigned int nLastFlushed = CWalletDB::GetUpdateCounter();
792+
static int64_t nLastWalletUpdate = GetTime();
798793

799-
if (nLastSeen != CWalletDB::GetUpdateCounter())
800-
{
801-
nLastSeen = CWalletDB::GetUpdateCounter();
802-
nLastWalletUpdate = GetTime();
803-
}
794+
if (nLastSeen != CWalletDB::GetUpdateCounter())
795+
{
796+
nLastSeen = CWalletDB::GetUpdateCounter();
797+
nLastWalletUpdate = GetTime();
798+
}
804799

805-
if (nLastFlushed != CWalletDB::GetUpdateCounter() && GetTime() - nLastWalletUpdate >= 2)
806-
{
807-
const std::string& strFile = pwalletMain->strWalletFile;
808-
if (CDB::PeriodicFlush(strFile))
809-
nLastFlushed = CWalletDB::GetUpdateCounter();
810-
}
800+
if (nLastFlushed != CWalletDB::GetUpdateCounter() && GetTime() - nLastWalletUpdate >= 2)
801+
{
802+
const std::string& strFile = pwalletMain->strWalletFile;
803+
if (CDB::PeriodicFlush(strFile))
804+
nLastFlushed = CWalletDB::GetUpdateCounter();
811805
}
806+
fOneThread = false;
812807
}
813808

814809
//

src/wallet/walletdb.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,6 @@ class CWalletDB : public CDB
193193
void operator=(const CWalletDB&);
194194
};
195195

196-
void ThreadFlushWalletDB();
196+
void MaybeFlushWalletDB();
197197

198198
#endif // BITCOIN_WALLET_WALLETDB_H

0 commit comments

Comments
 (0)