Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: rename threads on Windows too #5539

Merged
merged 5 commits into from
Aug 11, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 2 additions & 0 deletions src/BrowserExtension.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "BrowserExtension.hpp"

#include "singletons/NativeMessaging.hpp"
#include "util/RenameThread.hpp"

#include <iostream>
#include <memory>
Expand Down Expand Up @@ -69,6 +70,7 @@ void runLoop()
std::this_thread::sleep_for(10s);
}
});
renameThread(thread, "BrowserPingCheck");

while (true)
{
Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/common/network/NetworkManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 6 additions & 1 deletion src/providers/twitch/PubSubManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
#include "util/DebugCount.hpp"
#include "util/Helpers.hpp"
#include "util/RapidjsonHelpers.hpp"
#include "util/RenameThread.hpp"

#include <QJsonArray>

#include <algorithm>
#include <exception>
#include <future>
#include <iostream>
#include <memory>
#include <thread>

using websocketpp::lib::bind;
Expand Down Expand Up @@ -543,7 +545,10 @@ void PubSub::start()
{
this->work = std::make_shared<boost::asio::io_service::work>(
this->websocketClient.get_io_service());
this->thread.reset(new std::thread(std::bind(&PubSub::runThread, this)));
this->thread = std::make_unique<std::thread>([this] {
runThread();
});
renameThread(*this->thread, "PubSub");
}

void PubSub::stop()
Expand Down
1 change: 1 addition & 0 deletions src/singletons/NativeMessaging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ namespace nm::client {
NativeMessagingServer::NativeMessagingServer()
: thread(*this)
{
this->thread.setObjectName("NativeMessagingReceiver");
}

NativeMessagingServer::~NativeMessagingServer()
Expand Down
22 changes: 22 additions & 0 deletions src/util/RenameThread.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "util/RenameThread.hpp"

#include "common/QLogging.hpp"

#ifdef Q_OS_WIN
# include <Windows.h>
#endif

namespace chatterino::windows::detail {

void renameThread(HANDLE hThread, const QString &threadName)
Nerixyz marked this conversation as resolved.
Show resolved Hide resolved
{
auto hr = SetThreadDescription(hThread, threadName.toStdWString().c_str());
Nerixyz marked this conversation as resolved.
Show resolved Hide resolved
if (!SUCCEEDED(hr))
{
qCWarning(chatterinoCommon).nospace()
<< "Failed to set thread description, hresult=0x"
<< QString::number(hr, 16);
}
}
Nerixyz marked this conversation as resolved.
Show resolved Hide resolved

} // namespace chatterino::windows::detail
12 changes: 12 additions & 0 deletions src/util/RenameThread.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,25 @@
# include <pthread.h>
#endif

#ifdef Q_OS_WIN
using HANDLE = void *;
#endif

namespace chatterino {

#ifdef Q_OS_WIN
namespace windows::detail {
void renameThread(HANDLE hThread, const QString &name);
Nerixyz marked this conversation as resolved.
Show resolved Hide resolved
} // namespace windows::detail
#endif

template <typename T>
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
}

Expand Down
Loading