Skip to content

Commit f26866b

Browse files
committed
boost: drop boost threads for upnp
1 parent 0277173 commit f26866b

File tree

4 files changed

+60
-42
lines changed

4 files changed

+60
-42
lines changed

src/init.cpp

Lines changed: 5 additions & 2 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.
@@ -1669,7 +1670,9 @@ bool AppInitMain()
16691670
Discover(threadGroup);
16701671

16711672
// Map ports with UPnP
1672-
MapPort(gArgs.GetBoolArg("-upnp", DEFAULT_UPNP));
1673+
if (gArgs.GetBoolArg("-upnp", DEFAULT_UPNP)) {
1674+
StartMapPort();
1675+
}
16731676

16741677
CConnman::Options connOptions;
16751678
connOptions.nLocalServices = nLocalServices;

src/net.cpp

Lines changed: 46 additions & 38 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
}

src/net.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,9 @@ class CConnman
442442
};
443443
extern std::unique_ptr<CConnman> g_connman;
444444
void Discover(boost::thread_group& threadGroup);
445-
void MapPort(bool fUseUPnP);
445+
void StartMapPort();
446+
void InterruptMapPort();
447+
void StopMapPort();
446448
unsigned short GetListenPort();
447449
bool BindListenPort(const CService &bindAddr, std::string& strError, bool fWhitelisted = false);
448450

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();

0 commit comments

Comments
 (0)