Fix some data races in tst_qthreadpool.

Detected by helgrind.

Change-Id: I5f73a62610b4bdc7f26629f3dcf7b11467897a5c
Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
This commit is contained in:
David Faure 2013-05-19 12:27:54 +02:00 committed by The Qt Project
parent 94c7002df3
commit f22b29162c

View File

@ -212,22 +212,22 @@ void tst_QThreadPool::waitcomplete()
QCOMPARE(testFunctionCount, runs); QCOMPARE(testFunctionCount, runs);
} }
volatile bool ran; QAtomicInt ran; // bool
class TestTask : public QRunnable class TestTask : public QRunnable
{ {
public: public:
void run() void run()
{ {
ran = true; ran.store(true);
} }
}; };
void tst_QThreadPool::runTask() void tst_QThreadPool::runTask()
{ {
QThreadPool manager; QThreadPool manager;
ran = false; ran.store(false);
manager.start(new TestTask()); manager.start(new TestTask());
QTRY_VERIFY(ran); QTRY_VERIFY(ran.load());
} }
/* /*
@ -235,19 +235,19 @@ void tst_QThreadPool::runTask()
*/ */
void tst_QThreadPool::singleton() void tst_QThreadPool::singleton()
{ {
ran = false; ran.store(false);
QThreadPool::globalInstance()->start(new TestTask()); QThreadPool::globalInstance()->start(new TestTask());
QTRY_VERIFY(ran); QTRY_VERIFY(ran.load());
} }
int *value = 0; QAtomicInt *value = 0;
class IntAccessor : public QRunnable class IntAccessor : public QRunnable
{ {
public: public:
void run() void run()
{ {
for (int i = 0; i < 100; ++i) { for (int i = 0; i < 100; ++i) {
++(*value); value->ref();
QTest::qSleep(1); QTest::qSleep(1);
} }
} }
@ -259,7 +259,7 @@ public:
*/ */
void tst_QThreadPool::destruction() void tst_QThreadPool::destruction()
{ {
value = new int; value = new QAtomicInt;
QThreadPool *threadManager = new QThreadPool(); QThreadPool *threadManager = new QThreadPool();
threadManager->start(new IntAccessor()); threadManager->start(new IntAccessor());
threadManager->start(new IntAccessor()); threadManager->start(new IntAccessor());
@ -679,8 +679,8 @@ void tst_QThreadPool::tryStart()
} }
QMutex mutex; QMutex mutex;
int activeThreads = 0; QAtomicInt activeThreads;
int peakActiveThreads = 0; QAtomicInt peakActiveThreads;
void tst_QThreadPool::tryStartPeakThreadCount() void tst_QThreadPool::tryStartPeakThreadCount()
{ {
class CounterTask : public QRunnable class CounterTask : public QRunnable
@ -692,14 +692,14 @@ void tst_QThreadPool::tryStartPeakThreadCount()
{ {
{ {
QMutexLocker lock(&mutex); QMutexLocker lock(&mutex);
++activeThreads; activeThreads.ref();
peakActiveThreads = qMax(peakActiveThreads, activeThreads); peakActiveThreads.store(qMax(peakActiveThreads.load(), activeThreads.load()));
} }
QTest::qWait(100); QTest::qWait(100);
{ {
QMutexLocker lock(&mutex); QMutexLocker lock(&mutex);
--activeThreads; activeThreads.deref();
} }
} }
}; };
@ -711,13 +711,13 @@ void tst_QThreadPool::tryStartPeakThreadCount()
if (threadPool.tryStart(&task) == false) if (threadPool.tryStart(&task) == false)
QTest::qWait(10); QTest::qWait(10);
} }
QCOMPARE(peakActiveThreads, QThread::idealThreadCount()); QCOMPARE(peakActiveThreads.load(), QThread::idealThreadCount());
for (int i = 0; i < 20; ++i) { for (int i = 0; i < 20; ++i) {
if (threadPool.tryStart(&task) == false) if (threadPool.tryStart(&task) == false)
QTest::qWait(10); QTest::qWait(10);
} }
QCOMPARE(peakActiveThreads, QThread::idealThreadCount()); QCOMPARE(peakActiveThreads.load(), QThread::idealThreadCount());
} }
void tst_QThreadPool::tryStartCount() void tst_QThreadPool::tryStartCount()