diff options
author | Thiago Macieira <thiago.macieira@intel.com> | 2012-08-15 18:03:50 +0200 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-08-15 23:40:57 +0200 |
commit | 3ef51efbe75bfb9f1dfbe7df073e9eb745a72ad8 (patch) | |
tree | 0778012c6ea10341eba46b0f30b22b95b73fa207 /src | |
parent | 03b95247a1bef127e3de7a00778f52a37009edf0 (diff) |
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>
Diffstat (limited to 'src')
-rw-r--r-- | src/corelib/thread/qthread_unix.cpp | 28 |
1 files changed, 18 insertions, 10 deletions
diff --git a/src/corelib/thread/qthread_unix.cpp b/src/corelib/thread/qthread_unix.cpp index 0250ea1053..47d6f48ed9 100644 --- a/src/corelib/thread/qthread_unix.cpp +++ b/src/corelib/thread/qthread_unix.cpp @@ -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(); |