diff --git a/CHANGELOG.md b/CHANGELOG.md index 990eb4c68..89886d126 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -61,7 +61,7 @@ - Dev: Refactored 7TV/BTTV definitions out of `TwitchIrcServer` into `Application`. (#5532) - Dev: Refactored code that's responsible for deleting old update files. (#5535) - Dev: Cleanly exit on shutdown. (#5537) -- Dev: Renamed miniaudio backend thread name. (#5538) +- Dev: Renamed threads created by Chatterino on Linux and Windows. (#5538, #5539) - Dev: Refactored a few `#define`s into `const(expr)` and cleaned includes. (#5527) - Dev: Prepared for Qt 6.8 by addressing some deprecations. (#5529) diff --git a/src/BrowserExtension.cpp b/src/BrowserExtension.cpp index 8511cbcb0..4b0f7e69a 100644 --- a/src/BrowserExtension.cpp +++ b/src/BrowserExtension.cpp @@ -1,6 +1,7 @@ #include "BrowserExtension.hpp" #include "singletons/NativeMessaging.hpp" +#include "util/RenameThread.hpp" #include #include @@ -69,6 +70,7 @@ void runLoop() std::this_thread::sleep_for(10s); } }); + renameThread(thread, "BrowserPingCheck"); while (true) { diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 7c68aef35..1998646b2 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -511,6 +511,7 @@ set(SOURCE_FILES util/RapidjsonHelpers.hpp util/RatelimitBucket.cpp util/RatelimitBucket.hpp + util/RenameThread.cpp util/RenameThread.hpp util/SampleData.cpp util/SampleData.hpp diff --git a/src/common/network/NetworkManager.cpp b/src/common/network/NetworkManager.cpp index eb1b7ec52..b1b6bd28f 100644 --- a/src/common/network/NetworkManager.cpp +++ b/src/common/network/NetworkManager.cpp @@ -13,6 +13,7 @@ void NetworkManager::init() assert(!NetworkManager::accessManager); NetworkManager::workerThread = new QThread; + NetworkManager::workerThread->setObjectName("NetworkWorker"); NetworkManager::workerThread->start(); NetworkManager::accessManager = new QNetworkAccessManager; diff --git a/src/providers/twitch/PubSubManager.cpp b/src/providers/twitch/PubSubManager.cpp index 9f16951a7..c8050a24d 100644 --- a/src/providers/twitch/PubSubManager.cpp +++ b/src/providers/twitch/PubSubManager.cpp @@ -10,6 +10,7 @@ #include "util/DebugCount.hpp" #include "util/Helpers.hpp" #include "util/RapidjsonHelpers.hpp" +#include "util/RenameThread.hpp" #include @@ -17,6 +18,7 @@ #include #include #include +#include #include using websocketpp::lib::bind; @@ -543,7 +545,10 @@ void PubSub::start() { this->work = std::make_shared( this->websocketClient.get_io_service()); - this->thread.reset(new std::thread(std::bind(&PubSub::runThread, this))); + this->thread = std::make_unique([this] { + runThread(); + }); + renameThread(*this->thread, "PubSub"); } void PubSub::stop() diff --git a/src/singletons/NativeMessaging.cpp b/src/singletons/NativeMessaging.cpp index 1dfc93a5f..72dc1c3b6 100644 --- a/src/singletons/NativeMessaging.cpp +++ b/src/singletons/NativeMessaging.cpp @@ -134,6 +134,7 @@ namespace nm::client { NativeMessagingServer::NativeMessagingServer() : thread(*this) { + this->thread.setObjectName("NativeMessagingReceiver"); } NativeMessagingServer::~NativeMessagingServer() diff --git a/src/util/RenameThread.cpp b/src/util/RenameThread.cpp new file mode 100644 index 000000000..4b4978c82 --- /dev/null +++ b/src/util/RenameThread.cpp @@ -0,0 +1,41 @@ +#include "util/RenameThread.hpp" + +#include "common/QLogging.hpp" + +#include + +#ifdef Q_OS_WIN + +# include + +namespace chatterino::windows::detail { + +void renameThread(HANDLE hThread, const QString &threadName) +{ +# if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) // Qt 6 requires Windows 10 1809 + // Windows 10, version 1607 + constexpr QOperatingSystemVersion minVersion{ + QOperatingSystemVersion::Windows, + 10, + 0, + 14393, + }; + // minVersion is excluded, because it has some additional requirements + if (QOperatingSystemVersion::current() <= minVersion) + { + return; + } +# endif + + auto hr = SetThreadDescription(hThread, threadName.toStdWString().c_str()); + if (!SUCCEEDED(hr)) + { + qCWarning(chatterinoCommon).nospace() + << "Failed to set thread description, hresult=0x" + << QString::number(hr, 16); + } +} + +} // namespace chatterino::windows::detail + +#endif diff --git a/src/util/RenameThread.hpp b/src/util/RenameThread.hpp index 69c0b1d29..5212c8761 100644 --- a/src/util/RenameThread.hpp +++ b/src/util/RenameThread.hpp @@ -9,11 +9,19 @@ namespace chatterino { +#ifdef Q_OS_WIN +namespace windows::detail { + void renameThread(void *hThread, const QString &name); +} // namespace windows::detail +#endif + template void renameThread(T &thread, const QString &threadName) { #ifdef Q_OS_LINUX pthread_setname_np(thread.native_handle(), threadName.toLocal8Bit()); +#elif defined(Q_OS_WIN) + windows::detail::renameThread(thread.native_handle(), threadName); #endif }