Skip to content

Commit 2e30e32

Browse files
author
MarcoFalke
committed
Merge bitcoin#19064: refactor: Cleanup thread ctor calls
792be53 refactor: Replace std::bind with lambdas (Hennadii Stepanov) a508f71 refactor: Use appropriate thread constructor (Hennadii Stepanov) 30e4448 refactor: Make TraceThread a non-template free function (Hennadii Stepanov) Pull request description: This PR does not change behavior. Its goal is to improve readability and maintainability of the code. ACKs for top commit: jnewbery: utACK 792be53 jonatack: tACK 792be53 MarcoFalke: cr ACK 792be53 Tree-SHA512: a03142f04f370f6bc02bd3ddfa870819b51740fcd028772241d68c84087f95a2d78207cbd5edb3f7c636fcf2d76192d9c59873f8f0af451d3b05c0cf9cf234df
2 parents 03e16cb + 792be53 commit 2e30e32

File tree

11 files changed

+71
-39
lines changed

11 files changed

+71
-39
lines changed

src/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ BITCOIN_CORE_H = \
257257
util/spanparsing.h \
258258
util/string.h \
259259
util/system.h \
260+
util/thread.h \
260261
util/threadnames.h \
261262
util/time.h \
262263
util/tokenpipe.h \
@@ -590,6 +591,7 @@ libbitcoin_util_a_SOURCES = \
590591
util/rbf.cpp \
591592
util/readwritefile.cpp \
592593
util/settings.cpp \
594+
util/thread.cpp \
593595
util/threadnames.cpp \
594596
util/spanparsing.cpp \
595597
util/strencodings.cpp \

src/index/base.cpp

Lines changed: 2 additions & 3 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,8 +349,7 @@ void BaseIndex::Start()
349349
return;
350350
}
351351

352-
m_thread_sync = std::thread(&TraceThread<std::function<void()>>, GetName(),
353-
std::bind(&BaseIndex::ThreadSync, this));
352+
m_thread_sync = std::thread(&util::TraceThread, GetName(), [this] { ThreadSync(); });
354353
}
355354

356355
void BaseIndex::Stop()

src/init.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
#include <util/moneystr.h>
6060
#include <util/string.h>
6161
#include <util/system.h>
62+
#include <util/thread.h>
6263
#include <util/threadnames.h>
6364
#include <util/translation.h>
6465
#include <validation.h>
@@ -1114,7 +1115,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
11141115
node.scheduler = std::make_unique<CScheduler>();
11151116

11161117
// Start the lightweight task scheduler thread
1117-
node.scheduler->m_service_thread = std::thread([&] { TraceThread("scheduler", [&] { node.scheduler->serviceQueue(); }); });
1118+
node.scheduler->m_service_thread = std::thread(util::TraceThread, "scheduler", [&] { node.scheduler->serviceQueue(); });
11181119

11191120
// Gather some entropy once per minute.
11201121
node.scheduler->scheduleEvery([]{
@@ -1629,7 +1630,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
16291630
vImportFiles.push_back(strFile);
16301631
}
16311632

1632-
chainman.m_load_block = std::thread(&TraceThread<std::function<void()>>, "loadblk", [=, &chainman, &args] {
1633+
chainman.m_load_block = std::thread(&util::TraceThread, "loadblk", [=, &chainman, &args] {
16331634
ThreadImport(chainman, vImportFiles, args);
16341635
});
16351636

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(&util::TraceThread, "mapport", &ThreadMapPort);
259260
}
260261
}
261262

src/net.cpp

Lines changed: 11 additions & 8 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
@@ -2519,15 +2520,15 @@ bool CConnman::Start(CScheduler& scheduler, const Options& connOptions)
25192520
}
25202521

25212522
// Send and receive from sockets, accept connections
2522-
threadSocketHandler = std::thread(&TraceThread<std::function<void()> >, "net", std::function<void()>(std::bind(&CConnman::ThreadSocketHandler, this)));
2523+
threadSocketHandler = std::thread(&util::TraceThread, "net", [this] { ThreadSocketHandler(); });
25232524

25242525
if (!gArgs.GetBoolArg("-dnsseed", DEFAULT_DNSSEED))
25252526
LogPrintf("DNS seeding disabled\n");
25262527
else
2527-
threadDNSAddressSeed = std::thread(&TraceThread<std::function<void()> >, "dnsseed", std::function<void()>(std::bind(&CConnman::ThreadDNSAddressSeed, this)));
2528+
threadDNSAddressSeed = std::thread(&util::TraceThread, "dnsseed", [this] { ThreadDNSAddressSeed(); });
25282529

25292530
// Initiate manual connections
2530-
threadOpenAddedConnections = std::thread(&TraceThread<std::function<void()> >, "addcon", std::function<void()>(std::bind(&CConnman::ThreadOpenAddedConnections, this)));
2531+
threadOpenAddedConnections = std::thread(&util::TraceThread, "addcon", [this] { ThreadOpenAddedConnections(); });
25312532

25322533
if (connOptions.m_use_addrman_outgoing && !connOptions.m_specified_outgoing.empty()) {
25332534
if (clientInterface) {
@@ -2537,16 +2538,18 @@ bool CConnman::Start(CScheduler& scheduler, const Options& connOptions)
25372538
}
25382539
return false;
25392540
}
2540-
if (connOptions.m_use_addrman_outgoing || !connOptions.m_specified_outgoing.empty())
2541-
threadOpenConnections = std::thread(&TraceThread<std::function<void()> >, "opencon", std::function<void()>(std::bind(&CConnman::ThreadOpenConnections, this, connOptions.m_specified_outgoing)));
2541+
if (connOptions.m_use_addrman_outgoing || !connOptions.m_specified_outgoing.empty()) {
2542+
threadOpenConnections = std::thread(
2543+
&util::TraceThread, "opencon",
2544+
[this, connect = connOptions.m_specified_outgoing] { ThreadOpenConnections(connect); });
2545+
}
25422546

25432547
// Process messages
2544-
threadMessageHandler = std::thread(&TraceThread<std::function<void()> >, "msghand", std::function<void()>(std::bind(&CConnman::ThreadMessageHandler, this)));
2548+
threadMessageHandler = std::thread(&util::TraceThread, "msghand", [this] { ThreadMessageHandler(); });
25452549

25462550
if (connOptions.m_i2p_accept_incoming && m_i2p_sam_session.get() != nullptr) {
25472551
threadI2PAcceptIncoming =
2548-
std::thread(&TraceThread<std::function<void()>>, "i2paccept",
2549-
std::function<void()>(std::bind(&CConnman::ThreadI2PAcceptIncoming, this)));
2552+
std::thread(&util::TraceThread, "i2paccept", [this] { ThreadI2PAcceptIncoming(); });
25502553
}
25512554

25522555
// Dump network addresses

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>
@@ -135,7 +137,7 @@ ChainTestingSetup::ChainTestingSetup(const std::string& chainName, const std::ve
135137
// We have to run a scheduler thread to prevent ActivateBestChain
136138
// from blocking due to queue overrun.
137139
m_node.scheduler = std::make_unique<CScheduler>();
138-
m_node.scheduler->m_service_thread = std::thread([&] { TraceThread("scheduler", [&] { m_node.scheduler->serviceQueue(); }); });
140+
m_node.scheduler->m_service_thread = std::thread(util::TraceThread, "scheduler", [&] { m_node.scheduler->serviceQueue(); });
139141
GetMainSignals().RegisterBackgroundSignalScheduler(*m_node.scheduler);
140142

141143
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>
@@ -478,28 +477,6 @@ std::string HelpMessageOpt(const std::string& option, const std::string& message
478477
*/
479478
int GetNumCores();
480479

481-
/**
482-
* .. and a wrapper that just calls func once
483-
*/
484-
template <typename Callable> void TraceThread(const char* name, Callable func)
485-
{
486-
util::ThreadRename(name);
487-
try
488-
{
489-
LogPrintf("%s thread start\n", name);
490-
func();
491-
LogPrintf("%s thread exit\n", name);
492-
}
493-
catch (const std::exception& e) {
494-
PrintExceptionContinue(&e, name);
495-
throw;
496-
}
497-
catch (...) {
498-
PrintExceptionContinue(nullptr, name);
499-
throw;
500-
}
501-
}
502-
503480
std::string CopyrightHolders(const std::string& strPrefix);
504481

505482
/**

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)