Files
ppsspp/Core/SaveStateRewind.h
T
libretroadmin f1208a7a5a Make threading lock-free
The core and the headless tools no longer use mutexes, condition variables,
spinlocks, std::atomic, std::thread or volatile flags. Threads hand off through
libretro-common's retro_atomic and rthreads plus a few small pieces on top of
them in Common/Thread: Thread, MpscQueue, EventCounter, ParkingLot waits and
ReaderGate. Every wait blocks until it's woken; nothing polls on a timer.

State that only one thread touches just loses its lock. The PSP filesystem,
sceIo and the savedata dialog now belong to the emulation thread: async reads
and writes run when they're queued and complete later in emulated time, and
the IO and savedata threads are gone. The friend finder only talks to the
adhoc server and hands what it gets to the emulation thread. GPU init always
runs on the thread that starts the game.

The Vulkan render and compile queues, pipeline and render pass caches, delete
list and SPIR-V cache, the GL render queue, the software renderer's jit caches,
the file loader caches, the web debugger, UPnP, input, savestates, logging and
the GE debugger all hand off through queues and atomics. The IR jit's spinning
profiler thread goes.

A CPU thread with nothing to run now sleeps until work is queued instead of
spinning. Headless with --debugger waits for a client while stopped, and ends
the run once every client that connected has left. Web server shutdown keeps
servicing the CPU queue until the server has finished.
2026-10-09 04:56:16 +02:00

84 lines
2.4 KiB
C++

#pragma once
#include "Common/Serialize/Serializer.h"
#include "Common/CommonTypes.h"
#include "Common/TimeUtil.h"
#include "Common/Thread/Thread.h"
namespace SaveState {
// This ring buffer of states is for rewind save states, which are kept in RAM.
// Save states are compressed against one of two reference saves (bases_), and the reference
// is switched to a fresh save every N saves, where N is BASE_USAGE_INTERVAL.
// The compression is a simple block based scheme where 0 means to copy a block from the base,
// and 1 means that the following bytes are the next block. See Compress/Decompress.
class StateRingbuffer {
public:
StateRingbuffer() {
size_ = REWIND_NUM_STATES;
states_.resize(size_);
baseMapping_.resize(size_);
}
~StateRingbuffer() {
if (compressThread_.joinable()) {
compressThread_.join();
}
}
CChunkFileReader::Error Save();
CChunkFileReader::Error Restore(std::string *errorString, std::string *metadata);
void ScheduleCompress(std::vector<u8> *result, const std::vector<u8> *state, const std::vector<u8> *base);
void Compress(std::vector<u8> &result, const std::vector<u8> &state, const std::vector<u8> &base);
void Decompress(std::vector<u8> &result, const std::vector<u8> &compressed, const std::vector<u8> &base);
void Clear();
bool Empty() const {
return next_ == first_;
}
void Process();
void NotifyState();
double NextStateTimestamp() const;
private:
const int BLOCK_SIZE = 8192;
const int REWIND_NUM_STATES = 20;
// TODO: Instead, based on size of compressed state?
const int BASE_USAGE_INTERVAL = 15;
typedef std::vector<u8> StateBuffer;
struct RewindState {
StateBuffer stateBuffer;
double savedTime;
bool empty() const { return stateBuffer.empty(); }
void clear() { stateBuffer.clear(); savedTime = 0.0; }
};
int first_ = 0;
int next_ = 0;
int size_;
std::vector<RewindState> states_;
StateBuffer bases_[2];
// Which generation each base slot currently holds, and which generation each state was
// compressed against. There are more states than bases, so states do go stale.
int baseGeneration_[2] = {-1, -1};
int nextBaseGeneration_ = 0;
std::vector<int> baseMapping_;
// Emulation thread only. The compress thread only touches the buffers handed to it, and
// everything that reads or replaces those joins it first.
Thread compressThread_;
std::vector<u8> buffer_;
int base_ = -1;
int baseUsage_ = 0;
double rewindLastTime_ = 0.0f;
};
} // namespace SaveState