From 6b77cbb0c053d52349fe2fd145004343b88efd3b Mon Sep 17 00:00:00 2001 From: ITotalJustice <47043333+ITotalJustice@users.noreply.github.com> Date: Mon, 9 Jun 2025 12:32:43 +0100 Subject: [PATCH] enable boost mode as early as possible during init, and exit boost as late as possible during exit. --- sphaira/include/app.hpp | 22 ++++++++++++++++++++++ sphaira/source/app.cpp | 8 ++++---- sphaira/source/main.cpp | 5 ++++- sphaira/source/ui/menus/appstore.cpp | 4 ++-- sphaira/source/ui/menus/filebrowser.cpp | 4 ++-- sphaira/source/ui/menus/game_menu.cpp | 4 ++-- sphaira/source/ui/menus/gc_menu.cpp | 4 ++-- sphaira/source/ui/menus/themezer.cpp | 4 ++-- sphaira/source/ui/progress_box.cpp | 4 ++-- 9 files changed, 42 insertions(+), 17 deletions(-) diff --git a/sphaira/include/app.hpp b/sphaira/include/app.hpp index 5628593..4b72943 100644 --- a/sphaira/include/app.hpp +++ b/sphaira/include/app.hpp @@ -192,6 +192,28 @@ public: } } + static void SetBoostMode(bool enable, bool force = false) { + static Mutex mutex{}; + static int ref_count{}; + + mutexLock(&mutex); + ON_SCOPE_EXIT(mutexUnlock(&mutex)); + + if (enable) { + ref_count++; + appletSetCpuBoostMode(ApmCpuBoostMode_FastLoad); + } else { + if (ref_count) { + ref_count--; + } + } + + if (!ref_count || force) { + ref_count = 0; + appletSetCpuBoostMode(ApmCpuBoostMode_Normal); + } + } + static auto GetAccountList() -> std::vector { std::vector out; diff --git a/sphaira/source/app.cpp b/sphaira/source/app.cpp index 3325815..55fbc89 100644 --- a/sphaira/source/app.cpp +++ b/sphaira/source/app.cpp @@ -1283,8 +1283,8 @@ void App::ScanThemeEntries() { App::App(const char* argv0) { TimeStamp ts; - appletSetCpuBoostMode(ApmCpuBoostMode_FastLoad); - ON_SCOPE_EXIT(appletSetCpuBoostMode(ApmCpuBoostMode_Normal)); + // boost mode is enabled in userAppInit(). + ON_SCOPE_EXIT(App::SetBoostMode(false)); g_app = this; m_start_timestamp = armGetSystemTick(); @@ -1851,8 +1851,8 @@ void App::DisplayDumpOptions(bool left_side) { } App::~App() { - appletSetCpuBoostMode(ApmCpuBoostMode_FastLoad); - ON_SCOPE_EXIT(appletSetCpuBoostMode(ApmCpuBoostMode_Normal)); + // boost mode is disabled in userAppExit(). + App::SetBoostMode(true); log_write("starting to exit\n"); TimeStamp ts; diff --git a/sphaira/source/main.cpp b/sphaira/source/main.cpp index 9cac47b..03c9019 100644 --- a/sphaira/source/main.cpp +++ b/sphaira/source/main.cpp @@ -16,7 +16,7 @@ int main(int argc, char** argv) { extern "C" { void userAppInit(void) { - Result rc; + sphaira::App::SetBoostMode(true); // https://github.com/mtheall/ftpd/blob/e27898f0c3101522311f330e82a324861e0e3f7e/source/switch/init.c#L31 const SocketInitConfig socket_config_application = { @@ -47,6 +47,7 @@ void userAppInit(void) { const auto socket_config = is_application ? socket_config_application : socket_config_applet; + Result rc; if (R_FAILED(rc = appletLockExit())) diagAbortWithResult(rc); if (R_FAILED(rc = socketInitialize(&socket_config))) @@ -87,6 +88,8 @@ void userAppExit(void) { if (auto fs = fsdevGetDeviceFileSystem("sdmc:")) { fsFsCommit(fs); } + + sphaira::App::SetBoostMode(false); appletUnlockExit(); } diff --git a/sphaira/source/ui/menus/appstore.cpp b/sphaira/source/ui/menus/appstore.cpp index f50a4bd..f39b208 100644 --- a/sphaira/source/ui/menus/appstore.cpp +++ b/sphaira/source/ui/menus/appstore.cpp @@ -1170,8 +1170,8 @@ void Menu::SetIndex(s64 index) { } void Menu::ScanHomebrew() { - appletSetCpuBoostMode(ApmCpuBoostMode_FastLoad); - ON_SCOPE_EXIT(appletSetCpuBoostMode(ApmCpuBoostMode_Normal)); + App::SetBoostMode(true); + ON_SCOPE_EXIT(App::SetBoostMode(false)); from_json(REPO_PATH, m_entries); diff --git a/sphaira/source/ui/menus/filebrowser.cpp b/sphaira/source/ui/menus/filebrowser.cpp index b1cb010..cf2a739 100644 --- a/sphaira/source/ui/menus/filebrowser.cpp +++ b/sphaira/source/ui/menus/filebrowser.cpp @@ -968,8 +968,8 @@ void FsView::UploadFiles() { } auto FsView::Scan(const fs::FsPath& new_path, bool is_walk_up) -> Result { - appletSetCpuBoostMode(ApmCpuBoostMode_FastLoad); - ON_SCOPE_EXIT(appletSetCpuBoostMode(ApmCpuBoostMode_Normal)); + App::SetBoostMode(true); + ON_SCOPE_EXIT(App::SetBoostMode(false)); log_write("new scan path: %s\n", new_path.s); if (!is_walk_up && !m_path.empty() && !m_entries_current.empty()) { diff --git a/sphaira/source/ui/menus/game_menu.cpp b/sphaira/source/ui/menus/game_menu.cpp index 7563bf1..5283d30 100644 --- a/sphaira/source/ui/menus/game_menu.cpp +++ b/sphaira/source/ui/menus/game_menu.cpp @@ -1101,8 +1101,8 @@ void Menu::ScanHomebrew() { const auto hide_forwarders = m_hide_forwarders.Get(); TimeStamp ts; - appletSetCpuBoostMode(ApmCpuBoostMode_FastLoad); - ON_SCOPE_EXIT(appletSetCpuBoostMode(ApmCpuBoostMode_Normal)); + App::SetBoostMode(true); + ON_SCOPE_EXIT(App::SetBoostMode(false)); FreeEntries(); m_entries.reserve(ENTRY_CHUNK_COUNT); diff --git a/sphaira/source/ui/menus/gc_menu.cpp b/sphaira/source/ui/menus/gc_menu.cpp index 84b0865..fe5961b 100644 --- a/sphaira/source/ui/menus/gc_menu.cpp +++ b/sphaira/source/ui/menus/gc_menu.cpp @@ -980,8 +980,8 @@ Result Menu::DumpGames(u32 flags) { R_TRY(GcMountStorage()); const auto do_dump = [this](u32 flags) -> Result { - appletSetCpuBoostMode(ApmCpuBoostMode_FastLoad); - ON_SCOPE_EXIT(appletSetCpuBoostMode(ApmCpuBoostMode_Normal)); + App::SetBoostMode(true); + ON_SCOPE_EXIT(App::SetBoostMode(false)); u32 location_flags = dump::DumpLocationFlag_All; diff --git a/sphaira/source/ui/menus/themezer.cpp b/sphaira/source/ui/menus/themezer.cpp index 0d612ae..d95412f 100644 --- a/sphaira/source/ui/menus/themezer.cpp +++ b/sphaira/source/ui/menus/themezer.cpp @@ -581,8 +581,8 @@ void Menu::PackListDownload() { curl::Flags{curl::Flag_Cache}, curl::StopToken{this->GetToken()}, curl::OnComplete{[this, page_index](auto& result){ - appletSetCpuBoostMode(ApmCpuBoostMode_FastLoad); - ON_SCOPE_EXIT(appletSetCpuBoostMode(ApmCpuBoostMode_Normal)); + App::SetBoostMode(true); + ON_SCOPE_EXIT(App::SetBoostMode(false)); log_write("got themezer data\n"); if (!result.success) { diff --git a/sphaira/source/ui/progress_box.cpp b/sphaira/source/ui/progress_box.cpp index a61b8ec..0c0487b 100644 --- a/sphaira/source/ui/progress_box.cpp +++ b/sphaira/source/ui/progress_box.cpp @@ -21,7 +21,7 @@ void threadFunc(void* arg) { ProgressBox::ProgressBox(int image, const std::string& action, const std::string& title, ProgressBoxCallback callback, ProgressBoxDoneCallback done, int cpuid, int prio, int stack_size) { if (App::GetApp()->m_progress_boost_mode.Get()) { - appletSetCpuBoostMode(ApmCpuBoostMode_FastLoad); + App::SetBoostMode(true); } SetAction(Button::B, Action{"Back"_i18n, [this](){ @@ -67,7 +67,7 @@ ProgressBox::~ProgressBox() { FreeImage(); m_done(m_thread_data.result); - appletSetCpuBoostMode(ApmCpuBoostMode_Normal); + App::SetBoostMode(false); } auto ProgressBox::Update(Controller* controller, TouchInfo* touch) -> void {