Skip to content

Commit 1fabd59

Browse files
committed
Break circular dependency: init -> * -> init by extracting shutdown.h
Most includers just wanted to react to pending shutdown. This isolates access to `fRequestShutdown` and limits access to the shutdown api functions, including the new `AbortShutdown` for setting it to `false`. Note I originally called `AbortShutdown` `CancelShutdown` but that name was already taken by winuser.h https://travis-ci.org/bitcoin/bitcoin/jobs/386913329 This change also triggered a build error in bench. Fixing it required moving LIBBITCOIN_SERVER after LIBBITCOIN_WALLET in bench_bench_bitcoin_LDADD To make server definitions in src/net.cpp available to wallet methods in src/wallet/wallet.cpp. Specifically, solving: libbitcoin_wallet.a(libbitcoin_wallet_a-wallet.o): In function `CWalletTx::RelayWalletTransaction(CConnman*)': wallet.cpp:(.text+0x3f0e): undefined reference to `CConnman::NodeFullyConnected(CNode const*)' collect2: error: ld returned 1 exit status https://travis-ci.org/bitcoin/bitcoin/jobs/392133581 Need for remaining init.h includes confirmed via a thorough search with a more specific regex: \bInterrupt\(\)|\bShutdown\(\)|\bInitLogging\(\)|\bInitParameterInteraction\(\)|\bAppInitBasicSetup\(\)|\bAppInitParameterInteraction\(\)|\bAppInitSanityChecks\(\)|\bAppInitLockDataDirectory\(\)|\bAppInitMain\(\)|\bSetupServerArgs\(\)|\bLicenseInfo\(\)|g_wallet_init_interface|init.h
1 parent e62fdfe commit 1fabd59

18 files changed

+70
-44
lines changed

src/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ BITCOIN_CORE_H = \
157157
script/sigcache.h \
158158
script/sign.h \
159159
script/standard.h \
160+
shutdown.h \
160161
streams.h \
161162
support/allocators/secure.h \
162163
support/allocators/zeroafterfree.h \
@@ -237,6 +238,7 @@ libbitcoin_server_a_SOURCES = \
237238
rpc/server.cpp \
238239
rpc/util.cpp \
239240
script/sigcache.cpp \
241+
shutdown.cpp \
240242
timedata.cpp \
241243
torcontrol.cpp \
242244
txdb.cpp \

src/Makefile.bench.include

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ nodist_bench_bench_bitcoin_SOURCES = $(GENERATED_BENCH_FILES)
3434
bench_bench_bitcoin_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) $(EVENT_CLFAGS) $(EVENT_PTHREADS_CFLAGS) -I$(builddir)/bench/
3535
bench_bench_bitcoin_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
3636
bench_bench_bitcoin_LDADD = \
37-
$(LIBBITCOIN_SERVER) \
3837
$(LIBBITCOIN_WALLET) \
38+
$(LIBBITCOIN_SERVER) \
3939
$(LIBBITCOIN_COMMON) \
4040
$(LIBBITCOIN_UTIL) \
4141
$(LIBBITCOIN_CONSENSUS) \

src/bitcoind.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Copyright (c) 2009-2010 Satoshi Nakamoto
2-
// Copyright (c) 2009-2017 The Bitcoin Core developers
2+
// Copyright (c) 2009-2018 The Bitcoin Core developers
33
// Distributed under the MIT software license, see the accompanying
44
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

@@ -14,6 +14,7 @@
1414
#include <rpc/server.h>
1515
#include <init.h>
1616
#include <noui.h>
17+
#include <shutdown.h>
1718
#include <util.h>
1819
#include <httpserver.h>
1920
#include <httprpc.h>

src/index/base.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
#include <chainparams.h>
66
#include <index/base.h>
7-
#include <init.h>
7+
#include <shutdown.h>
88
#include <tinyformat.h>
99
#include <ui_interface.h>
1010
#include <util.h>

src/index/txindex.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

55
#include <index/txindex.h>
6-
#include <init.h>
6+
#include <shutdown.h>
77
#include <ui_interface.h>
88
#include <util.h>
99
#include <validation.h>

src/init.cpp

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Copyright (c) 2009-2010 Satoshi Nakamoto
2-
// Copyright (c) 2009-2017 The Bitcoin Core developers
2+
// Copyright (c) 2009-2018 The Bitcoin Core developers
33
// Distributed under the MIT software license, see the accompanying
44
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

@@ -35,6 +35,7 @@
3535
#include <script/standard.h>
3636
#include <script/sigcache.h>
3737
#include <scheduler.h>
38+
#include <shutdown.h>
3839
#include <timedata.h>
3940
#include <txdb.h>
4041
#include <txmempool.h>
@@ -126,7 +127,7 @@ static const char* FEE_ESTIMATES_FILENAME="fee_estimates.dat";
126127
// created by AppInit() or the Qt main() function.
127128
//
128129
// A clean exit happens when StartShutdown() or the SIGTERM
129-
// signal handler sets fRequestShutdown, which makes main thread's
130+
// signal handler sets ShutdownRequested(), which makes main thread's
130131
// WaitForShutdown() interrupts the thread group.
131132
// And then, WaitForShutdown() makes all other on-going threads
132133
// in the thread group join the main thread.
@@ -135,21 +136,10 @@ static const char* FEE_ESTIMATES_FILENAME="fee_estimates.dat";
135136
// threads have exited.
136137
//
137138
// Shutdown for Qt is very similar, only it uses a QTimer to detect
138-
// fRequestShutdown getting set, and then does the normal Qt
139+
// ShutdownRequested() getting set, and then does the normal Qt
139140
// shutdown thing.
140141
//
141142

142-
std::atomic<bool> fRequestShutdown(false);
143-
144-
void StartShutdown()
145-
{
146-
fRequestShutdown = true;
147-
}
148-
bool ShutdownRequested()
149-
{
150-
return fRequestShutdown;
151-
}
152-
153143
/**
154144
* This is a minimally invasive approach to shutdown on LevelDB read errors from the
155145
* chainstate, while keeping user interface out of the common library, which is shared
@@ -310,7 +300,7 @@ void Shutdown()
310300
#ifndef WIN32
311301
static void HandleSIGTERM(int)
312302
{
313-
fRequestShutdown = true;
303+
StartShutdown();
314304
}
315305

316306
static void HandleSIGHUP(int)
@@ -320,7 +310,7 @@ static void HandleSIGHUP(int)
320310
#else
321311
static BOOL WINAPI consoleCtrlHandler(DWORD dwCtrlType)
322312
{
323-
fRequestShutdown = true;
313+
StartShutdown();
324314
Sleep(INFINITE);
325315
return true;
326316
}
@@ -713,7 +703,7 @@ static void ThreadImport(std::vector<fs::path> vImportFiles)
713703
if (gArgs.GetArg("-persistmempool", DEFAULT_PERSIST_MEMPOOL)) {
714704
LoadMempool();
715705
}
716-
g_is_mempool_loaded = !fRequestShutdown;
706+
g_is_mempool_loaded = !ShutdownRequested();
717707
}
718708

719709
/** Sanity checks
@@ -1449,7 +1439,7 @@ bool AppInitMain()
14491439
LogPrintf("* Using %.1fMiB for in-memory UTXO set (plus up to %.1fMiB of unused mempool space)\n", nCoinCacheUsage * (1.0 / 1024 / 1024), nMempoolSizeMax * (1.0 / 1024 / 1024));
14501440

14511441
bool fLoaded = false;
1452-
while (!fLoaded && !fRequestShutdown) {
1442+
while (!fLoaded && !ShutdownRequested()) {
14531443
bool fReset = fReindex;
14541444
std::string strLoadError;
14551445

@@ -1476,7 +1466,7 @@ bool AppInitMain()
14761466
CleanupBlockRevFiles();
14771467
}
14781468

1479-
if (fRequestShutdown) break;
1469+
if (ShutdownRequested()) break;
14801470

14811471
// LoadBlockIndex will load fHavePruned if we've ever removed a
14821472
// block file from disk.
@@ -1583,7 +1573,7 @@ bool AppInitMain()
15831573
fLoaded = true;
15841574
} while(false);
15851575

1586-
if (!fLoaded && !fRequestShutdown) {
1576+
if (!fLoaded && !ShutdownRequested()) {
15871577
// first suggest a reindex
15881578
if (!fReset) {
15891579
bool fRet = uiInterface.ThreadSafeQuestion(
@@ -1592,7 +1582,7 @@ bool AppInitMain()
15921582
"", CClientUIInterface::MSG_ERROR | CClientUIInterface::BTN_ABORT);
15931583
if (fRet) {
15941584
fReindex = true;
1595-
fRequestShutdown = false;
1585+
AbortShutdown();
15961586
} else {
15971587
LogPrintf("Aborted block database rebuild. Exiting.\n");
15981588
return false;
@@ -1606,8 +1596,7 @@ bool AppInitMain()
16061596
// As LoadBlockIndex can take several minutes, it's possible the user
16071597
// requested to kill the GUI during the last operation. If so, exit.
16081598
// As the program has not fully started yet, Shutdown() is possibly overkill.
1609-
if (fRequestShutdown)
1610-
{
1599+
if (ShutdownRequested()) {
16111600
LogPrintf("Shutdown requested. Exiting.\n");
16121601
return false;
16131602
}

src/init.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Copyright (c) 2009-2010 Satoshi Nakamoto
2-
// Copyright (c) 2009-2017 The Bitcoin Core developers
2+
// Copyright (c) 2009-2018 The Bitcoin Core developers
33
// Distributed under the MIT software license, see the accompanying
44
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

@@ -21,8 +21,6 @@ namespace boost
2121
class thread_group;
2222
} // namespace boost
2323

24-
void StartShutdown();
25-
bool ShutdownRequested();
2624
/** Interrupt threads */
2725
void Interrupt();
2826
void Shutdown();

src/interfaces/node.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <primitives/block.h>
2222
#include <rpc/server.h>
2323
#include <scheduler.h>
24+
#include <shutdown.h>
2425
#include <sync.h>
2526
#include <txmempool.h>
2627
#include <ui_interface.h>

src/qt/bitcoingui.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ private Q_SLOTS:
257257
/** Simply calls showNormalIfMinimized(true) for use in SLOT() macro */
258258
void toggleHidden();
259259

260-
/** called by a timer to check if fRequestShutdown has been set **/
260+
/** called by a timer to check if ShutdownRequested() has been set **/
261261
void detectShutdown();
262262

263263
/** Show progress dialog e.g. for verifychain */

src/qt/winshutdownmonitor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include <qt/winshutdownmonitor.h>
66

77
#if defined(Q_OS_WIN)
8-
#include <init.h>
8+
#include <shutdown.h>
99
#include <util.h>
1010

1111
#include <windows.h>

0 commit comments

Comments
 (0)