From 1db24e80143b6f658500501af758b2aef2be870d Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Wed, 10 Aug 2022 17:26:52 +1000 Subject: [PATCH] IniSettingsInterface: Prevent multiple threads loading/saving at once --- src/util/ini_settings_interface.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/util/ini_settings_interface.cpp b/src/util/ini_settings_interface.cpp index ae5a4eb26..ef44c978b 100644 --- a/src/util/ini_settings_interface.cpp +++ b/src/util/ini_settings_interface.cpp @@ -4,9 +4,13 @@ #include "common/string_util.h" #include #include - +#include 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() @@ -20,6 +24,7 @@ bool INISettingsInterface::Load() if (m_filename.empty()) return false; + std::unique_lock lock(s_ini_load_save_mutex); SI_Error err = SI_FAIL; auto fp = FileSystem::OpenManagedCFile(m_filename.c_str(), "rb"); if (fp) @@ -33,6 +38,7 @@ bool INISettingsInterface::Save() if (m_filename.empty()) return false; + std::unique_lock lock(s_ini_load_save_mutex); SI_Error err = SI_FAIL; std::FILE* fp = FileSystem::OpenCFile(m_filename.c_str(), "wb"); if (fp)