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().
This commit is contained in:
James Ross-Gowan 2017-05-17 23:33:35 +10:00
parent 6a00059850
commit bc381dd05e
3 changed files with 27 additions and 1 deletions

View File

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

View File

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

View File

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