|
28 | 28 | #include <sys/resource.h>
|
29 | 29 | #endif
|
30 | 30 |
|
| 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; |
| 37 | + } |
| 38 | + |
| 39 | + wchar_t* wideCharStr = (wchar_t*)malloc(wideCharSize * sizeof(wchar_t)); |
| 40 | + if (wideCharStr == NULL) { |
| 41 | + return NULL; |
| 42 | + } |
| 43 | + |
| 44 | + int result = MultiByteToWideChar(CP_UTF8, 0, str, -1, wideCharStr, wideCharSize); |
| 45 | + if (result == 0) { |
| 46 | + free(wideCharStr); |
| 47 | + return NULL; |
| 48 | + } |
| 49 | + |
| 50 | + return wideCharStr; |
| 51 | +} |
| 52 | +#endif |
| 53 | + |
31 | 54 | static inline void _dispatch_root_queues_init(void);
|
32 | 55 | static void _dispatch_lane_barrier_complete(dispatch_lane_class_t dqu,
|
33 | 56 | dispatch_qos_t qos, dispatch_wakeup_flags_t flags);
|
@@ -6237,21 +6260,47 @@ _dispatch_worker_thread(void *context)
|
6237 | 6260 | // per‐thread attribute: different threads in the same process can have
|
6238 | 6261 | // different nice values. We can thus setup the thread's initial priority
|
6239 | 6262 | // by converting the QoS class and relative priority to a 'nice' value.
|
6240 |
| - #if defined(__linux__) |
| 6263 | +#if defined(__linux__) |
6241 | 6264 | pp = _dispatch_priority_to_pp_strip_flags(pri);
|
6242 | 6265 | int nice = _dispatch_pp_to_nice(pp);
|
6243 | 6266 |
|
6244 | 6267 | #if HAVE_PTHREAD_SETNAME_NP
|
| 6268 | + // pthread thread names are restricted to just 16 characters |
| 6269 | + // including NUL. It does not make sense to pass the queue's |
| 6270 | + // label as a name. |
6245 | 6271 | pthread_setname_np(pthread_self(), "DispatchWorker");
|
6246 | 6272 | #endif
|
6247 | 6273 |
|
6248 | 6274 | errno = 0;
|
6249 | 6275 | int rc = setpriority(PRIO_PROCESS, 0, nice);
|
6250 | 6276 | if (rc != -1 || errno == 0) {
|
6251 | 6277 | _dispatch_thread_setspecific(dispatch_priority_key, (void *)(uintptr_t)pp);
|
| 6278 | + } else { |
| 6279 | + _dispatch_log("Failed to set thread priority for worker thread: pqc=%p errno=%d\n", pqc, errno); |
6252 | 6280 | }
|
| 6281 | +#elif defined(_WIN32) |
| 6282 | + pp = _dispatch_priority_to_pp_strip_flags(pri); |
| 6283 | + int win_priority = _dispatch_pp_to_win32_priority(pp); |
| 6284 | + |
| 6285 | + HANDLE current = GetCurrentThread(); |
6253 | 6286 |
|
6254 |
| - #endif // defined(__linux__) |
| 6287 | + // Set thread description to the label of the root queue |
| 6288 | + 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 | + } |
| 6294 | + } |
| 6295 | + |
| 6296 | + int rc = SetThreadPriority(current, win_priority); |
| 6297 | + if (rc) { |
| 6298 | + _dispatch_thread_setspecific(dispatch_priority_key, (void *)(uintptr_t)pp); |
| 6299 | + } else { |
| 6300 | + DWORD dwError = GetLastError(); |
| 6301 | + _dispatch_log("Failed to set thread priority for worker thread: pqc=%p win_priority=%d dwError=%lu\n", pqc, win_priority, dwError); |
| 6302 | + } |
| 6303 | +#endif |
6255 | 6304 |
|
6256 | 6305 | const int64_t timeout = 5ull * NSEC_PER_SEC;
|
6257 | 6306 |
|
@@ -6292,6 +6341,14 @@ _dispatch_worker_thread(void *context)
|
6292 | 6341 | (void)os_atomic_inc2o(dq, dgq_thread_pool_size, release);
|
6293 | 6342 | _dispatch_root_queue_poke(dq, 1, 0);
|
6294 | 6343 | _dispatch_release(dq); // retained in _dispatch_root_queue_poke_slow
|
| 6344 | + |
| 6345 | +#if defined(_WIN32) |
| 6346 | + // Make sure to properly end the background processing mode |
| 6347 | + if (win_priority == THREAD_MODE_BACKGROUND_BEGIN) { |
| 6348 | + SetThreadPriority(current, THREAD_MODE_BACKGROUND_END); |
| 6349 | + } |
| 6350 | +#endif |
| 6351 | + |
6295 | 6352 | return NULL;
|
6296 | 6353 | }
|
6297 | 6354 | #if defined(_WIN32)
|
|
0 commit comments