Skip to content

Commit f735c96

Browse files
committed
Add thread names on linux
Truncate the name of threads from the front instead to avoid allocating a new string and the end of the name tends to be more unique when creating multiple threads.
1 parent 0bb6c10 commit f735c96

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/queue.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6265,10 +6265,18 @@ _dispatch_worker_thread(void *context)
62656265

62666266
#if HAVE_PTHREAD_SETNAME_NP
62676267
// pthread thread names are restricted to just 16 characters
6268-
// including NUL. It does not make sense to pass the queue's
6269-
// label as a name.
6270-
pthread_setname_np(pthread_self(), "DispatchWorker");
6271-
#endif
6268+
// including NUL. Truncate the label name from the beginning as it tends
6269+
// to be more unique at the end.
6270+
size_t label_length = strlen(dq->dq_label);
6271+
char * thread_name = dq->dq_label;
6272+
if (label_length > 0) {
6273+
const size_t max_thread_name_length = 16 - 1; // minus the NUL byte;
6274+
thread_name = thread_name + (label_length - max_thread_name_length);
6275+
} else {
6276+
thread_name = "DispatchWorker";
6277+
}
6278+
pthread_setname_np(pthread_self(), thread_name);
6279+
#endif // HAVE_PTHREAD_SETNAME_NP
62726280

62736281
errno = 0;
62746282
int rc = setpriority(PRIO_PROCESS, 0, nice);

0 commit comments

Comments
 (0)