diff options
author | rubpa <rubpa@users.noreply.github.com> | 2022-11-01 18:18:09 +0530 |
---|---|---|
committer | rubpa <rubpa@users.noreply.github.com> | 2022-11-04 07:36:59 +0530 |
commit | 8e675f35e34f622f13d1fda2c64e08dc7e794226 (patch) | |
tree | ed1c6465a062484f97b022924db1017955ac0462 | |
parent | 1f61ce445abaaeaa5c27bb48413d2c5c40671354 (diff) |
[Pictures] fix race condition in thumbnails loading
-rw-r--r-- | xbmc/TextureCache.cpp | 32 | ||||
-rw-r--r-- | xbmc/TextureCache.h | 12 | ||||
-rw-r--r-- | xbmc/TextureCacheJob.cpp | 7 |
3 files changed, 27 insertions, 24 deletions
diff --git a/xbmc/TextureCache.cpp b/xbmc/TextureCache.cpp index a31164d0d3..beca7cf9fd 100644 --- a/xbmc/TextureCache.cpp +++ b/xbmc/TextureCache.cpp @@ -130,6 +130,18 @@ void CTextureCache::BackgroundCacheImage(const std::string &url) AddJob(new CTextureCacheJob(path, details.hash)); } +bool CTextureCache::StartCacheImage(const std::string& image) +{ + std::unique_lock<CCriticalSection> lock(m_processingSection); + std::set<std::string>::iterator i = m_processinglist.find(image); + if (i == m_processinglist.end()) + { + m_processinglist.insert(image); + return true; + } + return false; +} + std::string CTextureCache::CacheImage(const std::string& image, std::unique_ptr<CTexture>* texture /*= nullptr*/, CTextureDetails* details /*= nullptr*/) @@ -306,26 +318,6 @@ void CTextureCache::OnJobComplete(unsigned int jobID, bool success, CJob *job) return CJobQueue::OnJobComplete(jobID, success, job); } -void CTextureCache::OnJobProgress(unsigned int jobID, unsigned int progress, unsigned int total, const CJob *job) -{ - if (strcmp(job->GetType(), kJobTypeCacheImage) == 0 && !progress) - { // check our processing list - { - std::unique_lock<CCriticalSection> lock(m_processingSection); - const CTextureCacheJob *cacheJob = static_cast<const CTextureCacheJob*>(job); - std::set<std::string>::iterator i = m_processinglist.find(cacheJob->m_url); - if (i == m_processinglist.end()) - { - m_processinglist.insert(cacheJob->m_url); - return; - } - } - CancelJob(job); - } - else - CJobQueue::OnJobProgress(jobID, progress, total, job); -} - bool CTextureCache::Export(const std::string &image, const std::string &destination, bool overwrite) { CTextureDetails details; diff --git a/xbmc/TextureCache.h b/xbmc/TextureCache.h index e4238c6255..dae043f448 100644 --- a/xbmc/TextureCache.h +++ b/xbmc/TextureCache.h @@ -70,6 +70,17 @@ public: */ void BackgroundCacheImage(const std::string &image); + /*! \brief Updates the in-process list. + + Inserts the image url into the currently processing list + to avoid 2 jobs being processed at once + + \param image url of the image to start processing + \return true if list updated, false otherwise + \sa CacheImage + */ + bool StartCacheImage(const std::string& image); + /*! \brief Cache an image to image cache, optionally return the texture Caches the given image, returning the texture if the caller wants it. @@ -199,7 +210,6 @@ private: bool SetCachedTextureValid(const std::string &url, bool updateable); void OnJobComplete(unsigned int jobID, bool success, CJob *job) override; - void OnJobProgress(unsigned int jobID, unsigned int progress, unsigned int total, const CJob *job) override; /*! \brief Called when a caching job has completed. Removes the job from our processing list, updates the database diff --git a/xbmc/TextureCacheJob.cpp b/xbmc/TextureCacheJob.cpp index cbb7f7e934..02ef0f450b 100644 --- a/xbmc/TextureCacheJob.cpp +++ b/xbmc/TextureCacheJob.cpp @@ -58,15 +58,16 @@ bool CTextureCacheJob::DoWork() { if (ShouldCancel(0, 0)) return false; - if (ShouldCancel(1, 0)) // HACK: second check is because we cancel the job in the first callback, but we don't detect it - return false; // until the second // check whether we need cache the job anyway bool needsRecaching = false; std::string path(CServiceBroker::GetTextureCache()->CheckCachedImage(m_url, needsRecaching)); if (!path.empty() && !needsRecaching) return false; - return CacheTexture(); + if (CServiceBroker::GetTextureCache()->StartCacheImage(m_url)) + return CacheTexture(); + + return false; } bool CTextureCacheJob::CacheTexture(std::unique_ptr<CTexture>* out_texture) |