Skip to content

Commit

Permalink
Use QMutexLocker to lock BonjourRegister
Browse files Browse the repository at this point in the history
Following 29fae07, use a QMutexLocker instead.
Rename static class member to follow MythTV coding style
  • Loading branch information
jyavenard committed Jul 10, 2013
1 parent 4000137 commit a8ec95a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
25 changes: 12 additions & 13 deletions mythtv/libs/libmythbase/bonjourregister.cpp
Expand Up @@ -8,10 +8,10 @@

#define LOC QString("Bonjour: ")

QMutex BonjourRegister::m_lock;
QMutex BonjourRegister::g_lock;

BonjourRegister::BonjourRegister(QObject *parent)
: QObject(parent), m_dnssref(0), m_socket(NULL), m_haslock(false)
: QObject(parent), m_dnssref(0), m_socket(NULL), m_lock(NULL)
{
setenv("AVAHI_COMPAT_NOWARN", "1", 1);
}
Expand All @@ -32,11 +32,8 @@ BonjourRegister::~BonjourRegister()

m_socket->deleteLater();
m_socket = NULL;

if (m_haslock)
{
m_lock.unlock();
}
delete m_lock;
m_lock = NULL;
}

bool BonjourRegister::Register(uint16_t port, const QByteArray &type,
Expand All @@ -48,8 +45,7 @@ bool BonjourRegister::Register(uint16_t port, const QByteArray &type,
return true;
}

m_lock.lock();
m_haslock = true;
m_lock = new QMutexLocker(&g_lock);

uint16_t qport = qToBigEndian(port);
DNSServiceErrorType res =
Expand All @@ -71,13 +67,15 @@ bool BonjourRegister::Register(uint16_t port, const QByteArray &type,
m_socket->setEnabled(true);
connect(m_socket, SIGNAL(activated(int)),
this, SLOT(socketReadyRead()));
delete m_lock; // would already have been deleted, but just in case
m_lock = NULL;
return true;
}
}

LOG(VB_GENERAL, LOG_ERR, LOC + "Failed to register service.");
m_haslock = false;
m_lock.unlock();
delete m_lock;
m_lock = NULL;

return false;
}
Expand All @@ -100,8 +98,9 @@ void BonjourRegister::BonjourCallback(DNSServiceRef ref, DNSServiceFlags flags,
(void)flags;

BonjourRegister *bonjour = static_cast<BonjourRegister *>(object);
bonjour->m_haslock = false;
bonjour->m_lock.unlock();
delete bonjour->m_lock;
bonjour->m_lock = NULL;

if (kDNSServiceErr_NoError != errorcode)
{
LOG(VB_GENERAL, LOG_ERR, LOC + QString("Callback Error: %1")
Expand Down
6 changes: 3 additions & 3 deletions mythtv/libs/libmythbase/bonjourregister.h
Expand Up @@ -13,7 +13,7 @@ class MBASE_PUBLIC BonjourRegister : public QObject
Q_OBJECT
public:
BonjourRegister(QObject *parent = 0);
~BonjourRegister();
virtual ~BonjourRegister();

bool Register(uint16_t port, const QByteArray &type, const QByteArray &name,
const QByteArray &txt);
Expand All @@ -32,7 +32,7 @@ class MBASE_PUBLIC BonjourRegister : public QObject
const char *domain, void *object);
DNSServiceRef m_dnssref;
QSocketNotifier *m_socket;
static QMutex m_lock;
bool m_haslock;
QMutexLocker *m_lock;
static QMutex g_lock;
};
#endif

0 comments on commit a8ec95a

Please sign in to comment.