-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTRIMCORE_ThreadName.h
63 lines (54 loc) · 2.02 KB
/
TRIMCORE_ThreadName.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#ifndef TRIMCORE_DLL_THREADNAME_H
#define TRIMCORE_DLL_THREADNAME_H
namespace TRIMCORE {
namespace Implementation {
TRIMCORE_DLL_IMPORT bool SetThreadNameImpl (HANDLE h, const wchar_t * name) noexcept;
TRIMCORE_DLL_IMPORT std::size_t GetThreadNameImpl (HANDLE h, wchar_t * buffer, std::size_t length) noexcept;
}
// SetThreadName
// -
//
inline bool SetThreadName (HANDLE h, const wchar_t * name) noexcept {
return Implementation::SetThreadNameImpl (h, name);
}
// SetThreadName
// - sets current thread description
//
inline bool SetThreadName (const wchar_t * name) noexcept {
return Implementation::SetThreadNameImpl (GetCurrentThread (), name);
}
// SetThreadName
// - opens thread handle and attempts to set it's description
//
inline bool SetThreadName (DWORD id, const wchar_t * name) noexcept {
bool r = false;
if (auto h = OpenThread (THREAD_SET_LIMITED_INFORMATION, FALSE, id)) {
r = Implementation::SetThreadNameImpl (h, name);
CloseHandle (h);
}
return r;
}
// GetThreadName
// - retrieves thread description either from Windows or internal map
// - does NUL-terminate ONLY if there is extra space for the character in the buffer
//
inline std::size_t GetThreadName (HANDLE h, wchar_t * buffer, std::size_t length) noexcept {
return Implementation::GetThreadNameImpl (h, buffer, length);
}
inline std::wstring GetThreadName (HANDLE h) {
Temporary64kB <wchar_t> buffer;
return std::wstring (buffer.data (), GetThreadName (h, buffer.data (), buffer.size ()));
}
inline std::wstring GetThreadName (DWORD id) {
std::wstring s;
if (auto h = OpenThread (THREAD_QUERY_LIMITED_INFORMATION, FALSE, id)) {
s = GetThreadName (h);
CloseHandle (h);
}
return s;
}
inline std::wstring GetThreadName () {
return GetThreadName (GetCurrentThread ());
}
}
#endif