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