summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Faure <[email protected]>2025-03-29 09:36:36 +0100
committerDavid Faure <[email protected]>2025-03-31 19:40:11 +0100
commit9e32306daea5578ac5cff1424678da25e2a158af (patch)
tree93ae0259943a999226b7079b4161dab09648b237
parent4fd7d31e0966d52af4dc40c7e47576d40d8b00ab (diff)
QTestLib: fix race on QElapsedTimers from WatchDog thread
The WatchDog thread calls printTestRunTime() which reads the two timers, created and started in the main thread. Pick-to: 6.9 6.8 6.5 Change-Id: I1a337648fddf87190075b7902311544d0ab21ac3 Reviewed-by: Marc Mutz <[email protected]>
-rw-r--r--src/testlib/qtestlog.cpp15
1 files changed, 12 insertions, 3 deletions
diff --git a/src/testlib/qtestlog.cpp b/src/testlib/qtestlog.cpp
index 2bde5a94eca..3938c9a85f8 100644
--- a/src/testlib/qtestlog.cpp
+++ b/src/testlib/qtestlog.cpp
@@ -69,6 +69,7 @@ static void saveCoverageTool(const char * appname, bool testfailed, bool install
#endif
}
+Q_CONSTINIT static QBasicMutex elapsedTimersMutex; // due to the WatchDog thread
Q_CONSTINIT static QElapsedTimer elapsedFunctionTime;
Q_CONSTINIT static QElapsedTimer elapsedTotalTime;
@@ -320,7 +321,10 @@ namespace QTest {
void QTestLog::enterTestFunction(const char* function)
{
- elapsedFunctionTime.start();
+ {
+ QMutexLocker locker(&elapsedTimersMutex);
+ elapsedFunctionTime.start();
+ }
if (printAvailableTags)
return;
@@ -548,8 +552,11 @@ void QTestLog::addBenchmarkResults(const QList<QBenchmarkResult> &results)
void QTestLog::startLogging()
{
- elapsedTotalTime.start();
- elapsedFunctionTime.start();
+ {
+ QMutexLocker locker(&elapsedTimersMutex);
+ elapsedTotalTime.start();
+ elapsedFunctionTime.start();
+ }
for (auto &logger : QTest::loggers->allLoggers())
logger->startLogging();
QTest::oldMessageHandler = qInstallMessageHandler(QTest::messageHandler);
@@ -767,11 +774,13 @@ bool QTestLog::installedTestCoverage()
qint64 QTestLog::nsecsTotalTime()
{
+ QMutexLocker locker(&elapsedTimersMutex);
return elapsedTotalTime.nsecsElapsed();
}
qint64 QTestLog::nsecsFunctionTime()
{
+ QMutexLocker locker(&elapsedTimersMutex);
return elapsedFunctionTime.nsecsElapsed();
}