summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/corelib/thread/qthreadpool/tst_qthreadpool.cpp56
1 files changed, 56 insertions, 0 deletions
diff --git a/tests/auto/corelib/thread/qthreadpool/tst_qthreadpool.cpp b/tests/auto/corelib/thread/qthreadpool/tst_qthreadpool.cpp
index 10a59ac301..cdf240d757 100644
--- a/tests/auto/corelib/thread/qthreadpool/tst_qthreadpool.cpp
+++ b/tests/auto/corelib/thread/qthreadpool/tst_qthreadpool.cpp
@@ -90,6 +90,7 @@ private slots:
void reserveThread();
void releaseThread_data();
void releaseThread();
+ void reserveAndStart();
void start();
void tryStart();
void tryStartPeakThreadCount();
@@ -630,6 +631,61 @@ void tst_QThreadPool::releaseThread()
threadpool->setMaxThreadCount(savedLimit);
}
+void tst_QThreadPool::reserveAndStart() // QTBUG-21051
+{
+ class WaitingTask : public QRunnable
+ {
+ public:
+ QAtomicInt count;
+ QSemaphore waitForStarted;
+
+ WaitingTask() { setAutoDelete(false); }
+
+ void run()
+ {
+ count.ref();
+ waitForStarted.release();
+ }
+ };
+
+ // Set up
+ QThreadPool *threadpool = QThreadPool::globalInstance();
+ int savedLimit = threadpool->maxThreadCount();
+ threadpool->setMaxThreadCount(1);
+ QCOMPARE(threadpool->activeThreadCount(), 0);
+
+ // reserve
+ threadpool->reserveThread();
+ QCOMPARE(threadpool->activeThreadCount(), 1);
+
+ // start a task, to get a running thread
+ WaitingTask *task = new WaitingTask;
+ threadpool->start(task);
+ QCOMPARE(threadpool->activeThreadCount(), 2);
+ task->waitForStarted.acquire();
+ QTRY_COMPARE(task->count.load(), 1);
+ QTRY_COMPARE(threadpool->activeThreadCount(), 1);
+
+ // now the thread is waiting, but tryStart() will fail since activeThreadCount() >= maxThreadCount()
+ QVERIFY(!threadpool->tryStart(task));
+ QTRY_COMPARE(threadpool->activeThreadCount(), 1);
+
+ // start() will therefore do a failing tryStart(), followed by enqueueTask()
+ // which will actually wake up the waiting thread.
+ threadpool->start(task);
+ QTRY_COMPARE(threadpool->activeThreadCount(), 2);
+ task->waitForStarted.acquire();
+ QTRY_COMPARE(task->count.load(), 2);
+ QTRY_COMPARE(threadpool->activeThreadCount(), 1);
+
+ threadpool->releaseThread();
+ QTRY_COMPARE(threadpool->activeThreadCount(), 0);
+
+ delete task;
+
+ threadpool->setMaxThreadCount(savedLimit);
+}
+
QAtomicInt count;
class CountingRunnable : public QRunnable
{