Skip to content

Commit 79e6779

Browse files
committed
Merge #13235: Break circular dependency: init -> * -> init by extracting shutdown.h
1fabd59 Break circular dependency: init -> * -> init by extracting shutdown.h (Ben Woosley) e62fdfe Drop unused init.h includes (Ben Woosley) Pull request description: 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 `CancelShutdown` for setting it to `false`. Tree-SHA512: df42f75dfbba163576710e9a67cf1228531fd99d70a2f187bfba0bcc476d6749cf88180a97e66a81bb5b6c3c7f0917de7402d26039ba7b644cb7509b02f7e267
2 parents 1756cb4 + 1fabd59 commit 79e6779

26 files changed

+70
-52
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
@@ -1450,7 +1440,7 @@ bool AppInitMain()
14501440
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));
14511441

14521442
bool fLoaded = false;
1453-
while (!fLoaded && !fRequestShutdown) {
1443+
while (!fLoaded && !ShutdownRequested()) {
14541444
bool fReset = fReindex;
14551445
std::string strLoadError;
14561446

@@ -1477,7 +1467,7 @@ bool AppInitMain()
14771467
CleanupBlockRevFiles();
14781468
}
14791469

1480-
if (fRequestShutdown) break;
1470+
if (ShutdownRequested()) break;
14811471

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

1587-
if (!fLoaded && !fRequestShutdown) {
1577+
if (!fLoaded && !ShutdownRequested()) {
15881578
// first suggest a reindex
15891579
if (!fReset) {
15901580
bool fRet = uiInterface.ThreadSafeQuestion(
@@ -1593,7 +1583,7 @@ bool AppInitMain()
15931583
"", CClientUIInterface::MSG_ERROR | CClientUIInterface::BTN_ABORT);
15941584
if (fRet) {
15951585
fReindex = true;
1596-
fRequestShutdown = false;
1586+
AbortShutdown();
15971587
} else {
15981588
LogPrintf("Aborted block database rebuild. Exiting.\n");
15991589
return false;
@@ -1607,8 +1597,7 @@ bool AppInitMain()
16071597
// As LoadBlockIndex can take several minutes, it's possible the user
16081598
// requested to kill the GUI during the last operation. If so, exit.
16091599
// As the program has not fully started yet, Shutdown() is possibly overkill.
1610-
if (fRequestShutdown)
1611-
{
1600+
if (ShutdownRequested()) {
16121601
LogPrintf("Shutdown requested. Exiting.\n");
16131602
return false;
16141603
}

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/net_processing.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#include <chainparams.h>
1212
#include <consensus/validation.h>
1313
#include <hash.h>
14-
#include <init.h>
1514
#include <validation.h>
1615
#include <merkleblock.h>
1716
#include <netmessagemaker.h>

src/qt/bitcoin.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
#include <qt/walletmodel.h>
2727
#endif
2828

29-
#include <init.h>
3029
#include <interfaces/handler.h>
3130
#include <interfaces/node.h>
3231
#include <rpc/server.h>

0 commit comments

Comments
 (0)