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.
This commit is contained in:
libretroadmin committed 2026-10-09 04:56:16 +02:00
1 parent f0b97a2e90
commit f1208a7a5a
211 files changed
+5465 -5625

No files matched your search

+14 -17
View File
@@ -32,8 +32,8 @@
#include <cstdio>
#include <cstdlib>
#include <thread>
#include "Common/Net/Cancel.h"
#include "Common/Net/HTTPServer.h"
#include "Common/Net/NetBuffer.h"
#include "Common/Net/Sinks.h"
@@ -45,24 +45,23 @@
void NewThreadExecutor::Run(std::function<void()> func) {
// Every connection gets a thread, and we only ever joined them at shutdown - so a server that
// had served N connections was still holding N joinable std::threads. Reap the finished ones.
// had served N connections was still holding N joinable threads. Reap the finished ones.
Prune();
auto done = std::make_shared<std::atomic<bool>>(false);
Worker worker;
worker.done = done;
worker.thread = std::thread([func, done]() {
std::unique_ptr<Worker> worker(new Worker());
Worker *w = worker.get();
w->thread = Thread([func, w]() {
func();
done->store(true, std::memory_order_release);
retro_atomic_store_release_int(&w->done, 1);
});
workers_.push_back(std::move(worker));
}
void NewThreadExecutor::Prune() {
for (size_t i = 0; i < workers_.size(); ) {
if (workers_[i].done->load(std::memory_order_acquire)) {
if (retro_atomic_load_acquire_int(&workers_[i]->done)) {
// Set right at the end of the thread body, so this join returns essentially at once.
workers_[i].thread.join();
workers_[i]->thread.join();
workers_.erase(workers_.begin() + i);
} else {
++i;
@@ -72,8 +71,9 @@ void NewThreadExecutor::Prune() {
NewThreadExecutor::~NewThreadExecutor() {
// If Run was ever called...
for (auto &worker : workers_)
worker.thread.join();
for (auto &worker : workers_) {
worker->thread.join();
}
workers_.clear();
}
@@ -306,15 +306,12 @@ bool Server::Listen6(int port, bool ipv6_only, const char *reason) {
#endif
}
bool Server::RunSlice(double timeout) {
bool Server::RunSlice(const net::CancelToken *cancel) {
if (listenerSock_ < 0 || port_ == 0) {
return false;
}
if (timeout <= 0.0) {
timeout = 86400.0;
}
if (!fd_util::WaitUntilReady(listenerSock_, timeout, false)) {
if (net::WaitSocket((uintptr_t)listenerSock_, false, -1.0, cancel) != net::WaitResult::READY) {
return false;
}
@@ -343,7 +340,7 @@ bool Server::Run(int port) {
}
while (true) {
RunSlice(0.0);
RunSlice(nullptr);
}
// We'll never get here. Ever.