IniSettingsInterface: Prevent multiple threads loading/saving at once

This commit is contained in:
Connor McLaughlin 2022-08-10 17:26:52 +10:00
parent 7134af509c
commit 1db24e8014

View file

@ -4,9 +4,13 @@
#include "common/string_util.h" #include "common/string_util.h"
#include <algorithm> #include <algorithm>
#include <iterator> #include <iterator>
#include <mutex>
Log_SetChannel(INISettingsInterface); Log_SetChannel(INISettingsInterface);
// To prevent races between saving and loading settings, particularly with game settings,
// we only allow one ini to be parsed at any point in time.
static std::mutex s_ini_load_save_mutex;
INISettingsInterface::INISettingsInterface(std::string filename) : m_filename(std::move(filename)), m_ini(true, true) {} INISettingsInterface::INISettingsInterface(std::string filename) : m_filename(std::move(filename)), m_ini(true, true) {}
INISettingsInterface::~INISettingsInterface() INISettingsInterface::~INISettingsInterface()
@ -20,6 +24,7 @@ bool INISettingsInterface::Load()
if (m_filename.empty()) if (m_filename.empty())
return false; return false;
std::unique_lock lock(s_ini_load_save_mutex);
SI_Error err = SI_FAIL; SI_Error err = SI_FAIL;
auto fp = FileSystem::OpenManagedCFile(m_filename.c_str(), "rb"); auto fp = FileSystem::OpenManagedCFile(m_filename.c_str(), "rb");
if (fp) if (fp)
@ -33,6 +38,7 @@ bool INISettingsInterface::Save()
if (m_filename.empty()) if (m_filename.empty())
return false; return false;
std::unique_lock lock(s_ini_load_save_mutex);
SI_Error err = SI_FAIL; SI_Error err = SI_FAIL;
std::FILE* fp = FileSystem::OpenCFile(m_filename.c_str(), "wb"); std::FILE* fp = FileSystem::OpenCFile(m_filename.c_str(), "wb");
if (fp) if (fp)