Skip to content

Commit bb3c2e5

Browse files
committed
provide thread names
1 parent 11755f1 commit bb3c2e5

File tree

9 files changed

+80
-10
lines changed

9 files changed

+80
-10
lines changed

cpp/server/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
set(CMAKE_CXX_STANDARD 17)
22

3+
include(CheckSymbolExists)
4+
35
if(CXX_GCC OR CXX_CLANG)
46
add_cxx_compiler_flag(-Wall)
57
add_cxx_compiler_flag(-Wextra)
@@ -27,6 +29,12 @@ if(OS_POSIX)
2729

2830
add_linker_flag(-Wl,--no-undefined)
2931
add_linker_flag(-Wl,--as-needed)
32+
33+
check_symbol_exists(pthread_setname_np "pthread.h" HAVE_PTHREAD_SETNAME_NP)
34+
35+
if(NOT HAVE_PTHREAD_SETNAME_NP)
36+
check_symbol_exists(pthread_set_name_np "pthread.h" HAVE_PTHREAD_SET_NAME_NP)
37+
endif()
3038
endif()
3139

3240
if(OS_WINDOWS)
@@ -51,6 +59,7 @@ find_package(Nljson REQUIRED)
5159
find_package(StringEncoders REQUIRED)
5260
find_package(ZLIB REQUIRED)
5361

62+
configure_file(env_info.hpp.in env_info.hpp)
5463
configure_file(project_info.hpp.in project_info.hpp)
5564

5665
include_directories(

cpp/server/deadbeef/player_misc.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ ColumnsQueryPtr PlayerImpl::createColumnsQuery(const std::vector<std::string>& c
6868

6969
std::unique_ptr<WorkQueue> PlayerImpl::createWorkQueue()
7070
{
71-
return std::make_unique<ThreadWorkQueue>();
71+
return std::make_unique<ThreadWorkQueue>(MSRV_THREAD_NAME("control"));
7272
}
7373

7474
void PlayerImpl::connect()

cpp/server/env_info.hpp.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#pragma once
2+
3+
#cmakedefine HAVE_PTHREAD_SETNAME_NP
4+
#cmakedefine HAVE_PTHREAD_SET_NAME_NP

cpp/server/server_host.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
namespace msrv {
1818

1919
ServerHost::ServerHost(Player* player)
20-
: player_(player), utilityQueue_(8)
20+
: player_(player), utilityQueue_(8, MSRV_THREAD_NAME("io"))
2121
{
2222
playerWorkQueue_ = player_->createWorkQueue();
2323
player_->onEvents([this](PlayerEvents event) { handlePlayerEvents(event); });

cpp/server/server_thread.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "server_thread.hpp"
22
#include "log.hpp"
3+
#include "project_info.hpp"
34

45
#include <boost/thread/reverse_lock.hpp>
56

@@ -9,7 +10,10 @@ ServerThread::ServerThread(ServerReadyCallback readyCallback)
910
: command_(Command::NONE),
1011
readyCallback_(std::move(readyCallback))
1112
{
12-
thread_ = std::thread([this] { run(); });
13+
thread_ = std::thread([this] {
14+
setThreadName(MSRV_THREAD_NAME("server"));
15+
run();
16+
});
1317
}
1418

1519
ServerThread::~ServerThread()

cpp/server/system.hpp

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,19 @@
44

55
#include <stdint.h>
66

7+
#if MSRV_OS_POSIX
8+
#include "env_info.hpp"
9+
#include <pthread.h>
10+
#endif
11+
712
#include <stdexcept>
813
#include <type_traits>
914
#include <vector>
1015
#include <string>
1116
#include <utility>
1217

13-
namespace msrv {
18+
namespace msrv
19+
{
1420

1521
template<typename Traits>
1622
class Handle
@@ -86,6 +92,21 @@ class Handle
8692

8793
#if MSRV_OS_POSIX
8894

95+
#define MSRV_THREAD_NAME(s) MSRV_PROJECT_ID "-" s
96+
97+
typedef const char* ThreadName;
98+
99+
inline void setThreadName(ThreadName name)
100+
{
101+
#if defined(HAVE_PTHREAD_SETNAME_NP)
102+
(void) pthread_setname_np(pthread_self(), name);
103+
#elif defined(HAVE_PTHREAD_SET_NAME_NP)
104+
(void) pthread_set_name_np(pthread_self(), name);
105+
#else
106+
(void)name;
107+
#endif
108+
}
109+
89110
struct PosixHandleTraits
90111
{
91112
using Type = int;
@@ -113,6 +134,14 @@ inline ErrorCode lastSystemError() noexcept
113134

114135
#if MSRV_OS_WINDOWS
115136

137+
#define MSRV_THREAD_NAME__(s) L ## s
138+
#define MSRV_THREAD_NAME_(s) MSRV_THREAD_NAME__(s)
139+
#define MSRV_THREAD_NAME(s) MSRV_THREAD_NAME_(MSRV_PROJECT_ID "-" s)
140+
141+
typedef const wchar_t* ThreadName;
142+
143+
void setThreadName(ThreadName name);
144+
116145
struct WindowsHandleTraits
117146
{
118147
typedef void* Type;

cpp/server/system_windows.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@
44

55
namespace msrv {
66

7+
typedef HRESULT (WINAPI *SetThreadDescriptionFunc)(HANDLE thread, LPCWSTR name);
8+
9+
void setThreadName(ThreadName name)
10+
{
11+
static auto setName = reinterpret_cast<SetThreadDescriptionFunc>(
12+
GetProcAddress(LoadLibraryA("kernelbase.dll"), "SetThreadDescription"));
13+
14+
if (setName)
15+
setName(GetCurrentThread(), name);
16+
}
17+
718
const char* formatError(ErrorCode errorCode, char* buffer, size_t size) noexcept
819
{
920
auto ret = ::FormatMessageA(

cpp/server/work_queue.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
#include "work_queue.hpp"
22
#include "log.hpp"
3+
#include "system.hpp"
34

45
#include <algorithm>
56

67
namespace msrv {
78

89
WorkQueue::~WorkQueue() = default;
910

10-
ThreadWorkQueue::ThreadWorkQueue()
11+
ThreadWorkQueue::ThreadWorkQueue(ThreadName name)
1112
{
12-
thread_ = std::thread([this] { run(); });
13+
thread_ = std::thread([this, name] {
14+
if (name)
15+
setThreadName(name);
16+
17+
run();
18+
});
1319
}
1420

1521
ThreadWorkQueue::~ThreadWorkQueue()
@@ -56,15 +62,21 @@ void ThreadWorkQueue::run()
5662
}
5763
}
5864

59-
ThreadPoolWorkQueue::ThreadPoolWorkQueue(size_t workers)
65+
ThreadPoolWorkQueue::ThreadPoolWorkQueue(size_t workers, ThreadName name)
6066
{
6167
assert(workers > 0);
6268

6369
threads_.reserve(workers);
6470

6571
for (size_t i = 0; i < workers; i++)
6672
{
67-
threads_.emplace_back([this] { run(); });
73+
threads_.emplace_back([this, name]
74+
{
75+
if (name)
76+
setThreadName(name);
77+
78+
run();
79+
});
6880
}
6981
}
7082

cpp/server/work_queue.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include "defines.hpp"
4+
#include "system.hpp"
45

56
#include <thread>
67
#include <mutex>
@@ -29,7 +30,7 @@ class WorkQueue
2930
class ThreadWorkQueue : public WorkQueue
3031
{
3132
public:
32-
ThreadWorkQueue();
33+
explicit ThreadWorkQueue(ThreadName name = nullptr);
3334
~ThreadWorkQueue();
3435

3536
void enqueue(WorkCallback callback) override;
@@ -48,7 +49,7 @@ class ThreadWorkQueue : public WorkQueue
4849
class ThreadPoolWorkQueue : public WorkQueue
4950
{
5051
public:
51-
explicit ThreadPoolWorkQueue(size_t workers);
52+
explicit ThreadPoolWorkQueue(size_t workers, ThreadName name = nullptr);
5253
~ThreadPoolWorkQueue();
5354

5455
void enqueue(WorkCallback callback) override;

0 commit comments

Comments
 (0)