vkd3d-common: Add a Windows implementation of vkd3d_set_thread_name().

This commit is contained in:
Brendan Shanks 2022-09-15 12:07:40 -07:00 committed by Alexandre Julliard
parent 0ef04659c7
commit 963ea98a52
Notes: Alexandre Julliard 2022-10-25 22:38:50 +02:00
Approved-by: Henri Verbeet (@hverbeet)
Approved-by: Alexandre Julliard (@julliard)
Merge-Request: https://gitlab.winehq.org/wine/vkd3d/-/merge_requests/39
4 changed files with 57 additions and 9 deletions

View File

@ -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

View File

@ -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 */

View File

@ -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
}

View File

@ -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,