Commit 734a145c authored by Lorenz Meier's avatar Lorenz Meier

LinkManager: Use proper multithreading design pattern

parent 0e52f685
...@@ -63,20 +63,26 @@ LinkManager::LinkManager() ...@@ -63,20 +63,26 @@ LinkManager::LinkManager()
LinkManager::~LinkManager() LinkManager::~LinkManager()
{ {
disconnectAll(); disconnectAll();
dataMutex.lock();
foreach (LinkInterface* link, links) foreach (LinkInterface* link, links)
{ {
if(link) link->deleteLater(); if(link) link->deleteLater();
} }
dataMutex.unlock();
} }
void LinkManager::add(LinkInterface* link) void LinkManager::add(LinkInterface* link)
{ {
dataMutex.lock();
if (!links.contains(link)) if (!links.contains(link))
{ {
if(!link) return; if(!link) return;
connect(link, SIGNAL(destroyed(QObject*)), this, SLOT(removeObj(QObject*))); connect(link, SIGNAL(destroyed(QObject*)), this, SLOT(removeObj(QObject*)));
links.append(link); links.append(link);
dataMutex.unlock();
emit newLink(link); emit newLink(link);
} else {
dataMutex.unlock();
} }
} }
...@@ -86,6 +92,7 @@ void LinkManager::addProtocol(LinkInterface* link, ProtocolInterface* protocol) ...@@ -86,6 +92,7 @@ void LinkManager::addProtocol(LinkInterface* link, ProtocolInterface* protocol)
// the protocol will receive new bytes from the link // the protocol will receive new bytes from the link
if (!link || !protocol) return; if (!link || !protocol) return;
dataMutex.lock();
QList<LinkInterface*> linkList = protocolLinks.values(protocol); QList<LinkInterface*> linkList = protocolLinks.values(protocol);
// If protocol has not been added before (list length == 0) // If protocol has not been added before (list length == 0)
...@@ -98,31 +105,42 @@ void LinkManager::addProtocol(LinkInterface* link, ProtocolInterface* protocol) ...@@ -98,31 +105,42 @@ void LinkManager::addProtocol(LinkInterface* link, ProtocolInterface* protocol)
connect(link, SIGNAL(connected(bool)), protocol, SLOT(linkStatusChanged(bool))); connect(link, SIGNAL(connected(bool)), protocol, SLOT(linkStatusChanged(bool)));
// Store the connection information in the protocol links map // Store the connection information in the protocol links map
protocolLinks.insertMulti(protocol, link); protocolLinks.insertMulti(protocol, link);
dataMutex.unlock();
// Make sure the protocol clears its metadata for this link. // Make sure the protocol clears its metadata for this link.
protocol->resetMetadataForLink(link); protocol->resetMetadataForLink(link);
} else {
dataMutex.unlock();
} }
//qDebug() << __FILE__ << __LINE__ << "ADDED LINK TO PROTOCOL" << link->getName() << protocol->getName() << "NEW SIZE OF LINK LIST:" << protocolLinks.size(); //qDebug() << __FILE__ << __LINE__ << "ADDED LINK TO PROTOCOL" << link->getName() << protocol->getName() << "NEW SIZE OF LINK LIST:" << protocolLinks.size();
} }
QList<LinkInterface*> LinkManager::getLinksForProtocol(ProtocolInterface* protocol) QList<LinkInterface*> LinkManager::getLinksForProtocol(ProtocolInterface* protocol)
{ {
return protocolLinks.values(protocol); dataMutex.lock();
QList<LinkInterface*> links = protocolLinks.values(protocol);
dataMutex.unlock();
return links;
} }
ProtocolInterface* LinkManager::getProtocolForLink(LinkInterface* link) ProtocolInterface* LinkManager::getProtocolForLink(LinkInterface* link)
{ {
return protocolLinks.key(link); dataMutex.lock();
ProtocolInterface* interface = protocolLinks.key(link);
dataMutex.unlock();
return interface;
} }
bool LinkManager::connectAll() bool LinkManager::connectAll()
{ {
bool allConnected = true; bool allConnected = true;
dataMutex.lock();
foreach (LinkInterface* link, links) foreach (LinkInterface* link, links)
{ {
if(!link) {} if(!link) {}
else if(!link->connect()) allConnected = false; else if(!link->connect()) allConnected = false;
} }
dataMutex.unlock();
return allConnected; return allConnected;
} }
...@@ -131,12 +149,14 @@ bool LinkManager::disconnectAll() ...@@ -131,12 +149,14 @@ bool LinkManager::disconnectAll()
{ {
bool allDisconnected = true; bool allDisconnected = true;
dataMutex.lock();
foreach (LinkInterface* link, links) foreach (LinkInterface* link, links)
{ {
//static int i=0; //static int i=0;
if(!link) {} if(!link) {}
else if(!link->disconnect()) allDisconnected = false; else if(!link->disconnect()) allDisconnected = false;
} }
dataMutex.unlock();
return allDisconnected; return allDisconnected;
} }
...@@ -166,6 +186,7 @@ bool LinkManager::removeLink(LinkInterface* link) ...@@ -166,6 +186,7 @@ bool LinkManager::removeLink(LinkInterface* link)
{ {
if(link) if(link)
{ {
dataMutex.lock();
for (int i=0; i < QList<LinkInterface*>(links).size(); i++) for (int i=0; i < QList<LinkInterface*>(links).size(); i++)
{ {
if(link==links.at(i)) if(link==links.at(i))
...@@ -179,6 +200,7 @@ bool LinkManager::removeLink(LinkInterface* link) ...@@ -179,6 +200,7 @@ bool LinkManager::removeLink(LinkInterface* link)
{ {
protocolLinks.remove(proto, link); protocolLinks.remove(proto, link);
} }
dataMutex.unlock();
// Emit removal of link // Emit removal of link
emit linkRemoved(link); emit linkRemoved(link);
...@@ -196,11 +218,17 @@ bool LinkManager::removeLink(LinkInterface* link) ...@@ -196,11 +218,17 @@ bool LinkManager::removeLink(LinkInterface* link)
*/ */
LinkInterface* LinkManager::getLinkForId(int id) LinkInterface* LinkManager::getLinkForId(int id)
{ {
dataMutex.lock();
LinkInterface* linkret = NULL;
foreach (LinkInterface* link, links) foreach (LinkInterface* link, links)
{ {
if (link->getId() == id) return link; if (link->getId() == id)
{
linkret = link;
}
} }
return NULL; dataMutex.unlock();
return linkret;
} }
/** /**
...@@ -208,11 +236,15 @@ LinkInterface* LinkManager::getLinkForId(int id) ...@@ -208,11 +236,15 @@ LinkInterface* LinkManager::getLinkForId(int id)
*/ */
const QList<LinkInterface*> LinkManager::getLinks() const QList<LinkInterface*> LinkManager::getLinks()
{ {
return QList<LinkInterface*>(links); dataMutex.lock();
QList<LinkInterface*> ret(links);
dataMutex.unlock();
return ret;
} }
const QList<SerialLink*> LinkManager::getSerialLinks() const QList<SerialLink*> LinkManager::getSerialLinks()
{ {
dataMutex.lock();
QList<SerialLink*> s; QList<SerialLink*> s;
foreach (LinkInterface* i, links) foreach (LinkInterface* i, links)
...@@ -222,6 +254,7 @@ const QList<SerialLink*> LinkManager::getSerialLinks() ...@@ -222,6 +254,7 @@ const QList<SerialLink*> LinkManager::getSerialLinks()
if (link) if (link)
s.append(link); s.append(link);
} }
dataMutex.unlock();
return s; return s;
} }
...@@ -35,6 +35,7 @@ This file is part of the PIXHAWK project ...@@ -35,6 +35,7 @@ This file is part of the PIXHAWK project
#include <QThread> #include <QThread>
#include <QList> #include <QList>
#include <QMultiMap> #include <QMultiMap>
#include <QMutex>
#include <LinkInterface.h> #include <LinkInterface.h>
#include <SerialLink.h> #include <SerialLink.h>
#include <ProtocolInterface.h> #include <ProtocolInterface.h>
...@@ -91,6 +92,7 @@ protected: ...@@ -91,6 +92,7 @@ protected:
LinkManager(); LinkManager();
QList<LinkInterface*> links; QList<LinkInterface*> links;
QMultiMap<ProtocolInterface*,LinkInterface*> protocolLinks; QMultiMap<ProtocolInterface*,LinkInterface*> protocolLinks;
QMutex dataMutex;
private: private:
static LinkManager* _instance; static LinkManager* _instance;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment