Avoid an expensive call to toLocal8Bit upon thread creation

QString::toLocal8Bit() will need to call QTextCodec::codecForLocale(),
which isn't the cheapest of the functions, at least the first time it's
run. So avoid calling it when in most scenarios, the name of the QObject
isn't set, and the information is purely for debugging.

Additionally, avoid allocating memory when setting the thread name to
the class name. The class name coming from the meta object is a static
constant string and we can use it directly.

Change-Id: Ief643bad87a51487b1d41c0a2f323e80bb53e8a7
Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
Reviewed-by: Simon Hausmann <simon.hausmann@nokia.com>
This commit is contained in:
Thiago Macieira 2012-08-15 18:03:50 +02:00 committed by Qt by Nokia
parent 03b95247a1
commit 3ef51efbe7

View File

@ -270,6 +270,19 @@ 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(const char *name)
{
# if defined(Q_OS_LINUX) && !defined(QT_LINUXBASE)
prctl(PR_SET_NAME, (unsigned long)name, 0, 0, 0);
# elif defined(Q_OS_MAC)
pthread_setname_np(name);
# elif defined(Q_OS_QNX)
pthread_setname_np(thr->d_func()->thread_id, name);
# endif
}
#endif
void *QThreadPrivate::start(void *arg)
{
#if !defined(Q_OS_LINUX_ANDROID)
@ -301,18 +314,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.
QByteArray objectName = thr->objectName().toLocal8Bit();
QString objectName = thr->objectName();
if (objectName.isEmpty())
objectName = thr->metaObject()->className();
if (Q_LIKELY(objectName.isEmpty()))
setCurrentThreadName(thr->metaObject()->className());
else
setCurrentThreadName(objectName.toLocal8Bit());
#if defined(Q_OS_LINUX) && !defined(QT_LINUXBASE)
prctl(PR_SET_NAME, (unsigned long)objectName.constData(), 0, 0, 0);
#elif defined(Q_OS_MAC)
pthread_setname_np(objectName.constData());
#elif defined(Q_OS_QNX)
pthread_setname_np(thr->d_func()->thread_id, objectName.constData());
#endif
#endif
emit thr->started();