mirror of
https://github.com/hrydgard/ppsspp.git
synced 2026-10-16 07:50:28 +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.
120 lines
3.4 KiB
C++
120 lines
3.4 KiB
C++
#include "Common/Net/SocketCompat.h"
|
|
#include "Core/HLE/NetInetConstants.h"
|
|
#include "Core/HLE/SocketManager.h"
|
|
#include "Common/Log.h"
|
|
|
|
|
|
// sceNetInet only, on the emulation thread.
|
|
SocketManager g_socketManager;
|
|
|
|
InetSocket *SocketManager::CreateSocket(int *index, int *returned_errno, SocketState state, int domain, int type, int protocol) {
|
|
_dbg_assert_(state != SocketState::Unused);
|
|
|
|
int hostDomain = convertSocketDomainPSP2Host(domain);
|
|
int hostType = convertSocketTypePSP2Host(type);
|
|
int hostProtocol = convertSocketProtoPSP2Host(protocol);
|
|
|
|
SOCKET hostSock = ::socket(hostDomain, hostType, hostProtocol);
|
|
if (hostSock < 0) {
|
|
*returned_errno = socket_errno;
|
|
return nullptr;
|
|
}
|
|
|
|
for (int i = MIN_VALID_INET_SOCKET; i < ARRAY_SIZE(inetSockets_); i++) {
|
|
if (inetSockets_[i].state == SocketState::Unused) {
|
|
*index = i;
|
|
InetSocket *inetSock = inetSockets_ + i;
|
|
*inetSock = {}; // Reset to default.
|
|
inetSock->sock = hostSock;
|
|
inetSock->state = state;
|
|
inetSock->domain = domain;
|
|
inetSock->type = type;
|
|
inetSock->protocol = protocol;
|
|
inetSock->nonblocking = false;
|
|
*returned_errno = 0;
|
|
return inetSock;
|
|
}
|
|
}
|
|
_dbg_assert_(false);
|
|
|
|
ERROR_LOG(Log::sceNet, "Ran out of socket handles! This is BAD.");
|
|
closesocket(hostSock);
|
|
*index = 0;
|
|
*returned_errno = ENOMEM; // or something..
|
|
return nullptr;
|
|
}
|
|
|
|
InetSocket *SocketManager::AdoptSocket(int *index, SOCKET hostSocket, const InetSocket *derive) {
|
|
for (int i = MIN_VALID_INET_SOCKET; i < ARRAY_SIZE(inetSockets_); i++) {
|
|
if (inetSockets_[i].state == SocketState::Unused) {
|
|
*index = i;
|
|
|
|
InetSocket *inetSock = inetSockets_ + i;
|
|
inetSock->sock = hostSocket;
|
|
inetSock->state = derive->state;
|
|
inetSock->domain = derive->domain;
|
|
inetSock->type = derive->type;
|
|
inetSock->protocol = derive->protocol;
|
|
inetSock->nonblocking = derive->nonblocking; // should we inherit blocking state?
|
|
return inetSock;
|
|
}
|
|
}
|
|
|
|
// No space? Return nullptr and let the caller handle it. Shouldn't ever happen.
|
|
*index = 0;
|
|
return nullptr;
|
|
}
|
|
|
|
bool SocketManager::Close(InetSocket *inetSocket) {
|
|
_dbg_assert_(inetSocket->state != SocketState::Unused);
|
|
if (closesocket(inetSocket->sock) != 0) {
|
|
ERROR_LOG(Log::sceNet, "closesocket(%d) failed", inetSocket->sock);
|
|
return false;
|
|
}
|
|
inetSocket->state = SocketState::Unused;
|
|
inetSocket->sock = 0;
|
|
return true;
|
|
}
|
|
|
|
bool SocketManager::GetInetSocket(int sock, InetSocket **inetSocket) {
|
|
if (sock < MIN_VALID_INET_SOCKET || sock >= ARRAY_SIZE(inetSockets_) || inetSockets_[sock].state == SocketState::Unused) {
|
|
*inetSocket = nullptr;
|
|
return false;
|
|
}
|
|
*inetSocket = inetSockets_ + sock;
|
|
return true;
|
|
}
|
|
|
|
// Simplified mappers, only really useful in select/poll
|
|
SOCKET SocketManager::GetHostSocketFromInetSocket(int sock) {
|
|
if (sock < MIN_VALID_INET_SOCKET || sock >= ARRAY_SIZE(inetSockets_) || inetSockets_[sock].state == SocketState::Unused) {
|
|
_dbg_assert_(false);
|
|
return -1;
|
|
}
|
|
if (sock == 0) {
|
|
// Map 0 to 0, special case.
|
|
return 0;
|
|
}
|
|
return inetSockets_[sock].sock;
|
|
}
|
|
|
|
void SocketManager::CloseAll() {
|
|
for (auto &sock : inetSockets_) {
|
|
if (sock.state != SocketState::Unused) {
|
|
closesocket(sock.sock);
|
|
}
|
|
sock.state = SocketState::Unused;
|
|
sock.sock = 0;
|
|
}
|
|
}
|
|
|
|
const char *SocketStateToString(SocketState state) {
|
|
switch (state) {
|
|
case SocketState::Unused: return "unused";
|
|
case SocketState::UsedNetInet: return "netInet";
|
|
case SocketState::UsedProAdhoc: return "proAdhoc";
|
|
default:
|
|
return "N/A";
|
|
}
|
|
}
|