DolphinQt2: Fix a race condition in GameTracker

We need to make sure that LoadCache finishes before Start begins
accessing m_cache. The old solution with mutexes didn't do this.
This commit is contained in:
JosJuice 2018-05-27 07:38:33 +02:00
parent 7b07424885
commit c0553afff1
2 changed files with 10 additions and 7 deletions

View File

@ -56,8 +56,8 @@ GameTracker::GameTracker(QObject* parent) : QFileSystemWatcher(parent)
void GameTracker::LoadCache() void GameTracker::LoadCache()
{ {
std::lock_guard<std::mutex> lk(m_mutex);
m_cache.Load(); m_cache.Load();
m_cache_loaded_event.Set();
} }
void GameTracker::Start() void GameTracker::Start()
@ -67,12 +67,14 @@ void GameTracker::Start()
m_initial_games_emitted = true; m_initial_games_emitted = true;
std::lock_guard<std::mutex> lk(m_mutex);
m_load_thread.EmplaceItem(Command{CommandType::Start, {}}); m_load_thread.EmplaceItem(Command{CommandType::Start, {}});
m_cache_loaded_event.Wait();
m_cache.ForEach( m_cache.ForEach(
[this](const std::shared_ptr<const UICommon::GameFile>& game) { emit GameLoaded(game); }); [this](const std::shared_ptr<const UICommon::GameFile>& game) { emit GameLoaded(game); });
m_initial_games_emitted_event.Set();
} }
void GameTracker::StartInternal() void GameTracker::StartInternal()
@ -92,7 +94,7 @@ void GameTracker::StartInternal()
}; };
auto emit_game_removed = [this](const std::string& path) { emit GameRemoved(path); }; auto emit_game_removed = [this](const std::string& path) { emit GameRemoved(path); };
std::lock_guard<std::mutex> lk(m_mutex); m_initial_games_emitted_event.Wait();
bool cache_updated = m_cache.Update(paths, emit_game_loaded, emit_game_removed); bool cache_updated = m_cache.Update(paths, emit_game_loaded, emit_game_removed);
cache_updated |= m_cache.UpdateAdditionalMetadata(m_title_database, emit_game_loaded); cache_updated |= m_cache.UpdateAdditionalMetadata(m_title_database, emit_game_loaded);

View File

@ -5,7 +5,6 @@
#pragma once #pragma once
#include <memory> #include <memory>
#include <mutex>
#include <string> #include <string>
#include <QFileSystemWatcher> #include <QFileSystemWatcher>
@ -13,6 +12,7 @@
#include <QSet> #include <QSet>
#include <QString> #include <QString>
#include "Common/Event.h"
#include "Common/WorkQueueThread.h" #include "Common/WorkQueueThread.h"
#include "Core/TitleDatabase.h" #include "Core/TitleDatabase.h"
#include "UICommon/GameFile.h" #include "UICommon/GameFile.h"
@ -75,9 +75,10 @@ private:
Common::WorkQueueThread<Command> m_load_thread; Common::WorkQueueThread<Command> m_load_thread;
UICommon::GameFileCache m_cache; UICommon::GameFileCache m_cache;
Core::TitleDatabase m_title_database; Core::TitleDatabase m_title_database;
std::mutex m_mutex; Common::Event m_cache_loaded_event;
bool m_started = false; Common::Event m_initial_games_emitted_event;
bool m_initial_games_emitted = false; bool m_initial_games_emitted = false;
bool m_started = false;
}; };
Q_DECLARE_METATYPE(std::shared_ptr<const UICommon::GameFile>) Q_DECLARE_METATYPE(std::shared_ptr<const UICommon::GameFile>)