From 3cf20fb94689daaf3cb7fce106cc968314689a52 Mon Sep 17 00:00:00 2001 From: leonarski_f Date: Mon, 27 Apr 2026 13:39:39 +0200 Subject: [PATCH] JFJochStateMachine: Enable async workflow to start measurement --- broker/JFJochStateMachine.cpp | 40 ++++++++++++++++++++++++----------- broker/JFJochStateMachine.h | 4 +++- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/broker/JFJochStateMachine.cpp b/broker/JFJochStateMachine.cpp index d853ce64..137d2811 100644 --- a/broker/JFJochStateMachine.cpp +++ b/broker/JFJochStateMachine.cpp @@ -344,7 +344,7 @@ void JFJochStateMachine::Trigger() { services.Trigger(); } -void JFJochStateMachine::Start(const DatasetSettings &settings) { +void JFJochStateMachine::Start(const DatasetSettings &settings, bool async) { std::unique_lock ul(m); if (state != JFJochState::Idle) @@ -363,18 +363,16 @@ void JFJochStateMachine::Start(const DatasetSettings &settings) { experiment.IncrementRunNumber(); - try { - SetState(JFJochState::Busy, "Preparing measurement", BrokerStatus::MessageSeverity::Info); - services.SetSpotFindingSettings(GetSpotFindingSettings()); - services.Start(experiment, pixel_mask, calibration.get()); + SetState(JFJochState::Busy, "Preparing measurement", BrokerStatus::MessageSeverity::Info); + measurement = std::async(std::launch::async, &JFJochStateMachine::MeasurementThread, this); + if (!async) + c.wait(ul, [&]() { return state != JFJochState::Busy; }); +} - SetState(JFJochState::Measuring, "Measuring ...", BrokerStatus::MessageSeverity::Info); - measurement = std::async(std::launch::async, &JFJochStateMachine::MeasurementThread, this); - } catch (const std::exception &e) { - SetState(JFJochState::Error, e.what(), BrokerStatus::MessageSeverity::Error); - services.Cancel(); - throw; - } +JFJochState JFJochStateMachine::WaitTillNotBusy(std::chrono::milliseconds timeout) { + std::unique_lock ul(m); + c.wait_for(ul, timeout, [&]() { return state != JFJochState::Busy; }); + return state; } void JFJochStateMachine::UpdatePixelMaskStatistics(const PixelMaskStatistics &input) { @@ -388,6 +386,24 @@ PixelMaskStatistics JFJochStateMachine::GetPixelMaskStatistics() const { } void JFJochStateMachine::MeasurementThread() { + try { + services.SetSpotFindingSettings(GetSpotFindingSettings()); + services.Start(experiment, pixel_mask, calibration.get()); + { + std::unique_lock ul(m); + SetState(JFJochState::Measuring, "Measuring ...", BrokerStatus::MessageSeverity::Info); + } + c.notify_all(); + } catch (std::exception &e) { + { + std::unique_lock ul(m); + SetState(JFJochState::Error, e.what(), BrokerStatus::MessageSeverity::Error); + } + services.Cancel(); + c.notify_all(); + return; + } + try { auto tmp_output = services.Stop(); { diff --git a/broker/JFJochStateMachine.h b/broker/JFJochStateMachine.h index 240183c2..cec18d6b 100644 --- a/broker/JFJochStateMachine.h +++ b/broker/JFJochStateMachine.h @@ -152,7 +152,9 @@ public: void Initialize(); void Pedestal(); void Deactivate(); - void Start(const DatasetSettings& settings); + void Start(const DatasetSettings& settings, bool async = false); + JFJochState WaitTillNotBusy(std::chrono::milliseconds timeout); + BrokerStatus WaitTillMeasurementDone(); BrokerStatus WaitTillMeasurementDone(std::chrono::milliseconds timeout); void Trigger();