Display QThread name for debugging.
authorLeonard Lee <leonard.lee@nokia.com>
Fri, 3 Feb 2012 12:41:47 +0000 (13:41 +0100)
committerQt by Nokia <qt-info@nokia.com>
Fri, 10 Feb 2012 15:16:00 +0000 (16:16 +0100)
Thanks to João's help.

Change-Id: I9ad3035f016945bed9fdf425fc7b7edd5d20822d
Reviewed-by: João Abecasis <joao.abecasis@nokia.com>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
src/corelib/thread/qthread_unix.cpp

index 9ce951c..d458ee9 100644 (file)
 # endif
 #endif
 
+#if defined(Q_OS_LINUX)
+#include <sys/prctl.h>
+#endif
+
 #if defined(Q_OS_LINUX) && !defined(SCHED_IDLE)
 // from linux/sched.h
 # define SCHED_IDLE    5
@@ -283,6 +287,20 @@ void *QThreadPrivate::start(void *arg)
     else
         createEventDispatcher(data);
 
+#if !defined(QT_NO_DEBUG)  && (defined(Q_OS_LINUX) || defined(Q_OS_MAC))
+    // sets the name of the current thread.
+    QByteArray objectName = thr->objectName().toLocal8Bit();
+
+    if (objectName.isEmpty())
+        objectName = thr->metaObject()->className();
+
+#if defined(Q_OS_LINUX)
+    prctl(PR_SET_NAME, (unsigned long)objectName.constData(), 0, 0, 0);
+#elif defined(Q_OS_MAC)
+    pthread_setname_np(objectName.constData());
+#endif
+#endif
+
     emit thr->started();
     pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
     pthread_testcancel();