From 10b1622f73c95f2fddffbb13c49f8af4a8981595 Mon Sep 17 00:00:00 2001 From: consti10 Date: Wed, 22 Nov 2023 11:23:56 +0100 Subject: [PATCH 1/2] improve terminating a bit --- app/main.cpp | 7 +++---- app/telemetry/MavlinkTelemetry.cpp | 16 +++++++++++++++- app/telemetry/MavlinkTelemetry.h | 1 + app/telemetry/action/impl/cmdsender.cpp | 15 +++++++++++---- app/telemetry/action/impl/cmdsender.h | 1 + app/telemetry/action/impl/xparam.cpp | 15 +++++++++++---- app/telemetry/action/impl/xparam.h | 1 + app/videostreaming/avcodec/avcodec_decoder.cpp | 4 +++- 8 files changed, 46 insertions(+), 14 deletions(-) diff --git a/app/main.cpp b/app/main.cpp index ae7f34f9f..012367b82 100755 --- a/app/main.cpp +++ b/app/main.cpp @@ -395,11 +395,10 @@ int main(int argc, char *argv[]) { MavlinkTelemetry::instance().start(); QRenderStats::instance().register_to_root_window(engine); - LogMessagesModel::instanceGround().addLogMessage("QOpenHD","running"); const int retval = app.exec(); - + // Terminating needs a bit of special care due to the singleton usage and threads + qDebug()<<"Terminating"; + MavlinkTelemetry::instance().terminate(); return retval; - - } diff --git a/app/telemetry/MavlinkTelemetry.cpp b/app/telemetry/MavlinkTelemetry.cpp index c656f5f6c..2bd491822 100644 --- a/app/telemetry/MavlinkTelemetry.cpp +++ b/app/telemetry/MavlinkTelemetry.cpp @@ -38,13 +38,27 @@ void MavlinkTelemetry::start() m_heartbeat_thread=std::make_unique(&MavlinkTelemetry::send_heartbeat_loop,this); } -MavlinkTelemetry::~MavlinkTelemetry() +void MavlinkTelemetry::terminate() { + // first stop any incoming telemetry if(m_heartbeat_thread){ m_heartbeat_thread_run=false; m_heartbeat_thread->join(); m_heartbeat_thread=nullptr; } + m_udp_connection=nullptr; + m_tcp_connection=nullptr; + // Cleanup those 2 threads + CmdSender::instance().terminate(); + XParam::instance().terminate(); + qDebug()<<"MavlinkTelemetry::stopped"; +} + +MavlinkTelemetry::~MavlinkTelemetry() +{ + qDebug()<<"MavlinkTelemetry::~() begin"; + terminate(); + qDebug()<<"MavlinkTelemetry::~() end"; } MavlinkTelemetry &MavlinkTelemetry::instance() diff --git a/app/telemetry/MavlinkTelemetry.h b/app/telemetry/MavlinkTelemetry.h index d234499f0..f39b7bcc9 100644 --- a/app/telemetry/MavlinkTelemetry.h +++ b/app/telemetry/MavlinkTelemetry.h @@ -36,6 +36,7 @@ class MavlinkTelemetry : public QObject ~MavlinkTelemetry(); static MavlinkTelemetry& instance(); void start(); + void terminate(); /** * Send a message to the OHD ground unit. If no connection has been established (yet), this should return immediately. * The message can be aimed at either the OHD ground unit, the OHD air unit (forwarded by OpenHD) or the FC connected to the diff --git a/app/telemetry/action/impl/cmdsender.cpp b/app/telemetry/action/impl/cmdsender.cpp index 8200816e2..5954408d3 100644 --- a/app/telemetry/action/impl/cmdsender.cpp +++ b/app/telemetry/action/impl/cmdsender.cpp @@ -16,9 +16,16 @@ CmdSender::CmdSender() CmdSender::~CmdSender() { - m_timeout_thread_run=false; - m_timeout_thread->join(); - m_timeout_thread=nullptr; + terminate(); +} + +void CmdSender::terminate() +{ + if(m_timeout_thread){ + m_timeout_thread_run=false; + m_timeout_thread->join(); + m_timeout_thread=nullptr; + } } CmdSender &CmdSender::instance() @@ -178,7 +185,7 @@ void CmdSender::send_mavlink_command_long(const mavlink_command_long_t &cmd) void CmdSender::loop_timeout() { - while(true){ + while(m_timeout_thread_run){ std::this_thread::sleep_for(std::chrono::milliseconds(100)); handle_timeout(); } diff --git a/app/telemetry/action/impl/cmdsender.h b/app/telemetry/action/impl/cmdsender.h index 7a14704dc..bb8483e37 100644 --- a/app/telemetry/action/impl/cmdsender.h +++ b/app/telemetry/action/impl/cmdsender.h @@ -18,6 +18,7 @@ class CmdSender public: CmdSender(); ~CmdSender(); + void terminate(); static CmdSender& instance(); /** * returns true if this message has been consumed, false otherwise. diff --git a/app/telemetry/action/impl/xparam.cpp b/app/telemetry/action/impl/xparam.cpp index 712989ae3..6c8f78c55 100644 --- a/app/telemetry/action/impl/xparam.cpp +++ b/app/telemetry/action/impl/xparam.cpp @@ -17,9 +17,16 @@ XParam::XParam() XParam::~XParam() { - m_timeout_thread_run=false; - m_timeout_thread->join(); - m_timeout_thread=nullptr; + terminate(); +} + +void XParam::terminate() +{ + if(m_timeout_thread){ + m_timeout_thread_run=false; + m_timeout_thread->join(); + m_timeout_thread=nullptr; + } } XParam &XParam::instance() @@ -400,7 +407,7 @@ void XParam::update_progress_get_all(const RunningParamCmdGetAll &cmd) void XParam::loop_timeout() { - while(true){ + while(m_timeout_thread_run){ std::this_thread::sleep_for(std::chrono::milliseconds(100)); check_timeout_param_get_all(); check_timeout_param_set(); diff --git a/app/telemetry/action/impl/xparam.h b/app/telemetry/action/impl/xparam.h index b80b7da44..ecc7ed95a 100644 --- a/app/telemetry/action/impl/xparam.h +++ b/app/telemetry/action/impl/xparam.h @@ -16,6 +16,7 @@ class XParam public: explicit XParam(); ~XParam(); + void terminate(); static XParam& instance(); /** * returns true if this message has been consumed, false otherwise. diff --git a/app/videostreaming/avcodec/avcodec_decoder.cpp b/app/videostreaming/avcodec/avcodec_decoder.cpp index 602486c2b..a67945f9a 100644 --- a/app/videostreaming/avcodec/avcodec_decoder.cpp +++ b/app/videostreaming/avcodec/avcodec_decoder.cpp @@ -100,6 +100,7 @@ void AVCodecDecoder::terminate() // Wait for everything to cleanup and stop decode_thread->join(); } + qDebug()<<"Decoding terminated"; } void AVCodecDecoder::timer_check_settings_changed_callback() @@ -158,7 +159,8 @@ void AVCodecDecoder::constant_decode() } } qDebug()<<"Decode stopped,restarting"; - std::this_thread::sleep_for(std::chrono::seconds(1)); + // wait a bit before starting again, to save CPU + if(!m_should_terminate)std::this_thread::sleep_for(std::chrono::seconds(1)); } } From cffb1c1ad532975a87b67e08806b14b99cee5a1f Mon Sep 17 00:00:00 2001 From: consti10 Date: Wed, 22 Nov 2023 11:30:15 +0100 Subject: [PATCH 2/2] improve terminating a bit --- app/telemetry/MavlinkTelemetry.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/telemetry/MavlinkTelemetry.h b/app/telemetry/MavlinkTelemetry.h index f39b7bcc9..edef2c436 100644 --- a/app/telemetry/MavlinkTelemetry.h +++ b/app/telemetry/MavlinkTelemetry.h @@ -35,6 +35,8 @@ class MavlinkTelemetry : public QObject MavlinkTelemetry(QObject *parent = nullptr); ~MavlinkTelemetry(); static MavlinkTelemetry& instance(); + // start / terminate needs a bit of care due to singleton usage, + // not clean, but works void start(); void terminate(); /**