From cb5f13e766cade954ffea4f6a62d7b9c81b5fb90 Mon Sep 17 00:00:00 2001 From: Mikolaj Boc Date: Fri, 14 Apr 2023 16:12:31 +0200 Subject: QSampleCache: Wait for load thread to finish before restarting it One cannot rely on QThread::isRunning for checking whether the thread hasn't finished yet, as the thread might be quitting and still return true from isRunning. In that case, the thread won't process the sample load request that misses the start() call in this case. Fixes: QTBUG-109167 Pick-to: 6.5 Change-Id: I33382ad3d8a19ceb86857c51227e81a86a88eea7 Reviewed-by: Lars Knoll --- src/multimedia/audio/qsamplecache_p.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/multimedia/audio/qsamplecache_p.cpp b/src/multimedia/audio/qsamplecache_p.cpp index e753402fb..825c79685 100644 --- a/src/multimedia/audio/qsamplecache_p.cpp +++ b/src/multimedia/audio/qsamplecache_p.cpp @@ -130,6 +130,7 @@ QSample* QSampleCache::requestSample(const QUrl& url) { //lock and add first to make sure live loadingThread will not be killed during this function call m_loadingMutex.lock(); + const bool needsThreadStart = m_loadingRefCount == 0; m_loadingRefCount++; m_loadingMutex.unlock(); @@ -138,8 +139,11 @@ QSample* QSampleCache::requestSample(const QUrl& url) QMap::iterator it = m_samples.find(url); QSample* sample; if (it == m_samples.end()) { - if (!m_loadingThread.isRunning()) + if (needsThreadStart) { + // Previous thread might be finishing, need to wait for it. If not, this is a no-op. + m_loadingThread.wait(); m_loadingThread.start(); + } sample = new QSample(url, this); m_samples.insert(url, sample); #if QT_CONFIG(thread) -- cgit v1.2.1