mirror of
https://github.com/hrydgard/ppsspp.git
synced 2026-10-15 23:40:29 +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.
104 lines
3.4 KiB
C++
104 lines
3.4 KiB
C++
// Copyright (c) 2012- 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. Every backend read happens on the loader's own worker
|
|
// thread, so the backend is only ever read from that one thread.
|
|
class CachingFileLoader : public ProxiedFileLoader {
|
|
public:
|
|
CachingFileLoader(FileLoader *backend);
|
|
~CachingFileLoader();
|
|
|
|
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;
|
|
|
|
private:
|
|
void Prepare();
|
|
void InitCache();
|
|
void ShutdownCache();
|
|
size_t ReadFromCache(s64 pos, size_t bytes, void *data);
|
|
// Reader side: makes room, then has the worker read at least one block into the cache.
|
|
void RequestIntoCache(s64 pos, size_t bytes, Flags flags);
|
|
size_t RequestUncached(s64 pos, size_t bytes, void *data, Flags flags);
|
|
size_t RunRequest();
|
|
// Worker only.
|
|
void SaveIntoCache(s64 pos, size_t bytes, Flags flags, bool readingAhead = false);
|
|
void ReadAhead(s64 block);
|
|
void WorkerLoop();
|
|
void MakeCacheSpaceFor(size_t blocks);
|
|
void StartReadAhead(s64 pos);
|
|
|
|
u8 *BlockAt(s64 block) {
|
|
return (u8 *)retro_atomic_load_acquire_ptr(&slots_[(size_t)block]);
|
|
}
|
|
|
|
enum {
|
|
BLOCK_SIZE = 65536,
|
|
BLOCK_SHIFT = 16,
|
|
MAX_BLOCKS_PER_READ = 16,
|
|
MAX_BLOCKS_CACHED = 4096, // 256 MB
|
|
BLOCK_READAHEAD = 4,
|
|
};
|
|
|
|
// Reader thread only, except filesize_ and numBlocks_, fixed before the worker starts.
|
|
bool prepared_ = false;
|
|
s64 filesize_ = 0;
|
|
s64 numBlocks_ = 0;
|
|
int exists_ = -1;
|
|
int isDirectory_ = -1;
|
|
u64 generation_ = 0;
|
|
u64 oldestGeneration_ = 0;
|
|
std::unique_ptr<u64[]> generations_;
|
|
|
|
// One slot per block. Only the worker fills an empty slot, only the reader empties one,
|
|
// and the worker never touches a block's data once published.
|
|
std::unique_ptr<retro_atomic_ptr_t[]> slots_;
|
|
retro_atomic_int_t cachedCount_{ 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 };
|
|
// Next block to read ahead from, or -1.
|
|
retro_atomic_int_t aheadBlock_{ -1 };
|
|
retro_atomic_int_t quit_{ 0 };
|
|
EventCounter wake_;
|
|
Thread worker_;
|
|
};
|