Skip to content

Commit cc5c5c1

Browse files
Platform Misc: add SetCurrentThreadName function
Use it to set thread pool worker thread names
1 parent 2be49b7 commit cc5c5c1

File tree

5 files changed

+43
-9
lines changed

5 files changed

+43
-9
lines changed

Common/src/ThreadPool.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ class ThreadPoolImpl final : public ObjectBase<IThreadPool>
5555
m_WorkerThreads.reserve(PoolCI.NumThreads);
5656
for (Uint32 i = 0; i < PoolCI.NumThreads; ++i)
5757
{
58+
std::string ThreadName = "Diligent::ThreadPool Worker " + std::to_string(i);
59+
PlatformMisc::SetCurrentThreadName(ThreadName.c_str());
60+
5861
m_WorkerThreads.emplace_back(
5962
[this, PoolCI, i] //
6063
{

Platforms/Basic/interface/BasicPlatformMisc.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ struct BasicPlatformMisc
111111
/// Sets the current thread affinity mask and on success returns the previous mask.
112112
static Uint64 SetCurrentThreadAffinity(Uint64 Mask);
113113

114+
/// Sets the name of the current thread.
115+
static void SetCurrentThreadName(const char* Name);
116+
114117
private:
115118
static void SwapBytes16(Uint16& Val)
116119
{

Platforms/Basic/src/BasicPlatformMisc.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2024 Diligent Graphics LLC
2+
* Copyright 2019-2025 Diligent Graphics LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -48,4 +48,9 @@ Uint64 BasicPlatformMisc::SetCurrentThreadAffinity(Uint64 Mask)
4848
return 0;
4949
}
5050

51+
void BasicPlatformMisc::SetCurrentThreadName(const char* Name)
52+
{
53+
LOG_WARNING_MESSAGE_ONCE("SetCurrentThreadName is not implemented on this platform.");
54+
}
55+
5156
} // namespace Diligent

Platforms/Win32/interface/Win32PlatformMisc.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2024 Diligent Graphics LLC
2+
* Copyright 2019-2025 Diligent Graphics LLC
33
* Copyright 2015-2019 Egor Yusov
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -173,6 +173,9 @@ struct WindowsMisc : public BasicPlatformMisc
173173
/// Sets the current thread priority and on success returns the previous priority.
174174
/// On failure, returns ThreadPriority::Unknown.
175175
static ThreadPriority SetCurrentThreadPriority(ThreadPriority Priority);
176+
177+
/// Sets the name of the current thread.
178+
static void SetCurrentThreadName(const char* Name);
176179
#endif
177180
};
178181

Platforms/Win32/src/Win32PlatformMisc.cpp

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2022 Diligent Graphics LLC
2+
* Copyright 2019-2025 Diligent Graphics LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,12 +30,16 @@
3030
#include <Windows.h>
3131
#include "WinHPostface.h"
3232

33+
#include <string>
34+
#include <sstream>
35+
#include <locale>
36+
3337
namespace Diligent
3438
{
3539

3640
Uint64 WindowsMisc::SetCurrentThreadAffinity(Uint64 Mask)
3741
{
38-
const auto hCurrThread = GetCurrentThread();
42+
const HANDLE hCurrThread = GetCurrentThread();
3943
return SetThreadAffinityMask(hCurrThread, static_cast<DWORD_PTR>(Mask));
4044
}
4145

@@ -72,20 +76,36 @@ static int ThreadPiorityToWndPriority(ThreadPriority Priority)
7276

7377
ThreadPriority WindowsMisc::GetCurrentThreadPriority()
7478
{
75-
const auto hCurrThread = GetCurrentThread();
76-
const auto WndPriority = GetThreadPriority(hCurrThread);
79+
const HANDLE hCurrThread = GetCurrentThread();
80+
const int WndPriority = GetThreadPriority(hCurrThread);
7781
return WndPriorityToThreadPiority(WndPriority);
7882
}
7983

8084
ThreadPriority WindowsMisc::SetCurrentThreadPriority(ThreadPriority Priority)
8185
{
82-
const auto hCurrThread = GetCurrentThread();
83-
const auto OrigWndPriority = GetThreadPriority(hCurrThread);
84-
const auto NewWndPriority = ThreadPiorityToWndPriority(Priority);
86+
const HANDLE hCurrThread = GetCurrentThread();
87+
const int OrigWndPriority = GetThreadPriority(hCurrThread);
88+
const int NewWndPriority = ThreadPiorityToWndPriority(Priority);
8589
if (SetThreadPriority(hCurrThread, NewWndPriority))
8690
return WndPriorityToThreadPiority(OrigWndPriority);
8791
else
8892
return ThreadPriority::Unknown;
8993
}
9094

95+
void WindowsMisc::SetCurrentThreadName(const char* Name)
96+
{
97+
if (Name == nullptr || Name[0] == '\0')
98+
return;
99+
100+
const size_t Len = strlen(Name);
101+
std::wstring wName(Len, L'\0');
102+
103+
const std::ctype<wchar_t>& ctfacet = std::use_facet<std::ctype<wchar_t>>(std::wstringstream().getloc());
104+
for (size_t i = 0; i < Len; ++i)
105+
wName[i] = ctfacet.widen(Name[i]);
106+
107+
const HANDLE hCurrThread = GetCurrentThread();
108+
SetThreadDescription(hCurrThread, wName.c_str());
109+
}
110+
91111
} // namespace Diligent

0 commit comments

Comments
 (0)