Module: vkd3d Branch: master Commit: 963ea98a52148cbfa1a05e74cb42c61ea12c8573 URL: https://gitlab.winehq.org/wine/vkd3d/-/commit/963ea98a52148cbfa1a05e74cb42c6...
Author: Brendan Shanks bshanks@codeweavers.com Date: Thu Sep 15 12:07:40 2022 -0700
vkd3d-common: Add a Windows implementation of vkd3d_set_thread_name().
---
Makefile.am | 1 + include/private/vkd3d_debug.h | 1 + libs/vkd3d-common/debug.c | 55 +++++++++++++++++++++++++++++++++++++++++++ libs/vkd3d/vkd3d_private.h | 9 ------- 4 files changed, 57 insertions(+), 9 deletions(-)
diff --git a/Makefile.am b/Makefile.am index 1340be10..3787389b 100644 --- a/Makefile.am +++ b/Makefile.am @@ -179,6 +179,7 @@ libvkd3d_common_la_SOURCES = \ libs/vkd3d-common/error.c \ libs/vkd3d-common/memory.c \ libs/vkd3d-common/utf8.c +libvkd3d_common_la_LIBADD = @PTHREAD_LIBS@
lib_LTLIBRARIES = libvkd3d-shader.la libvkd3d.la libvkd3d-utils.la
diff --git a/include/private/vkd3d_debug.h b/include/private/vkd3d_debug.h index 579c33d3..4f6d43af 100644 --- a/include/private/vkd3d_debug.h +++ b/include/private/vkd3d_debug.h @@ -115,5 +115,6 @@ struct vkd3d_debug_option bool vkd3d_debug_list_has_member(const char *string, const char *member); uint64_t vkd3d_parse_debug_options(const char *string, const struct vkd3d_debug_option *options, unsigned int option_count); +void vkd3d_set_thread_name(const char *name);
#endif /* __VKD3D_DEBUG_H */ diff --git a/libs/vkd3d-common/debug.c b/libs/vkd3d-common/debug.c index 0b9e765a..e07ed366 100644 --- a/libs/vkd3d-common/debug.c +++ b/libs/vkd3d-common/debug.c @@ -16,6 +16,10 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA */
+#ifdef _WIN32 +# define _WIN32_WINNT 0x0600 /* For InitOnceExecuteOnce(). */ +#endif + #include "vkd3d_debug.h"
#include <assert.h> @@ -27,6 +31,11 @@ #include <stdlib.h> #include <stdbool.h> #include <string.h> +#ifdef HAVE_PTHREAD_H +#include <pthread.h> +#endif + +#include "vkd3d_memory.h"
#define VKD3D_DEBUG_BUFFER_COUNT 64 #define VKD3D_DEBUG_BUFFER_SIZE 512 @@ -369,3 +378,49 @@ uint64_t vkd3d_parse_debug_options(const char *string,
return flags; } + +#ifdef _WIN32 + +static HRESULT (WINAPI *pfn_SetThreadDescription)(HANDLE, const WCHAR *); +static BOOL WINAPI resolve_SetThreadDescription(INIT_ONCE *once, void *param, void **context) +{ + HMODULE kernelbase; + + if (!(kernelbase = LoadLibraryA("kernelbase.dll"))) + return TRUE; + + if (!(pfn_SetThreadDescription = (void *)GetProcAddress(kernelbase, "SetThreadDescription"))) + FreeLibrary(kernelbase); + + return TRUE; +} + +#endif + +void vkd3d_set_thread_name(const char *name) +{ +#ifdef _WIN32 + static INIT_ONCE init_once = INIT_ONCE_STATIC_INIT; + WCHAR *wname; + int ret; + + InitOnceExecuteOnce(&init_once, resolve_SetThreadDescription, NULL, NULL); + if (!pfn_SetThreadDescription) + return; + + if ((ret = MultiByteToWideChar(CP_UTF8, 0, name, -1, NULL, 0)) <= 0) + return; + + if (!(wname = vkd3d_malloc(ret * sizeof(*wname)))) + return; + + if ((ret = MultiByteToWideChar(CP_UTF8, 0, name, -1, wname, ret)) > 0) + pfn_SetThreadDescription(GetCurrentThread(), wname); + + vkd3d_free(wname); +#elif defined(HAVE_PTHREAD_SETNAME_NP_2) + pthread_setname_np(pthread_self(), name); +#elif defined(HAVE_PTHREAD_SETNAME_NP_1) + pthread_setname_np(name); +#endif +} diff --git a/libs/vkd3d/vkd3d_private.h b/libs/vkd3d/vkd3d_private.h index c9b70d9c..2da1ab5d 100644 --- a/libs/vkd3d/vkd3d_private.h +++ b/libs/vkd3d/vkd3d_private.h @@ -1685,15 +1685,6 @@ extern const char vkd3d_build[];
bool vkd3d_get_program_name(char program_name[PATH_MAX]);
-static inline void vkd3d_set_thread_name(const char *name) -{ -#if defined(HAVE_PTHREAD_SETNAME_NP_2) - pthread_setname_np(pthread_self(), name); -#elif defined(HAVE_PTHREAD_SETNAME_NP_1) - pthread_setname_np(name); -#endif -} - VkResult vkd3d_set_vk_object_name_utf8(struct d3d12_device *device, uint64_t vk_object, VkDebugReportObjectTypeEXT vk_object_type, const char *name); HRESULT vkd3d_set_vk_object_name(struct d3d12_device *device, uint64_t vk_object,