diff --git a/src/queue.c b/src/queue.c index 0965cfbe5..0bfb3a04a 100644 --- a/src/queue.c +++ b/src/queue.c @@ -29,25 +29,28 @@ #endif #if defined(_WIN32) -// Needs to be free'd after use -static inline wchar_t *_Nullable _dispatch_char_to_wchar_str(const char *str) { - int wideCharSize = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0); - if (wideCharSize == 0) { +// Wrapper around SetThreadDescription for UTF-8 strings +void _dispatch_win32_set_thread_description(HANDLE hThread, const char *description) { + int wcsize = MultiByteToWideChar(CP_UTF8, 0, description, -1, NULL, 0); + if (wcsize == 0) { return NULL; } - wchar_t* wideCharStr = (wchar_t*)malloc(wideCharSize * sizeof(wchar_t)); - if (wideCharStr == NULL) { + wchar_t* wcstr = (wchar_t*)malloc(wcsize * sizeof(wchar_t)); + if (wcstr == NULL) { return NULL; } - int result = MultiByteToWideChar(CP_UTF8, 0, str, -1, wideCharStr, wideCharSize); + int result = MultiByteToWideChar(CP_UTF8, 0, description, -1, wcstr, wcsize); if (result == 0) { - free(wideCharStr); - return NULL; + free(wcstr); + return NULL; } - return wideCharStr; + if (likely(wcstr != NULL)) { + SetThreadDescription(hThread, wcstr); + free(desc); + } } #endif @@ -6286,11 +6289,7 @@ _dispatch_worker_thread(void *context) // Set thread description to the label of the root queue if (dq->dq_label) { - wchar_t *desc = _dispatch_char_to_wchar_str(dq->dq_label); - if (likely(desc != NULL)) { - SetThreadDescription(current, desc); - free(desc); - } + _dispatch_win32_set_thread_description(current, dq->dq_label); } int rc = SetThreadPriority(current, win_priority);