MCPcopy Create free account
hub / github.com/deathkiller/jazz2-native / SetThreadName

Function SetThreadName

Sources/nCine/Threading/Thread.cpp:107–143  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

105 constexpr std::uint32_t MaxThreadNameLength = 256;
106
107 void SetThreadName(HANDLE handle, const char* name)
108 {
109 using _SetThreadDescription = HRESULT(WINAPI*)(HANDLE hThread, PCWSTR lpThreadDescription);
110
111 static auto setThreadDescription = reinterpret_cast<_SetThreadDescription>(::GetProcAddress(::GetModuleHandle(L"kernel32.dll"), "SetThreadDescription"));
112 if (setThreadDescription != nullptr) {
113 wchar_t buffer[MaxThreadNameLength];
114 Death::Utf8::ToUtf16(buffer, name);
115 const HANDLE threadHandle = (handle != reinterpret_cast<HANDLE>(-1)) ? handle : ::GetCurrentThread();
116 setThreadDescription(threadHandle, buffer);
117 return;
118 }
119
120# if !defined(DEATH_TARGET_MINGW)
121 constexpr DWORD MS_VC_EXCEPTION = 0x406D1388;
122
123# pragma pack(push, 8)
124 struct THREADNAME_INFO {
125 DWORD dwType;
126 LPCSTR szName;
127 DWORD dwThreadID;
128 DWORD dwFlags;
129 };
130# pragma pack(pop)
131
132 THREADNAME_INFO info;
133 info.dwType = 0x1000;
134 info.szName = name;
135 info.dwThreadID = (handle == reinterpret_cast<HANDLE>(-1) ? ::GetCurrentThreadId() : ::GetThreadId(handle));
136 info.dwFlags = 0;
137
138 __try {
139 ::RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR), reinterpret_cast<ULONG_PTR*>(&info));
140 } __except (EXCEPTION_EXECUTE_HANDLER) {
141 }
142# endif
143 }
144#else
145 const std::uint32_t MaxThreadNameLength = 16;
146#endif

Callers 2

SetNameMethod · 0.70
SetCurrentNameMethod · 0.70

Calls 1

ToUtf16Function · 0.50

Tested by

no test coverage detected