Browse Source

Force write to happen on correct thread

QGC4.4
Don Gagne 3 years ago committed by Don Gagne
parent
commit
87182b8af1
  1. 9
      src/comm/LinkInterface.cc
  2. 8
      src/comm/LinkInterface.h

9
src/comm/LinkInterface.cc

@ -22,8 +22,10 @@ LinkInterface::LinkInterface(SharedLinkConfigurationPtr& config, bool isPX4Flow) @@ -22,8 +22,10 @@ LinkInterface::LinkInterface(SharedLinkConfigurationPtr& config, bool isPX4Flow)
, _isPX4Flow(isPX4Flow)
{
QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership);
qRegisterMetaType<LinkInterface*>("LinkInterface*");
// This will cause the writeBytes calls to end up on the thread of the link
QObject::connect(this, &LinkInterface::_invokeWriteBytes, this, &LinkInterface::_writeBytes);
}
LinkInterface::~LinkInterface()
@ -80,10 +82,7 @@ void LinkInterface::_freeMavlinkChannel() @@ -80,10 +82,7 @@ void LinkInterface::_freeMavlinkChannel()
void LinkInterface::writeBytesThreadSafe(const char *bytes, int length)
{
QByteArray byteArray(bytes, length);
_writeBytesMutex.lock();
_writeBytes(byteArray);
_writeBytesMutex.unlock();
emit _invokeWriteBytes(QByteArray(bytes, length));
}
void LinkInterface::addVehicleReference(void)

8
src/comm/LinkInterface.h

@ -76,6 +76,7 @@ signals: @@ -76,6 +76,7 @@ signals:
void connected (void);
void disconnected (void);
void communicationError (const QString& title, const QString& error);
void _invokeWriteBytes (QByteArray);
protected:
// Links are only created by LinkManager so constructor is not public
@ -96,19 +97,18 @@ protected: @@ -96,19 +97,18 @@ protected:
virtual bool _allocateMavlinkChannel();
virtual void _freeMavlinkChannel ();
private slots:
virtual void _writeBytes(const QByteArray) = 0; // Not thread safe if called directly, only writeBytesThreadSafe is thread safe
private:
// connect is private since all links should be created through LinkManager::createConnectedLink calls
virtual bool _connect(void) = 0;
virtual void _writeBytes(const QByteArray) = 0; // Not thread safe, only writeBytesThreadSafe is thread safe
uint8_t _mavlinkChannel = std::numeric_limits<uint8_t>::max();
bool _decodedFirstMavlinkPacket = false;
bool _isPX4Flow = false;
int _vehicleReferenceCount = 0;
mutable QMutex _writeBytesMutex;
QMap<int /* vehicle id */, MavlinkMessagesTimer*> _mavlinkMessagesTimers;
};

Loading…
Cancel
Save