Skip to content

Commit 779f2f9

Browse files
committed
Merge #9605: Use CScheduler for wallet flushing, remove ThreadFlushWalletDB
0235be1 Rename FlushWalletDB -> CompactWalletDB, add function description (Matt Corallo) 735d9b5 Use CScheduler for wallet flushing, remove ThreadFlushWalletDB (Matt Corallo) 73296f5 CScheduler boost->std::function, use millisecs for times, not secs (Matt Corallo) Tree-SHA512: c04f97beab65706c444c126be229d02887df9b0972d8fb15ca1f779ef0e628cf7ecef2bf533c650d9b44645b63e01de22f17266a05907e778938d64cc6e19de6
2 parents 309bf16 + 0235be1 commit 779f2f9

File tree

8 files changed

+42
-46
lines changed

8 files changed

+42
-46
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/net.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2288,7 +2288,7 @@ bool CConnman::Start(CScheduler& scheduler, std::string& strNodeError, Options c
22882288
threadMessageHandler = std::thread(&TraceThread<std::function<void()> >, "msghand", std::function<void()>(std::bind(&CConnman::ThreadMessageHandler, this)));
22892289

22902290
// Dump network addresses
2291-
scheduler.scheduleEvery(boost::bind(&CConnman::DumpData, this), DUMP_ADDRESSES_INTERVAL);
2291+
scheduler.scheduleEvery(std::bind(&CConnman::DumpData, this), DUMP_ADDRESSES_INTERVAL * 1000);
22922292

22932293
return true;
22942294
}

src/scheduler.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,20 +104,20 @@ void CScheduler::schedule(CScheduler::Function f, boost::chrono::system_clock::t
104104
newTaskScheduled.notify_one();
105105
}
106106

107-
void CScheduler::scheduleFromNow(CScheduler::Function f, int64_t deltaSeconds)
107+
void CScheduler::scheduleFromNow(CScheduler::Function f, int64_t deltaMilliSeconds)
108108
{
109-
schedule(f, boost::chrono::system_clock::now() + boost::chrono::seconds(deltaSeconds));
109+
schedule(f, boost::chrono::system_clock::now() + boost::chrono::milliseconds(deltaMilliSeconds));
110110
}
111111

112-
static void Repeat(CScheduler* s, CScheduler::Function f, int64_t deltaSeconds)
112+
static void Repeat(CScheduler* s, CScheduler::Function f, int64_t deltaMilliSeconds)
113113
{
114114
f();
115-
s->scheduleFromNow(boost::bind(&Repeat, s, f, deltaSeconds), deltaSeconds);
115+
s->scheduleFromNow(boost::bind(&Repeat, s, f, deltaMilliSeconds), deltaMilliSeconds);
116116
}
117117

118-
void CScheduler::scheduleEvery(CScheduler::Function f, int64_t deltaSeconds)
118+
void CScheduler::scheduleEvery(CScheduler::Function f, int64_t deltaMilliSeconds)
119119
{
120-
scheduleFromNow(boost::bind(&Repeat, this, f, deltaSeconds), deltaSeconds);
120+
scheduleFromNow(boost::bind(&Repeat, this, f, deltaMilliSeconds), deltaMilliSeconds);
121121
}
122122

123123
size_t CScheduler::getQueueInfo(boost::chrono::system_clock::time_point &first,

src/scheduler.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
// boost::thread / boost::function / boost::chrono should be ported to
1111
// std::thread / std::function / std::chrono when we support C++11.
1212
//
13-
#include <boost/function.hpp>
1413
#include <boost/chrono/chrono.hpp>
1514
#include <boost/thread.hpp>
1615
#include <map>
@@ -23,7 +22,7 @@
2322
//
2423
// CScheduler* s = new CScheduler();
2524
// s->scheduleFromNow(doSomething, 11); // Assuming a: void doSomething() { }
26-
// s->scheduleFromNow(boost::bind(Class::func, this, argument), 3);
25+
// s->scheduleFromNow(std::bind(Class::func, this, argument), 3);
2726
// boost::thread* t = new boost::thread(boost::bind(CScheduler::serviceQueue, s));
2827
//
2928
// ... then at program shutdown, clean up the thread running serviceQueue:
@@ -39,20 +38,20 @@ class CScheduler
3938
CScheduler();
4039
~CScheduler();
4140

42-
typedef boost::function<void(void)> Function;
41+
typedef std::function<void(void)> Function;
4342

4443
// Call func at/after time t
4544
void schedule(Function f, boost::chrono::system_clock::time_point t);
4645

4746
// Convenience method: call f once deltaSeconds from now
48-
void scheduleFromNow(Function f, int64_t deltaSeconds);
47+
void scheduleFromNow(Function f, int64_t deltaMilliSeconds);
4948

5049
// Another convenience method: call f approximately
5150
// every deltaSeconds forever, starting deltaSeconds from now.
5251
// To be more precise: every time f is finished, it
5352
// is rescheduled to run deltaSeconds later. If you
5453
// need more accurate scheduling, don't use this method.
55-
void scheduleEvery(Function f, int64_t deltaSeconds);
54+
void scheduleEvery(Function f, int64_t deltaMilliSeconds);
5655

5756
// To keep things as simple as possible, there is no unschedule.
5857

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(MaybeCompactWalletDB, 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 MaybeCompactWalletDB()
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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ class CWalletDB : public CDB
193193
void operator=(const CWalletDB&);
194194
};
195195

196-
void ThreadFlushWalletDB();
196+
//! Compacts BDB state so that wallet.dat is self-contained (if there are changes)
197+
void MaybeCompactWalletDB();
197198

198199
#endif // BITCOIN_WALLET_WALLETDB_H

0 commit comments

Comments
 (0)