Skip to content

Commit cda1429

Browse files
committed
Give CMainSignals a reference to the global scheduler
...so that it can run some signals in the background later
1 parent 3a19fed commit cda1429

5 files changed

+32
-0
lines changed

src/init.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ void Shutdown()
251251
}
252252
#endif
253253
UnregisterAllValidationInterfaces();
254+
GetMainSignals().UnregisterBackgroundSignalScheduler();
254255
#ifdef ENABLE_WALLET
255256
for (CWalletRef pwallet : vpwallets) {
256257
delete pwallet;
@@ -1203,6 +1204,8 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
12031204
CScheduler::Function serviceLoop = boost::bind(&CScheduler::serviceQueue, &scheduler);
12041205
threadGroup.create_thread(boost::bind(&TraceThread<CScheduler::Function>, "scheduler", serviceLoop));
12051206

1207+
GetMainSignals().RegisterBackgroundSignalScheduler(scheduler);
1208+
12061209
/* Start the RPC server already. It will be started in "warmup" mode
12071210
* and not really process calls already (but it will signify connections
12081211
* that the server is there and will be ready later). Warmup mode will

src/test/test_bitcoin.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ TestingSetup::TestingSetup(const std::string& chainName) : BasicTestingSetup(cha
6262
pathTemp = GetTempPath() / strprintf("test_bitcoin_%lu_%i", (unsigned long)GetTime(), (int)(InsecureRandRange(100000)));
6363
fs::create_directories(pathTemp);
6464
ForceSetArg("-datadir", pathTemp.string());
65+
66+
// Note that because we don't bother running a scheduler thread here,
67+
// callbacks via CValidationInterface are unreliable, but that's OK,
68+
// our unit tests aren't testing multiple parts of the code at once.
69+
GetMainSignals().RegisterBackgroundSignalScheduler(scheduler);
70+
6571
mempool.setSanityCheck(1.0);
6672
pblocktree = new CBlockTreeDB(1 << 20, true);
6773
pcoinsdbview = new CCoinsViewDB(1 << 23, true);
@@ -88,6 +94,7 @@ TestingSetup::~TestingSetup()
8894
UnregisterNodeSignals(GetNodeSignals());
8995
threadGroup.interrupt_all();
9096
threadGroup.join_all();
97+
GetMainSignals().UnregisterBackgroundSignalScheduler();
9198
UnloadBlockIndex();
9299
delete pcoinsTip;
93100
delete pcoinsdbview;

src/test/test_bitcoin.h

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "key.h"
1111
#include "pubkey.h"
1212
#include "random.h"
13+
#include "scheduler.h"
1314
#include "txdb.h"
1415
#include "txmempool.h"
1516

@@ -53,6 +54,7 @@ struct TestingSetup: public BasicTestingSetup {
5354
fs::path pathTemp;
5455
boost::thread_group threadGroup;
5556
CConnman* connman;
57+
CScheduler scheduler;
5658

5759
TestingSetup(const std::string& chainName = CBaseChainParams::MAIN);
5860
~TestingSetup();

src/validationinterface.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

66
#include "validationinterface.h"
7+
#include "init.h"
8+
#include "scheduler.h"
79

810
#include <boost/signals2/signal.hpp>
911

@@ -17,6 +19,8 @@ struct MainSignalsInstance {
1719
boost::signals2::signal<void (int64_t nBestBlockTime, CConnman* connman)> Broadcast;
1820
boost::signals2::signal<void (const CBlock&, const CValidationState&)> BlockChecked;
1921
boost::signals2::signal<void (const CBlockIndex *, const std::shared_ptr<const CBlock>&)> NewPoWValidBlock;
22+
23+
CScheduler *m_scheduler = NULL;
2024
};
2125

2226
static CMainSignals g_signals;
@@ -25,6 +29,15 @@ CMainSignals::CMainSignals() {
2529
m_internals.reset(new MainSignalsInstance());
2630
}
2731

32+
void CMainSignals::RegisterBackgroundSignalScheduler(CScheduler& scheduler) {
33+
assert(!m_internals->m_scheduler);
34+
m_internals->m_scheduler = &scheduler;
35+
}
36+
37+
void CMainSignals::UnregisterBackgroundSignalScheduler() {
38+
m_internals->m_scheduler = NULL;
39+
}
40+
2841
CMainSignals& GetMainSignals()
2942
{
3043
return g_signals;

src/validationinterface.h

+7
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class CReserveScript;
1919
class CValidationInterface;
2020
class CValidationState;
2121
class uint256;
22+
class CScheduler;
2223

2324
// These functions dispatch to one or all registered wallets
2425

@@ -72,9 +73,15 @@ class CMainSignals {
7273
friend void ::RegisterValidationInterface(CValidationInterface*);
7374
friend void ::UnregisterValidationInterface(CValidationInterface*);
7475
friend void ::UnregisterAllValidationInterfaces();
76+
7577
public:
7678
CMainSignals();
7779

80+
/** Register a CScheduler to give callbacks which should run in the background (may only be called once) */
81+
void RegisterBackgroundSignalScheduler(CScheduler& scheduler);
82+
/** Unregister a CScheduler to give callbacks which should run in the background - these callbacks will now be dropped! */
83+
void UnregisterBackgroundSignalScheduler();
84+
7885
void UpdatedBlockTip(const CBlockIndex *, const CBlockIndex *, bool fInitialDownload);
7986
void TransactionAddedToMempool(const CTransactionRef &);
8087
void BlockConnected(const std::shared_ptr<const CBlock> &, const CBlockIndex *pindex, const std::vector<CTransactionRef> &);

0 commit comments

Comments
 (0)