Skip to content

Commit

Permalink
GameInfoCache: Properly lock around accesses to the gameinfo map.
Browse files Browse the repository at this point in the history
  • Loading branch information
hrydgard committed Jan 28, 2024
1 parent 0615ba3 commit 82f269c
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions UI/GameInfoCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -801,10 +801,12 @@ void GameInfoCache::Shutdown() {
void GameInfoCache::Clear() {
CancelAll();

std::lock_guard<std::mutex> lock(mapLock_);
info_.clear();
}

void GameInfoCache::CancelAll() {
std::lock_guard<std::mutex> lock(mapLock_);
for (auto info : info_) {
// GetFileLoader will create one if there isn't one already.
// Avoid that by checking.
Expand All @@ -818,6 +820,7 @@ void GameInfoCache::CancelAll() {
}

void GameInfoCache::FlushBGs() {
std::lock_guard<std::mutex> lock(mapLock_);
for (auto iter = info_.begin(); iter != info_.end(); iter++) {
std::lock_guard<std::mutex> lock(iter->second->lock);
iter->second->pic0.Clear();
Expand All @@ -831,19 +834,28 @@ void GameInfoCache::FlushBGs() {
}

void GameInfoCache::PurgeType(IdentifiedFileType fileType) {
for (auto iter = info_.begin(); iter != info_.end();) {
auto &info = iter->second;

// TODO: Find a better way to wait here.
while (info->pendingFlags != (GameInfoFlags)0) {
sleep_ms(1);
}
if (info->fileType == fileType) {
iter = info_.erase(iter);
} else {
iter++;
bool retry = false;
// Trickery to avoid sleeping with the lock held.
do {
{
std::lock_guard<std::mutex> lock(mapLock_);
for (auto iter = info_.begin(); iter != info_.end();) {
auto &info = iter->second;

// TODO: Find a better way to wait here.
while (info->pendingFlags != (GameInfoFlags)0) {
retry = true;
break;
}
if (info->fileType == fileType) {
iter = info_.erase(iter);
} else {
iter++;
}
}
}
}
sleep_ms(1);
} while (retry);
}

// Call on the main thread ONLY - that is from stuff called from NativeFrame.
Expand Down

0 comments on commit 82f269c

Please sign in to comment.