DolphinQt2: Don't use a mutex in GameFileCache

GameTracker's usage of GameFileCache is thread-safe even without
using a mutex. All of its access to GameFileCache happens on the
thread m_load_thread, except for the call to GameFileCache::Load,
which finishes before m_load_thread starts executing.
This commit is contained in:
JosJuice 2018-01-04 14:07:01 +01:00
parent 637fbec35d
commit e44b64b82c
2 changed files with 12 additions and 24 deletions

View File

@ -6,6 +6,7 @@
#include <QDataStream>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include "Common/FileUtil.h"
@ -20,24 +21,18 @@ GameFileCache::GameFileCache()
{
}
bool GameFileCache::IsCached(const QString& path)
bool GameFileCache::IsCached(const QString& path) const
{
std::lock_guard<std::mutex> guard(m_mutex);
return m_gamefiles.contains(path);
}
GameFile GameFileCache::GetFile(const QString& path)
GameFile GameFileCache::GetFile(const QString& path) const
{
std::lock_guard<std::mutex> guard(m_mutex);
return m_gamefiles[path];
}
void GameFileCache::Load()
{
std::lock_guard<std::mutex> guard(m_mutex);
QFile file(m_path);
if (!file.open(QIODevice::ReadOnly))
@ -56,10 +51,8 @@ void GameFileCache::Load()
stream >> m_gamefiles;
}
void GameFileCache::Save()
void GameFileCache::Save() const
{
std::lock_guard<std::mutex> guard(m_mutex);
QFile file(m_path);
if (!file.open(QIODevice::WriteOnly))
@ -74,14 +67,10 @@ void GameFileCache::Save()
void GameFileCache::Update(const GameFile& gamefile)
{
std::lock_guard<std::mutex> guard(m_mutex);
m_gamefiles[gamefile.GetFilePath()] = gamefile;
}
QList<QString> GameFileCache::GetCached()
QList<QString> GameFileCache::GetCached() const
{
std::lock_guard<std::mutex> guard(m_mutex);
return m_gamefiles.keys();
}

View File

@ -4,9 +4,9 @@
#pragma once
#include <QFile>
#include <mutex>
#include <QList>
#include <QMap>
#include <QString>
#include "DolphinQt2/GameList/GameFile.h"
@ -16,15 +16,14 @@ public:
explicit GameFileCache();
void Update(const GameFile& gamefile);
void Save();
void Save() const;
void Load();
bool IsCached(const QString& path);
GameFile GetFile(const QString& path);
QList<QString> GetCached();
bool IsCached(const QString& path) const;
GameFile GetFile(const QString& path) const;
QList<QString> GetCached() const;
private:
QString m_path;
QMap<QString, GameFile> m_gamefiles;
std::mutex m_mutex;
};