replace the Node mutex with a QMutex

This commit is contained in:
Stephen Birarda 2014-01-14 10:15:31 -08:00
parent 53d435006c
commit 57f977668f
5 changed files with 13 additions and 33 deletions

View file

@ -2030,7 +2030,7 @@ void Application::updateAvatars(float deltaTime, glm::vec3 mouseRayOrigin, glm::
PerformanceWarning warn(showWarnings, "Application::updateAvatars()"); PerformanceWarning warn(showWarnings, "Application::updateAvatars()");
foreach(SharedNodePointer node, NodeList::getInstance()->getNodeHash()) { foreach(SharedNodePointer node, NodeList::getInstance()->getNodeHash()) {
node->lock(); QMutexLocker(&node->getMutex());
if (node->getLinkedData()) { if (node->getLinkedData()) {
Avatar *avatar = (Avatar *)node->getLinkedData(); Avatar *avatar = (Avatar *)node->getLinkedData();
if (!avatar->isInitialized()) { if (!avatar->isInitialized()) {
@ -2039,7 +2039,6 @@ void Application::updateAvatars(float deltaTime, glm::vec3 mouseRayOrigin, glm::
avatar->simulate(deltaTime, NULL); avatar->simulate(deltaTime, NULL);
avatar->setMouseRay(mouseRayOrigin, mouseRayDirection); avatar->setMouseRay(mouseRayOrigin, mouseRayDirection);
} }
node->unlock();
} }
// simulate avatar fades // simulate avatar fades
@ -3759,7 +3758,7 @@ void Application::renderAvatars(bool forceRenderHead, bool selfAvatarOnly) {
NodeList* nodeList = NodeList::getInstance(); NodeList* nodeList = NodeList::getInstance();
foreach(SharedNodePointer node, nodeList->getNodeHash()) { foreach(SharedNodePointer node, nodeList->getNodeHash()) {
node->lock(); QMutexLocker(&node->getMutex());
if (node->getLinkedData() != NULL && node->getType() == NODE_TYPE_AGENT) { if (node->getLinkedData() != NULL && node->getType() == NODE_TYPE_AGENT) {
Avatar *avatar = (Avatar *)node->getLinkedData(); Avatar *avatar = (Avatar *)node->getLinkedData();
@ -3769,8 +3768,6 @@ void Application::renderAvatars(bool forceRenderHead, bool selfAvatarOnly) {
avatar->render(false); avatar->render(false);
avatar->setDisplayingLookatVectors(Menu::getInstance()->isOptionChecked(MenuOption::LookAtVectors)); avatar->setDisplayingLookatVectors(Menu::getInstance()->isOptionChecked(MenuOption::LookAtVectors));
} }
node->unlock();
} }
// render avatar fades // render avatar fades

View file

@ -34,7 +34,7 @@ bool OctreeSendThread::process() {
if (node) { if (node) {
// make sure the node list doesn't kill our node while we're using it // make sure the node list doesn't kill our node while we're using it
if (node->trylock()) { if (node->getMutex().tryLock()) {
gotLock = true; gotLock = true;
OctreeQueryNode* nodeData = NULL; OctreeQueryNode* nodeData = NULL;
@ -51,7 +51,7 @@ bool OctreeSendThread::process() {
packetsSent = packetDistributor(node.data(), nodeData, viewFrustumChanged); packetsSent = packetDistributor(node.data(), nodeData, viewFrustumChanged);
} }
node->unlock(); // we're done with this node for now. node->getMutex().unlock(); // we're done with this node for now.
} }
} }
} else { } else {

View file

@ -33,9 +33,10 @@ Node::Node(const QUuid& uuid, char type, const HifiSockAddr& publicSocket, const
_bytesReceivedMovingAverage(NULL), _bytesReceivedMovingAverage(NULL),
_linkedData(NULL), _linkedData(NULL),
_isAlive(true), _isAlive(true),
_clockSkewUsec(0) _clockSkewUsec(0),
_mutex()
{ {
pthread_mutex_init(&_mutex, 0);
} }
Node::~Node() { Node::~Node() {
@ -46,8 +47,6 @@ Node::~Node() {
} }
delete _bytesReceivedMovingAverage; delete _bytesReceivedMovingAverage;
pthread_mutex_destroy(&_mutex);
} }
// Names of Node Types // Names of Node Types

View file

@ -19,6 +19,7 @@
#endif #endif
#include <QtCore/QDebug> #include <QtCore/QDebug>
#include <QtCore/QMutex>
#include <QtCore/QUuid> #include <QtCore/QUuid>
#include "HifiSockAddr.h" #include "HifiSockAddr.h"
@ -73,13 +74,7 @@ public:
int getClockSkewUsec() const { return _clockSkewUsec; } int getClockSkewUsec() const { return _clockSkewUsec; }
void setClockSkewUsec(int clockSkew) { _clockSkewUsec = clockSkew; } void setClockSkewUsec(int clockSkew) { _clockSkewUsec = clockSkew; }
void lock() { pthread_mutex_lock(&_mutex); } QMutex& getMutex() { return _mutex; }
/// returns false if lock failed, true if you got the lock
bool trylock() { return (pthread_mutex_trylock(&_mutex) == 0); }
void unlock() { pthread_mutex_unlock(&_mutex); }
static void printLog(Node const&);
private: private:
// privatize copy and assignment operator to disallow Node copying // privatize copy and assignment operator to disallow Node copying
@ -98,12 +93,9 @@ private:
bool _isAlive; bool _isAlive;
int _pingMs; int _pingMs;
int _clockSkewUsec; int _clockSkewUsec;
pthread_mutex_t _mutex; QMutex _mutex;
}; };
int unpackNodeId(unsigned char *packedData, uint16_t *nodeId);
int packNodeId(unsigned char *packStore, uint16_t nodeId);
QDebug operator<<(QDebug debug, const Node &message); QDebug operator<<(QDebug debug, const Node &message);
#endif /* defined(__hifi__Node__) */ #endif /* defined(__hifi__Node__) */

View file

@ -227,7 +227,7 @@ void NodeList::processBulkNodeData(const HifiSockAddr& senderAddress, unsigned c
} }
int NodeList::updateNodeWithData(Node *node, const HifiSockAddr& senderSockAddr, unsigned char *packetData, int dataBytes) { int NodeList::updateNodeWithData(Node *node, const HifiSockAddr& senderSockAddr, unsigned char *packetData, int dataBytes) {
node->lock(); QMutexLocker(&node->getMutex());
node->setLastHeardMicrostamp(usecTimestampNow()); node->setLastHeardMicrostamp(usecTimestampNow());
@ -244,12 +244,9 @@ int NodeList::updateNodeWithData(Node *node, const HifiSockAddr& senderSockAddr,
int numParsedBytes = node->getLinkedData()->parseData(packetData, dataBytes); int numParsedBytes = node->getLinkedData()->parseData(packetData, dataBytes);
node->unlock();
return numParsedBytes; return numParsedBytes;
} else { } else {
// we weren't able to match the sender address to the address we have for this node, unlock and don't parse // we weren't able to match the sender address to the address we have for this node, unlock and don't parse
node->unlock();
return 0; return 0;
} }
} }
@ -695,7 +692,7 @@ SharedNodePointer NodeList::addOrUpdateNode(const QUuid& uuid, char nodeType,
return newNodeSharedPointer; return newNodeSharedPointer;
} else { } else {
SharedNodePointer node = matchingNodeItem.value(); SharedNodePointer node = matchingNodeItem.value();
matchingNodeItem.value()->lock(); QMutexLocker(&node->getMutex());
if (node->getType() == NODE_TYPE_AUDIO_MIXER || if (node->getType() == NODE_TYPE_AUDIO_MIXER ||
node->getType() == NODE_TYPE_VOXEL_SERVER || node->getType() == NODE_TYPE_VOXEL_SERVER ||
@ -716,8 +713,6 @@ SharedNodePointer NodeList::addOrUpdateNode(const QUuid& uuid, char nodeType,
qDebug() << "Local socket change for node" << *node << "\n"; qDebug() << "Local socket change for node" << *node << "\n";
} }
node->unlock();
// we had this node already, do nothing for now // we had this node already, do nothing for now
return node; return node;
} }
@ -797,7 +792,7 @@ void NodeList::removeSilentNodes() {
SharedNodePointer node = nodeItem.value(); SharedNodePointer node = nodeItem.value();
node->lock(); QMutexLocker(&node->getMutex());
if ((usecTimestampNow() - node->getLastHeardMicrostamp()) > NODE_SILENCE_THRESHOLD_USECS) { if ((usecTimestampNow() - node->getLastHeardMicrostamp()) > NODE_SILENCE_THRESHOLD_USECS) {
@ -805,9 +800,6 @@ void NodeList::removeSilentNodes() {
_nodeHash.erase(nodeItem); _nodeHash.erase(nodeItem);
} }
// unlock the node
node->unlock();
nodeItem++; nodeItem++;
} }
} }