Skip to content

Commit

Permalink
Concurrency Fix (#4318)
Browse files Browse the repository at this point in the history
* Removed all CVarLoad uses from code to prevent thread concurrency issues.

* Add mutext locks to save and load functions to prevent concurrent operations between those two.
  • Loading branch information
Malkierian authored Sep 15, 2024
1 parent b5037a0 commit 7f503c3
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 6 deletions.
3 changes: 1 addition & 2 deletions soh/soh/Enhancements/debugconsole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1590,6 +1590,5 @@ void DebugConsole_Init(void) {
{"group_name", Ship::ArgumentType::TEXT, true},
}});

CVarSave();
CVarLoad();
Ship::Context::GetInstance()->GetWindow()->GetGui()->SaveConsoleVariablesOnNextTick();
}
3 changes: 1 addition & 2 deletions soh/soh/Enhancements/randomizer/3drando/rando_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ void RandoMain::GenerateRando(std::unordered_map<RandomizerSettingKey, u8> cvarS
std::string fileName = Ship::Context::GetPathRelativeToAppDirectory(GenerateRandomizer(cvarSettings, excludedLocations, enabledTricks, seedString).c_str());
CVarSetString(CVAR_GENERAL("SpoilerLog"), fileName.c_str());

CVarSave();
CVarLoad();
Ship::Context::GetInstance()->GetWindow()->GetGui()->SaveConsoleVariablesOnNextTick();
CVarSetInteger(CVAR_GENERAL("NewSeedGenerated"), 1);
}

Expand Down
3 changes: 1 addition & 2 deletions soh/soh/Enhancements/randomizer/randomizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3053,8 +3053,7 @@ void GenerateRandomizerImgui(std::string seed = "") {
RandoMain::GenerateRando(cvarSettings, excludedLocations, enabledTricks, seed);

CVarSetInteger(CVAR_GENERAL("RandoGenerating"), 0);
CVarSave();
CVarLoad();
Ship::Context::GetInstance()->GetWindow()->GetGui()->SaveConsoleVariablesOnNextTick();

generated = 1;
}
Expand Down
5 changes: 5 additions & 0 deletions soh/soh/SaveManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <fstream>
#include <filesystem>
#include <array>
#include <mutex>

extern "C" SaveContext gSaveContext;
using namespace std::string_literals;
Expand Down Expand Up @@ -909,6 +910,7 @@ int copy_file(const char* src, const char* dst) {
// Threaded SaveFile takes copy of gSaveContext for local unmodified storage

void SaveManager::SaveFileThreaded(int fileNum, SaveContext* saveContext, int sectionID) {
saveMtx.lock();
SPDLOG_INFO("Save File - fileNum: {}", fileNum);
// Needed for first time save, hasn't changed in forever anyway
saveBlock["version"] = 1;
Expand Down Expand Up @@ -983,6 +985,7 @@ void SaveManager::SaveFileThreaded(int fileNum, SaveContext* saveContext, int se
InitMeta(fileNum);
GameInteractor::Instance->ExecuteHooks<GameInteractor::OnSaveFile>(fileNum);
SPDLOG_INFO("Save File Finish - fileNum: {}", fileNum);
saveMtx.unlock();
}

// SaveSection creates a copy of gSaveContext to prevent mid-save data modification, and passes its reference to SaveFileThreaded
Expand Down Expand Up @@ -1025,6 +1028,7 @@ void SaveManager::SaveGlobal() {
}

void SaveManager::LoadFile(int fileNum) {
saveMtx.lock();
SPDLOG_INFO("Load File - fileNum: {}", fileNum);
std::filesystem::path fileName = GetFileName(fileNum);
assert(std::filesystem::exists(fileName));
Expand Down Expand Up @@ -1087,6 +1091,7 @@ void SaveManager::LoadFile(int fileNum) {
SohGui::RegisterPopup("Error loading save file", "A problem occurred loading the save in slot " + std::to_string(fileNum + 1) + ".\nSave file corruption is suspected.\n" +
"The file has been renamed to prevent further issues.");
}
saveMtx.unlock();
}

void SaveManager::ThreadPoolWait() {
Expand Down
1 change: 1 addition & 0 deletions soh/soh/SaveManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ class SaveManager {
nlohmann::json* currentJsonContext = nullptr;
nlohmann::json::iterator currentJsonArrayContext;
std::shared_ptr<BS::thread_pool> smThreadPool;
std::mutex saveMtx;
};

#else
Expand Down

0 comments on commit 7f503c3

Please sign in to comment.