From b1fe198d8743e9581e8074d0cdc404a96fa8c077 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Wed, 31 Jan 2018 19:08:06 +0100 Subject: [PATCH] Simplify how we set thread name for UNIX threads Passing on the thread ID is confusing, as it's not really what the function does. The QNX code path can resolve the thread ID by itself. Change-Id: I5f0d54621058576cdcf3707d36a11762fe2383c8 Reviewed-by: Thiago Macieira --- src/corelib/thread/qthread_unix.cpp | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/src/corelib/thread/qthread_unix.cpp b/src/corelib/thread/qthread_unix.cpp index fb5c9fd770..1bb8e613e0 100644 --- a/src/corelib/thread/qthread_unix.cpp +++ b/src/corelib/thread/qthread_unix.cpp @@ -311,16 +311,14 @@ void QThreadPrivate::createEventDispatcher(QThreadData *data) #ifndef QT_NO_THREAD #if (defined(Q_OS_LINUX) || defined(Q_OS_MAC) || defined(Q_OS_QNX)) -static void setCurrentThreadName(pthread_t threadId, const char *name) +static void setCurrentThreadName(const char *name) { # if defined(Q_OS_LINUX) && !defined(QT_LINUXBASE) - Q_UNUSED(threadId); prctl(PR_SET_NAME, (unsigned long)name, 0, 0, 0); # elif defined(Q_OS_MAC) - Q_UNUSED(threadId); pthread_setname_np(name); # elif defined(Q_OS_QNX) - pthread_setname_np(threadId, name); + pthread_setname_np(pthread_self(), name); # endif } #endif @@ -361,14 +359,13 @@ void *QThreadPrivate::start(void *arg) #if (defined(Q_OS_LINUX) || defined(Q_OS_MAC) || defined(Q_OS_QNX)) { - // sets the name of the current thread. - QString objectName = thr->objectName(); - - pthread_t thread_id = from_HANDLE(data->threadId.load()); - if (Q_LIKELY(objectName.isEmpty())) - setCurrentThreadName(thread_id, thr->metaObject()->className()); + // Sets the name of the current thread. We can only do this + // when the thread is starting, as we don't have a cross + // platform way of setting the name of an arbitrary thread. + if (Q_LIKELY(thr->objectName().isEmpty())) + setCurrentThreadName(thr->metaObject()->className()); else - setCurrentThreadName(thread_id, objectName.toLocal8Bit()); + setCurrentThreadName(thr->objectName().toLocal8Bit()); } #endif