Skip to content

Commit 0dfc25f

Browse files
committed
Merge #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
2 parents a8cbbdb + 004f999 commit 0dfc25f

File tree

8 files changed

+74
-58
lines changed

8 files changed

+74
-58
lines changed

src/init.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ void Interrupt()
165165
InterruptRPC();
166166
InterruptREST();
167167
InterruptTorControl();
168+
InterruptMapPort();
168169
if (g_connman)
169170
g_connman->Interrupt();
170171
}
@@ -191,7 +192,7 @@ void Shutdown()
191192
#ifdef ENABLE_WALLET
192193
FlushWallets();
193194
#endif
194-
MapPort(false);
195+
StopMapPort();
195196

196197
// Because these depend on each-other, we make sure that neither can be
197198
// using the other before destroying them.
@@ -545,7 +546,8 @@ static void BlockNotifyCallback(bool initialSync, const CBlockIndex *pBlockIndex
545546
std::string strCmd = gArgs.GetArg("-blocknotify", "");
546547
if (!strCmd.empty()) {
547548
boost::replace_all(strCmd, "%s", pBlockIndex->GetBlockHash().GetHex());
548-
boost::thread t(runCommand, strCmd); // thread runs free
549+
std::thread t(runCommand, strCmd);
550+
t.detach(); // thread runs free
549551
}
550552
}
551553

@@ -1674,12 +1676,14 @@ bool AppInitMain()
16741676
LogPrintf("nBestHeight = %d\n", chain_active_height);
16751677

16761678
if (gArgs.GetBoolArg("-listenonion", DEFAULT_LISTEN_ONION))
1677-
StartTorControl(threadGroup, scheduler);
1679+
StartTorControl();
16781680

1679-
Discover(threadGroup);
1681+
Discover();
16801682

16811683
// Map ports with UPnP
1682-
MapPort(gArgs.GetBoolArg("-upnp", DEFAULT_UPNP));
1684+
if (gArgs.GetBoolArg("-upnp", DEFAULT_UPNP)) {
1685+
StartMapPort();
1686+
}
16831687

16841688
CConnman::Options connOptions;
16851689
connOptions.nLocalServices = nLocalServices;

src/net.cpp

Lines changed: 47 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,6 +1459,8 @@ void CConnman::WakeMessageHandler()
14591459

14601460

14611461
#ifdef USE_UPNP
1462+
static CThreadInterrupt g_upnp_interrupt;
1463+
static std::thread g_upnp_thread;
14621464
void ThreadMapPort()
14631465
{
14641466
std::string port = strprintf("%u", GetListenPort());
@@ -1509,35 +1511,29 @@ void ThreadMapPort()
15091511

15101512
std::string strDesc = "Bitcoin " + FormatFullVersion();
15111513

1512-
try {
1513-
while (true) {
1514+
do {
15141515
#ifndef UPNPDISCOVER_SUCCESS
1515-
/* miniupnpc 1.5 */
1516-
r = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype,
1517-
port.c_str(), port.c_str(), lanaddr, strDesc.c_str(), "TCP", 0);
1516+
/* miniupnpc 1.5 */
1517+
r = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype,
1518+
port.c_str(), port.c_str(), lanaddr, strDesc.c_str(), "TCP", 0);
15181519
#else
1519-
/* miniupnpc 1.6 */
1520-
r = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype,
1521-
port.c_str(), port.c_str(), lanaddr, strDesc.c_str(), "TCP", 0, "0");
1520+
/* miniupnpc 1.6 */
1521+
r = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype,
1522+
port.c_str(), port.c_str(), lanaddr, strDesc.c_str(), "TCP", 0, "0");
15221523
#endif
15231524

1524-
if(r!=UPNPCOMMAND_SUCCESS)
1525-
LogPrintf("AddPortMapping(%s, %s, %s) failed with code %d (%s)\n",
1526-
port, port, lanaddr, r, strupnperror(r));
1527-
else
1528-
LogPrintf("UPnP Port Mapping successful.\n");
1529-
1530-
MilliSleep(20*60*1000); // Refresh every 20 minutes
1531-
}
1532-
}
1533-
catch (const boost::thread_interrupted&)
1534-
{
1535-
r = UPNP_DeletePortMapping(urls.controlURL, data.first.servicetype, port.c_str(), "TCP", 0);
1536-
LogPrintf("UPNP_DeletePortMapping() returned: %d\n", r);
1537-
freeUPNPDevlist(devlist); devlist = nullptr;
1538-
FreeUPNPUrls(&urls);
1539-
throw;
1525+
if(r!=UPNPCOMMAND_SUCCESS)
1526+
LogPrintf("AddPortMapping(%s, %s, %s) failed with code %d (%s)\n",
1527+
port, port, lanaddr, r, strupnperror(r));
1528+
else
1529+
LogPrintf("UPnP Port Mapping successful.\n");
15401530
}
1531+
while(g_upnp_interrupt.sleep_for(std::chrono::minutes(20)));
1532+
1533+
r = UPNP_DeletePortMapping(urls.controlURL, data.first.servicetype, port.c_str(), "TCP", 0);
1534+
LogPrintf("UPNP_DeletePortMapping() returned: %d\n", r);
1535+
freeUPNPDevlist(devlist); devlist = nullptr;
1536+
FreeUPNPUrls(&urls);
15411537
} else {
15421538
LogPrintf("No valid UPnP IGDs found\n");
15431539
freeUPNPDevlist(devlist); devlist = nullptr;
@@ -1546,27 +1542,39 @@ void ThreadMapPort()
15461542
}
15471543
}
15481544

1549-
void MapPort(bool fUseUPnP)
1545+
void StartMapPort()
15501546
{
1551-
static std::unique_ptr<boost::thread> upnp_thread;
1547+
if (!g_upnp_thread.joinable()) {
1548+
assert(!g_upnp_interrupt);
1549+
g_upnp_thread = std::thread((std::bind(&TraceThread<void (*)()>, "upnp", &ThreadMapPort)));
1550+
}
1551+
}
15521552

1553-
if (fUseUPnP)
1554-
{
1555-
if (upnp_thread) {
1556-
upnp_thread->interrupt();
1557-
upnp_thread->join();
1558-
}
1559-
upnp_thread.reset(new boost::thread(boost::bind(&TraceThread<void (*)()>, "upnp", &ThreadMapPort)));
1553+
void InterruptMapPort()
1554+
{
1555+
if(g_upnp_thread.joinable()) {
1556+
g_upnp_interrupt();
15601557
}
1561-
else if (upnp_thread) {
1562-
upnp_thread->interrupt();
1563-
upnp_thread->join();
1564-
upnp_thread.reset();
1558+
}
1559+
1560+
void StopMapPort()
1561+
{
1562+
if(g_upnp_thread.joinable()) {
1563+
g_upnp_thread.join();
1564+
g_upnp_interrupt.reset();
15651565
}
15661566
}
15671567

15681568
#else
1569-
void MapPort(bool)
1569+
void StartMapPort()
1570+
{
1571+
// Intentionally left blank.
1572+
}
1573+
void InterruptMapPort()
1574+
{
1575+
// Intentionally left blank.
1576+
}
1577+
void StopMapPort()
15701578
{
15711579
// Intentionally left blank.
15721580
}
@@ -2121,7 +2129,7 @@ bool CConnman::BindListenPort(const CService &addrBind, std::string& strError, b
21212129
return true;
21222130
}
21232131

2124-
void Discover(boost::thread_group& threadGroup)
2132+
void Discover()
21252133
{
21262134
if (!fDiscover)
21272135
return;

src/net.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,6 @@
3737
class CScheduler;
3838
class CNode;
3939

40-
namespace boost {
41-
class thread_group;
42-
} // namespace boost
43-
4440
/** Time between pings automatically sent out for latency probing and keepalive (in seconds). */
4541
static const int PING_INTERVAL = 2 * 60;
4642
/** Time after which to disconnect, after waiting for a ping response (or inactivity). */
@@ -441,8 +437,10 @@ class CConnman
441437
friend struct CConnmanTest;
442438
};
443439
extern std::unique_ptr<CConnman> g_connman;
444-
void Discover(boost::thread_group& threadGroup);
445-
void MapPort(bool fUseUPnP);
440+
void Discover();
441+
void StartMapPort();
442+
void InterruptMapPort();
443+
void StopMapPort();
446444
unsigned short GetListenPort();
447445
bool BindListenPort(const CService &bindAddr, std::string& strError, bool fWhitelisted = false);
448446

src/qt/optionsmodel.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,12 @@ bool OptionsModel::setData(const QModelIndex & index, const QVariant & value, in
315315
break;
316316
case MapPortUPnP: // core option - can be changed on-the-fly
317317
settings.setValue("fUseUPnP", value.toBool());
318-
MapPort(value.toBool());
318+
if (value.toBool()) {
319+
StartMapPort();
320+
} else {
321+
InterruptMapPort();
322+
StopMapPort();
323+
}
319324
break;
320325
case MinimizeOnClose:
321326
fMinimizeOnClose = value.toBool();

src/torcontrol.cpp

Lines changed: 3 additions & 3 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()

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
@@ -1182,7 +1182,8 @@ static void AlertNotify(const std::string& strMessage)
11821182
safeStatus = singleQuote+safeStatus+singleQuote;
11831183
boost::replace_all(strCmd, "%s", safeStatus);
11841184

1185-
boost::thread t(runCommand, strCmd); // thread runs free
1185+
std::thread t(runCommand, strCmd);
1186+
t.detach(); // thread runs free
11861187
}
11871188

11881189
static void CheckForkWarningConditions()

src/wallet/wallet.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
#include <future>
3535

3636
#include <boost/algorithm/string/replace.hpp>
37-
#include <boost/thread.hpp>
3837

3938
std::vector<CWalletRef> vpwallets;
4039
/** Transaction fee set by the user */
@@ -976,7 +975,8 @@ bool CWallet::AddToWallet(const CWalletTx& wtxIn, bool fFlushOnClose)
976975
if (!strCmd.empty())
977976
{
978977
boost::replace_all(strCmd, "%s", wtxIn.GetHash().GetHex());
979-
boost::thread t(runCommand, strCmd); // thread runs free
978+
std::thread t(runCommand, strCmd);
979+
t.detach(); // thread runs free
980980
}
981981

982982
return true;

0 commit comments

Comments
 (0)