mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-10 10:52:31 -05:00
refactor: Make TraceThread a non-template free function
Also it is moved into its own module.
This commit is contained in:
parent
a1f0b8b62e
commit
30e4448215
11 changed files with 67 additions and 36 deletions
|
@ -250,6 +250,7 @@ BITCOIN_CORE_H = \
|
||||||
util/spanparsing.h \
|
util/spanparsing.h \
|
||||||
util/string.h \
|
util/string.h \
|
||||||
util/system.h \
|
util/system.h \
|
||||||
|
util/thread.h \
|
||||||
util/threadnames.h \
|
util/threadnames.h \
|
||||||
util/time.h \
|
util/time.h \
|
||||||
util/tokenpipe.h \
|
util/tokenpipe.h \
|
||||||
|
@ -577,6 +578,7 @@ libbitcoin_util_a_SOURCES = \
|
||||||
util/rbf.cpp \
|
util/rbf.cpp \
|
||||||
util/readwritefile.cpp \
|
util/readwritefile.cpp \
|
||||||
util/settings.cpp \
|
util/settings.cpp \
|
||||||
|
util/thread.cpp \
|
||||||
util/threadnames.cpp \
|
util/threadnames.cpp \
|
||||||
util/spanparsing.cpp \
|
util/spanparsing.cpp \
|
||||||
util/strencodings.cpp \
|
util/strencodings.cpp \
|
||||||
|
|
|
@ -8,7 +8,7 @@
|
||||||
#include <node/ui_interface.h>
|
#include <node/ui_interface.h>
|
||||||
#include <shutdown.h>
|
#include <shutdown.h>
|
||||||
#include <tinyformat.h>
|
#include <tinyformat.h>
|
||||||
#include <util/system.h>
|
#include <util/thread.h>
|
||||||
#include <util/translation.h>
|
#include <util/translation.h>
|
||||||
#include <validation.h> // For g_chainman
|
#include <validation.h> // For g_chainman
|
||||||
#include <warnings.h>
|
#include <warnings.h>
|
||||||
|
@ -349,7 +349,7 @@ void BaseIndex::Start()
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
m_thread_sync = std::thread(&TraceThread<std::function<void()>>, GetName(),
|
m_thread_sync = std::thread(&util::TraceThread, GetName(),
|
||||||
std::bind(&BaseIndex::ThreadSync, this));
|
std::bind(&BaseIndex::ThreadSync, this));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -58,6 +58,7 @@
|
||||||
#include <util/moneystr.h>
|
#include <util/moneystr.h>
|
||||||
#include <util/string.h>
|
#include <util/string.h>
|
||||||
#include <util/system.h>
|
#include <util/system.h>
|
||||||
|
#include <util/thread.h>
|
||||||
#include <util/threadnames.h>
|
#include <util/threadnames.h>
|
||||||
#include <util/translation.h>
|
#include <util/translation.h>
|
||||||
#include <validation.h>
|
#include <validation.h>
|
||||||
|
@ -1266,7 +1267,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
|
||||||
node.scheduler = std::make_unique<CScheduler>();
|
node.scheduler = std::make_unique<CScheduler>();
|
||||||
|
|
||||||
// Start the lightweight task scheduler thread
|
// Start the lightweight task scheduler thread
|
||||||
node.scheduler->m_service_thread = std::thread([&] { TraceThread("scheduler", [&] { node.scheduler->serviceQueue(); }); });
|
node.scheduler->m_service_thread = std::thread([&] { util::TraceThread("scheduler", [&] { node.scheduler->serviceQueue(); }); });
|
||||||
|
|
||||||
// Gather some entropy once per minute.
|
// Gather some entropy once per minute.
|
||||||
node.scheduler->scheduleEvery([]{
|
node.scheduler->scheduleEvery([]{
|
||||||
|
@ -1791,7 +1792,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
|
||||||
vImportFiles.push_back(strFile);
|
vImportFiles.push_back(strFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
chainman.m_load_block = std::thread(&TraceThread<std::function<void()>>, "loadblk", [=, &chainman, &args] {
|
chainman.m_load_block = std::thread(&util::TraceThread, "loadblk", [=, &chainman, &args] {
|
||||||
ThreadImport(chainman, vImportFiles, args);
|
ThreadImport(chainman, vImportFiles, args);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -15,6 +15,7 @@
|
||||||
#include <netbase.h>
|
#include <netbase.h>
|
||||||
#include <threadinterrupt.h>
|
#include <threadinterrupt.h>
|
||||||
#include <util/system.h>
|
#include <util/system.h>
|
||||||
|
#include <util/thread.h>
|
||||||
|
|
||||||
#ifdef USE_NATPMP
|
#ifdef USE_NATPMP
|
||||||
#include <compat.h>
|
#include <compat.h>
|
||||||
|
@ -255,7 +256,7 @@ void StartThreadMapPort()
|
||||||
{
|
{
|
||||||
if (!g_mapport_thread.joinable()) {
|
if (!g_mapport_thread.joinable()) {
|
||||||
assert(!g_mapport_interrupt);
|
assert(!g_mapport_interrupt);
|
||||||
g_mapport_thread = std::thread(std::bind(&TraceThread<void (*)()>, "mapport", &ThreadMapPort));
|
g_mapport_thread = std::thread(std::bind(&util::TraceThread, "mapport", &ThreadMapPort));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
13
src/net.cpp
13
src/net.cpp
|
@ -23,6 +23,7 @@
|
||||||
#include <scheduler.h>
|
#include <scheduler.h>
|
||||||
#include <util/sock.h>
|
#include <util/sock.h>
|
||||||
#include <util/strencodings.h>
|
#include <util/strencodings.h>
|
||||||
|
#include <util/thread.h>
|
||||||
#include <util/translation.h>
|
#include <util/translation.h>
|
||||||
|
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
|
@ -2527,15 +2528,15 @@ bool CConnman::Start(CScheduler& scheduler, const Options& connOptions)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send and receive from sockets, accept connections
|
// Send and receive from sockets, accept connections
|
||||||
threadSocketHandler = std::thread(&TraceThread<std::function<void()> >, "net", std::function<void()>(std::bind(&CConnman::ThreadSocketHandler, this)));
|
threadSocketHandler = std::thread(&util::TraceThread, "net", std::bind(&CConnman::ThreadSocketHandler, this));
|
||||||
|
|
||||||
if (!gArgs.GetBoolArg("-dnsseed", DEFAULT_DNSSEED))
|
if (!gArgs.GetBoolArg("-dnsseed", DEFAULT_DNSSEED))
|
||||||
LogPrintf("DNS seeding disabled\n");
|
LogPrintf("DNS seeding disabled\n");
|
||||||
else
|
else
|
||||||
threadDNSAddressSeed = std::thread(&TraceThread<std::function<void()> >, "dnsseed", std::function<void()>(std::bind(&CConnman::ThreadDNSAddressSeed, this)));
|
threadDNSAddressSeed = std::thread(&util::TraceThread, "dnsseed", std::bind(&CConnman::ThreadDNSAddressSeed, this));
|
||||||
|
|
||||||
// Initiate manual connections
|
// Initiate manual connections
|
||||||
threadOpenAddedConnections = std::thread(&TraceThread<std::function<void()> >, "addcon", std::function<void()>(std::bind(&CConnman::ThreadOpenAddedConnections, this)));
|
threadOpenAddedConnections = std::thread(&util::TraceThread, "addcon", std::bind(&CConnman::ThreadOpenAddedConnections, this));
|
||||||
|
|
||||||
if (connOptions.m_use_addrman_outgoing && !connOptions.m_specified_outgoing.empty()) {
|
if (connOptions.m_use_addrman_outgoing && !connOptions.m_specified_outgoing.empty()) {
|
||||||
if (clientInterface) {
|
if (clientInterface) {
|
||||||
|
@ -2546,14 +2547,14 @@ bool CConnman::Start(CScheduler& scheduler, const Options& connOptions)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (connOptions.m_use_addrman_outgoing || !connOptions.m_specified_outgoing.empty())
|
if (connOptions.m_use_addrman_outgoing || !connOptions.m_specified_outgoing.empty())
|
||||||
threadOpenConnections = std::thread(&TraceThread<std::function<void()> >, "opencon", std::function<void()>(std::bind(&CConnman::ThreadOpenConnections, this, connOptions.m_specified_outgoing)));
|
threadOpenConnections = std::thread(&util::TraceThread, "opencon", std::bind(&CConnman::ThreadOpenConnections, this, connOptions.m_specified_outgoing));
|
||||||
|
|
||||||
// Process messages
|
// Process messages
|
||||||
threadMessageHandler = std::thread(&TraceThread<std::function<void()> >, "msghand", std::function<void()>(std::bind(&CConnman::ThreadMessageHandler, this)));
|
threadMessageHandler = std::thread(&util::TraceThread, "msghand", std::bind(&CConnman::ThreadMessageHandler, this));
|
||||||
|
|
||||||
if (connOptions.m_i2p_accept_incoming && m_i2p_sam_session.get() != nullptr) {
|
if (connOptions.m_i2p_accept_incoming && m_i2p_sam_session.get() != nullptr) {
|
||||||
threadI2PAcceptIncoming =
|
threadI2PAcceptIncoming =
|
||||||
std::thread(&TraceThread<std::function<void()>>, "i2paccept",
|
std::thread(&util::TraceThread, "i2paccept",
|
||||||
std::function<void()>(std::bind(&CConnman::ThreadI2PAcceptIncoming, this)));
|
std::function<void()>(std::bind(&CConnman::ThreadI2PAcceptIncoming, this)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -28,6 +28,8 @@
|
||||||
#include <txdb.h>
|
#include <txdb.h>
|
||||||
#include <util/strencodings.h>
|
#include <util/strencodings.h>
|
||||||
#include <util/string.h>
|
#include <util/string.h>
|
||||||
|
#include <util/thread.h>
|
||||||
|
#include <util/threadnames.h>
|
||||||
#include <util/time.h>
|
#include <util/time.h>
|
||||||
#include <util/translation.h>
|
#include <util/translation.h>
|
||||||
#include <util/url.h>
|
#include <util/url.h>
|
||||||
|
@ -132,7 +134,7 @@ ChainTestingSetup::ChainTestingSetup(const std::string& chainName, const std::ve
|
||||||
// We have to run a scheduler thread to prevent ActivateBestChain
|
// We have to run a scheduler thread to prevent ActivateBestChain
|
||||||
// from blocking due to queue overrun.
|
// from blocking due to queue overrun.
|
||||||
m_node.scheduler = std::make_unique<CScheduler>();
|
m_node.scheduler = std::make_unique<CScheduler>();
|
||||||
m_node.scheduler->m_service_thread = std::thread([&] { TraceThread("scheduler", [&] { m_node.scheduler->serviceQueue(); }); });
|
m_node.scheduler->m_service_thread = std::thread([&] { util::TraceThread("scheduler", [&] { m_node.scheduler->serviceQueue(); }); });
|
||||||
GetMainSignals().RegisterBackgroundSignalScheduler(*m_node.scheduler);
|
GetMainSignals().RegisterBackgroundSignalScheduler(*m_node.scheduler);
|
||||||
|
|
||||||
pblocktree.reset(new CBlockTreeDB(1 << 20, true));
|
pblocktree.reset(new CBlockTreeDB(1 << 20, true));
|
||||||
|
|
|
@ -15,6 +15,7 @@
|
||||||
#include <util/readwritefile.h>
|
#include <util/readwritefile.h>
|
||||||
#include <util/strencodings.h>
|
#include <util/strencodings.h>
|
||||||
#include <util/system.h>
|
#include <util/system.h>
|
||||||
|
#include <util/thread.h>
|
||||||
#include <util/time.h>
|
#include <util/time.h>
|
||||||
|
|
||||||
#include <deque>
|
#include <deque>
|
||||||
|
@ -596,7 +597,7 @@ void StartTorControl(CService onion_service_target)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
torControlThread = std::thread(&TraceThread<std::function<void()>>, "torcontrol", [onion_service_target] {
|
torControlThread = std::thread(&util::TraceThread, "torcontrol", [onion_service_target] {
|
||||||
TorControlThread(onion_service_target);
|
TorControlThread(onion_service_target);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,6 +18,7 @@
|
||||||
#include <validation.h>
|
#include <validation.h>
|
||||||
#include <validationinterface.h>
|
#include <validationinterface.h>
|
||||||
|
|
||||||
|
#include <cmath>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
|
||||||
CTxMemPoolEntry::CTxMemPoolEntry(const CTransactionRef& _tx, const CAmount& _nFee,
|
CTxMemPoolEntry::CTxMemPoolEntry(const CTransactionRef& _tx, const CAmount& _nFee,
|
||||||
|
|
|
@ -22,7 +22,6 @@
|
||||||
#include <sync.h>
|
#include <sync.h>
|
||||||
#include <tinyformat.h>
|
#include <tinyformat.h>
|
||||||
#include <util/settings.h>
|
#include <util/settings.h>
|
||||||
#include <util/threadnames.h>
|
|
||||||
#include <util/time.h>
|
#include <util/time.h>
|
||||||
|
|
||||||
#include <any>
|
#include <any>
|
||||||
|
@ -458,28 +457,6 @@ std::string HelpMessageOpt(const std::string& option, const std::string& message
|
||||||
*/
|
*/
|
||||||
int GetNumCores();
|
int GetNumCores();
|
||||||
|
|
||||||
/**
|
|
||||||
* .. and a wrapper that just calls func once
|
|
||||||
*/
|
|
||||||
template <typename Callable> void TraceThread(const char* name, Callable func)
|
|
||||||
{
|
|
||||||
util::ThreadRename(name);
|
|
||||||
try
|
|
||||||
{
|
|
||||||
LogPrintf("%s thread start\n", name);
|
|
||||||
func();
|
|
||||||
LogPrintf("%s thread exit\n", name);
|
|
||||||
}
|
|
||||||
catch (const std::exception& e) {
|
|
||||||
PrintExceptionContinue(&e, name);
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
catch (...) {
|
|
||||||
PrintExceptionContinue(nullptr, name);
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string CopyrightHolders(const std::string& strPrefix);
|
std::string CopyrightHolders(const std::string& strPrefix);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
27
src/util/thread.cpp
Normal file
27
src/util/thread.cpp
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
// Copyright (c) 2021 The Bitcoin Core developers
|
||||||
|
// Distributed under the MIT software license, see the accompanying
|
||||||
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||||
|
|
||||||
|
#include <util/thread.h>
|
||||||
|
|
||||||
|
#include <logging.h>
|
||||||
|
#include <util/system.h>
|
||||||
|
#include <util/threadnames.h>
|
||||||
|
|
||||||
|
#include <exception>
|
||||||
|
|
||||||
|
void util::TraceThread(const char* thread_name, std::function<void()> thread_func)
|
||||||
|
{
|
||||||
|
util::ThreadRename(thread_name);
|
||||||
|
try {
|
||||||
|
LogPrintf("%s thread start\n", thread_name);
|
||||||
|
thread_func();
|
||||||
|
LogPrintf("%s thread exit\n", thread_name);
|
||||||
|
} catch (const std::exception& e) {
|
||||||
|
PrintExceptionContinue(&e, thread_name);
|
||||||
|
throw;
|
||||||
|
} catch (...) {
|
||||||
|
PrintExceptionContinue(nullptr, thread_name);
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
18
src/util/thread.h
Normal file
18
src/util/thread.h
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
// Copyright (c) 2021 The Bitcoin Core developers
|
||||||
|
// Distributed under the MIT software license, see the accompanying
|
||||||
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||||
|
|
||||||
|
#ifndef BITCOIN_UTIL_THREAD_H
|
||||||
|
#define BITCOIN_UTIL_THREAD_H
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
|
namespace util {
|
||||||
|
/**
|
||||||
|
* A wrapper for do-something-once thread functions.
|
||||||
|
*/
|
||||||
|
void TraceThread(const char* thread_name, std::function<void()> thread_func);
|
||||||
|
|
||||||
|
} // namespace util
|
||||||
|
|
||||||
|
#endif // BITCOIN_UTIL_THREAD_H
|
Loading…
Add table
Reference in a new issue