From bc381dd05e654d106b6cb4d7d94b27b9afe2bf30 Mon Sep 17 00:00:00 2001 From: James Ross-Gowan Date: Wed, 17 May 2017 23:33:35 +1000 Subject: win32: pthread: use the new thread naming API Windows, as of the Creators Update, finally has a sane API for giving a name to a thread that can be used by debuggers. This is similar to pthread_setname_np on Linux and some Unixes. Expose it in the pthread wrapper and use it for mpthread_set_name(). --- osdep/threads.c | 2 +- osdep/win32/include/pthread.h | 3 +++ osdep/win32/pthread.c | 23 +++++++++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) (limited to 'osdep') diff --git a/osdep/threads.c b/osdep/threads.c index e4bd0b7758..53d332396d 100644 --- a/osdep/threads.c +++ b/osdep/threads.c @@ -47,7 +47,7 @@ void mpthread_set_name(const char *name) tname[15] = '\0'; // glibc-checked kernel limit pthread_setname_np(pthread_self(), tname); } -#elif HAVE_BSD_THREAD_NAME +#elif HAVE_WIN32_INTERNAL_PTHREADS || HAVE_BSD_THREAD_NAME pthread_set_name_np(pthread_self(), tname); #elif HAVE_NETBSD_THREAD_NAME pthread_setname_np(pthread_self(), "%s", (void *)tname); diff --git a/osdep/win32/include/pthread.h b/osdep/win32/include/pthread.h index 870e9d7d5c..271cae0f0a 100644 --- a/osdep/win32/include/pthread.h +++ b/osdep/win32/include/pthread.h @@ -33,6 +33,7 @@ #define pthread_join m_pthread_join #define pthread_detach m_pthread_detach #define pthread_create m_pthread_create +#define pthread_set_name_np m_pthread_set_name_np #define pthread_once_t INIT_ONCE #define PTHREAD_ONCE_INIT INIT_ONCE_STATIC_INIT @@ -97,4 +98,6 @@ int pthread_detach(pthread_t thread); int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg); +void pthread_set_name_np(pthread_t thread, const char *name); + #endif diff --git a/osdep/win32/pthread.c b/osdep/win32/pthread.c index 4f42cc3cd4..57a9d85810 100644 --- a/osdep/win32/pthread.c +++ b/osdep/win32/pthread.c @@ -196,6 +196,29 @@ int pthread_create(pthread_t *thread, const pthread_attr_t *attr, return 0; } +void pthread_set_name_np(pthread_t thread, const char *name) +{ + HMODULE kernel32 = GetModuleHandleW(L"kernel32.dll"); + if (!kernel32) + return; + HRESULT (WINAPI *pSetThreadDescription)(HANDLE, PCWSTR) = + (void*)GetProcAddress(kernel32, "SetThreadDescription"); + if (!pSetThreadDescription) + return; + + HANDLE th = OpenThread(THREAD_SET_LIMITED_INFORMATION, FALSE, thread.id); + if (!th) + return; + wchar_t wname[80]; + int wc = MultiByteToWideChar(CP_UTF8, 0, name, -1, wname, + sizeof(wname) / sizeof(wchar_t) - 1); + if (wc > 0) { + wname[wc] = L'\0'; + pSetThreadDescription(th, wname); + } + CloseHandle(th); +} + int sem_init(sem_t *sem, int pshared, unsigned int value) { if (pshared) -- cgit v1.2.3