Skip to content

Commit

Permalink
Avoid simultaneous access of the same file for two diffrent width
Browse files Browse the repository at this point in the history
  • Loading branch information
daschuer committed Oct 30, 2023
1 parent 2740400 commit a1c7e56
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
39 changes: 27 additions & 12 deletions src/library/coverartcache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,13 @@ void CoverArtCache::tryLoadCover(
return;
}

const auto requestedCacheKey = coverInfo.cacheKey();
QString cacheKey = pixmapCacheKey(requestedCacheKey, desiredWidth);
// keep a list of cacheKeys for which a future is currently running
// to avoid loading the same picture again while we are loading it
bool requestPending = m_runningRequests.contains(cacheKey);
m_runningRequests.insert(cacheKey, pRequester);
const mixxx::cache_key_t requestedCacheKey = coverInfo.cacheKey();
// keep a list of cache keys for which a future is currently running
// to avoid loading the same picture again while we are loading it.
// This fixes also https://github.com/mixxxdj/mixxx/issues/11131 on
// Windows where simultaneous open the same file from two threads fails.
bool requestPending = m_runningRequests.contains(requestedCacheKey);
m_runningRequests.insert(requestedCacheKey, {pRequester, desiredWidth});
if (requestPending) {
return;
}
Expand Down Expand Up @@ -294,11 +295,25 @@ void CoverArtCache::coverLoaded() {
}
}

const QObject* pRequester;
while ((pRequester = m_runningRequests.take(cacheKey)) != nullptr) {
emit coverFound(
pRequester,
res.coverArt,
pixmap);
auto runningRequests = m_runningRequests;
// First remove all requests for this cover that way we can
// re-add cover with different sizes via tryLoadCover() as usual
m_runningRequests.remove(res.coverArt.cacheKey());

auto i = runningRequests.find(res.coverArt.cacheKey());
while (i != runningRequests.end() && i.key() == res.coverArt.cacheKey()) {
if (i.value().desiredWidth == res.coverArt.resizedToWidth) {
emit coverFound(
i.value().pRequester,
res.coverArt,
pixmap);
} else {
tryLoadCover(
i.value().pRequester,
nullptr,
res.coverArt,
i.value().desiredWidth);
}
++i;
}
}
6 changes: 5 additions & 1 deletion src/library/coverartcache.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,9 @@ class CoverArtCache : public QObject, public Singleton<CoverArtCache> {
const CoverInfo& info,
int desiredWidth);

QMultiHash<QString, const QObject*> m_runningRequests;
struct RequestData {
const QObject* pRequester;
int desiredWidth;
};
QMultiHash<mixxx::cache_key_t, RequestData> m_runningRequests;
};

0 comments on commit a1c7e56

Please sign in to comment.