Skip to content

Commit 30e4448

Browse files
committed
refactor: Make TraceThread a non-template free function
Also it is moved into its own module.
1 parent a1f0b8b commit 30e4448

File tree

11 files changed

+67
-36
lines changed

11 files changed

+67
-36
lines changed

src/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ BITCOIN_CORE_H = \
250250
util/spanparsing.h \
251251
util/string.h \
252252
util/system.h \
253+
util/thread.h \
253254
util/threadnames.h \
254255
util/time.h \
255256
util/tokenpipe.h \
@@ -577,6 +578,7 @@ libbitcoin_util_a_SOURCES = \
577578
util/rbf.cpp \
578579
util/readwritefile.cpp \
579580
util/settings.cpp \
581+
util/thread.cpp \
580582
util/threadnames.cpp \
581583
util/spanparsing.cpp \
582584
util/strencodings.cpp \

src/index/base.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include <node/ui_interface.h>
99
#include <shutdown.h>
1010
#include <tinyformat.h>
11-
#include <util/system.h>
11+
#include <util/thread.h>
1212
#include <util/translation.h>
1313
#include <validation.h> // For g_chainman
1414
#include <warnings.h>
@@ -349,7 +349,7 @@ void BaseIndex::Start()
349349
return;
350350
}
351351

352-
m_thread_sync = std::thread(&TraceThread<std::function<void()>>, GetName(),
352+
m_thread_sync = std::thread(&util::TraceThread, GetName(),
353353
std::bind(&BaseIndex::ThreadSync, this));
354354
}
355355

src/init.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
#include <util/moneystr.h>
5959
#include <util/string.h>
6060
#include <util/system.h>
61+
#include <util/thread.h>
6162
#include <util/threadnames.h>
6263
#include <util/translation.h>
6364
#include <validation.h>
@@ -1266,7 +1267,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
12661267
node.scheduler = std::make_unique<CScheduler>();
12671268

12681269
// Start the lightweight task scheduler thread
1269-
node.scheduler->m_service_thread = std::thread([&] { TraceThread("scheduler", [&] { node.scheduler->serviceQueue(); }); });
1270+
node.scheduler->m_service_thread = std::thread([&] { util::TraceThread("scheduler", [&] { node.scheduler->serviceQueue(); }); });
12701271

12711272
// Gather some entropy once per minute.
12721273
node.scheduler->scheduleEvery([]{
@@ -1791,7 +1792,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
17911792
vImportFiles.push_back(strFile);
17921793
}
17931794

1794-
chainman.m_load_block = std::thread(&TraceThread<std::function<void()>>, "loadblk", [=, &chainman, &args] {
1795+
chainman.m_load_block = std::thread(&util::TraceThread, "loadblk", [=, &chainman, &args] {
17951796
ThreadImport(chainman, vImportFiles, args);
17961797
});
17971798

src/mapport.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <netbase.h>
1616
#include <threadinterrupt.h>
1717
#include <util/system.h>
18+
#include <util/thread.h>
1819

1920
#ifdef USE_NATPMP
2021
#include <compat.h>
@@ -255,7 +256,7 @@ void StartThreadMapPort()
255256
{
256257
if (!g_mapport_thread.joinable()) {
257258
assert(!g_mapport_interrupt);
258-
g_mapport_thread = std::thread(std::bind(&TraceThread<void (*)()>, "mapport", &ThreadMapPort));
259+
g_mapport_thread = std::thread(std::bind(&util::TraceThread, "mapport", &ThreadMapPort));
259260
}
260261
}
261262

src/net.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <scheduler.h>
2424
#include <util/sock.h>
2525
#include <util/strencodings.h>
26+
#include <util/thread.h>
2627
#include <util/translation.h>
2728

2829
#ifdef WIN32
@@ -2527,15 +2528,15 @@ bool CConnman::Start(CScheduler& scheduler, const Options& connOptions)
25272528
}
25282529

25292530
// Send and receive from sockets, accept connections
2530-
threadSocketHandler = std::thread(&TraceThread<std::function<void()> >, "net", std::function<void()>(std::bind(&CConnman::ThreadSocketHandler, this)));
2531+
threadSocketHandler = std::thread(&util::TraceThread, "net", std::bind(&CConnman::ThreadSocketHandler, this));
25312532

25322533
if (!gArgs.GetBoolArg("-dnsseed", DEFAULT_DNSSEED))
25332534
LogPrintf("DNS seeding disabled\n");
25342535
else
2535-
threadDNSAddressSeed = std::thread(&TraceThread<std::function<void()> >, "dnsseed", std::function<void()>(std::bind(&CConnman::ThreadDNSAddressSeed, this)));
2536+
threadDNSAddressSeed = std::thread(&util::TraceThread, "dnsseed", std::bind(&CConnman::ThreadDNSAddressSeed, this));
25362537

25372538
// Initiate manual connections
2538-
threadOpenAddedConnections = std::thread(&TraceThread<std::function<void()> >, "addcon", std::function<void()>(std::bind(&CConnman::ThreadOpenAddedConnections, this)));
2539+
threadOpenAddedConnections = std::thread(&util::TraceThread, "addcon", std::bind(&CConnman::ThreadOpenAddedConnections, this));
25392540

25402541
if (connOptions.m_use_addrman_outgoing && !connOptions.m_specified_outgoing.empty()) {
25412542
if (clientInterface) {
@@ -2546,14 +2547,14 @@ bool CConnman::Start(CScheduler& scheduler, const Options& connOptions)
25462547
return false;
25472548
}
25482549
if (connOptions.m_use_addrman_outgoing || !connOptions.m_specified_outgoing.empty())
2549-
threadOpenConnections = std::thread(&TraceThread<std::function<void()> >, "opencon", std::function<void()>(std::bind(&CConnman::ThreadOpenConnections, this, connOptions.m_specified_outgoing)));
2550+
threadOpenConnections = std::thread(&util::TraceThread, "opencon", std::bind(&CConnman::ThreadOpenConnections, this, connOptions.m_specified_outgoing));
25502551

25512552
// Process messages
2552-
threadMessageHandler = std::thread(&TraceThread<std::function<void()> >, "msghand", std::function<void()>(std::bind(&CConnman::ThreadMessageHandler, this)));
2553+
threadMessageHandler = std::thread(&util::TraceThread, "msghand", std::bind(&CConnman::ThreadMessageHandler, this));
25532554

25542555
if (connOptions.m_i2p_accept_incoming && m_i2p_sam_session.get() != nullptr) {
25552556
threadI2PAcceptIncoming =
2556-
std::thread(&TraceThread<std::function<void()>>, "i2paccept",
2557+
std::thread(&util::TraceThread, "i2paccept",
25572558
std::function<void()>(std::bind(&CConnman::ThreadI2PAcceptIncoming, this)));
25582559
}
25592560

src/test/util/setup_common.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
#include <txdb.h>
2929
#include <util/strencodings.h>
3030
#include <util/string.h>
31+
#include <util/thread.h>
32+
#include <util/threadnames.h>
3133
#include <util/time.h>
3234
#include <util/translation.h>
3335
#include <util/url.h>
@@ -132,7 +134,7 @@ ChainTestingSetup::ChainTestingSetup(const std::string& chainName, const std::ve
132134
// We have to run a scheduler thread to prevent ActivateBestChain
133135
// from blocking due to queue overrun.
134136
m_node.scheduler = std::make_unique<CScheduler>();
135-
m_node.scheduler->m_service_thread = std::thread([&] { TraceThread("scheduler", [&] { m_node.scheduler->serviceQueue(); }); });
137+
m_node.scheduler->m_service_thread = std::thread([&] { util::TraceThread("scheduler", [&] { m_node.scheduler->serviceQueue(); }); });
136138
GetMainSignals().RegisterBackgroundSignalScheduler(*m_node.scheduler);
137139

138140
pblocktree.reset(new CBlockTreeDB(1 << 20, true));

src/torcontrol.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <util/readwritefile.h>
1616
#include <util/strencodings.h>
1717
#include <util/system.h>
18+
#include <util/thread.h>
1819
#include <util/time.h>
1920

2021
#include <deque>
@@ -596,7 +597,7 @@ void StartTorControl(CService onion_service_target)
596597
return;
597598
}
598599

599-
torControlThread = std::thread(&TraceThread<std::function<void()>>, "torcontrol", [onion_service_target] {
600+
torControlThread = std::thread(&util::TraceThread, "torcontrol", [onion_service_target] {
600601
TorControlThread(onion_service_target);
601602
});
602603
}

src/txmempool.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <validation.h>
1919
#include <validationinterface.h>
2020

21+
#include <cmath>
2122
#include <optional>
2223

2324
CTxMemPoolEntry::CTxMemPoolEntry(const CTransactionRef& _tx, const CAmount& _nFee,

src/util/system.h

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include <sync.h>
2323
#include <tinyformat.h>
2424
#include <util/settings.h>
25-
#include <util/threadnames.h>
2625
#include <util/time.h>
2726

2827
#include <any>
@@ -458,28 +457,6 @@ std::string HelpMessageOpt(const std::string& option, const std::string& message
458457
*/
459458
int GetNumCores();
460459

461-
/**
462-
* .. and a wrapper that just calls func once
463-
*/
464-
template <typename Callable> void TraceThread(const char* name, Callable func)
465-
{
466-
util::ThreadRename(name);
467-
try
468-
{
469-
LogPrintf("%s thread start\n", name);
470-
func();
471-
LogPrintf("%s thread exit\n", name);
472-
}
473-
catch (const std::exception& e) {
474-
PrintExceptionContinue(&e, name);
475-
throw;
476-
}
477-
catch (...) {
478-
PrintExceptionContinue(nullptr, name);
479-
throw;
480-
}
481-
}
482-
483460
std::string CopyrightHolders(const std::string& strPrefix);
484461

485462
/**

src/util/thread.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright (c) 2021 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <util/thread.h>
6+
7+
#include <logging.h>
8+
#include <util/system.h>
9+
#include <util/threadnames.h>
10+
11+
#include <exception>
12+
13+
void util::TraceThread(const char* thread_name, std::function<void()> thread_func)
14+
{
15+
util::ThreadRename(thread_name);
16+
try {
17+
LogPrintf("%s thread start\n", thread_name);
18+
thread_func();
19+
LogPrintf("%s thread exit\n", thread_name);
20+
} catch (const std::exception& e) {
21+
PrintExceptionContinue(&e, thread_name);
22+
throw;
23+
} catch (...) {
24+
PrintExceptionContinue(nullptr, thread_name);
25+
throw;
26+
}
27+
}

0 commit comments

Comments
 (0)