tst_QThreadPool: don't deadlock when a cancel() test fails

We keep the runnables from finishing by having them block
on a QSemaphore::acquire() call inside run().

If we fail a test that precedes the call to sem.release()
further into the test, the early return will cause the
thread pool to be destroyed, which will then attempt to
wait for the runnables to finished, which, in turn wait
for the semaphore to be released.

-> dead lock

Fix by introducing a RAII object to release the semaphore
with a sufficiently large number to unblock all runnables.
That number will in some situations be too large, but that
does not matter.

Change-Id: I1ec7e29b37bc36309e93e6e30708cc7db3c9579c
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Marc Mutz 2017-02-10 11:29:38 +01:00
parent 410a14cc76
commit b4689401a5

View File

@ -960,6 +960,21 @@ void tst_QThreadPool::cancel()
QSemaphore sem(0);
QSemaphore startedThreads(0);
class SemaphoreReleaser
{
QSemaphore &sem;
int n;
Q_DISABLE_COPY(SemaphoreReleaser)
public:
explicit SemaphoreReleaser(QSemaphore &sem, int n)
: sem(sem), n(n) {}
~SemaphoreReleaser()
{
sem.release(n);
}
};
class BlockingRunnable : public QRunnable
{
public:
@ -995,6 +1010,11 @@ void tst_QThreadPool::cancel()
QThreadPool threadPool;
threadPool.setMaxThreadCount(MaxThreadCount);
BlockingRunnable *runnables[runs];
// ensure that the QThreadPool doesn't deadlock if any of the checks fail
// and cause an early return:
const SemaphoreReleaser semReleaser(sem, runs);
count.store(0);
QAtomicInt dtorCounter = 0;
QAtomicInt runCounter = 0;