diff --git a/mingw-w64-libraries/winpthreads/src/misc.c b/mingw-w64-libraries/winpthreads/src/misc.c index b56e8a1b8..c426f59cb 100644 --- a/mingw-w64-libraries/winpthreads/src/misc.c +++ b/mingw-w64-libraries/winpthreads/src/misc.c @@ -26,6 +26,7 @@ void (WINAPI *_pthread_get_system_time_best_as_file_time) (LPFILETIME) = NULL; static ULONGLONG (WINAPI *_pthread_get_tick_count_64) (VOID); +HRESULT (WINAPI *_pthread_set_thread_description) (HANDLE, PCWSTR) = NULL; #if defined(__GNUC__) || defined(__clang__) __attribute__((constructor(0))) @@ -46,6 +47,13 @@ static void winpthreads_init(void) if (!_pthread_get_system_time_best_as_file_time) /* >15ms precision on Windows 10 */ _pthread_get_system_time_best_as_file_time = GetSystemTimeAsFileTime; + + mod = GetModuleHandleA("kernelbase.dll"); + if (mod) + { + _pthread_set_thread_description = + (HRESULT (WINAPI *)(HANDLE, PCWSTR))(void*) GetProcAddress(mod, "SetThreadDescription"); + } } #if defined(_MSC_VER) && !defined(__clang__) diff --git a/mingw-w64-libraries/winpthreads/src/misc.h b/mingw-w64-libraries/winpthreads/src/misc.h index edefb0d54..a853dd085 100644 --- a/mingw-w64-libraries/winpthreads/src/misc.h +++ b/mingw-w64-libraries/winpthreads/src/misc.h @@ -108,6 +108,7 @@ unsigned long _pthread_wait_for_single_object (void *handle, unsigned long timeo unsigned long _pthread_wait_for_multiple_objects (unsigned long count, void **handles, unsigned int all, unsigned long timeout); extern void (WINAPI *_pthread_get_system_time_best_as_file_time) (LPFILETIME); +extern HRESULT (WINAPI *_pthread_set_thread_description) (HANDLE, PCWSTR); #if defined(__GNUC__) || defined(__clang__) #define likely(cond) __builtin_expect((cond) != 0, 1) diff --git a/mingw-w64-libraries/winpthreads/src/thread.c b/mingw-w64-libraries/winpthreads/src/thread.c index 3fc5cedf1..68858cfa6 100644 --- a/mingw-w64-libraries/winpthreads/src/thread.c +++ b/mingw-w64-libraries/winpthreads/src/thread.c @@ -1877,6 +1877,21 @@ pthread_setname_np (pthread_t thread, const char *name) tv->thread_name = stored_name; SetThreadName (tv->tid, name); + + if (_pthread_set_thread_description != NULL) + { + size_t required_size = mbstowcs(NULL, name, 0); + if (required_size != (size_t)-1) + { + wchar_t *wname = malloc((required_size + 1) * sizeof(wchar_t)); + if (wname != NULL) + { + mbstowcs(wname, name, required_size + 1); + _pthread_set_thread_description(tv->h, wname); + free(wname); + } + } + } return 0; }