Skip to content

Commit a631215

Browse files
malfetfacebook-github-bot
authored andcommitted
Call [Get|Set]ThreadDescription via runtime linking (#224)
Summary: SetThreadDescription is not available on some older runtimes/older versions of Windows, which can lead to to following linker errors: ``` kineto.lib(ThreadUtil.cpp.obj) : error LNK2019: unresolved external symbol __imp_SetThreadDescription referenced in function "bool __cdecl libkineto::setThreadName(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?setThreadName@libkineto@YA_NAEBV?$basic_string@DU?$char_traits@D@std@V?$allocator@D@2@std@@z) ``` Use runtime linking method recommended in See https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-setthreaddescription Pull Request resolved: #224 Reviewed By: ilia-cher Differential Revision: D28404596 Pulled By: malfet fbshipit-source-id: 01937821a131aeff562aed5ebf274eac74bd816e
1 parent 582aefa commit a631215

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

libkineto/src/ThreadUtil.cpp

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,26 @@ static constexpr const char* basename(const char* s, int off = 0) {
7676
? s
7777
: s[off] == '/' ? basename(&s[off + 1]) : basename(s, off + 1);
7878
}
79+
#if defined(_MSC_VER)
80+
void *getKernel32Func(const char* procName) {
81+
return GetProcAddress(GetModuleHandleA("KERNEL32.DLL"), procName);
82+
}
83+
#endif
7984
}
8085

8186
bool setThreadName(const std::string& name) {
8287
#ifdef __APPLE__
8388
return 0 == pthread_setname_np(name.c_str());
8489
#elif defined _MSC_VER
90+
// Per https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-setthreaddescription
91+
// Use runtime linking to set thread description
92+
static auto _SetThreadDescription = reinterpret_cast<decltype(&SetThreadDescription)>(getKernel32Func("SetThreadDescription"));
93+
if (!_SetThreadDescription) {
94+
return false;
95+
}
8596
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> conv;
8697
std::wstring wname = conv.from_bytes(name);
87-
HRESULT hr = SetThreadDescription(GetCurrentThread(), wname.c_str());
98+
HRESULT hr = _SetThreadDescription(GetCurrentThread(), wname.c_str());
8899
return SUCCEEDED(hr);
89100
#else
90101
return 0 == pthread_setname_np(pthread_self(), name.c_str());
@@ -105,16 +116,19 @@ std::string getThreadName() {
105116
}
106117
return buf;
107118
#else // _MSC_VER
119+
static auto _GetThreadDescription = reinterpret_cast<decltype(&GetThreadDescription)>(getKernel32Func("GetThreadDescription"));
120+
if (!_GetThreadDescription) {
121+
return "Unknown";
122+
}
108123
PWSTR data;
109-
HRESULT hr = GetThreadDescription(GetCurrentThread(), &data);
110-
if (SUCCEEDED(hr)) {
111-
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> conv;
112-
std::string name = conv.to_bytes(data);
113-
LocalFree(data);
114-
return name;
115-
} else {
124+
HRESULT hr = _GetThreadDescription(GetCurrentThread(), &data);
125+
if (!SUCCEEDED(hr)) {
116126
return "";
117127
}
128+
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> conv;
129+
std::string name = conv.to_bytes(data);
130+
LocalFree(data);
131+
return name;
118132
#endif
119133
}
120134

0 commit comments

Comments
 (0)