mirror of
https://github.com/hrydgard/ppsspp.git
synced 2026-10-16 16:00:30 +02:00
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.
101 lines
3.1 KiB
C++
101 lines
3.1 KiB
C++
// Copyright (c) 2015- PPSSPP Project.
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, version 2.0 or later versions.
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License 2.0 for more details.
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
// Official git repository and contact information can be found at
|
|
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
|
|
|
|
#pragma once
|
|
|
|
#include <memory>
|
|
|
|
#include <retro_atomic.h>
|
|
|
|
#include "Common/CommonTypes.h"
|
|
#include "Core/Loaders.h"
|
|
#include "Common/Thread/ParkingLot.h"
|
|
#include "Common/Thread/Thread.h"
|
|
|
|
// ReadAt from one thread at a time. While the cache exists, every backend read happens on
|
|
// the loader's own worker thread, so the backend is only ever read from that one thread.
|
|
class RamCachingFileLoader : public ProxiedFileLoader {
|
|
public:
|
|
RamCachingFileLoader(FileLoader *backend);
|
|
~RamCachingFileLoader();
|
|
|
|
bool Exists() override;
|
|
bool ExistsFast() override;
|
|
bool IsDirectory() override;
|
|
s64 FileSize() override;
|
|
|
|
size_t ReadAt(s64 absolutePos, size_t bytes, size_t count, void *data, Flags flags = Flags::NONE) override {
|
|
return ReadAt(absolutePos, bytes * count, data, flags) / bytes;
|
|
}
|
|
size_t ReadAt(s64 absolutePos, size_t bytes, void *data, Flags flags = Flags::NONE) override;
|
|
|
|
void Cancel() override;
|
|
|
|
private:
|
|
void InitCache();
|
|
void ShutdownCache();
|
|
size_t ReadFromCache(s64 pos, size_t bytes, void *data);
|
|
size_t RunRequest(s64 pos, size_t bytes, void *data, Flags flags);
|
|
void StartReadAhead(s64 pos);
|
|
// Worker only.
|
|
void WorkerLoop();
|
|
// Returns how many blocks it loaded.
|
|
u32 SaveIntoCache(s64 pos, size_t bytes, Flags flags);
|
|
u32 NextAheadBlock(u32 startFrom);
|
|
|
|
bool BlockReady(size_t block) {
|
|
return retro_atomic_load_acquire_int(&blocks_[block]) != 0;
|
|
}
|
|
|
|
enum {
|
|
BLOCK_SIZE = 65536,
|
|
BLOCK_SHIFT = 16,
|
|
MAX_BLOCKS_PER_READ = 16,
|
|
BLOCK_READAHEAD = 4,
|
|
};
|
|
|
|
// Fixed before the worker starts.
|
|
s64 filesize_ = 0;
|
|
u8 *cache_ = nullptr;
|
|
size_t blockCount_ = 0;
|
|
// Reader thread only.
|
|
int exists_ = -1;
|
|
int isDirectory_ = -1;
|
|
|
|
// Only the worker writes cache_ data and sets a block, with a release store after the data.
|
|
std::unique_ptr<retro_atomic_int_t[]> blocks_;
|
|
// Worker only.
|
|
u32 aheadRemaining_ = 0;
|
|
retro_atomic_int_t allLoaded_{ 0 };
|
|
|
|
// One request in flight at a time: the reader fills req*, publishes reqSeq_, waits for doneSeq_.
|
|
s64 reqPos_ = 0;
|
|
size_t reqBytes_ = 0;
|
|
void *reqData_ = nullptr;
|
|
Flags reqFlags_ = Flags::NONE;
|
|
size_t reqResult_ = 0;
|
|
int reqSeqLocal_ = 0;
|
|
retro_atomic_int_t reqSeq_{ 0 };
|
|
retro_atomic_int_t doneSeq_{ 0 };
|
|
// Block to read ahead from next, or -1. Reading ahead starts with the first one posted.
|
|
retro_atomic_int_t aheadBlock_{ -1 };
|
|
retro_atomic_int_t aheadCancel_{ 0 };
|
|
retro_atomic_int_t quit_{ 0 };
|
|
EventCounter wake_;
|
|
Thread worker_;
|
|
};
|