diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2019-04-30 22:48:52 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2019-04-30 22:48:52 +0200 |
commit | 7eab970bfad0e31b4e915110bc79267067e32376 (patch) | |
tree | c1bc21325d43d3dba33c8b63c41a7f66fc175b75 | |
parent | c5443e1b3912cd1b7091db9814cf65a429d951e7 (diff) |
compat/thread-name: only call dlsym once
-rw-r--r-- | compat/thread-name.cpp | 21 |
1 files changed, 13 insertions, 8 deletions
diff --git a/compat/thread-name.cpp b/compat/thread-name.cpp index d12a59ca..504c6f19 100644 --- a/compat/thread-name.cpp +++ b/compat/thread-name.cpp @@ -47,21 +47,26 @@ static inline void set_curthread_name_old(const QString&) {} #endif +using SetThreadDescr_type = HRESULT (__stdcall *)(HANDLE, const wchar_t*); + +static SetThreadDescr_type get_funptr() +{ + HMODULE module; + if (GetModuleHandleExA(0, "kernel32.dll", &module)) + return (SetThreadDescr_type)GetProcAddress(module, "SetThreadDescription"); + else + return nullptr; +} + void set_curthread_name(const QString& name) { static_assert(sizeof(wchar_t) == sizeof(decltype(*QString().utf16()))); + static const SetThreadDescr_type fn = get_funptr(); - HMODULE module; - HRESULT (__stdcall *fn)(HANDLE, const wchar_t*); - if (GetModuleHandleExA(0, "kernel32.dll", &module) && - (fn = (decltype(fn))GetProcAddress(module, "SetThreadDescription"))) - { + if (fn) fn(GetCurrentThread(), (const wchar_t*)name.utf16()); - } else - { set_curthread_name_old(name); - } } #else |