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.
102 lines
2.5 KiB
C++
102 lines
2.5 KiB
C++
#pragma once
|
|
|
|
#include <cstdint>
|
|
|
|
#include <queues/mpsc_stack.h>
|
|
|
|
// The new threadpool.
|
|
|
|
// To help smart scheduling.
|
|
enum class TaskType {
|
|
CPU_COMPUTE,
|
|
IO_BLOCKING, // NOTE: Only these can access scoped storage on Android (they initialize the JNI context).
|
|
DEDICATED_THREAD, // These can never get stuck in queue behind others, but are more expensive to launch. Cannot use I/O.
|
|
};
|
|
|
|
enum class TaskPriority {
|
|
HIGH = 0,
|
|
NORMAL = 1,
|
|
LOW = 2,
|
|
|
|
COUNT,
|
|
};
|
|
|
|
class Task;
|
|
|
|
// Intrusive link for the thread manager's lock-free queues. node stays the first member,
|
|
// so a node drained from an mpsc_stack converts straight back to its TaskLink.
|
|
struct TaskLink {
|
|
mpsc_stack_node_t node;
|
|
Task *self;
|
|
};
|
|
|
|
// Implement this to make something that you can run on the thread manager.
|
|
class Task {
|
|
public:
|
|
Task() {
|
|
link_.node.next = nullptr;
|
|
link_.self = this;
|
|
}
|
|
virtual ~Task() {}
|
|
virtual TaskType Type() const = 0;
|
|
virtual TaskPriority Priority() const = 0;
|
|
virtual void Run() = 0;
|
|
virtual bool Cancellable() const { return false; }
|
|
virtual void Cancel() {}
|
|
virtual void Release() { delete this; }
|
|
|
|
// Belongs to whichever thread manager queue holds the task.
|
|
TaskLink link_;
|
|
};
|
|
|
|
class Waitable {
|
|
public:
|
|
virtual ~Waitable() {}
|
|
|
|
virtual void Wait() = 0;
|
|
|
|
void WaitAndRelease() {
|
|
Wait();
|
|
delete this;
|
|
}
|
|
};
|
|
|
|
struct TaskThreadContext;
|
|
struct GlobalThreadContext;
|
|
|
|
class ThreadManager {
|
|
public:
|
|
ThreadManager();
|
|
~ThreadManager();
|
|
|
|
// The distinction here is to be able to take hyper-threading into account.
|
|
// It gets even trickier when you think about mobile chips with BIG/LITTLE, but we'll
|
|
// just ignore it and let the OS handle it.
|
|
void Init(int numCores, int numLogicalCoresPerCpu);
|
|
void EnqueueTask(Task *task);
|
|
// Directly assigns a task to a specific worker thread's private queue, bypassing
|
|
// the global queue and load balancing. Used to pin related tasks (e.g. tiles of a
|
|
// parallel loop) to distinct threads by index.
|
|
void EnqueueTaskOnThread(int threadNum, Task *task);
|
|
void Teardown();
|
|
|
|
bool IsInitialized() const;
|
|
|
|
// Parallel loops (assumed compute-limited) get one thread per logical core. We have a few extra threads too
|
|
// for I/O bounds tasks, that can be run concurrently with those.
|
|
int GetNumLooperThreads() const;
|
|
|
|
private:
|
|
void TeardownTask(Task *task);
|
|
|
|
// This is always pointing to a context, initialized in the constructor.
|
|
GlobalThreadContext *global_;
|
|
|
|
int numThreads_ = 0;
|
|
int numComputeThreads_ = 0;
|
|
|
|
friend struct TaskThreadContext;
|
|
};
|
|
|
|
extern ThreadManager g_threadManager;
|