From 7eab970bfad0e31b4e915110bc79267067e32376 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Tue, 30 Apr 2019 22:48:52 +0200 Subject: compat/thread-name: only call dlsym once --- compat/thread-name.cpp | 21 +++++++++++++-------- 1 file 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 -- cgit v1.2.3