Skip to content

Commit

Permalink
App: manage scheduler state from the main thread
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
wirew0rm committed Nov 13, 2024
1 parent 74674bc commit 2688de1
Showing 1 changed file with 15 additions and 22 deletions.
37 changes: 15 additions & 22 deletions src/ui/App.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,7 @@ class App {

template<typename TScheduler>
struct HandlerImpl : Handler {
TScheduler _scheduler;
std::thread _thread;
std::atomic<bool> stopRequested = false;
TScheduler _scheduler;

gr::MsgPortIn _fromScheduler;
gr::MsgPortOut _toScheduler;
Expand All @@ -99,23 +97,14 @@ class App {
gr::sendMessage<gr::message::Command::Subscribe>(_toScheduler, "", gr::block::property::kSetting, {}, "UI");
gr::sendMessage<gr::message::Command::Get>(_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; }
Expand All @@ -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
}
}
};

Expand Down

0 comments on commit 2688de1

Please sign in to comment.