Achievements: Release lock while waiting for HTTP requests

This commit is contained in:
Stenzek 2025-06-22 00:27:48 +10:00
parent ac9be0110f
commit d18910a619
No known key found for this signature in database
3 changed files with 19 additions and 3 deletions

View File

@ -2013,7 +2013,7 @@ bool Achievements::Login(const char* username, const char* password, Error* erro
}
// Wait until the login request completes.
http->WaitForAllRequests();
http->WaitForAllRequestsWithYield([&lock]() { lock.unlock(); }, [&lock]() { lock.lock(); });
Assert(!params.request);
// Success? Assume the callback set the error message.
@ -3413,8 +3413,7 @@ void Achievements::DrawLeaderboardEntry(const rc_client_leaderboard_entry_t& ent
ImRect bb;
bool visible, hovered;
bool pressed =
ImGuiFullscreen::MenuButtonFrame(entry.user, UIStyle.LargeFontSize, true, &bb, &visible, &hovered);
bool pressed = ImGuiFullscreen::MenuButtonFrame(entry.user, UIStyle.LargeFontSize, true, &bb, &visible, &hovered);
if (!visible)
return;

View File

@ -214,6 +214,22 @@ void HTTPDownloader::WaitForAllRequests()
}
}
void HTTPDownloader::WaitForAllRequestsWithYield(std::function<void()> before_sleep_cb,
std::function<void()> after_sleep_cb)
{
std::unique_lock lock(m_pending_http_request_lock);
while (!m_pending_http_requests.empty())
{
// Don't burn too much CPU.
if (before_sleep_cb)
before_sleep_cb();
Timer::NanoSleep(1000000);
if (after_sleep_cb)
after_sleep_cb();
LockedPollRequests(lock);
}
}
void HTTPDownloader::LockedAddRequest(Request* request)
{
m_pending_http_requests.push_back(request);

View File

@ -78,6 +78,7 @@ public:
ProgressCallback* progress = nullptr);
void PollRequests();
void WaitForAllRequests();
void WaitForAllRequestsWithYield(std::function<void()> before_sleep_cb, std::function<void()> after_sleep_cb);
bool HasAnyRequests();
static const char DEFAULT_USER_AGENT[];