From 25c9b6ed488b1446cbdd38186992957264596314 Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Wed, 24 Nov 2010 15:32:23 +0100 Subject: QThread: fix a race condition when destroying or restarting thread from finished() Since we do not keep the mutex locked in QThreadPrivate::finish, We could have races if the thread is destroyed or restarted from another thread while we are still in that function This solve tst_QCoreApplication::deliverInDefinedOrder on mac Regression since a43583e0221311b7fe666726a Reviewed-by: Brad --- tests/auto/qthread/tst_qthread.cpp | 46 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'tests/auto/qthread') diff --git a/tests/auto/qthread/tst_qthread.cpp b/tests/auto/qthread/tst_qthread.cpp index 1e14b6a03a..01f080fcff 100644 --- a/tests/auto/qthread/tst_qthread.cpp +++ b/tests/auto/qthread/tst_qthread.cpp @@ -109,6 +109,8 @@ private slots: void connectThreadFinishedSignalToObjectDeleteLaterSlot(); void wait2(); void wait3_slowDestructor(); + void destroyFinishRace(); + void startFinishRace(); void stressTest(); }; @@ -1067,5 +1069,49 @@ void tst_QThread::wait3_slowDestructor() QVERIFY(thread.wait(one_minute)); } +void tst_QThread::destroyFinishRace() +{ + class Thread : public QThread { void run() {} }; + for (int i = 0; i < 15; i++) { + Thread *thr = new Thread; + connect(thr, SIGNAL(finished()), thr, SLOT(deleteLater())); + QWeakPointer weak(static_cast(thr)); + thr->start(); + while (weak) { + qApp->processEvents(); + qApp->processEvents(); + qApp->processEvents(); + qApp->processEvents(); + } + } +} + +void tst_QThread::startFinishRace() +{ + class Thread : public QThread { + public: + Thread() : i (50) {} + void run() { + i--; + if (!i) disconnect(this, SIGNAL(finished()), 0, 0); + } + int i; + }; + for (int i = 0; i < 15; i++) { + Thread thr; + connect(&thr, SIGNAL(finished()), &thr, SLOT(start())); + thr.start(); + while (!thr.isFinished() || thr.i != 0) { + qApp->processEvents(); + qApp->processEvents(); + qApp->processEvents(); + qApp->processEvents(); + } + QCOMPARE(thr.i, 0); + } +} + + + QTEST_MAIN(tst_QThread) #include "tst_qthread.moc" -- cgit v1.2.3