From 3100d4f90224094608215bba049f52e81e4e75da Mon Sep 17 00:00:00 2001 From: R4SAS Date: Mon, 7 Dec 2020 06:22:16 +0300 Subject: [PATCH] move thread naming to util Signed-off-by: R4SAS --- daemon/HTTPServer.cpp | 4 ++-- daemon/I2PControl.cpp | 4 ++-- daemon/UPnP.cpp | 4 ++-- libi2pd/Log.cpp | 5 +++-- libi2pd/NetDb.cpp | 12 +++++------ libi2pd/SSU.cpp | 8 ++++---- libi2pd/Timestamp.cpp | 5 +++-- libi2pd/Transports.cpp | 6 +++--- libi2pd/Tunnel.cpp | 4 ++-- libi2pd/util.cpp | 40 +++++++++++++++++++++++++++--------- libi2pd/util.h | 6 ++++-- libi2pd_client/I2PTunnel.cpp | 4 ++-- 12 files changed, 63 insertions(+), 39 deletions(-) diff --git a/daemon/HTTPServer.cpp b/daemon/HTTPServer.cpp index 73849e4d..25b6ab19 100644 --- a/daemon/HTTPServer.cpp +++ b/daemon/HTTPServer.cpp @@ -9,7 +9,6 @@ #include #include #include -#include #include #include @@ -1313,7 +1312,8 @@ namespace http { void HTTPServer::Run () { - pthread_setname_np(pthread_self(), "Webconsole"); + i2p::util::SetThreadName("Webconsole"); + while (m_IsRunning) { try diff --git a/daemon/I2PControl.cpp b/daemon/I2PControl.cpp index 1fde9109..3f0033e5 100644 --- a/daemon/I2PControl.cpp +++ b/daemon/I2PControl.cpp @@ -7,7 +7,6 @@ #include #include #include -#include #include "Crypto.h" #include "FS.h" @@ -132,7 +131,8 @@ namespace client void I2PControlService::Run () { - pthread_setname_np(pthread_self(), "I2PC"); + i2p::util::SetThreadName("I2PC"); + while (m_IsRunning) { try { diff --git a/daemon/UPnP.cpp b/daemon/UPnP.cpp index 0076ddd7..6ea33c46 100644 --- a/daemon/UPnP.cpp +++ b/daemon/UPnP.cpp @@ -1,7 +1,6 @@ #ifdef USE_UPNP #include #include -#include #include #include @@ -61,7 +60,8 @@ namespace transport void UPnP::Run () { - pthread_setname_np(pthread_self(), "UPnP"); + i2p::util::SetThreadName("UPnP"); + while (m_IsRunning) { try diff --git a/libi2pd/Log.cpp b/libi2pd/Log.cpp index d7386642..2b555663 100644 --- a/libi2pd/Log.cpp +++ b/libi2pd/Log.cpp @@ -7,7 +7,7 @@ */ #include "Log.h" -#include +#include "util.h" //for std::transform #include @@ -180,7 +180,8 @@ namespace log { void Log::Run () { - pthread_setname_np(pthread_self(), "Logging"); + i2p::util::SetThreadName("Logging"); + Reopen (); while (m_IsRunning) { diff --git a/libi2pd/NetDb.cpp b/libi2pd/NetDb.cpp index 66dac010..20b54376 100644 --- a/libi2pd/NetDb.cpp +++ b/libi2pd/NetDb.cpp @@ -11,7 +11,6 @@ #include #include #include -#include #include "I2PEndian.h" #include "Base.h" @@ -27,6 +26,7 @@ #include "ECIESX25519AEADRatchetSession.h" #include "Config.h" #include "NetDb.hpp" +#include "util.h" using namespace i2p::transport; @@ -89,7 +89,7 @@ namespace data void NetDb::Run () { - pthread_setname_np(pthread_self(), "NetDB"); + i2p::util::SetThreadName("NetDB"); uint32_t lastSave = 0, lastPublish = 0, lastExploratory = 0, lastManageRequest = 0, lastDestinationCleanup = 0; while (m_IsRunning) @@ -116,7 +116,7 @@ namespace data break; case eI2NPDeliveryStatus: HandleDeliveryStatusMsg (msg); - break; + break; case eI2NPDummyMsg: // plain RouterInfo from NTCP2 with flags for now HandleNTCP2RouterInfoMsg (msg); @@ -158,12 +158,12 @@ namespace data if (!m_HiddenMode && i2p::transport::transports.IsOnline ()) { bool publish = false; - if (m_PublishReplyToken) - { + if (m_PublishReplyToken) + { if (ts - lastPublish >= NETDB_PUBLISH_CONFIRMATION_TIMEOUT) publish = true; } else if (i2p::context.GetLastUpdateTime () > lastPublish || - ts - lastPublish >= NETDB_PUBLISH_INTERVAL) publish = true; + ts - lastPublish >= NETDB_PUBLISH_INTERVAL) publish = true; if (publish) // update timestamp and publish { i2p::context.UpdateTimestamp (ts); diff --git a/libi2pd/SSU.cpp b/libi2pd/SSU.cpp index a75306bb..3b6280dd 100644 --- a/libi2pd/SSU.cpp +++ b/libi2pd/SSU.cpp @@ -7,12 +7,12 @@ */ #include -#include #include "Log.h" #include "Timestamp.h" #include "RouterContext.h" #include "NetDb.hpp" #include "SSU.h" +#include "util.h" #ifdef _WIN32 #include @@ -143,7 +143,7 @@ namespace transport void SSUServer::Run () { - pthread_setname_np(pthread_self(), "SSU"); + i2p::util::SetThreadName("SSU"); while (m_IsRunning) { @@ -160,7 +160,7 @@ namespace transport void SSUServer::RunReceivers () { - pthread_setname_np(pthread_self(), "SSUv4"); + i2p::util::SetThreadName("SSUv4"); while (m_IsRunning) { @@ -184,7 +184,7 @@ namespace transport void SSUServer::RunReceiversV6 () { - pthread_setname_np(pthread_self(), "SSUv6"); + i2p::util::SetThreadName("SSUv6"); while (m_IsRunning) { diff --git a/libi2pd/Timestamp.cpp b/libi2pd/Timestamp.cpp index db5c3946..3cd336ed 100644 --- a/libi2pd/Timestamp.cpp +++ b/libi2pd/Timestamp.cpp @@ -14,11 +14,11 @@ #include #include #include -#include #include "Config.h" #include "Log.h" #include "I2PEndian.h" #include "Timestamp.h" +#include "util.h" #ifdef _WIN32 #ifndef _WIN64 @@ -149,7 +149,8 @@ namespace util void NTPTimeSync::Run () { - pthread_setname_np(pthread_self(), "Timesync"); + i2p::util::SetThreadName("Timesync"); + while (m_IsRunning) { try diff --git a/libi2pd/Transports.cpp b/libi2pd/Transports.cpp index 585ef8e0..5c2fcb6d 100644 --- a/libi2pd/Transports.cpp +++ b/libi2pd/Transports.cpp @@ -6,7 +6,6 @@ * See full license text in LICENSE file at top of project tree */ -#include #include "Log.h" #include "Crypto.h" #include "RouterContext.h" @@ -15,6 +14,7 @@ #include "Transports.h" #include "Config.h" #include "HTTP.h" +#include "util.h" using namespace i2p::data; @@ -60,7 +60,7 @@ namespace transport template void EphemeralKeysSupplier::Run () { - pthread_setname_np(pthread_self(), "Ephemerals"); + i2p::util::SetThreadName("Ephemerals"); while (m_IsRunning) { @@ -275,7 +275,7 @@ namespace transport void Transports::Run () { - pthread_setname_np(pthread_self(), "Transports"); + i2p::util::SetThreadName("Transports"); while (m_IsRunning && m_Service) { diff --git a/libi2pd/Tunnel.cpp b/libi2pd/Tunnel.cpp index a5f20963..42eeeb5d 100644 --- a/libi2pd/Tunnel.cpp +++ b/libi2pd/Tunnel.cpp @@ -10,7 +10,6 @@ #include "I2PEndian.h" #include #include -#include #include #include #include "Crypto.h" @@ -23,6 +22,7 @@ #include "Config.h" #include "Tunnel.h" #include "TunnelPool.h" +#include "util.h" namespace i2p { @@ -473,7 +473,7 @@ namespace tunnel void Tunnels::Run () { - pthread_setname_np(pthread_self(), "Tunnels"); + i2p::util::SetThreadName("Tunnels"); std::this_thread::sleep_for (std::chrono::seconds(1)); // wait for other parts are ready uint64_t lastTs = 0, lastPoolsTs = 0; diff --git a/libi2pd/util.cpp b/libi2pd/util.cpp index e98f939f..2e8e67ba 100644 --- a/libi2pd/util.cpp +++ b/libi2pd/util.cpp @@ -13,6 +13,15 @@ #include "util.h" #include "Log.h" +#if not defined (__FreeBSD__) +#include +#endif + +#if defined(__OpenBSD__) || defined(__FreeBSD__) +#include +#endif + + #ifdef _WIN32 #include #include @@ -32,7 +41,7 @@ // inet_pton exists Windows since Vista, but XP doesn't have that function! // This function was written by Petar Korponai?. See http://stackoverflow.com/questions/15660203/inet-pton-identifier-not-found -int inet_pton_xp(int af, const char *src, void *dst) +int inet_pton_xp (int af, const char *src, void *dst) { struct sockaddr_storage ss; int size = sizeof (ss); @@ -94,7 +103,8 @@ namespace util void RunnableService::Run () { - pthread_setname_np(pthread_self(), m_Name.c_str()); + SetThreadName(m_Name.c_str()); + while (m_IsRunning) { try @@ -108,10 +118,20 @@ namespace util } } + void SetThreadName (const char *name) { +#if defined (__APPLE__) + pthread_setname_np(name); +#elif defined(__FreeBSD__) + pthread_set_name_np(pthread_self(), name) +#else + pthread_setname_np(pthread_self(), name); +#endif + } + namespace net { #ifdef _WIN32 - bool IsWindowsXPorLater() + bool IsWindowsXPorLater () { static bool isRequested = false; static bool isXP = false; @@ -130,7 +150,7 @@ namespace net return isXP; } - int GetMTUWindowsIpv4(sockaddr_in inputAddress, int fallback) + int GetMTUWindowsIpv4 (sockaddr_in inputAddress, int fallback) { ULONG outBufLen = 0; PIP_ADAPTER_ADDRESSES pAddresses = nullptr; @@ -184,7 +204,7 @@ namespace net return fallback; } - int GetMTUWindowsIpv6(sockaddr_in6 inputAddress, int fallback) + int GetMTUWindowsIpv6 (sockaddr_in6 inputAddress, int fallback) { ULONG outBufLen = 0; PIP_ADAPTER_ADDRESSES pAddresses = nullptr; @@ -249,7 +269,7 @@ namespace net return fallback; } - int GetMTUWindows(const boost::asio::ip::address& localAddress, int fallback) + int GetMTUWindows (const boost::asio::ip::address& localAddress, int fallback) { #ifdef UNICODE string localAddress_temporary = localAddress.to_string(); @@ -281,7 +301,7 @@ namespace net } } #else // assume unix - int GetMTUUnix(const boost::asio::ip::address& localAddress, int fallback) + int GetMTUUnix (const boost::asio::ip::address& localAddress, int fallback) { ifaddrs* ifaddr, *ifa = nullptr; if(getifaddrs(&ifaddr) == -1) @@ -336,7 +356,7 @@ namespace net } #endif // _WIN32 - int GetMTU(const boost::asio::ip::address& localAddress) + int GetMTU (const boost::asio::ip::address& localAddress) { int fallback = localAddress.is_v6 () ? 1280 : 620; // fallback MTU @@ -348,7 +368,7 @@ namespace net return fallback; } - const boost::asio::ip::address GetInterfaceAddress(const std::string & ifname, bool ipv6) + const boost::asio::ip::address GetInterfaceAddress (const std::string & ifname, bool ipv6) { #ifdef _WIN32 LogPrint(eLogError, "NetIface: cannot get address by interface name, not implemented on WIN32"); @@ -396,7 +416,7 @@ namespace net #endif } - bool IsInReservedRange(const boost::asio::ip::address& host) { + bool IsInReservedRange (const boost::asio::ip::address& host) { // https://en.wikipedia.org/wiki/Reserved_IP_addresses if(host.is_v4()) { diff --git a/libi2pd/util.h b/libi2pd/util.h index 56ce1e08..f6222b9f 100644 --- a/libi2pd/util.h +++ b/libi2pd/util.h @@ -168,11 +168,13 @@ namespace util boost::asio::io_service::work m_Work; }; + void SetThreadName (const char *name); + namespace net { int GetMTU (const boost::asio::ip::address& localAddress); - const boost::asio::ip::address GetInterfaceAddress(const std::string & ifname, bool ipv6=false); - bool IsInReservedRange(const boost::asio::ip::address& host); + const boost::asio::ip::address GetInterfaceAddress (const std::string & ifname, bool ipv6=false); + bool IsInReservedRange (const boost::asio::ip::address& host); } } } diff --git a/libi2pd_client/I2PTunnel.cpp b/libi2pd_client/I2PTunnel.cpp index 18b7c5a0..61c42b24 100644 --- a/libi2pd_client/I2PTunnel.cpp +++ b/libi2pd_client/I2PTunnel.cpp @@ -7,12 +7,12 @@ */ #include -#include #include "Base.h" #include "Log.h" #include "Destination.h" #include "ClientContext.h" #include "I2PTunnel.h" +#include "util.h" namespace i2p { @@ -941,8 +941,8 @@ namespace client } void I2PUDPClientTunnel::TryResolving() { + i2p::util::SetThreadName("UDP Resolver"); LogPrint(eLogInfo, "UDP Tunnel: Trying to resolve ", m_RemoteDest); - pthread_setname_np(pthread_self(), "UDP Resolver"); std::shared_ptr addr; while(!(addr = context.GetAddressBook().GetAddress(m_RemoteDest)) && !m_cancel_resolve)