From 57f977668f46653c9c1cc485d41c3d0e76ea8451 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Tue, 14 Jan 2014 10:15:31 -0800 Subject: [PATCH] replace the Node mutex with a QMutex --- interface/src/Application.cpp | 7 ++----- libraries/octree-server/src/OctreeSendThread.cpp | 4 ++-- libraries/shared/src/Node.cpp | 7 +++---- libraries/shared/src/Node.h | 14 +++----------- libraries/shared/src/NodeList.cpp | 14 +++----------- 5 files changed, 13 insertions(+), 33 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 35b61ae95c..ac6ddf2a2b 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -2030,7 +2030,7 @@ void Application::updateAvatars(float deltaTime, glm::vec3 mouseRayOrigin, glm:: PerformanceWarning warn(showWarnings, "Application::updateAvatars()"); foreach(SharedNodePointer node, NodeList::getInstance()->getNodeHash()) { - node->lock(); + QMutexLocker(&node->getMutex()); if (node->getLinkedData()) { Avatar *avatar = (Avatar *)node->getLinkedData(); if (!avatar->isInitialized()) { @@ -2039,7 +2039,6 @@ void Application::updateAvatars(float deltaTime, glm::vec3 mouseRayOrigin, glm:: avatar->simulate(deltaTime, NULL); avatar->setMouseRay(mouseRayOrigin, mouseRayDirection); } - node->unlock(); } // simulate avatar fades @@ -3759,7 +3758,7 @@ void Application::renderAvatars(bool forceRenderHead, bool selfAvatarOnly) { NodeList* nodeList = NodeList::getInstance(); foreach(SharedNodePointer node, nodeList->getNodeHash()) { - node->lock(); + QMutexLocker(&node->getMutex()); if (node->getLinkedData() != NULL && node->getType() == NODE_TYPE_AGENT) { Avatar *avatar = (Avatar *)node->getLinkedData(); @@ -3769,8 +3768,6 @@ void Application::renderAvatars(bool forceRenderHead, bool selfAvatarOnly) { avatar->render(false); avatar->setDisplayingLookatVectors(Menu::getInstance()->isOptionChecked(MenuOption::LookAtVectors)); } - - node->unlock(); } // render avatar fades diff --git a/libraries/octree-server/src/OctreeSendThread.cpp b/libraries/octree-server/src/OctreeSendThread.cpp index 41450022d2..f8a3b77aa3 100644 --- a/libraries/octree-server/src/OctreeSendThread.cpp +++ b/libraries/octree-server/src/OctreeSendThread.cpp @@ -34,7 +34,7 @@ bool OctreeSendThread::process() { if (node) { // make sure the node list doesn't kill our node while we're using it - if (node->trylock()) { + if (node->getMutex().tryLock()) { gotLock = true; OctreeQueryNode* nodeData = NULL; @@ -51,7 +51,7 @@ bool OctreeSendThread::process() { 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 { diff --git a/libraries/shared/src/Node.cpp b/libraries/shared/src/Node.cpp index 36ee1682a6..2b815d57aa 100644 --- a/libraries/shared/src/Node.cpp +++ b/libraries/shared/src/Node.cpp @@ -33,9 +33,10 @@ Node::Node(const QUuid& uuid, char type, const HifiSockAddr& publicSocket, const _bytesReceivedMovingAverage(NULL), _linkedData(NULL), _isAlive(true), - _clockSkewUsec(0) + _clockSkewUsec(0), + _mutex() { - pthread_mutex_init(&_mutex, 0); + } Node::~Node() { @@ -46,8 +47,6 @@ Node::~Node() { } delete _bytesReceivedMovingAverage; - - pthread_mutex_destroy(&_mutex); } // Names of Node Types diff --git a/libraries/shared/src/Node.h b/libraries/shared/src/Node.h index fe6e95dac8..c5cfa61046 100644 --- a/libraries/shared/src/Node.h +++ b/libraries/shared/src/Node.h @@ -19,6 +19,7 @@ #endif #include +#include #include #include "HifiSockAddr.h" @@ -73,13 +74,7 @@ public: int getClockSkewUsec() const { return _clockSkewUsec; } void setClockSkewUsec(int clockSkew) { _clockSkewUsec = clockSkew; } - void lock() { pthread_mutex_lock(&_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&); + QMutex& getMutex() { return _mutex; } private: // privatize copy and assignment operator to disallow Node copying @@ -98,12 +93,9 @@ private: bool _isAlive; int _pingMs; 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); #endif /* defined(__hifi__Node__) */ diff --git a/libraries/shared/src/NodeList.cpp b/libraries/shared/src/NodeList.cpp index 91afb66db0..c3b8fdebc6 100644 --- a/libraries/shared/src/NodeList.cpp +++ b/libraries/shared/src/NodeList.cpp @@ -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) { - node->lock(); + QMutexLocker(&node->getMutex()); node->setLastHeardMicrostamp(usecTimestampNow()); @@ -244,12 +244,9 @@ int NodeList::updateNodeWithData(Node *node, const HifiSockAddr& senderSockAddr, int numParsedBytes = node->getLinkedData()->parseData(packetData, dataBytes); - node->unlock(); - return numParsedBytes; } else { // 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; } } @@ -695,7 +692,7 @@ SharedNodePointer NodeList::addOrUpdateNode(const QUuid& uuid, char nodeType, return newNodeSharedPointer; } else { SharedNodePointer node = matchingNodeItem.value(); - matchingNodeItem.value()->lock(); + QMutexLocker(&node->getMutex()); if (node->getType() == NODE_TYPE_AUDIO_MIXER || 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"; } - node->unlock(); - // we had this node already, do nothing for now return node; } @@ -797,7 +792,7 @@ void NodeList::removeSilentNodes() { SharedNodePointer node = nodeItem.value(); - node->lock(); + QMutexLocker(&node->getMutex()); if ((usecTimestampNow() - node->getLastHeardMicrostamp()) > NODE_SILENCE_THRESHOLD_USECS) { @@ -805,9 +800,6 @@ void NodeList::removeSilentNodes() { _nodeHash.erase(nodeItem); } - // unlock the node - node->unlock(); - nodeItem++; } }