From 2688de1cfbeee6b6a70fc4ac73ebbfc370df8b68 Mon Sep 17 00:00:00 2001 From: Alexander Krimm Date: Tue, 12 Nov 2024 16:06:15 +0100 Subject: [PATCH] App: manage scheduler state from the main thread This is required for the emscripten fetch calls to be issued in the main browser thread. If they are issued on another thread, they are only processed once the thread is finished. Otherwise we would have to investigate if there is a way to control on which thread the callbacks should be processed or we would have to proxy the emscripten_fetch calls to the main thread. This is further complicated because it needs to be transparent to the part of the code that also compiles to native. Signed-off-by: Alexander Krimm --- src/ui/App.hpp | 37 +++++++++++++++---------------------- 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/src/ui/App.hpp b/src/ui/App.hpp index b2464744..773190b1 100644 --- a/src/ui/App.hpp +++ b/src/ui/App.hpp @@ -80,9 +80,7 @@ class App { template struct HandlerImpl : Handler { - TScheduler _scheduler; - std::thread _thread; - std::atomic stopRequested = false; + TScheduler _scheduler; gr::MsgPortIn _fromScheduler; gr::MsgPortOut _toScheduler; @@ -99,23 +97,14 @@ class App { gr::sendMessage(_toScheduler, "", gr::block::property::kSetting, {}, "UI"); gr::sendMessage(_toScheduler, "", gr::block::property::kSetting, {}, "UI"); - _thread = std::thread([this]() { - if (auto e = _scheduler.changeStateTo(gr::lifecycle::State::INITIALISED); !e) { - // TODO: handle error return message - } - if (auto e = _scheduler.changeStateTo(gr::lifecycle::State::RUNNING); !e) { - // TODO: handle error return message - } - while (!stopRequested && _scheduler.isProcessing()) { - std::this_thread::sleep_for(std::chrono::milliseconds(100)); - } - if (auto e = _scheduler.changeStateTo(gr::lifecycle::State::REQUESTED_STOP); !e) { - // TODO: handle error return message - } - if (auto e = _scheduler.changeStateTo(gr::lifecycle::State::STOPPED); !e) { - // TODO: handle error return message - } - }); + if (auto e = _scheduler.changeStateTo(gr::lifecycle::State::INITIALISED); !e) { + fmt::print("Error initializíng scheduler: {}\n", e.error().message); + // TODO: handle error return message + } + if (auto e = _scheduler.changeStateTo(gr::lifecycle::State::RUNNING); !e) { + fmt::print("Error starting scheduler: {}\n", e.error().message); + // TODO: handle error return message + } } std::string_view uniqueName() const override { return _scheduler.unique_name; } @@ -138,8 +127,12 @@ class App { } ~HandlerImpl() { - stopRequested = true; - _thread.join(); + if (auto e = _scheduler.changeStateTo(gr::lifecycle::State::REQUESTED_STOP); !e) { + // TODO: handle error return message + } + if (auto e = _scheduler.changeStateTo(gr::lifecycle::State::STOPPED); !e) { + // TODO: handle error return message + } } };