|
29 | 29 | #endif |
30 | 30 |
|
31 | 31 | #if defined(_WIN32) |
32 | | -// Needs to be free'd after use |
33 | | -static inline wchar_t *_Nullable _dispatch_char_to_wchar_str(const char *str) { |
34 | | - int wideCharSize = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0); |
35 | | - if (wideCharSize == 0) { |
36 | | - return NULL; |
| 32 | +// Wrapper around SetThreadDescription for UTF-8 strings |
| 33 | +void _dispatch_win32_set_thread_description(HANDLE hThread, const char *description) { |
| 34 | + int wcsize = MultiByteToWideChar(CP_UTF8, 0, description, -1, NULL, 0); |
| 35 | + if (wcsize == 0) { |
| 36 | + return; |
37 | 37 | } |
38 | 38 |
|
39 | | - wchar_t* wideCharStr = (wchar_t*)malloc(wideCharSize * sizeof(wchar_t)); |
40 | | - if (wideCharStr == NULL) { |
41 | | - return NULL; |
| 39 | + wchar_t* wcstr = (wchar_t*)malloc(wcsize * sizeof(wchar_t)); |
| 40 | + if (wcstr == NULL) { |
| 41 | + return; |
42 | 42 | } |
43 | 43 |
|
44 | | - int result = MultiByteToWideChar(CP_UTF8, 0, str, -1, wideCharStr, wideCharSize); |
| 44 | + int result = MultiByteToWideChar(CP_UTF8, 0, description, -1, wcstr, wcsize); |
45 | 45 | if (result == 0) { |
46 | | - free(wideCharStr); |
47 | | - return NULL; |
| 46 | + free(wcstr); |
| 47 | + return; |
48 | 48 | } |
49 | 49 |
|
50 | | - return wideCharStr; |
| 50 | + if (likely(wcstr != NULL)) { |
| 51 | + SetThreadDescription(hThread, wcstr); |
| 52 | + free(wcstr); |
| 53 | + } |
51 | 54 | } |
52 | 55 | #endif |
53 | 56 |
|
@@ -6286,11 +6289,7 @@ _dispatch_worker_thread(void *context) |
6286 | 6289 |
|
6287 | 6290 | // Set thread description to the label of the root queue |
6288 | 6291 | if (dq->dq_label) { |
6289 | | - wchar_t *desc = _dispatch_char_to_wchar_str(dq->dq_label); |
6290 | | - if (likely(desc != NULL)) { |
6291 | | - SetThreadDescription(current, desc); |
6292 | | - free(desc); |
6293 | | - } |
| 6292 | + _dispatch_win32_set_thread_description(current, dq->dq_label); |
6294 | 6293 | } |
6295 | 6294 |
|
6296 | 6295 | int rc = SetThreadPriority(current, win_priority); |
|
0 commit comments