Skip to content

Commit

Permalink
Implement _dispatch_win32_set_thread_description
Browse files Browse the repository at this point in the history
This function is a wrapper around SetThreadDescription, which
accepts UTF-8 strings.
  • Loading branch information
hmelder committed Nov 18, 2024
1 parent d5afe08 commit 672d2d1
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions src/queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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(dq->dq_label);
}

int rc = SetThreadPriority(current, win_priority);
Expand Down

0 comments on commit 672d2d1

Please sign in to comment.