From d18910a61922c6ee82cf19d0da9d9c568babd879 Mon Sep 17 00:00:00 2001 From: Stenzek Date: Sun, 22 Jun 2025 00:27:48 +1000 Subject: [PATCH] Achievements: Release lock while waiting for HTTP requests --- src/core/achievements.cpp | 5 ++--- src/util/http_downloader.cpp | 16 ++++++++++++++++ src/util/http_downloader.h | 1 + 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/core/achievements.cpp b/src/core/achievements.cpp index 61b3c3580..287a3298e 100644 --- a/src/core/achievements.cpp +++ b/src/core/achievements.cpp @@ -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; diff --git a/src/util/http_downloader.cpp b/src/util/http_downloader.cpp index 4df8c73b1..96b08d7b9 100644 --- a/src/util/http_downloader.cpp +++ b/src/util/http_downloader.cpp @@ -214,6 +214,22 @@ void HTTPDownloader::WaitForAllRequests() } } +void HTTPDownloader::WaitForAllRequestsWithYield(std::function before_sleep_cb, + std::function 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); diff --git a/src/util/http_downloader.h b/src/util/http_downloader.h index 4fa2b95f3..8512b5e4e 100644 --- a/src/util/http_downloader.h +++ b/src/util/http_downloader.h @@ -78,6 +78,7 @@ public: ProgressCallback* progress = nullptr); void PollRequests(); void WaitForAllRequests(); + void WaitForAllRequestsWithYield(std::function before_sleep_cb, std::function after_sleep_cb); bool HasAnyRequests(); static const char DEFAULT_USER_AGENT[];