From 56d52e1f1a8709520b62b558121237686acf5fae Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Fri, 6 Jul 2018 13:33:11 -0700 Subject: QThread: Centralize the creation of the event dispatcher In some places we call startingUp(), in others we don't. It's probably ok for those that have just created an object of a given class, which knows whether the virtual call is necessary or not. But for the generic case, we do call it. Change-Id: If48c5c2e920c433298f1fffd153ee1cc75703204 Reviewed-by: Friedemann Kleint Reviewed-by: Thiago Macieira --- src/corelib/kernel/qcoreapplication.cpp | 2 +- src/corelib/kernel/qeventloop.cpp | 6 ++---- src/corelib/thread/qthread.cpp | 8 ++++++++ src/corelib/thread/qthread_p.h | 10 +++++++++- src/corelib/thread/qthread_unix.cpp | 8 +------- src/corelib/thread/qthread_win.cpp | 8 +------- 6 files changed, 22 insertions(+), 20 deletions(-) (limited to 'src/corelib') diff --git a/src/corelib/kernel/qcoreapplication.cpp b/src/corelib/kernel/qcoreapplication.cpp index cf5ca53dc3..aea8fe6658 100644 --- a/src/corelib/kernel/qcoreapplication.cpp +++ b/src/corelib/kernel/qcoreapplication.cpp @@ -540,7 +540,7 @@ void QCoreApplicationPrivate::createEventDispatcher() Q_Q(QCoreApplication); QThreadData *data = QThreadData::current(); Q_ASSERT(!data->hasEventDispatcher()); - eventDispatcher = QThreadPrivate::createEventDispatcher(data); + eventDispatcher = data->createEventDispatcher(); eventDispatcher->setParent(q); } diff --git a/src/corelib/kernel/qeventloop.cpp b/src/corelib/kernel/qeventloop.cpp index 6034698349..f1d32b15d1 100644 --- a/src/corelib/kernel/qeventloop.cpp +++ b/src/corelib/kernel/qeventloop.cpp @@ -101,10 +101,8 @@ QEventLoop::QEventLoop(QObject *parent) Q_D(QEventLoop); if (!QCoreApplication::instance() && QCoreApplicationPrivate::threadRequiresCoreApplication()) { qWarning("QEventLoop: Cannot be used without QApplication"); - } else if (!d->threadData->hasEventDispatcher()) { - QAbstractEventDispatcher *eventDispatcher = QThreadPrivate::createEventDispatcher(d->threadData); - d->threadData->eventDispatcher.storeRelease(eventDispatcher); - eventDispatcher->startingUp(); + } else { + d->threadData->ensureEventDispatcher(); } } diff --git a/src/corelib/thread/qthread.cpp b/src/corelib/thread/qthread.cpp index c21877485f..472c6f6795 100644 --- a/src/corelib/thread/qthread.cpp +++ b/src/corelib/thread/qthread.cpp @@ -117,6 +117,14 @@ void QThreadData::deref() #endif } +QAbstractEventDispatcher *QThreadData::createEventDispatcher() +{ + QAbstractEventDispatcher *ed = QThreadPrivate::createEventDispatcher(this); + eventDispatcher.storeRelease(ed); + ed->startingUp(); + return ed; +} + /* QAdoptedThread */ diff --git a/src/corelib/thread/qthread_p.h b/src/corelib/thread/qthread_p.h index 73736ce13b..93f245ff6e 100644 --- a/src/corelib/thread/qthread_p.h +++ b/src/corelib/thread/qthread_p.h @@ -248,7 +248,15 @@ public: void ref(); void deref(); inline bool hasEventDispatcher() const - { return eventDispatcher.load() != 0; } + { return eventDispatcher.load() != nullptr; } + QAbstractEventDispatcher *createEventDispatcher(); + QAbstractEventDispatcher *ensureEventDispatcher() + { + QAbstractEventDispatcher *ed = eventDispatcher.load(); + if (Q_LIKELY(ed)) + return ed; + return createEventDispatcher(); + } bool canWaitLocked() { diff --git a/src/corelib/thread/qthread_unix.cpp b/src/corelib/thread/qthread_unix.cpp index c5de46d07d..0b3c7ddf10 100644 --- a/src/corelib/thread/qthread_unix.cpp +++ b/src/corelib/thread/qthread_unix.cpp @@ -339,13 +339,7 @@ void *QThreadPrivate::start(void *arg) data->quitNow = thr->d_func()->exited; } - QAbstractEventDispatcher *eventDispatcher = data->eventDispatcher.load(); - if (!eventDispatcher) { - eventDispatcher = createEventDispatcher(data); - data->eventDispatcher.storeRelease(eventDispatcher); - } - - eventDispatcher->startingUp(); + data->ensureEventDispatcher(); #if (defined(Q_OS_LINUX) || defined(Q_OS_MAC) || defined(Q_OS_QNX)) { diff --git a/src/corelib/thread/qthread_win.cpp b/src/corelib/thread/qthread_win.cpp index 6c9f939b7d..83bcb7d751 100644 --- a/src/corelib/thread/qthread_win.cpp +++ b/src/corelib/thread/qthread_win.cpp @@ -360,13 +360,7 @@ unsigned int __stdcall QT_ENSURE_STACK_ALIGNED_FOR_SSE QThreadPrivate::start(voi data->quitNow = thr->d_func()->exited; } - QAbstractEventDispatcher *eventDispatcher = data->eventDispatcher.load(); - if (!eventDispatcher) { - eventDispatcher = createEventDispatcher(data); - data->eventDispatcher.storeRelease(eventDispatcher); - } - - eventDispatcher->startingUp(); + data->ensureEventDispatcher(); #if !defined(QT_NO_DEBUG) && defined(Q_CC_MSVC) && !defined(Q_OS_WINRT) // sets the name of the current thread. -- cgit v1.2.3