summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2013-05-15 08:18:05 -0600
committerThe Qt Project <gerrit-noreply@qt-project.org>2013-05-20 12:06:48 +0200
commit6c98035c99700b4cd727fbf1158e346b9a02744c (patch)
tree73628b120db8e321bb046a09ba439eef755d6fef
parent0cad334ab6c6d3f16edf85dadd0f588be6860740 (diff)
QThreadPool: Fix regression from Qt 4 in dealing with priority starts
The optimisation done in cbaf52b09971edf6f3e1458f7dd677b80a1568ed for Qt 5.0 got the order wrong of the comparison. The queue must be sorted in decreasing priority order. But since higher numbers mean higher priority, that means the queue must be sorted in decreasing priority number order. Task-number: QTBUG-29163 Change-Id: Iaf3424b9bb445bf5c71518927f37253cead454f3 Reviewed-by: Konstantin Ritt <ritt.ks@gmail.com>
-rw-r--r--src/corelib/thread/qthreadpool.cpp2
-rw-r--r--tests/auto/corelib/thread/qthreadpool/tst_qthreadpool.cpp53
2 files changed, 54 insertions, 1 deletions
diff --git a/src/corelib/thread/qthreadpool.cpp b/src/corelib/thread/qthreadpool.cpp
index 1616fb9fab..a7d52f9652 100644
--- a/src/corelib/thread/qthreadpool.cpp
+++ b/src/corelib/thread/qthreadpool.cpp
@@ -215,7 +215,7 @@ void QThreadPoolPrivate::enqueueTask(QRunnable *runnable, int priority)
// put it on the queue
QList<QPair<QRunnable *, int> >::const_iterator begin = queue.constBegin();
QList<QPair<QRunnable *, int> >::const_iterator it = queue.constEnd();
- if (it != begin && priority < (*(it - 1)).second)
+ if (it != begin && priority > (*(it - 1)).second)
it = std::upper_bound(begin, --it, priority);
queue.insert(it - begin, qMakePair(runnable, priority));
runnableReady.wakeOne();
diff --git a/tests/auto/corelib/thread/qthreadpool/tst_qthreadpool.cpp b/tests/auto/corelib/thread/qthreadpool/tst_qthreadpool.cpp
index a93a94a470..3c0e132a0a 100644
--- a/tests/auto/corelib/thread/qthreadpool/tst_qthreadpool.cpp
+++ b/tests/auto/corelib/thread/qthreadpool/tst_qthreadpool.cpp
@@ -94,6 +94,8 @@ private slots:
void tryStart();
void tryStartPeakThreadCount();
void tryStartCount();
+ void priorityStart_data();
+ void priorityStart();
void waitForDone();
void waitForDoneTimeout();
void destroyingWaitsForTasksToFinish();
@@ -747,6 +749,57 @@ void tst_QThreadPool::tryStartCount()
}
}
+void tst_QThreadPool::priorityStart_data()
+{
+ QTest::addColumn<int>("otherCount");
+ QTest::newRow("0") << 0;
+ QTest::newRow("1") << 1;
+ QTest::newRow("2") << 2;
+}
+
+void tst_QThreadPool::priorityStart()
+{
+ class Holder : public QRunnable
+ {
+ public:
+ QSemaphore &sem;
+ Holder(QSemaphore &sem) : sem(sem) {}
+ void run()
+ {
+ sem.acquire();
+ }
+ };
+ class Runner : public QRunnable
+ {
+ public:
+ QAtomicPointer<QRunnable> &ptr;
+ Runner(QAtomicPointer<QRunnable> &ptr) : ptr(ptr) {}
+ void run()
+ {
+ ptr.testAndSetRelaxed(0, this);
+ }
+ };
+
+ QFETCH(int, otherCount);
+ QSemaphore sem;
+ QAtomicPointer<QRunnable> firstStarted;
+ QRunnable *expected;
+ QThreadPool threadPool;
+ threadPool.setMaxThreadCount(1); // start only one thread at a time
+
+ // queue the holder first
+ // We need to be sure that all threads are active when we
+ // queue the two Runners
+ threadPool.start(new Holder(sem));
+ while (otherCount--)
+ threadPool.start(new Runner(firstStarted), 0); // priority 0
+ threadPool.start(expected = new Runner(firstStarted), 1); // priority 1
+
+ sem.release();
+ QVERIFY(threadPool.waitForDone());
+ QCOMPARE(firstStarted.load(), expected);
+}
+
void tst_QThreadPool::waitForDone()
{
QTime total, pass;