mirror of
https://github.com/hrydgard/ppsspp.git
synced 2026-10-16 16:00:30 +02:00
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:
1 parent
f0b97a2e90
commit
f1208a7a5a
211 files changed
+5465
-5625
No files matched your search
@@ -27,104 +27,106 @@ HTTPFileLoader::HTTPFileLoader(const ::Path &filename)
|
||||
}
|
||||
|
||||
void HTTPFileLoader::Prepare() {
|
||||
std::call_once(preparedFlag_, [this](){
|
||||
client_.SetUserAgent(StringFromFormat("PPSSPP/%s", PPSSPP_GIT_VERSION));
|
||||
if (prepared_) {
|
||||
return;
|
||||
}
|
||||
prepared_ = true;
|
||||
client_.SetUserAgent(StringFromFormat("PPSSPP/%s", PPSSPP_GIT_VERSION));
|
||||
|
||||
std::vector<std::string> responseHeaders;
|
||||
Url resourceURL = url_;
|
||||
int redirectsLeft = 20;
|
||||
while (redirectsLeft > 0) {
|
||||
responseHeaders.clear();
|
||||
int code = SendHEAD(resourceURL, responseHeaders);
|
||||
if (code == -400) {
|
||||
// Already reported the error.
|
||||
return;
|
||||
}
|
||||
|
||||
if (code == 301 || code == 302 || code == 303 || code == 307 || code == 308) {
|
||||
Disconnect();
|
||||
|
||||
std::string redirectURL;
|
||||
if (http::GetHeaderValue(responseHeaders, "Location", &redirectURL)) {
|
||||
Url url(resourceURL);
|
||||
url = url.Relative(redirectURL);
|
||||
|
||||
if (url.ToString() == url_.ToString() || url.ToString() == resourceURL.ToString()) {
|
||||
ERROR_LOG(Log::Loader, "HTTP request failed, hit a redirect loop");
|
||||
latestError_ = "Could not connect (redirect loop)";
|
||||
return;
|
||||
}
|
||||
|
||||
resourceURL = url;
|
||||
redirectsLeft--;
|
||||
continue;
|
||||
}
|
||||
|
||||
// No Location header?
|
||||
ERROR_LOG(Log::Loader, "HTTP request failed, invalid redirect");
|
||||
latestError_ = "Could not connect (invalid response)";
|
||||
return;
|
||||
}
|
||||
|
||||
if (code != 200) {
|
||||
// Leave size at 0, invalid.
|
||||
ERROR_LOG(Log::Loader, "HTTP request failed, got %03d for %s", code, filename_.c_str());
|
||||
latestError_ = "Could not connect (invalid response)";
|
||||
Disconnect();
|
||||
return;
|
||||
}
|
||||
|
||||
// We got a good, non-redirect response.
|
||||
redirectsLeft = 0;
|
||||
url_ = resourceURL;
|
||||
std::vector<std::string> responseHeaders;
|
||||
Url resourceURL = url_;
|
||||
int redirectsLeft = 20;
|
||||
while (redirectsLeft > 0) {
|
||||
responseHeaders.clear();
|
||||
int code = SendHEAD(resourceURL, responseHeaders);
|
||||
if (code == -400) {
|
||||
// Already reported the error.
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO: Expire cache via ETag, etc.
|
||||
bool acceptsRange = false;
|
||||
for (std::string header : responseHeaders) {
|
||||
if (startsWithNoCase(header, "Content-Length:")) {
|
||||
size_t size_pos = header.find_first_of(' ');
|
||||
if (size_pos != header.npos) {
|
||||
size_pos = header.find_first_not_of(' ', size_pos);
|
||||
}
|
||||
if (size_pos != header.npos) {
|
||||
filesize_ = atoll(&header[size_pos]);
|
||||
if (code == 301 || code == 302 || code == 303 || code == 307 || code == 308) {
|
||||
Disconnect();
|
||||
|
||||
std::string redirectURL;
|
||||
if (http::GetHeaderValue(responseHeaders, "Location", &redirectURL)) {
|
||||
Url url(resourceURL);
|
||||
url = url.Relative(redirectURL);
|
||||
|
||||
if (url.ToString() == url_.ToString() || url.ToString() == resourceURL.ToString()) {
|
||||
ERROR_LOG(Log::Loader, "HTTP request failed, hit a redirect loop");
|
||||
SetError("Could not connect (redirect loop)");
|
||||
return;
|
||||
}
|
||||
|
||||
resourceURL = url;
|
||||
redirectsLeft--;
|
||||
continue;
|
||||
}
|
||||
if (startsWithNoCase(header, "Accept-Ranges:")) {
|
||||
std::string lowerHeader = header;
|
||||
std::transform(lowerHeader.begin(), lowerHeader.end(), lowerHeader.begin(), tolower);
|
||||
// TODO: Delimited.
|
||||
if (lowerHeader.find("bytes") != lowerHeader.npos) {
|
||||
acceptsRange = true;
|
||||
}
|
||||
|
||||
// No Location header?
|
||||
ERROR_LOG(Log::Loader, "HTTP request failed, invalid redirect");
|
||||
SetError("Could not connect (invalid response)");
|
||||
return;
|
||||
}
|
||||
|
||||
if (code != 200) {
|
||||
// Leave size at 0, invalid.
|
||||
ERROR_LOG(Log::Loader, "HTTP request failed, got %03d for %s", code, filename_.c_str());
|
||||
SetError("Could not connect (invalid response)");
|
||||
Disconnect();
|
||||
return;
|
||||
}
|
||||
|
||||
// We got a good, non-redirect response.
|
||||
redirectsLeft = 0;
|
||||
url_ = resourceURL;
|
||||
}
|
||||
|
||||
// TODO: Expire cache via ETag, etc.
|
||||
bool acceptsRange = false;
|
||||
for (std::string header : responseHeaders) {
|
||||
if (startsWithNoCase(header, "Content-Length:")) {
|
||||
size_t size_pos = header.find_first_of(' ');
|
||||
if (size_pos != header.npos) {
|
||||
size_pos = header.find_first_not_of(' ', size_pos);
|
||||
}
|
||||
if (size_pos != header.npos) {
|
||||
filesize_ = atoll(&header[size_pos]);
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Keepalive instead.
|
||||
Disconnect();
|
||||
|
||||
if (!acceptsRange) {
|
||||
WARN_LOG(Log::Loader, "HTTP server did not advertise support for range requests.");
|
||||
}
|
||||
if (filesize_ == 0) {
|
||||
ERROR_LOG(Log::Loader, "Could not determine file size for %s", filename_.c_str());
|
||||
if (startsWithNoCase(header, "Accept-Ranges:")) {
|
||||
std::string lowerHeader = header;
|
||||
std::transform(lowerHeader.begin(), lowerHeader.end(), lowerHeader.begin(), tolower);
|
||||
// TODO: Delimited.
|
||||
if (lowerHeader.find("bytes") != lowerHeader.npos) {
|
||||
acceptsRange = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If we didn't end up with a filesize_ (e.g. chunked response), give up. File invalid.
|
||||
});
|
||||
// TODO: Keepalive instead.
|
||||
Disconnect();
|
||||
|
||||
if (!acceptsRange) {
|
||||
WARN_LOG(Log::Loader, "HTTP server did not advertise support for range requests.");
|
||||
}
|
||||
if (filesize_ == 0) {
|
||||
ERROR_LOG(Log::Loader, "Could not determine file size for %s", filename_.c_str());
|
||||
}
|
||||
|
||||
// If we didn't end up with a filesize_ (e.g. chunked response), give up. File invalid.
|
||||
}
|
||||
|
||||
int HTTPFileLoader::SendHEAD(const Url &url, std::vector<std::string> &responseHeaders) {
|
||||
if (!url.Valid()) {
|
||||
ERROR_LOG(Log::Loader, "HTTP request failed, invalid URL: '%s'", url.ToString().c_str());
|
||||
latestError_ = "Invalid URL";
|
||||
SetError("Invalid URL");
|
||||
return -400;
|
||||
}
|
||||
|
||||
if (!client_.Resolve(url.Host().c_str(), url.Port())) {
|
||||
ERROR_LOG(Log::Loader, "HTTP request failed, unable to resolve: |%s| port %d", url.Host().c_str(), url.Port());
|
||||
latestError_ = "Could not connect (name not resolved)";
|
||||
SetError("Could not connect (name not resolved)");
|
||||
return -400;
|
||||
}
|
||||
|
||||
@@ -134,7 +136,7 @@ int HTTPFileLoader::SendHEAD(const Url &url, std::vector<std::string> &responseH
|
||||
Connect(10.0);
|
||||
if (!connected_) {
|
||||
ERROR_LOG(Log::Loader, "HTTP request failed, failed to connect: %s port %d (resource: '%s')", url.Host().c_str(), url.Port(), url.Resource().c_str());
|
||||
latestError_ = "Could not connect (refused to connect)";
|
||||
SetError("Could not connect (refused to connect)");
|
||||
return -400;
|
||||
}
|
||||
|
||||
@@ -142,7 +144,7 @@ int HTTPFileLoader::SendHEAD(const Url &url, std::vector<std::string> &responseH
|
||||
int err = client_.SendRequest("HEAD", req, nullptr, &progress_);
|
||||
if (err < 0) {
|
||||
ERROR_LOG(Log::Loader, "HTTP request failed, failed to send request: %s port %d", url.Host().c_str(), url.Port());
|
||||
latestError_ = "Could not connect (could not request data)";
|
||||
SetError("Could not connect (could not request data)");
|
||||
Disconnect();
|
||||
return -400;
|
||||
}
|
||||
@@ -180,7 +182,6 @@ Path HTTPFileLoader::GetPath() const {
|
||||
|
||||
size_t HTTPFileLoader::ReadAt(s64 absolutePos, size_t bytes, void *data, Flags flags) {
|
||||
Prepare();
|
||||
std::lock_guard<std::mutex> guard(readAtMutex_);
|
||||
|
||||
s64 absoluteEnd = std::min(absolutePos + (s64)bytes, filesize_);
|
||||
if (absolutePos >= filesize_ || bytes == 0) {
|
||||
@@ -201,7 +202,7 @@ size_t HTTPFileLoader::ReadAt(s64 absolutePos, size_t bytes, void *data, Flags f
|
||||
http::RequestParams req(url_.Resource(), "*/*");
|
||||
int err = client_.SendRequest("GET", req, requestHeaders, &progress_);
|
||||
if (err < 0) {
|
||||
latestError_ = "Invalid response reading data";
|
||||
SetError("Invalid response reading data");
|
||||
Disconnect();
|
||||
return 0;
|
||||
}
|
||||
@@ -211,7 +212,7 @@ size_t HTTPFileLoader::ReadAt(s64 absolutePos, size_t bytes, void *data, Flags f
|
||||
int code = client_.ReadResponseHeaders(&readbuf, responseHeaders, &progress_);
|
||||
if (code != 206) {
|
||||
ERROR_LOG(Log::Loader, "HTTP server did not respond with range, received code=%03d", code);
|
||||
latestError_ = "Invalid response reading data";
|
||||
SetError("Invalid response reading data");
|
||||
Disconnect();
|
||||
return 0;
|
||||
}
|
||||
@@ -250,7 +251,7 @@ size_t HTTPFileLoader::ReadAt(s64 absolutePos, size_t bytes, void *data, Flags f
|
||||
|
||||
if (!supportedResponse) {
|
||||
ERROR_LOG(Log::Loader, "HTTP server did not respond with the range we wanted.");
|
||||
latestError_ = "Invalid response reading data";
|
||||
SetError("Invalid response reading data");
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user