diff options
author | Bradley T. Hughes <bradley.hughes@nokia.com> | 2010-04-12 12:13:35 (GMT) |
---|---|---|
committer | Bradley T. Hughes <bradley.hughes@nokia.com> | 2010-04-12 12:48:19 (GMT) |
commit | 9aa4538b219ed759a47e8d1f93c2797bf07b5e2f (patch) | |
tree | 072af7618be8bca90aba91c8b02cae2c920d74f2 | |
parent | 3ae7b648fb8531ca269bc44fffb52ba1aab75f97 (diff) | |
download | Qt-9aa4538b219ed759a47e8d1f93c2797bf07b5e2f.zip Qt-9aa4538b219ed759a47e8d1f93c2797bf07b5e2f.tar.gz Qt-9aa4538b219ed759a47e8d1f93c2797bf07b5e2f.tar.bz2 |
Fix a race where QThread::exit() is "lost" when called after start()
QThread::exit() tries to stop all running event loops, but does nothing
if the eventloop has not started yet. This is often the case for short-
exit method on an object that has affinity to the thread. This ensures
that the exit is called from the running eventloop, meaning the exit()
will never be lost.
Task-number: QTBUG-1184
Reviewed-by: Morten Sørvig
-rw-r--r-- | src/corelib/thread/qthread.cpp | 25 | ||||
-rw-r--r-- | src/corelib/thread/qthread_p.h | 10 | ||||
-rw-r--r-- | tests/auto/qthread/tst_qthread.cpp | 47 |
3 files changed, 70 insertions, 12 deletions
diff --git a/src/corelib/thread/qthread.cpp b/src/corelib/thread/qthread.cpp index cb84538..c35eb28 100644 --- a/src/corelib/thread/qthread.cpp +++ b/src/corelib/thread/qthread.cpp @@ -174,7 +174,7 @@ void QAdoptedThread::run() QThreadPrivate::QThreadPrivate(QThreadData *d) : QObjectPrivate(), running(false), finished(false), terminated(false), - stackSize(0), priority(QThread::InheritPriority), data(d) + stackSize(0), priority(QThread::InheritPriority), data(d), object(0) { #if defined (Q_OS_UNIX) thread_id = 0; @@ -377,6 +377,9 @@ QThread::QThread(QObject *parent) Q_D(QThread); // fprintf(stderr, "QThreadData %p created for thread %p\n", d->data, this); d->data->thread = this; + + d->object = new QThreadPrivateInternalObject; + d->object->moveToThread(this); } /*! \internal @@ -387,6 +390,8 @@ QThread::QThread(QThreadPrivate &dd, QObject *parent) Q_D(QThread); // fprintf(stderr, "QThreadData %p taken from private data for thread %p\n", d->data, this); d->data->thread = this; + + // do not create the internal object for adopted threads } /*! @@ -408,6 +413,9 @@ QThread::~QThread() d->data->thread = 0; } + + delete d->object; + d->object = 0; } /*! @@ -510,6 +518,21 @@ int QThread::exec() void QThread::exit(int returnCode) { Q_D(QThread); + if (d->object) { + QMetaObject::invokeMethod(d->object, "exit", Q_ARG(int, returnCode)); + } else { + QMutexLocker locker(&d->mutex); + d->data->quitNow = true; + for (int i = 0; i < d->data->eventLoops.size(); ++i) { + QEventLoop *eventLoop = d->data->eventLoops.at(i); + eventLoop->exit(returnCode); + } + } +} + +void QThreadPrivateInternalObject::exit(int returnCode) +{ + QThreadPrivate *d = static_cast<QThreadPrivate *>(QObjectPrivate::get(thread())); QMutexLocker locker(&d->mutex); d->data->quitNow = true; for (int i = 0; i < d->data->eventLoops.size(); ++i) { diff --git a/src/corelib/thread/qthread_p.h b/src/corelib/thread/qthread_p.h index 6825718..54ffd80 100644 --- a/src/corelib/thread/qthread_p.h +++ b/src/corelib/thread/qthread_p.h @@ -112,6 +112,15 @@ public: }; #ifndef QT_NO_THREAD + +class QThreadPrivateInternalObject : public QObject +{ + Q_OBJECT + +public slots: + void exit(int); +}; + class QThreadPrivate : public QObjectPrivate { Q_DECLARE_PUBLIC(QThread) @@ -156,6 +165,7 @@ public: bool terminationEnabled, terminatePending; # endif QThreadData *data; + QThreadPrivateInternalObject *object; static void createEventDispatcher(QThreadData *data); }; diff --git a/tests/auto/qthread/tst_qthread.cpp b/tests/auto/qthread/tst_qthread.cpp index bd1bc53..871578e 100644 --- a/tests/auto/qthread/tst_qthread.cpp +++ b/tests/auto/qthread/tst_qthread.cpp @@ -168,16 +168,18 @@ public slots: class Exit_Thread : public Simple_Thread { public: + Exit_Object *object; int code; int result; void run() { - Simple_Thread::run(); - Exit_Object o; - o.thread = this; - o.code = code; - QTimer::singleShot(100, &o, SLOT(slot())); + if (object) { + Simple_Thread::run(); + object->thread = this; + object->code = code; + QTimer::singleShot(100, object, SLOT(slot())); + } result = exec(); } }; @@ -211,17 +213,16 @@ public slots: class Quit_Thread : public Simple_Thread { public: + Quit_Object *object; int result; void run() { - { - QMutexLocker locker(&mutex); - cond.wakeOne(); + if (object) { + Simple_Thread::run(); + object->thread = this; + QTimer::singleShot(100, object, SLOT(slot())); } - Quit_Object o; - o.thread = this; - QTimer::singleShot(100, &o, SLOT(slot())); result = exec(); } }; @@ -420,6 +421,8 @@ void tst_QThread::stackSize() void tst_QThread::exit() { Exit_Thread thread; + thread.object = new Exit_Object; + thread.object->moveToThread(&thread); thread.code = 42; thread.result = 0; QVERIFY(!thread.isFinished()); @@ -433,6 +436,16 @@ void tst_QThread::exit() QVERIFY(thread.isFinished()); QVERIFY(!thread.isRunning()); QCOMPARE(thread.result, thread.code); + delete thread.object; + + Exit_Thread thread2; + thread2.object = 0; + thread2.code = 53; + thread2.result = 0; + thread2.start(); + thread2.exit(thread.code); + QVERIFY(thread2.wait(five_minutes)); + QCOMPARE(thread.result, thread.code); } void tst_QThread::start() @@ -480,6 +493,9 @@ void tst_QThread::terminate() void tst_QThread::quit() { Quit_Thread thread; + thread.object = new Quit_Object; + thread.object->moveToThread(&thread); + thread.result = -1; QVERIFY(!thread.isFinished()); QVERIFY(!thread.isRunning()); QMutexLocker locker(&thread.mutex); @@ -491,6 +507,15 @@ void tst_QThread::quit() QVERIFY(thread.isFinished()); QVERIFY(!thread.isRunning()); QCOMPARE(thread.result, 0); + delete thread.object; + + Quit_Thread thread2; + thread2.object = 0; + thread2.result = -1; + thread2.start(); + thread2.quit(); + QVERIFY(thread2.wait(five_minutes)); + QCOMPARE(thread.result, 0); } void tst_QThread::wait() |