Skip to content

Commit 8d249d4

Browse files
laanwjPastaPastaPasta
authored andcommitted
Merge bitcoin#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
1 parent 9c8c12e commit 8d249d4

File tree

8 files changed

+42
-46
lines changed

8 files changed

+42
-46
lines changed

src/init.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2087,7 +2087,7 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
20872087

20882088
#ifdef ENABLE_WALLET
20892089
if (pwalletMain)
2090-
pwalletMain->postInitProcess(threadGroup);
2090+
pwalletMain->postInitProcess(scheduler);
20912091
#endif
20922092

20932093
threadGroup.create_thread(boost::bind(&ThreadSendAlert, boost::ref(connman)));

src/net.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2409,7 +2409,7 @@ bool CConnman::Start(CScheduler& scheduler, std::string& strNodeError, Options c
24092409
threadMessageHandler = std::thread(&TraceThread<std::function<void()> >, "msghand", std::function<void()>(std::bind(&CConnman::ThreadMessageHandler, this)));
24102410

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

24142414
return true;
24152415
}

src/scheduler.cpp

+6-6
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

+4-5
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

+5-4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "primitives/transaction.h"
2222
#include "script/script.h"
2323
#include "script/sign.h"
24+
#include "scheduler.h"
2425
#include "timedata.h"
2526
#include "txmempool.h"
2627
#include "util.h"
@@ -5047,17 +5048,17 @@ bool CWallet::InitLoadWallet()
50475048
return true;
50485049
}
50495050

5050-
std::atomic<bool> CWallet::fFlushThreadRunning(false);
5051+
std::atomic<bool> CWallet::fFlushScheduled(false);
50515052

5052-
void CWallet::postInitProcess(boost::thread_group& threadGroup)
5053+
void CWallet::postInitProcess(CScheduler& scheduler)
50535054
{
50545055
// Add wallet transactions that aren't already in a block to mempool
50555056
// Do this here as mempool requires genesis block to be loaded
50565057
ReacceptWalletTransactions();
50575058

50585059
// Run a thread to flush wallet periodically
5059-
if (!CWallet::fFlushThreadRunning.exchange(true)) {
5060-
threadGroup.create_thread(ThreadFlushWalletDB);
5060+
if (!CWallet::fFlushScheduled.exchange(true)) {
5061+
scheduler.scheduleEvery(MaybeCompactWalletDB, 500);
50615062
}
50625063
}
50635064

src/wallet/wallet.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
#include <vector>
3434

3535
#include <boost/shared_ptr.hpp>
36-
#include <boost/thread.hpp>
3736

3837
extern CWallet* pwalletMain;
3938

@@ -84,6 +83,7 @@ class CCoinControl;
8483
class COutput;
8584
class CReserveKey;
8685
class CScript;
86+
class CScheduler;
8787
class CTxMemPool;
8888
class CWalletTx;
8989

@@ -654,7 +654,7 @@ class CAccountingEntry
654654
class CWallet : public CCryptoKeyStore, public CValidationInterface
655655
{
656656
private:
657-
static std::atomic<bool> fFlushThreadRunning;
657+
static std::atomic<bool> fFlushScheduled;
658658

659659
/**
660660
* Select a set of coins such that nValueRet >= nTargetValue and at least
@@ -1124,7 +1124,7 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
11241124
* Wallet post-init setup
11251125
* Gives the wallet a chance to register repetitive tasks and complete post-init tasks
11261126
*/
1127-
void postInitProcess(boost::thread_group& threadGroup);
1127+
void postInitProcess(CScheduler& scheduler);
11281128

11291129
/* Wallets parameter interaction */
11301130
static bool ParameterInteraction();

src/wallet/walletdb.cpp

+20-25
Original file line numberDiff line numberDiff line change
@@ -808,38 +808,33 @@ DBErrors CWalletDB::ZapWalletTx(std::vector<CWalletTx>& vWtx)
808808
return DB_LOAD_OK;
809809
}
810810

811-
void ThreadFlushWalletDB()
811+
void MaybeCompactWalletDB()
812812
{
813-
// Make this thread recognisable as the wallet flushing thread
814-
RenameThread("dash-wallet");
815-
816-
static bool fOneThread;
817-
if (fOneThread)
813+
static std::atomic<bool> fOneThread;
814+
if (fOneThread.exchange(true)) {
818815
return;
819-
fOneThread = true;
820-
if (!GetBoolArg("-flushwallet", DEFAULT_FLUSHWALLET))
816+
}
817+
if (!GetBoolArg("-flushwallet", DEFAULT_FLUSHWALLET)) {
821818
return;
819+
}
822820

823-
unsigned int nLastSeen = CWalletDB::GetUpdateCounter();
824-
unsigned int nLastFlushed = CWalletDB::GetUpdateCounter();
825-
int64_t nLastWalletUpdate = GetTime();
826-
while (true)
827-
{
828-
MilliSleep(500);
821+
static unsigned int nLastSeen = CWalletDB::GetUpdateCounter();
822+
static unsigned int nLastFlushed = CWalletDB::GetUpdateCounter();
823+
static int64_t nLastWalletUpdate = GetTime();
829824

830-
if (nLastSeen != CWalletDB::GetUpdateCounter())
831-
{
832-
nLastSeen = CWalletDB::GetUpdateCounter();
833-
nLastWalletUpdate = GetTime();
834-
}
825+
if (nLastSeen != CWalletDB::GetUpdateCounter())
826+
{
827+
nLastSeen = CWalletDB::GetUpdateCounter();
828+
nLastWalletUpdate = GetTime();
829+
}
835830

836-
if (nLastFlushed != CWalletDB::GetUpdateCounter() && GetTime() - nLastWalletUpdate >= 2)
837-
{
838-
const std::string& strFile = pwalletMain->strWalletFile;
839-
if (CDB::PeriodicFlush(strFile))
840-
nLastFlushed = CWalletDB::GetUpdateCounter();
841-
}
831+
if (nLastFlushed != CWalletDB::GetUpdateCounter() && GetTime() - nLastWalletUpdate >= 2)
832+
{
833+
const std::string& strFile = pwalletMain->strWalletFile;
834+
if (CDB::PeriodicFlush(strFile))
835+
nLastFlushed = CWalletDB::GetUpdateCounter();
842836
}
837+
fOneThread = false;
843838
}
844839

845840
//

src/wallet/walletdb.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ class CWalletDB : public CDB
156156
void operator=(const CWalletDB&);
157157
};
158158

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

161162
#endif // BITCOIN_WALLET_WALLETDB_H

0 commit comments

Comments
 (0)