Savedata: Keep the IO thread out of the dialog's state

Replaces the temporary fix that did the hidden modes' IO inside Update.

The IO thread read and wrote the dialog's request, display state and save
list, all shared with the emulator thread, which kept using them to draw the
dialog and reload the request from the game.

Now the IO thread works on its own copy of the request, its own SavedataParam
and directory names resolved up front, and shares nothing else with the
emulator thread but the (locked) file system, MemoryStick_FreeSpace's cached
use and sceChnnlsv's scratch buffer and kirk state, the last two now under
locks too. It still reads and writes the game's buffers directly, like a PSP's
utility threads and sceIoReadAsync do, so a savestate waits for it before it
saves or loads memory. Save
bookkeeping, the save indicator and display changes happen on the emulator
thread when the results are taken, and only the request fields the IO changed
are copied back, so a game's own edits in the meantime survive.

Hidden modes take the results at the next Update (or, with Host IO timing,
the first Update that finds them done). The visible dialogs keep drawing and
take them once the IO is done; save and load used to stall the emulator
thread for the whole operation. Savestates keep results that haven't been
taken yet.

When the results land in PSP memory doesn't matter to games, so
utility/savedata/filelist now only prints them once the utility has finished.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Henrik RydgårdandClaude Opus 5.5 committed 2026-09-25 12:50:39 -06:00
1 parent 1f1c478c92
commit 9e69c1f9aa
10 files changed
+261 -139

No files matched your search

+22 -10
View File
@@ -16,6 +16,8 @@
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
#include <algorithm>
#include <atomic>
#include <mutex>
#include <string>
#include <vector>
#include <string_view>
@@ -42,8 +44,12 @@ static MemStickFatState memStickFatState;
static bool memStickNeedsAssign = false;
static uint64_t memStickInsertedAt = 0;
static uint64_t memstickInitialFree = 0;
// The savedata IO thread asks for the free space too, so the cached use is guarded, and a write
// during the calculation leaves it stale rather than marked current.
static std::mutex memstickCurrentUseLock;
static uint64_t memstickCurrentUse = 0;
static bool memstickCurrentUseValid = false;
static uint32_t memstickCurrentUseGeneration = 0;
static std::atomic<uint32_t> memstickWriteGeneration{ 1 };
enum FreeCalcStatus {
NONE,
@@ -122,15 +128,21 @@ u64 MemoryStick_FreeSpace(std::string gameID) {
const u64 memStickSize = flags.ReportSmallMemstick ? smallMemstickSize : (u64)g_Config.iMemStickSizeGB * 1024 * 1024 * 1024;
// Assume the memory stick is only used to store savedata, for the current game only.
if (!memstickCurrentUseValid) {
Path saveFolder = GetSysDirectory(DIRECTORY_SAVEDATA);
memstickCurrentUse = ComputeSizeOfSavedataForGame(saveFolder, gameID);
memstickCurrentUseValid = true;
u64 currentUse;
{
std::lock_guard<std::mutex> guard(memstickCurrentUseLock);
const uint32_t generation = memstickWriteGeneration;
if (memstickCurrentUseGeneration != generation) {
Path saveFolder = GetSysDirectory(DIRECTORY_SAVEDATA);
memstickCurrentUse = ComputeSizeOfSavedataForGame(saveFolder, gameID);
memstickCurrentUseGeneration = generation;
}
currentUse = memstickCurrentUse;
}
u64 simulatedFreeSpace = 0;
if (memstickCurrentUse < memStickSize) {
simulatedFreeSpace = memStickSize - memstickCurrentUse;
if (currentUse < memStickSize) {
simulatedFreeSpace = memStickSize - currentUse;
} else if (flags.ReportSmallMemstick) {
// There's more stuff in the memstick than the size we report.
// This doesn't work, so we'll just have to lie. Not sure what the best way is.
@@ -144,8 +156,8 @@ u64 MemoryStick_FreeSpace(std::string gameID) {
// Assassin's Creed: Bloodlines fails to save if free space changes incorrectly during game.
// See issue #12761
u64 realFreeSpace = 0;
if (memstickCurrentUse <= memstickInitialFree) {
realFreeSpace = memstickInitialFree - memstickCurrentUse;
if (currentUse <= memstickInitialFree) {
realFreeSpace = memstickInitialFree - currentUse;
}
space = std::min(simulatedFreeSpace, realFreeSpace);
} else if (System_GetPropertyBool(SYSPROP_CAN_GET_FREE_SPACE_FAST) || g_Config.bReportAccurateFreeStorageSpace) {
@@ -161,7 +173,7 @@ u64 MemoryStick_FreeSpace(std::string gameID) {
}
void MemoryStick_NotifyWrite() {
memstickCurrentUseValid = false;
memstickWriteGeneration++;
}
void MemoryStick_SetFatState(MemStickFatState state) {