Skip to content

Commit

Permalink
Improved synchronization on boot. (#27)
Browse files Browse the repository at this point in the history
* created upstream branch

--HG--
branch : upstream

* backported changes to upstream

--HG--
branch : upstream

* fixed a typo, now the sync option should work properly

--HG--
branch : upstream

* backported changes to upstream

--HG--
branch : upstream

* Backported to upstream.

--HG--
branch : upstream

* Backported to upstream.

--HG--
branch : upstream

* Backported changes.

--HG--
branch : upstream

* Backported changes to upstream.

--HG--
branch : upstream

* Merged libcurlwrapper and timezone service changes.

--HG--
branch : upstream

* Forgot to add curl-related sources.

--HG--
branch : upstream

* Backported Time Sync changes to upstream.

--HG--
branch : upstream

* Update workflow to use git submodules.

--HG--
branch : upstream

* - Ensure background thread is finished when plugin unloads.
- Updated libwupsxx.

--HG--
branch : upstream

* Changed Makefile to build objects in subdirectories, to avoid conflicts when two different
files in different directories produce the same object file name.

--HG--
branch : upstream

* Ensure the network is initialized before doing network operations.

--HG--
branch : upstream

* Forgot to merge all changes.

--HG--
branch : upstream

* Ensure synchronization task is launched asynchronously.

--HG--
branch : upstream

* Changes to synchronization on boot:

- Added a 5 s delay, to avoid spurious errors while Aroma is booting.

- The background thread is now safely canceled whenever the application changes, reducing
  the chance of leaking resources.

--HG--
branch : upstream

* Ensure devkitPro always has the most up-to-date packages in the Docker environment.

--HG--
branch : upstream

* Removed dkp-pacman update command.

--HG--
branch : upstream

---------

Co-authored-by: Daniel K. O. (dkosmari) <none@none>
  • Loading branch information
dkosmari and Daniel K. O. (dkosmari) authored Nov 14, 2024
1 parent 5a05063 commit ef1da2a
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on: [push, pull_request]

jobs:
build-binary:
runs-on: ubuntu-latest
runs-on: ubuntu-24.04
steps:
- name: Checkout Code
uses: actions/checkout@v4
Expand Down
1 change: 1 addition & 0 deletions include/core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ namespace core {
namespace background {

void run();
void run_once();
void stop();

} // namespace background
Expand Down
4 changes: 3 additions & 1 deletion source/cfg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,10 @@ namespace cfg {
void
menu_close()
{
if (cfg::sync_on_changes && important_vars_changed())
if (cfg::sync_on_changes && important_vars_changed()) {
core::background::stop();
core::background::run();
}

cfg::save();

Expand Down
55 changes: 43 additions & 12 deletions source/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,19 @@ namespace core {
}


void
sleep_for(std::chrono::milliseconds t,
std::stop_token token)
{
using clock = std::chrono::steady_clock;
const auto deadline = clock::now() + t;
while (clock::now() < deadline) {
check_stop(token);
std::this_thread::sleep_for(100ms);
}
}


// Note: hardcoded for IPv4, the Wii U doesn't have IPv6.
std::pair<dbl_seconds, dbl_seconds>
ntp_query(std::stop_token token,
Expand Down Expand Up @@ -438,28 +451,39 @@ namespace core {
namespace background {

std::stop_source stopper{std::nostopstate};
std::atomic_bool running;

enum class state_t : unsigned {
none,
started,
finished,
canceled,
};
std::atomic<state_t> state{state_t::none};


void
run()
{
if (running)
return;

running = true;
state = state_t::started;

std::jthread t{
[](std::stop_token token)
{
wups::logger::guard lguard{PLUGIN_NAME};
notify::guard nguard;
wups::logger::guard logger_guard{PLUGIN_NAME};
notify::guard notify_guard;
try {
// Note: we wait 5 seconds, to minimize spurious network errors.
sleep_for(5s, token);
core::run(token, false);
state = state_t::finished;
}
catch (canceled_error& e) {
state = state_t::canceled;
}
catch (std::exception& e) {
notify::error(notify::level::normal, e.what());
state = state_t::finished;
}
running = false;
}
};

Expand All @@ -469,26 +493,33 @@ namespace core {
}


void
run_once()
{
if (state != state_t::finished)
run();
}


void
stop()
{
if (running) {
if (state == state_t::started) {
stopper.request_stop();

// Wait up to ~10 seconds for the thread to flag it stopped running.
unsigned max_tries = 100;
do {
std::this_thread::sleep_for(100ms);
} while (running && --max_tries);
} while ((state == state_t::started) && --max_tries);

if (max_tries == 0)
if (state == state_t::started)
logger::printf("WARNING: Background thread did not stop!\n");

stopper = std::stop_source{std::nostopstate};
}
}


} // namespace background

} // namespace core
14 changes: 12 additions & 2 deletions source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,23 @@ INITIALIZE_PLUGIN()
wups::logger::guard guard{PLUGIN_NAME};

cfg::init();
}


DEINITIALIZE_PLUGIN()
{
core::background::stop();
}


ON_APPLICATION_START()
{
if (cfg::sync_on_boot)
core::background::run();
core::background::run_once();
}


DEINITIALIZE_PLUGIN()
ON_APPLICATION_REQUESTS_EXIT()
{
core::background::stop();
}
1 change: 1 addition & 0 deletions source/synchronize_item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

#include <cstdio>
#include <utility>

#include <wupsxx/cafe_glyphs.h>
#include <wupsxx/logger.hpp>
Expand Down

0 comments on commit ef1da2a

Please sign in to comment.