From 5b3e8eb2a0c52172a3773a1689354ed7e3765902 Mon Sep 17 00:00:00 2001 From: Lorenz Meier Date: Thu, 15 May 2014 18:15:58 +0200 Subject: [PATCH] More cleanup on threading --- src/comm/MAVLinkProtocol.cc | 18 +++++++++++++++++- src/comm/MAVLinkProtocol.h | 2 ++ src/comm/ProtocolInterface.h | 2 +- src/comm/QGCXPlaneLink.cc | 5 +++++ src/comm/SerialLink.cc | 6 ++++++ src/comm/TCPLink.cc | 6 ++++++ src/comm/UDPLink.cc | 6 ++++++ src/uas/QGCMAVLinkUASFactory.cc | 1 + src/uas/UAS.cc | 1 - src/ui/MAVLinkDecoder.cc | 2 +- 10 files changed, 45 insertions(+), 4 deletions(-) diff --git a/src/comm/MAVLinkProtocol.cc b/src/comm/MAVLinkProtocol.cc index 8ecb5194c..533bdb382 100644 --- a/src/comm/MAVLinkProtocol.cc +++ b/src/comm/MAVLinkProtocol.cc @@ -64,7 +64,7 @@ MAVLinkProtocol::MAVLinkProtocol() : m_authKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; loadSettings(); - //start(QThread::LowPriority); + moveToThread(this); // Start heartbeat timer, emitting a heartbeat at the configured rate connect(heartbeatTimer, SIGNAL(timeout()), this, SLOT(sendHeartbeat())); heartbeatTimer->start(1000/heartbeatRate); @@ -81,6 +81,8 @@ MAVLinkProtocol::MAVLinkProtocol() : } } + start(QThread::HighPriority); + emit versionCheckChanged(m_enable_version_check); } @@ -166,6 +168,20 @@ MAVLinkProtocol::~MAVLinkProtocol() delete m_logfile; m_logfile = NULL; } + + // Tell the thread to exit + quit(); + // Wait for it to exit + wait(); +} + +/** + * @brief Runs the thread + * + **/ +void MAVLinkProtocol::run() +{ + exec(); } QString MAVLinkProtocol::getLogfileName() diff --git a/src/comm/MAVLinkProtocol.h b/src/comm/MAVLinkProtocol.h index 3dc72563f..539b518e1 100644 --- a/src/comm/MAVLinkProtocol.h +++ b/src/comm/MAVLinkProtocol.h @@ -150,6 +150,8 @@ public: */ virtual void resetMetadataForLink(const LinkInterface *link); + void run(); + public slots: /** @brief Receive bytes from a communication interface */ void receiveBytes(LinkInterface* link, QByteArray b); diff --git a/src/comm/ProtocolInterface.h b/src/comm/ProtocolInterface.h index 150d5eb8f..a3a5fef12 100644 --- a/src/comm/ProtocolInterface.h +++ b/src/comm/ProtocolInterface.h @@ -46,7 +46,7 @@ This file is part of the PIXHAWK project * @see LinkManager. * **/ -class ProtocolInterface : public QObject +class ProtocolInterface : public QThread { Q_OBJECT public: diff --git a/src/comm/QGCXPlaneLink.cc b/src/comm/QGCXPlaneLink.cc index 66c6e5b71..1d974b43c 100644 --- a/src/comm/QGCXPlaneLink.cc +++ b/src/comm/QGCXPlaneLink.cc @@ -74,6 +74,11 @@ QGCXPlaneLink::QGCXPlaneLink(UASInterface* mav, QString remoteHost, QHostAddress QGCXPlaneLink::~QGCXPlaneLink() { storeSettings(); + // Tell the thread to exit + quit(); + // Wait for it to exit + wait(); + // if(connectState) { // disconnectSimulation(); // } diff --git a/src/comm/SerialLink.cc b/src/comm/SerialLink.cc index 1dbce7d65..ff555cc8b 100644 --- a/src/comm/SerialLink.cc +++ b/src/comm/SerialLink.cc @@ -87,6 +87,11 @@ SerialLink::~SerialLink() disconnect(); if(m_port) delete m_port; m_port = NULL; + + // Tell the thread to exit + quit(); + // Wait for it to exit + wait(); } QList SerialLink::getCurrentPorts() @@ -455,6 +460,7 @@ bool SerialLink::hardwareConnect(QString &type) qDebug() << "SerialLink: hardwareConnect to " << m_portName; m_port = new QSerialPort(m_portName); + m_port->moveToThread(this); if (!m_port) { emit communicationUpdate(getName(),"Error opening port: " + m_portName); diff --git a/src/comm/TCPLink.cc b/src/comm/TCPLink.cc index 392edbfcb..146e7a393 100644 --- a/src/comm/TCPLink.cc +++ b/src/comm/TCPLink.cc @@ -56,6 +56,12 @@ TCPLink::TCPLink(QHostAddress hostAddress, quint16 socketPort) : TCPLink::~TCPLink() { disconnect(); + + // Tell the thread to exit + quit(); + // Wait for it to exit + wait(); + deleteLater(); } diff --git a/src/comm/UDPLink.cc b/src/comm/UDPLink.cc index 7c401c734..d02e9290a 100644 --- a/src/comm/UDPLink.cc +++ b/src/comm/UDPLink.cc @@ -60,6 +60,12 @@ UDPLink::UDPLink(QHostAddress host, quint16 port) : UDPLink::~UDPLink() { disconnect(); + + // Tell the thread to exit + quit(); + // Wait for it to exit + wait(); + this->deleteLater(); } diff --git a/src/uas/QGCMAVLinkUASFactory.cc b/src/uas/QGCMAVLinkUASFactory.cc index b6fac1408..a5b183306 100644 --- a/src/uas/QGCMAVLinkUASFactory.cc +++ b/src/uas/QGCMAVLinkUASFactory.cc @@ -132,6 +132,7 @@ UASInterface* QGCMAVLinkUASFactory::createUAS(MAVLinkProtocol* mavlink, LinkInte UASManager::instance()->addUAS(uas); worker->start(QThread::HighPriority); + connect(uas, SIGNAL(destroyed()), worker, SLOT(quit())); return uas; } diff --git a/src/uas/UAS.cc b/src/uas/UAS.cc index 857e41dae..20e29ff3b 100644 --- a/src/uas/UAS.cc +++ b/src/uas/UAS.cc @@ -172,7 +172,6 @@ UAS::UAS(MAVLinkProtocol* protocol, int id) : UASInterface(), componentMulti[i] = false; } - // Store a list of available actions for this UAS. // Basically everything exposted as a SLOT with no return value or arguments. diff --git a/src/ui/MAVLinkDecoder.cc b/src/ui/MAVLinkDecoder.cc index 7f5ab0679..e957d8e55 100644 --- a/src/ui/MAVLinkDecoder.cc +++ b/src/ui/MAVLinkDecoder.cc @@ -2,7 +2,7 @@ #include "UASManager.h" MAVLinkDecoder::MAVLinkDecoder(MAVLinkProtocol* protocol, QObject *parent) : - QThread(parent) + QThread() { // We're doing it wrong - because the Qt folks got the API wrong: // http://blog.qt.digia.com/blog/2010/06/17/youre-doing-it-wrong/ -- 2.22.0