Skip to content

Commit 360a4ad

Browse files
PastaPastaPastalaanwjUdjinM6
committed
Backport 12381 (dashpay#3528)
* Merge bitcoin#12381: Remove more boost threads 004f999 boost: drop boost threads for [alert|block|wallet]notify (Cory Fields) 0827267 boost: drop boost threads from torcontrol (Cory Fields) ba91724 boost: remove useless threadGroup parameter from Discover (Cory Fields) f26866b boost: drop boost threads for upnp (Cory Fields) Pull request description: This doesn't completely get rid of boost::thread, but this batch should be easy to review, and leaves us with only threadGroup (scheduler + scriptcheck) remaining. Note to reviewers: The upnp diff changes a bunch of whitespace, it's much more clear with 'git diff -w' Tree-SHA512: 5a356798d0785f93ed143d1f0afafe890bc82f0d470bc969473da2d2aa78bcb9b096f7ba11b92564d546fb447d4bd0d347e7842994ea0170aafd53fda7e0a66e * fix using std::thread Signed-off-by: pasta <pasta@dashboost.org> * Switch to std::thread in NotifyTransactionLock * Move StopTorControl call from Shutdown to PrepareShutdown Co-authored-by: Wladimir J. van der Laan <laanwj@gmail.com> Co-authored-by: UdjinM6 <UdjinM6@users.noreply.github.com>
1 parent da0e0f5 commit 360a4ad

File tree

8 files changed

+80
-68
lines changed

8 files changed

+80
-68
lines changed

src/init.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ void Interrupt()
220220
InterruptREST();
221221
InterruptTorControl();
222222
llmq::InterruptLLMQSystem();
223+
InterruptMapPort();
223224
if (g_connman)
224225
g_connman->Interrupt();
225226
}
@@ -251,14 +252,16 @@ void PrepareShutdown()
251252
bool fRPCInWarmup = RPCIsInWarmup(&statusmessage);
252253

253254
g_wallet_init_interface->Flush();
254-
MapPort(false);
255+
StopMapPort();
255256

256257
// Because these depend on each-other, we make sure that neither can be
257258
// using the other before destroying them.
258259
if (peerLogic) UnregisterValidationInterface(peerLogic.get());
259260
if (g_connman) g_connman->Stop();
260261
// if (g_txindex) g_txindex->Stop(); //TODO watch out when backporting bitcoin#13033 (don't accidently put the reset here, as we've already backported bitcoin#13894)
261262

263+
StopTorControl();
264+
262265
// After everything has been shut down, but before things get flushed, stop the
263266
// CScheduler/checkqueue threadGroup
264267
threadGroup.interrupt_all();
@@ -378,8 +381,7 @@ void Shutdown()
378381
if(!fRequestRestart) {
379382
PrepareShutdown();
380383
}
381-
// Shutdown part 2: Stop TOR thread and delete wallet instance
382-
StopTorControl();
384+
// Shutdown part 2: delete wallet instance
383385
g_wallet_init_interface->Close();
384386
globalVerifyHandle.reset();
385387
ECC_Stop();
@@ -695,7 +697,8 @@ static void BlockNotifyCallback(bool initialSync, const CBlockIndex *pBlockIndex
695697
std::string strCmd = gArgs.GetArg("-blocknotify", "");
696698
if (!strCmd.empty()) {
697699
boost::replace_all(strCmd, "%s", pBlockIndex->GetBlockHash().GetHex());
698-
boost::thread t(runCommand, strCmd); // thread runs free
700+
std::thread t(runCommand, strCmd);
701+
t.detach(); // thread runs free
699702
}
700703
}
701704

@@ -2221,12 +2224,14 @@ bool AppInitMain()
22212224
}
22222225
LogPrintf("chainActive.Height() = %d\n", chain_active_height);
22232226
if (gArgs.GetBoolArg("-listenonion", DEFAULT_LISTEN_ONION))
2224-
StartTorControl(threadGroup, scheduler);
2227+
StartTorControl();
22252228

2226-
Discover(threadGroup);
2229+
Discover();
22272230

22282231
// Map ports with UPnP
2229-
MapPort(gArgs.GetBoolArg("-upnp", DEFAULT_UPNP));
2232+
if (gArgs.GetBoolArg("-upnp", DEFAULT_UPNP)) {
2233+
StartMapPort();
2234+
}
22302235

22312236
CConnman::Options connOptions;
22322237
connOptions.nLocalServices = nLocalServices;

src/net.cpp

Lines changed: 47 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1948,6 +1948,8 @@ void CConnman::WakeSelect()
19481948

19491949

19501950
#ifdef USE_UPNP
1951+
static CThreadInterrupt g_upnp_interrupt;
1952+
static std::thread g_upnp_thread;
19511953
void ThreadMapPort()
19521954
{
19531955
std::string port = strprintf("%u", GetListenPort());
@@ -1998,35 +2000,29 @@ void ThreadMapPort()
19982000

19992001
std::string strDesc = "Dash Core " + FormatFullVersion();
20002002

2001-
try {
2002-
while (true) {
2003+
do {
20032004
#ifndef UPNPDISCOVER_SUCCESS
2004-
/* miniupnpc 1.5 */
2005-
r = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype,
2006-
port.c_str(), port.c_str(), lanaddr, strDesc.c_str(), "TCP", 0);
2005+
/* miniupnpc 1.5 */
2006+
r = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype,
2007+
port.c_str(), port.c_str(), lanaddr, strDesc.c_str(), "TCP", 0);
20072008
#else
2008-
/* miniupnpc 1.6 */
2009-
r = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype,
2010-
port.c_str(), port.c_str(), lanaddr, strDesc.c_str(), "TCP", 0, "0");
2009+
/* miniupnpc 1.6 */
2010+
r = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype,
2011+
port.c_str(), port.c_str(), lanaddr, strDesc.c_str(), "TCP", 0, "0");
20112012
#endif
20122013

2013-
if(r!=UPNPCOMMAND_SUCCESS)
2014-
LogPrintf("AddPortMapping(%s, %s, %s) failed with code %d (%s)\n",
2015-
port, port, lanaddr, r, strupnperror(r));
2016-
else
2017-
LogPrintf("UPnP Port Mapping successful.\n");
2018-
2019-
MilliSleep(20*60*1000); // Refresh every 20 minutes
2020-
}
2021-
}
2022-
catch (const boost::thread_interrupted&)
2023-
{
2024-
r = UPNP_DeletePortMapping(urls.controlURL, data.first.servicetype, port.c_str(), "TCP", 0);
2025-
LogPrintf("UPNP_DeletePortMapping() returned: %d\n", r);
2026-
freeUPNPDevlist(devlist); devlist = nullptr;
2027-
FreeUPNPUrls(&urls);
2028-
throw;
2014+
if(r!=UPNPCOMMAND_SUCCESS)
2015+
LogPrintf("AddPortMapping(%s, %s, %s) failed with code %d (%s)\n",
2016+
port, port, lanaddr, r, strupnperror(r));
2017+
else
2018+
LogPrintf("UPnP Port Mapping successful.\n");
20292019
}
2020+
while(g_upnp_interrupt.sleep_for(std::chrono::minutes(20)));
2021+
2022+
r = UPNP_DeletePortMapping(urls.controlURL, data.first.servicetype, port.c_str(), "TCP", 0);
2023+
LogPrintf("UPNP_DeletePortMapping() returned: %d\n", r);
2024+
freeUPNPDevlist(devlist); devlist = nullptr;
2025+
FreeUPNPUrls(&urls);
20302026
} else {
20312027
LogPrintf("No valid UPnP IGDs found\n");
20322028
freeUPNPDevlist(devlist); devlist = nullptr;
@@ -2035,27 +2031,39 @@ void ThreadMapPort()
20352031
}
20362032
}
20372033

2038-
void MapPort(bool fUseUPnP)
2034+
void StartMapPort()
20392035
{
2040-
static std::unique_ptr<boost::thread> upnp_thread;
2036+
if (!g_upnp_thread.joinable()) {
2037+
assert(!g_upnp_interrupt);
2038+
g_upnp_thread = std::thread((std::bind(&TraceThread<void (*)()>, "upnp", &ThreadMapPort)));
2039+
}
2040+
}
20412041

2042-
if (fUseUPnP)
2043-
{
2044-
if (upnp_thread) {
2045-
upnp_thread->interrupt();
2046-
upnp_thread->join();
2047-
}
2048-
upnp_thread.reset(new boost::thread(boost::bind(&TraceThread<void (*)()>, "upnp", &ThreadMapPort)));
2042+
void InterruptMapPort()
2043+
{
2044+
if(g_upnp_thread.joinable()) {
2045+
g_upnp_interrupt();
20492046
}
2050-
else if (upnp_thread) {
2051-
upnp_thread->interrupt();
2052-
upnp_thread->join();
2053-
upnp_thread.reset();
2047+
}
2048+
2049+
void StopMapPort()
2050+
{
2051+
if(g_upnp_thread.joinable()) {
2052+
g_upnp_thread.join();
2053+
g_upnp_interrupt.reset();
20542054
}
20552055
}
20562056

20572057
#else
2058-
void MapPort(bool)
2058+
void StartMapPort()
2059+
{
2060+
// Intentionally left blank.
2061+
}
2062+
void InterruptMapPort()
2063+
{
2064+
// Intentionally left blank.
2065+
}
2066+
void StopMapPort()
20592067
{
20602068
// Intentionally left blank.
20612069
}
@@ -2827,7 +2835,7 @@ bool CConnman::BindListenPort(const CService &addrBind, std::string& strError, b
28272835
return true;
28282836
}
28292837

2830-
void Discover(boost::thread_group& threadGroup)
2838+
void Discover()
28312839
{
28322840
if (!fDiscover)
28332841
return;

src/net.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,6 @@
4646
class CScheduler;
4747
class CNode;
4848

49-
namespace boost {
50-
class thread_group;
51-
} // namespace boost
52-
5349
/** Time between pings automatically sent out for latency probing and keepalive (in seconds). */
5450
static const int PING_INTERVAL = 2 * 60;
5551
/** Time after which to disconnect, after waiting for a ping response (or inactivity). */
@@ -651,8 +647,10 @@ friend class CNode;
651647
friend struct CConnmanTest;
652648
};
653649
extern std::unique_ptr<CConnman> g_connman;
654-
void Discover(boost::thread_group& threadGroup);
655-
void MapPort(bool fUseUPnP);
650+
void Discover();
651+
void StartMapPort();
652+
void InterruptMapPort();
653+
void StopMapPort();
656654
unsigned short GetListenPort();
657655
bool BindListenPort(const CService &bindAddr, std::string& strError, bool fWhitelisted = false);
658656

src/qt/optionsmodel.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,12 @@ bool OptionsModel::setData(const QModelIndex & index, const QVariant & value, in
379379
break;
380380
case MapPortUPnP: // core option - can be changed on-the-fly
381381
settings.setValue("fUseUPnP", value.toBool());
382-
MapPort(value.toBool());
382+
if (value.toBool()) {
383+
StartMapPort();
384+
} else {
385+
InterruptMapPort();
386+
StopMapPort();
387+
}
383388
break;
384389
case MinimizeOnClose:
385390
fMinimizeOnClose = value.toBool();

src/torcontrol.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,7 @@ void TorController::reconnect_cb(evutil_socket_t fd, short what, void *arg)
731731

732732
/****** Thread ********/
733733
static struct event_base *gBase;
734-
static boost::thread torControlThread;
734+
static std::thread torControlThread;
735735

736736
static void TorControlThread()
737737
{
@@ -740,7 +740,7 @@ static void TorControlThread()
740740
event_base_dispatch(gBase);
741741
}
742742

743-
void StartTorControl(boost::thread_group& threadGroup, CScheduler& scheduler)
743+
void StartTorControl()
744744
{
745745
assert(!gBase);
746746
#ifdef WIN32
@@ -754,7 +754,7 @@ void StartTorControl(boost::thread_group& threadGroup, CScheduler& scheduler)
754754
return;
755755
}
756756

757-
torControlThread = boost::thread(boost::bind(&TraceThread<void (*)()>, "torcontrol", &TorControlThread));
757+
torControlThread = std::thread(std::bind(&TraceThread<void (*)()>, "torcontrol", &TorControlThread));
758758
}
759759

760760
void InterruptTorControl()
@@ -767,14 +767,8 @@ void InterruptTorControl()
767767

768768
void StopTorControl()
769769
{
770-
// timed_join() avoids the wallet not closing during a repair-restart. For a 'normal' wallet exit
771-
// it behaves for our cases exactly like the normal join()
772770
if (gBase) {
773-
#if BOOST_VERSION >= 105000
774-
torControlThread.try_join_for(boost::chrono::seconds(1));
775-
#else
776-
torControlThread.timed_join(boost::posix_time::seconds(1));
777-
#endif
771+
torControlThread.join();
778772
event_base_free(gBase);
779773
gBase = nullptr;
780774
}

src/torcontrol.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
extern const std::string DEFAULT_TOR_CONTROL;
1414
static const bool DEFAULT_LISTEN_ONION = true;
1515

16-
void StartTorControl(boost::thread_group& threadGroup, CScheduler& scheduler);
16+
void StartTorControl();
1717
void InterruptTorControl();
1818
void StopTorControl();
1919

src/validation.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1214,7 +1214,8 @@ static void AlertNotify(const std::string& strMessage)
12141214
safeStatus = singleQuote+safeStatus+singleQuote;
12151215
boost::replace_all(strCmd, "%s", safeStatus);
12161216

1217-
boost::thread t(runCommand, strCmd); // thread runs free
1217+
std::thread t(runCommand, strCmd);
1218+
t.detach(); // thread runs free
12181219
}
12191220

12201221
static void CheckForkWarningConditions()

src/wallet/wallet.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
#include <future>
4646

4747
#include <boost/algorithm/string/replace.hpp>
48-
#include <boost/thread.hpp>
4948

5049
static CCriticalSection cs_wallets;
5150
static std::vector<CWallet*> vpwallets GUARDED_BY(cs_wallets);
@@ -1169,7 +1168,8 @@ bool CWallet::AddToWallet(const CWalletTx& wtxIn, bool fFlushOnClose)
11691168
if (!strCmd.empty())
11701169
{
11711170
boost::replace_all(strCmd, "%s", wtxIn.GetHash().GetHex());
1172-
boost::thread t(runCommand, strCmd); // thread runs free
1171+
std::thread t(runCommand, strCmd);
1172+
t.detach(); // thread runs free
11731173
}
11741174

11751175
fAnonymizableTallyCached = false;
@@ -5439,7 +5439,8 @@ void CWallet::NotifyTransactionLock(const CTransaction &tx, const llmq::CInstant
54395439
std::string strCmd = gArgs.GetArg("-instantsendnotify", "");
54405440
if (!strCmd.empty()) {
54415441
boost::replace_all(strCmd, "%s", txHash.GetHex());
5442-
boost::thread t(runCommand, strCmd); // thread runs free
5442+
std::thread t(runCommand, strCmd);
5443+
t.detach(); // thread runs free
54435444
}
54445445
}
54455446
}

0 commit comments

Comments
 (0)