From 1f4fed5cc3be0737305e342f753c42716d6bf432 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kacper=20Michaj=C5=82ow?= Date: Tue, 4 Mar 2025 13:49:56 +0100 Subject: [PATCH] w32pthreads: add support for setting thread name MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Kacper Michajłow --- compat/w32pthreads.h | 35 +++++++++++++++++++++++++++++++++++ libavutil/thread.h | 2 ++ 2 files changed, 37 insertions(+) diff --git a/compat/w32pthreads.h b/compat/w32pthreads.h index fd6428e29f..6f2734b470 100644 --- a/compat/w32pthreads.h +++ b/compat/w32pthreads.h @@ -44,6 +44,7 @@ #include "libavutil/internal.h" #include "libavutil/mem.h" #include "libavutil/time.h" +#include "libavutil/wchar_filename.h" typedef struct pthread_t { void *handle; @@ -209,4 +210,38 @@ static inline int pthread_setcancelstate(int state, int *oldstate) return 0; } +static inline int win32_thread_setname(const char *name) +{ +#if !HAVE_UWP + typedef HRESULT (WINAPI *SetThreadDescriptionFn)(HANDLE, PCWSTR); + + // Although SetThreadDescription lives in kernel32.dll, on Windows Server 2016, + // Windows 10 LTSB 2016 and Windows 10 version 1607, it was only available in + // kernelbase.dll. So, load it from there for maximum coverage. + HMODULE kernelbase = GetModuleHandleW(L"kernelbase.dll"); + if (!kernelbase) + return AVERROR(ENOSYS); + + SetThreadDescriptionFn pSetThreadDescription = + (SetThreadDescriptionFn)GetProcAddress(kernelbase, "SetThreadDescription"); + if (!pSetThreadDescription) + return AVERROR(ENOSYS); + + wchar_t *wname; + if (utf8towchar(name, &wname) < 0) + return AVERROR(ENOMEM); + + HRESULT hr = pSetThreadDescription(GetCurrentThread(), wname); + av_free(wname); + return SUCCEEDED(hr) ? 0 : AVERROR(EINVAL); +#else + // UWP is not supported because we cannot use LoadLibrary/GetProcAddress to + // detect the availability of the SetThreadDescription API. There is a small + // gap in Windows builds 1507-1607 where it was not available. UWP allows + // querying the availability of APIs with QueryOptionalDelayLoadedAPI, but it + // requires /DELAYLOAD:kernel32.dll during linking, and we cannot enforce that. + return AVERROR(ENOSYS); +#endif +} + #endif /* COMPAT_W32PTHREADS_H */ diff --git a/libavutil/thread.h b/libavutil/thread.h index 2c00c7cc35..184e2d8c5f 100644 --- a/libavutil/thread.h +++ b/libavutil/thread.h @@ -229,6 +229,8 @@ static inline int ff_thread_setname(const char *name) #endif #elif HAVE_PTHREAD_SET_NAME_NP pthread_set_name_np(pthread_self(), name); +#elif HAVE_W32THREADS + ret = win32_thread_setname(name); #else ret = AVERROR(ENOSYS); #endif