Skip to content

Play your Steam library's Windows games on a jailbroken PS5.

C++

Keep the preparation queue in order with Prepare next

Leonardo Lobato8bdeaf6e8b55
diff --git a/src/homebrew/controller.cpp b/src/homebrew/controller.cpp
index d860395..a7231fb 100644
--- a/src/homebrew/controller.cpp
+++ b/src/homebrew/controller.cpp
@@ -818,7 +818,7 @@ void Controller::TickRuntime() {
const auto id = it->first;
if (prepared.preparing_app == id) { it->second.seen_preparing = true; ++it; continue; }
// A preparation still queued in the runtime is not lost, however long it waits.
- const bool unseen_too_long = !it->second.seen_preparing && !prepared.queued_prepares.count(id) &&
+ const bool unseen_too_long = !it->second.seen_preparing && !PrepareQueued(prepared, id) &&
std::chrono::steady_clock::now() - it->second.submitted_at > std::chrono::seconds(15);
if (!it->second.seen_preparing && !unseen_too_long) { ++it; continue; }
const auto* record = record_of(id);
@@ -858,7 +858,7 @@ void Controller::TickRuntime() {
continue;
}
if (prepared.preparing_app == id) { pending.seen_preparing = true; ++it; continue; }
- const bool unseen_too_long = !pending.seen_preparing && !prepared.queued_prepares.count(id) &&
+ const bool unseen_too_long = !pending.seen_preparing && !PrepareQueued(prepared, id) &&
std::chrono::steady_clock::now() - pending.submitted_at > std::chrono::seconds(15);
if (!pending.seen_preparing && !unseen_too_long) { ++it; continue; }
const auto* record = record_of(id);
@@ -895,7 +895,7 @@ void Controller::TickRuntime() {
// Prepare ended without a Ready title, or was refused before it was seen starting:
// Play says why, so a press never ends with nothing on screen.
// Another game's preparation finishing says nothing about this one while it is still queued.
- const bool queued = prepared.queued_prepares.count(id) > 0;
+ const bool queued = PrepareQueued(prepared, id);
const bool handled = !queued && prepared.prepares_handled > pending.prepares_handled && prepared.preparing_app != id;
const bool unseen_too_long = !pending.seen_preparing && !queued && !prepared.preparing_app &&
std::chrono::steady_clock::now() - pending.queued_at > std::chrono::seconds(15);
@@ -1072,11 +1072,11 @@ void Controller::TickRuntime() {
snapshot.launch->state == runtime::LaunchState::kRunning);
// Updating or repairing waits while this game is being started or played, and a queued
// preparation is not asked for twice.
- if ((launching && busy) || snapshot.queued_prepares.count(id)) game.can_prepare = false;
+ if ((launching && busy) || PrepareQueued(snapshot, id)) game.can_prepare = false;
// Queued behind another game's setup, from the runtime's queue or a Play that waits to ask it.
const auto waiting_play = play_after_prepare_.find(id);
game.queued = snapshot.preparing_app && *snapshot.preparing_app != id &&
- (snapshot.queued_prepares.count(id) ||
+ (PrepareQueued(snapshot, id) ||
(waiting_play != play_after_prepare_.end() && !waiting_play->second.queued));
game.can_play = game.ready && !busy && snapshot.preparing_app != id && !cloud_busy_.count(id) &&
play_queued_.empty() && !sync_before_play_.count(id);
diff --git a/src/homebrew/runtime_host.cpp b/src/homebrew/runtime_host.cpp
index 6d75aa2..0e4c862 100644
--- a/src/homebrew/runtime_host.cpp
+++ b/src/homebrew/runtime_host.cpp
@@ -411,14 +411,46 @@ void RuntimeHost::Prepare(RuntimePrepareRequest request) {
{
std::lock_guard<std::mutex> lock(mutex_);
if (stopping_) return;
- snapshot_.queued_prepares.insert(request.app_id);
+ // A game asked for again keeps its place, with the newer request (launch options may have arrived).
+ const auto queued = std::find_if(prepare_requests_.begin(), prepare_requests_.end(),
+ [&](const auto& waiting) { return waiting.app_id == request.app_id; });
+ if (queued != prepare_requests_.end()) {
+ *queued = std::move(request);
+ } else {
+ snapshot_.queued_prepares.push_back(request.app_id);
+ prepare_requests_.push_back(std::move(request));
+ }
++snapshot_.revision;
- prepare_requests_.push_back(std::move(request));
}
published_.notify_all();
wake_.notify_all();
}
+bool RuntimeHost::PrepareNext(std::uint32_t app_id) {
+ {
+ std::lock_guard<std::mutex> lock(mutex_);
+ const auto queued = std::find_if(prepare_requests_.begin(), prepare_requests_.end(),
+ [&](const auto& waiting) { return waiting.app_id == app_id; });
+ if (queued == prepare_requests_.end()) return false;
+ // The others keep their order behind it.
+ std::rotate(prepare_requests_.begin(), queued, queued + 1);
+ snapshot_.queued_prepares.clear();
+ for (const auto& waiting : prepare_requests_) snapshot_.queued_prepares.push_back(waiting.app_id);
+ ++snapshot_.revision;
+ }
+ published_.notify_all();
+ return true;
+}
+
+void RuntimeHost::HoldPreparations(bool hold) {
+ {
+ std::lock_guard<std::mutex> lock(mutex_);
+ if (prepares_held_ == hold) return;
+ prepares_held_ = hold;
+ }
+ wake_.notify_all();
+}
+
void RuntimeHost::Play(std::uint32_t app_id) {
{
std::lock_guard<std::mutex> lock(mutex_);
@@ -3078,7 +3110,6 @@ void RuntimeHost::Run() {
}
startup_hold.reset();
for (;;) {
- std::vector<RuntimePrepareRequest> requests;
std::vector<std::uint32_t> plays;
std::vector<std::pair<std::uint32_t, bool>> removals;
std::vector<std::uint32_t> kept_deletes;
@@ -3094,7 +3125,8 @@ void RuntimeHost::Run() {
// Preparation changes install records that Handoff reads on the
// application thread, so it waits while a handoff is pending.
const auto ready = [this] {
- return stopping_ || (!prepare_requests_.empty() && !snapshot_.handoff_ready) || !play_requests_.empty() ||
+ return stopping_ || (!prepare_requests_.empty() && !snapshot_.handoff_ready && !prepares_held_) ||
+ !play_requests_.empty() ||
cleanup_requested_ || measure_requested_ || !activity_requests_.empty() || title_names_changed_ ||
(!remove_requests_.empty() && !snapshot_.handoff_ready) ||
(!kept_delete_requests_.empty() && !snapshot_.handoff_ready) ||
@@ -3104,7 +3136,6 @@ void RuntimeHost::Run() {
if (launch_poll_) wake_.wait_for(lock, kLaunchPollInterval, ready);
else wake_.wait(lock, [&] { return ready() || launch_poll_; });
if (stopping_) break;
- if (!snapshot_.handoff_ready) requests.swap(prepare_requests_);
plays.swap(play_requests_);
// Removal changes install records that Handoff reads, like preparation.
if (!snapshot_.handoff_ready) removals.swap(remove_requests_);
@@ -3227,29 +3258,33 @@ void RuntimeHost::Run() {
std::lock_guard<std::mutex> lock(mutex_);
launch_poll_ = again;
}
- for (std::size_t index = 0; index < requests.size(); ++index) {
- const auto& request = requests[index];
- // A handoff that became ready during this pass reads install records on
- // the application thread: hold the remaining preparations until it is done.
- {
- std::lock_guard<std::mutex> lock(mutex_);
- if (snapshot_.handoff_ready) {
- prepare_requests_.insert(prepare_requests_.begin(), requests.begin() + static_cast<std::ptrdiff_t>(index),
- requests.end());
- break;
+ // One preparation per pass, taken from the front of the queue only as it starts, so PrepareNext
+ // reorders everything not started yet. A handoff that became ready during this pass reads install
+ // records on the application thread, and a quit to Home holds the queue: the queue waits for both.
+ std::optional<RuntimePrepareRequest> next;
+ {
+ std::lock_guard<std::mutex> lock(mutex_);
+ if (!prepare_requests_.empty() && !snapshot_.handoff_ready && !prepares_held_ && !stopping_) {
+ next = std::move(prepare_requests_.front());
+ prepare_requests_.erase(prepare_requests_.begin());
+ auto& queued = snapshot_.queued_prepares;
+ queued.erase(std::remove(queued.begin(), queued.end(), next->app_id), queued.end());
+ if (composition) {
+ snapshot_.preparing_app = next->app_id;
+ snapshot_.refusals.erase(next->app_id);
}
+ ++snapshot_.revision;
}
+ }
+ if (next) {
+ published_.notify_all();
+ const auto& request = *next;
std::string refusal;
bool awaiting = false;
if (!composition) {
const auto current = Snapshot();
refusal = current.reason.empty() ? "The runtime services are unavailable." : current.reason;
} else {
- Publish([&](RuntimeSnapshot& s) {
- s.preparing_app = request.app_id;
- s.queued_prepares.erase(request.app_id);
- s.refusals.erase(request.app_id);
- });
refusal = RunPrepare(*composition, request, &awaiting);
// A resumed publication of a moved game may have been the last the 0.6 move waited for.
FinishGamesMove(*composition);
@@ -3276,7 +3311,6 @@ void RuntimeHost::Run() {
ClearWork();
Publish([&](RuntimeSnapshot& s) {
s.preparing_app.reset();
- s.queued_prepares.erase(request.app_id);
++s.prepares_handled;
if (refusal.empty()) s.refusals.erase(request.app_id);
else s.refusals[request.app_id] = refusal;
diff --git a/src/homebrew/runtime_host.hpp b/src/homebrew/runtime_host.hpp
index 234be92..831338a 100644
--- a/src/homebrew/runtime_host.hpp
+++ b/src/homebrew/runtime_host.hpp
@@ -11,6 +11,7 @@
#include "runtime/launch_service.h"
#include "runtime/package_installer.h"
+#include <algorithm>
#include <chrono>
#include <condition_variable>
#include "runtime/layout_migration.h"
@@ -229,9 +230,9 @@ struct RuntimeSnapshot {
std::map<std::uint32_t, std::string> refusals;
std::uint64_t prepares_handled = 0;
std::optional<std::uint32_t> preparing_app; // the game being prepared now
- // Games whose preparation is asked for but not started yet: it waits behind another game's
- // preparation, or for the runtime to finish starting.
- std::set<std::uint32_t> queued_prepares;
+ // Games whose preparation is asked for but not started yet, in the order they will start: each
+ // waits behind another game's preparation, or for the runtime to finish starting.
+ std::vector<std::uint32_t> queued_prepares;
// How far the current copy (preparing_app) or deletion (removing_app) is, when
// that is measurable; absent for steps whose progress is unknown.
std::optional<float> work_progress;
@@ -270,6 +271,12 @@ struct RuntimeSnapshot {
std::map<std::uint32_t, std::string> play_progress;
};
+// Whether the game's preparation waits in the runtime's queue.
+inline bool PrepareQueued(const RuntimeSnapshot& snapshot, std::uint32_t app_id) {
+ return std::find(snapshot.queued_prepares.begin(), snapshot.queued_prepares.end(), app_id) !=
+ snapshot.queued_prepares.end();
+}
+
inline constexpr const char* kPlayUpdatingForLayout = "Updating this game for Playden 0.6…";
// A downloaded game to prepare. The game files stay at install_path.
@@ -304,8 +311,14 @@ class RuntimeHost {
// The same state without a copy per call: shared and read-only, made again only after a change.
// The UI reads it many times a frame; Snapshot() copies every field each time.
std::shared_ptr<const RuntimeSnapshot> Shared() const;
- // Queues preparation of a downloaded game, or a retry of a failed one.
+ // Queues preparation of a downloaded game, or a retry of a failed one. Games are prepared one
+ // at a time, in the order asked; asking again for a queued game keeps its place.
void Prepare(RuntimePrepareRequest request);
+ // Moves a queued preparation to the front of the queue. The one running now finishes first.
+ // False when the game is not queued (it started or was never asked for): nothing changes.
+ bool PrepareNext(std::uint32_t app_id);
+ // While held, no queued preparation starts (a quit to Home waits for the running one only).
+ void HoldPreparations(bool hold);
// Asks the console whether a game's PS5 title runs (from any origin, Home
// included). A game with no prepared title reports stopped.
void CheckActivity(std::uint32_t app_id);
@@ -464,6 +477,7 @@ class RuntimeHost {
bool launch_poll_ = false;
bool cleanup_requested_ = false;
bool measure_requested_ = false;
+ bool prepares_held_ = false;
std::map<std::uint32_t, std::string> title_names_; // guarded by mutex_
std::string steam_account_name_; // guarded by mutex_
std::uint64_t steam_id_ = 0; // guarded by mutex_
diff --git a/tests/homebrew_runtime_host_test.cpp b/tests/homebrew_runtime_host_test.cpp
index c61442f..6e56a93 100644
--- a/tests/homebrew_runtime_host_test.cpp
+++ b/tests/homebrew_runtime_host_test.cpp
@@ -19,6 +19,7 @@
#include <sys/stat.h>
#include <thread>
#include <atomic>
+#include <condition_variable>
#include <memory>
#include <unistd.h>
@@ -315,13 +316,110 @@ void QueuedPreparationIsPublishedUntilTaken() {
Fixture fixture;
RuntimeHost host(fixture.Config());
host.Prepare(Hl2());
- CHECK(host.Snapshot().queued_prepares.count(220) == 1);
+ CHECK(host.Snapshot().queued_prepares == std::vector<std::uint32_t>{220});
CHECK(host.Start());
const auto refused = WaitFor(host, [](const RuntimeSnapshot& s) { return s.refusals.count(220) != 0; });
CHECK(refused.queued_prepares.empty() && !refused.preparing_app);
host.Shutdown();
}
+RuntimePrepareRequest Game(std::uint32_t app) {
+ return {app, "/data/homebrew/FAKE77031/games/" + std::to_string(app), "internal", "Game " + std::to_string(app)};
+}
+
+// The order the worker finished preparations in, from its log.
+struct PrepareLog {
+ std::mutex mutex;
+ std::vector<std::uint32_t> order;
+ void Attach(RuntimeHostConfig& config) {
+ config.log = [this](const std::string& line) {
+ constexpr const char* kResult = "prepare result app=";
+ if (line.rfind(kResult, 0) != 0) return;
+ std::lock_guard<std::mutex> lock(mutex);
+ order.push_back(static_cast<std::uint32_t>(std::stoul(line.substr(std::strlen(kResult)))));
+ };
+ }
+ std::vector<std::uint32_t> Order() {
+ std::lock_guard<std::mutex> lock(mutex);
+ return order;
+ }
+};
+
+// Preparations run one at a time in the order asked. Prepare next moves a waiting game to the front even
+// after the worker began a pass with every request waiting; the others keep their order. Asking again
+// for a queued game keeps its place, and a game that is not queued is left alone.
+void PrepareNextReordersWaitingPreparations() {
+ Fixture fixture;
+ auto config = fixture.Config();
+ PrepareLog log;
+ log.Attach(config);
+ std::mutex gate_mutex;
+ std::condition_variable gate_changed;
+ bool block = false;
+ bool blocked = false;
+ config.hold_effective_uid = [&]() -> std::shared_ptr<void> {
+ std::unique_lock<std::mutex> lock(gate_mutex);
+ if (block) {
+ blocked = true;
+ gate_changed.notify_all();
+ gate_changed.wait(lock, [&] { return !block; });
+ }
+ return nullptr;
+ };
+ config.admit_kit = [](std::string* reason) -> std::optional<RuntimeKit> {
+ *reason = "No verified compatibility kit is installed.";
+ return std::nullopt;
+ };
+ RuntimeHost host(std::move(config));
+ CHECK(host.Start());
+ CHECK(WaitFor(host, Started).status == RuntimeHostStatus::kReady);
+ {
+ std::lock_guard<std::mutex> lock(gate_mutex);
+ block = true;
+ }
+ host.Prepare(Game(10));
+ host.Prepare(Game(20));
+ host.Prepare(Game(30));
+ host.Prepare(Game(40));
+ host.Prepare(Game(20));
+ {
+ std::unique_lock<std::mutex> lock(gate_mutex);
+ CHECK(gate_changed.wait_for(lock, std::chrono::seconds(5), [&] { return blocked; }));
+ }
+ CHECK((host.Snapshot().queued_prepares == std::vector<std::uint32_t>{10, 20, 30, 40}));
+ CHECK(host.PrepareNext(30));
+ CHECK(host.PrepareNext(30)); // already first: nothing moves
+ CHECK(!host.PrepareNext(99));
+ CHECK((host.Snapshot().queued_prepares == std::vector<std::uint32_t>{30, 10, 20, 40}));
+ {
+ std::lock_guard<std::mutex> lock(gate_mutex);
+ block = false;
+ }
+ gate_changed.notify_all();
+ const auto done = WaitFor(host, [](const RuntimeSnapshot& s) { return s.prepares_handled == 4; });
+ CHECK(done.queued_prepares.empty() && !done.preparing_app);
+ CHECK((log.Order() == std::vector<std::uint32_t>{30, 10, 20, 40}));
+ CHECK(!host.PrepareNext(30)); // handled: no longer queued
+ host.Shutdown();
+}
+
+// A quit to Home holds the queue: queued preparations wait until the hold is released.
+void HeldPreparationsWait() {
+ Fixture fixture;
+ RuntimeHost host(fixture.Config());
+ CHECK(host.Start());
+ CHECK(WaitFor(host, Started).status == RuntimeHostStatus::kReady);
+ host.HoldPreparations(true);
+ host.Prepare(Hl2());
+ usleep(100000);
+ const auto held = host.Snapshot();
+ CHECK(held.prepares_handled == 0 && held.queued_prepares == std::vector<std::uint32_t>{220});
+ host.HoldPreparations(false);
+ const auto refused = WaitFor(host, [](const RuntimeSnapshot& s) { return s.refusals.count(220) != 0; });
+ CHECK(refused.queued_prepares.empty());
+ host.Shutdown();
+}
+
// Startup reads durable records only. A journaled record is reported as needing
// recovery; it is not resumed, re-registered or rewritten.
void StartupReportsJournalsWithoutResuming() {
@@ -3683,6 +3781,8 @@ int main(int argc, char** argv) {
WorkerMovesGamesUnderPublicationLease();
NoKitRefusesPreparationWithoutDurableEffects();
QueuedPreparationIsPublishedUntilTaken();
+ PrepareNextReordersWaitingPreparations();
+ HeldPreparationsWait();
HoldsEffectiveUidWhileWorking();
StartupReportsJournalsWithoutResuming();
PublicStateRootIsUnavailable();