From 9bc4a3df41d8227212ffb9beff72e147ed23e564 Mon Sep 17 00:00:00 2001 From: Brad Hefta-Gaub Date: Fri, 10 Jan 2014 18:01:36 -0800 Subject: [PATCH] switch from pthread mutex to QMutex --- interface/src/VoxelSystem.cpp | 42 ++++++------- interface/src/VoxelSystem.h | 6 +- interface/src/ui/LogDialog.cpp | 9 ++- interface/src/ui/LogDialog.h | 6 +- libraries/octree/src/JurisdictionSender.cpp | 2 - libraries/octree/src/JurisdictionSender.h | 9 +-- libraries/octree/src/Octree.cpp | 36 +++++------ libraries/octree/src/Octree.h | 66 ++++++++++----------- libraries/shared/src/GenericThread.cpp | 16 +++-- libraries/shared/src/GenericThread.h | 7 ++- libraries/shared/src/Node.cpp | 4 -- libraries/shared/src/Node.h | 39 ++++++------ 12 files changed, 111 insertions(+), 131 deletions(-) diff --git a/interface/src/VoxelSystem.cpp b/interface/src/VoxelSystem.cpp index ede9a8e15d..bfd6d92d1a 100644 --- a/interface/src/VoxelSystem.cpp +++ b/interface/src/VoxelSystem.cpp @@ -74,9 +74,6 @@ VoxelSystem::VoxelSystem(float treeScale, int maxVoxels) _tree = new VoxelTree(); _tree->getRoot()->setVoxelSystem(this); - pthread_mutex_init(&_bufferWriteLock, NULL); - pthread_mutex_init(&_treeLock, NULL); - pthread_mutex_init(&_freeIndexLock, NULL); VoxelTreeElement::addDeleteHook(this); VoxelTreeElement::addUpdateHook(this); @@ -188,10 +185,10 @@ glBufferIndex VoxelSystem::getNextBufferIndex() { glBufferIndex output = GLBUFFER_INDEX_UNKNOWN; // if there's a free index, use it... if (_freeIndexes.size() > 0) { - pthread_mutex_lock(&_freeIndexLock); + _freeIndexLock.lock(); output = _freeIndexes.back(); _freeIndexes.pop_back(); - pthread_mutex_unlock(&_freeIndexLock); + _freeIndexLock.unlock(); } else { output = _voxelsInWriteArrays; _voxelsInWriteArrays++; @@ -222,9 +219,9 @@ void VoxelSystem::freeBufferIndex(glBufferIndex index) { } if (!inList) { // make the index available for next node that needs to be drawn - pthread_mutex_lock(&_freeIndexLock); + _freeIndexLock.lock(); _freeIndexes.push_back(index); - pthread_mutex_unlock(&_freeIndexLock); + _freeIndexLock.unlock(); // make the VBO slot "invisible" in case this slot is not used const glm::vec3 startVertex(FLT_MAX, FLT_MAX, FLT_MAX); @@ -243,14 +240,14 @@ void VoxelSystem::clearFreeBufferIndexes() { // clear out freeIndexes { - PerformanceWarning warn(showWarnings,"clearFreeBufferIndexes() : pthread_mutex_lock(&_freeIndexLock)"); - pthread_mutex_lock(&_freeIndexLock); + PerformanceWarning warn(showWarnings,"clearFreeBufferIndexes() : _freeIndexLock.lock()"); + _freeIndexLock.lock(); } { PerformanceWarning warn(showWarnings,"clearFreeBufferIndexes() : _freeIndexes.clear()"); _freeIndexes.clear(); } - pthread_mutex_unlock(&_freeIndexLock); + _freeIndexLock.unlock(); } VoxelSystem::~VoxelSystem() { @@ -259,9 +256,6 @@ VoxelSystem::~VoxelSystem() { cleanupVoxelMemory(); delete _tree; - pthread_mutex_destroy(&_bufferWriteLock); - pthread_mutex_destroy(&_treeLock); - pthread_mutex_destroy(&_freeIndexLock); } void VoxelSystem::setMaxVoxels(int maxVoxels) { @@ -345,7 +339,7 @@ void VoxelSystem::setVoxelsAsPoints(bool voxelsAsPoints) { void VoxelSystem::cleanupVoxelMemory() { if (_initialized) { - pthread_mutex_lock(&_bufferWriteLock); + _bufferWriteLock.lock(); _initialized = false; // no longer initialized if (_useVoxelShader) { // these are used when in VoxelShader mode. @@ -383,7 +377,7 @@ void VoxelSystem::cleanupVoxelMemory() { delete[] _writeVoxelDirtyArray; delete[] _readVoxelDirtyArray; _writeVoxelDirtyArray = _readVoxelDirtyArray = NULL; - pthread_mutex_unlock(&_bufferWriteLock); + _bufferWriteLock.unlock(); } } @@ -416,7 +410,7 @@ void VoxelSystem::setupFaceIndices(GLuint& faceVBOID, GLubyte faceIdentityIndice } void VoxelSystem::initVoxelMemory() { - pthread_mutex_lock(&_bufferWriteLock); + _bufferWriteLock.lock(); _memoryUsageRAM = 0; _memoryUsageVBO = 0; // our VBO allocations as we know them @@ -531,7 +525,7 @@ void VoxelSystem::initVoxelMemory() { _initialized = true; - pthread_mutex_unlock(&_bufferWriteLock); + _bufferWriteLock.unlock(); } void VoxelSystem::writeToSVOFile(const char* filename, VoxelTreeElement* element) const { @@ -685,7 +679,7 @@ void VoxelSystem::setupNewVoxelsForDrawing() { } // lock on the buffer write lock so we can't modify the data when the GPU is reading it - pthread_mutex_lock(&_bufferWriteLock); + _bufferWriteLock.lock(); if (_voxelsUpdated) { _voxelsDirty=true; @@ -694,7 +688,7 @@ void VoxelSystem::setupNewVoxelsForDrawing() { // copy the newly written data to the arrays designated for reading, only does something if _voxelsDirty && _voxelsUpdated copyWrittenDataToReadArrays(didWriteFullVBO); - pthread_mutex_unlock(&_bufferWriteLock); + _bufferWriteLock.unlock(); uint64_t end = usecTimestampNow(); int elapsedmsec = (end - start) / 1000; @@ -725,8 +719,8 @@ void VoxelSystem::setupNewVoxelsForDrawingSingleNode(bool allowBailEarly) { // lock on the buffer write lock so we can't modify the data when the GPU is reading it { PerformanceWarning warn(Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings), - "setupNewVoxelsForDrawingSingleNode()... pthread_mutex_lock(&_bufferWriteLock);"); - pthread_mutex_lock(&_bufferWriteLock); + "setupNewVoxelsForDrawingSingleNode()... _bufferWriteLock.lock();" ); + _bufferWriteLock.lock(); } _voxelsDirty = true; // if we got this far, then we can assume some voxels are dirty @@ -737,7 +731,7 @@ void VoxelSystem::setupNewVoxelsForDrawingSingleNode(bool allowBailEarly) { // after... _voxelsUpdated = 0; - pthread_mutex_unlock(&_bufferWriteLock); + _bufferWriteLock.unlock(); uint64_t end = usecTimestampNow(); int elapsedmsec = (end - start) / 1000; @@ -2766,13 +2760,13 @@ unsigned long VoxelSystem::getVoxelMemoryUsageGPU() { } void VoxelSystem::lockTree() { - pthread_mutex_lock(&_treeLock); + _treeLock.lock(); _treeIsBusy = true; } void VoxelSystem::unlockTree() { _treeIsBusy = false; - pthread_mutex_unlock(&_treeLock); + _treeLock.unlock(); } diff --git a/interface/src/VoxelSystem.h b/interface/src/VoxelSystem.h index e9f0cc47ab..9b1c07a3cd 100644 --- a/interface/src/VoxelSystem.h +++ b/interface/src/VoxelSystem.h @@ -260,8 +260,8 @@ private: GLuint _vboIndicesFront; GLuint _vboIndicesBack; - pthread_mutex_t _bufferWriteLock; - pthread_mutex_t _treeLock; + QMutex _bufferWriteLock; + QMutex _treeLock; ViewFrustum _lastKnownViewFrustum; ViewFrustum _lastStableViewFrustum; @@ -287,7 +287,7 @@ private: int _hookID; std::vector _freeIndexes; - pthread_mutex_t _freeIndexLock; + QMutex _freeIndexLock; void freeBufferIndex(glBufferIndex index); void clearFreeBufferIndexes(); diff --git a/interface/src/ui/LogDialog.cpp b/interface/src/ui/LogDialog.cpp index 4b5ee7f80a..b532c0fe52 100644 --- a/interface/src/ui/LogDialog.cpp +++ b/interface/src/ui/LogDialog.cpp @@ -111,11 +111,11 @@ void LogDialog::resizeEvent(QResizeEvent*) { void LogDialog::appendLogLine(QString logLine) { if (isVisible()) { - pthread_mutex_lock(& _mutex); + _mutex.lock(); if (logLine.contains(_searchTerm, Qt::CaseInsensitive)) { _logTextBox->appendPlainText(logLine.simplified()); } - pthread_mutex_unlock(& _mutex); + _mutex.unlock(); _logTextBox->ensureCursorVisible(); } } @@ -140,13 +140,12 @@ void LogDialog::handleSearchTextChanged(const QString searchText) { void LogDialog::showLogData() { _logTextBox->clear(); - pthread_mutex_lock(& _mutex); + _mutex.lock(); QStringList _logData = _logger->getLogData(); for (int i = 0; i < _logData.size(); ++i) { appendLogLine(_logData[i]); } - - pthread_mutex_unlock(& _mutex); + _mutex.unlock(); } KeywordHighlighter::KeywordHighlighter(QTextDocument *parent) : QSyntaxHighlighter(parent), keywordFormat() { diff --git a/interface/src/ui/LogDialog.h b/interface/src/ui/LogDialog.h index 6741012a26..c8e7441d3c 100644 --- a/interface/src/ui/LogDialog.h +++ b/interface/src/ui/LogDialog.h @@ -10,12 +10,12 @@ #define __interface__LogDialog__ #include +#include #include #include #include #include #include -#include #include "AbstractLoggerInterface.h" @@ -31,7 +31,7 @@ protected: private: QTextCharFormat keywordFormat; - + }; class LogDialog : public QDialog { @@ -60,7 +60,7 @@ private: QCheckBox* _extraDebuggingBox; QPushButton* _revealLogButton; QPlainTextEdit* _logTextBox; - pthread_mutex_t _mutex; + QMutex _mutex; QString _searchTerm; KeywordHighlighter* _highlighter; diff --git a/libraries/octree/src/JurisdictionSender.cpp b/libraries/octree/src/JurisdictionSender.cpp index 289b2b2f07..c165f29ece 100644 --- a/libraries/octree/src/JurisdictionSender.cpp +++ b/libraries/octree/src/JurisdictionSender.cpp @@ -22,11 +22,9 @@ JurisdictionSender::JurisdictionSender(JurisdictionMap* map, NODE_TYPE type, Pac _jurisdictionMap(map) { _nodeType = type; - pthread_mutex_init(&_requestingNodeMutex, 0); } JurisdictionSender::~JurisdictionSender() { - pthread_mutex_destroy(&_requestingNodeMutex); } diff --git a/libraries/octree/src/JurisdictionSender.h b/libraries/octree/src/JurisdictionSender.h index c3a47ffee3..77127e201f 100644 --- a/libraries/octree/src/JurisdictionSender.h +++ b/libraries/octree/src/JurisdictionSender.h @@ -12,6 +12,7 @@ #define __shared__JurisdictionSender__ #include +#include #include #include @@ -38,14 +39,14 @@ protected: virtual void processPacket(const HifiSockAddr& senderAddress, unsigned char* packetData, ssize_t packetLength); /// Locks all the resources of the thread. - void lockRequestingNodes() { pthread_mutex_lock(&_requestingNodeMutex); } + void lockRequestingNodes() { _requestingNodeMutex.lock(); } /// Unlocks all the resources of the thread. - void unlockRequestingNodes() { pthread_mutex_unlock(&_requestingNodeMutex); } - + void unlockRequestingNodes() { _requestingNodeMutex.unlock(); } + private: - pthread_mutex_t _requestingNodeMutex; + QMutex _requestingNodeMutex; JurisdictionMap* _jurisdictionMap; std::queue _nodesRequestingJurisdictions; NODE_TYPE _nodeType; diff --git a/libraries/octree/src/Octree.cpp b/libraries/octree/src/Octree.cpp index 2113fea5b1..b76468499e 100644 --- a/libraries/octree/src/Octree.cpp +++ b/libraries/octree/src/Octree.cpp @@ -43,20 +43,12 @@ Octree::Octree(bool shouldReaverage) : _shouldReaverage(shouldReaverage), _stopImport(false) { _rootNode = NULL; - - pthread_mutex_init(&_encodeSetLock, NULL); - pthread_mutex_init(&_deleteSetLock, NULL); - pthread_mutex_init(&_deletePendingSetLock, NULL); } Octree::~Octree() { // delete the children of the root node // this recursively deletes the tree delete _rootNode; - - pthread_mutex_destroy(&_encodeSetLock); - pthread_mutex_destroy(&_deleteSetLock); - pthread_mutex_destroy(&_deletePendingSetLock); } // Recurses voxel tree calling the RecurseOctreeOperation function for each node. @@ -1504,53 +1496,53 @@ void dumpSetContents(const char* name, std::set set) { } void Octree::startEncoding(OctreeElement* node) { - pthread_mutex_lock(&_encodeSetLock); + _encodeSetLock.lock(); _codesBeingEncoded.insert(node->getOctalCode()); - pthread_mutex_unlock(&_encodeSetLock); + _encodeSetLock.unlock(); } void Octree::doneEncoding(OctreeElement* node) { - pthread_mutex_lock(&_encodeSetLock); + _encodeSetLock.lock(); _codesBeingEncoded.erase(node->getOctalCode()); - pthread_mutex_unlock(&_encodeSetLock); + _encodeSetLock.unlock(); // if we have any pending delete codes, then delete them now. emptyDeleteQueue(); } void Octree::startDeleting(const unsigned char* code) { - pthread_mutex_lock(&_deleteSetLock); + _deleteSetLock.lock(); _codesBeingDeleted.insert(code); - pthread_mutex_unlock(&_deleteSetLock); + _deleteSetLock.unlock(); } void Octree::doneDeleting(const unsigned char* code) { - pthread_mutex_lock(&_deleteSetLock); + _deleteSetLock.lock(); _codesBeingDeleted.erase(code); - pthread_mutex_unlock(&_deleteSetLock); + _deleteSetLock.unlock(); } bool Octree::isEncoding(const unsigned char* codeBuffer) { - pthread_mutex_lock(&_encodeSetLock); + _encodeSetLock.lock(); bool isEncoding = (_codesBeingEncoded.find(codeBuffer) != _codesBeingEncoded.end()); - pthread_mutex_unlock(&_encodeSetLock); + _encodeSetLock.unlock(); return isEncoding; } void Octree::queueForLaterDelete(const unsigned char* codeBuffer) { - pthread_mutex_lock(&_deletePendingSetLock); + _deletePendingSetLock.lock(); _codesPendingDelete.insert(codeBuffer); - pthread_mutex_unlock(&_deletePendingSetLock); + _deletePendingSetLock.unlock(); } void Octree::emptyDeleteQueue() { - pthread_mutex_lock(&_deletePendingSetLock); + _deletePendingSetLock.lock(); for (std::set::iterator i = _codesPendingDelete.begin(); i != _codesPendingDelete.end(); ++i) { const unsigned char* codeToDelete = *i; _codesBeingDeleted.erase(codeToDelete); deleteOctalCodeFromTree(codeToDelete, COLLAPSE_EMPTY_TREE); } - pthread_mutex_unlock(&_deletePendingSetLock); + _deletePendingSetLock.unlock(); } void Octree::cancelImport() { diff --git a/libraries/octree/src/Octree.h b/libraries/octree/src/Octree.h index e9851d6ae0..878b3ea439 100644 --- a/libraries/octree/src/Octree.h +++ b/libraries/octree/src/Octree.h @@ -72,12 +72,12 @@ public: OctreeSceneStats* stats; CoverageMap* map; JurisdictionMap* jurisdictionMap; - + // output hints from the encode process typedef enum { - UNKNOWN, - DIDNT_FIT, - NULL_NODE, + UNKNOWN, + DIDNT_FIT, + NULL_NODE, TOO_DEEP, OUT_OF_JURISDICTION, LOD_SKIP, @@ -87,14 +87,14 @@ public: OCCLUDED } reason; reason stopReason; - + EncodeBitstreamParams( - int maxEncodeLevel = INT_MAX, + int maxEncodeLevel = INT_MAX, const ViewFrustum* viewFrustum = IGNORE_VIEW_FRUSTUM, - bool includeColor = WANT_COLOR, + bool includeColor = WANT_COLOR, bool includeExistsBits = WANT_EXISTS_BITS, - int chopLevels = 0, - bool deltaViewFrustum = false, + int chopLevels = 0, + bool deltaViewFrustum = false, const ViewFrustum* lastViewFrustum = IGNORE_VIEW_FRUSTUM, bool wantOcclusionCulling = NO_OCCLUSION_CULLING, CoverageMap* map = IGNORE_COVERAGE_MAP, @@ -122,13 +122,13 @@ public: jurisdictionMap(jurisdictionMap), stopReason(UNKNOWN) {} - + void displayStopReason() { printf("StopReason: "); switch (stopReason) { default: case UNKNOWN: printf("UNKNOWN\n"); break; - + case DIDNT_FIT: printf("DIDNT_FIT\n"); break; case NULL_NODE: printf("NULL_NODE\n"); break; case TOO_DEEP: printf("TOO_DEEP\n"); break; @@ -158,9 +158,9 @@ public: QUuid sourceUUID; Node* sourceNode; bool wantImportProgress; - + ReadBitstreamToTreeParams( - bool includeColor = WANT_COLOR, + bool includeColor = WANT_COLOR, bool includeExistsBits = WANT_EXISTS_BITS, OctreeElement* destinationNode = NULL, QUuid sourceUUID = QUuid(), @@ -180,7 +180,7 @@ class Octree : public QObject { public: Octree(bool shouldReaverage = false); ~Octree(); - + /// Your tree class must implement this to create the correct element type virtual OctreeElement* createNewElement(unsigned char * octalCode = NULL) const = 0; @@ -209,8 +209,8 @@ public: OctreeElement* getOrCreateChildElementAt(float x, float y, float z, float s); void recurseTreeWithOperation(RecurseOctreeOperation operation, void* extraData=NULL); - - void recurseTreeWithOperationDistanceSorted(RecurseOctreeOperation operation, + + void recurseTreeWithOperationDistanceSorted(RecurseOctreeOperation operation, const glm::vec3& point, void* extraData=NULL); int encodeTreeBitstream(OctreeElement* node, OctreePacketData* packetData, OctreeElementBag& bag, @@ -223,9 +223,9 @@ public: bool findRayIntersection(const glm::vec3& origin, const glm::vec3& direction, OctreeElement*& node, float& distance, BoxFace& face); - bool findSpherePenetration(const glm::vec3& center, float radius, glm::vec3& penetration, + bool findSpherePenetration(const glm::vec3& center, float radius, glm::vec3& penetration, void** penetratedObject = NULL); - + bool findCapsulePenetration(const glm::vec3& start, const glm::vec3& end, float radius, glm::vec3& penetration); // Note: this assumes the fileFormat is the HIO individual voxels code files @@ -237,7 +237,7 @@ public: // reads voxels from square image with alpha as a Y-axis bool readFromSquareARGB32Pixels(const char *filename); bool readFromSchematicFile(const char* filename); - + // Octree does not currently handle its own locking, caller must use these to lock/unlock void lockForRead() { lock.lockForRead(); } void tryLockForRead() { lock.tryLockForRead(); } @@ -249,13 +249,13 @@ public: void copySubTreeIntoNewTree(OctreeElement* startNode, Octree* destinationTree, bool rebaseToRoot); void copyFromTreeIntoSubTree(Octree* sourceTree, OctreeElement* destinationNode); - + bool getShouldReaverage() const { return _shouldReaverage; } - void recurseNodeWithOperation(OctreeElement* node, RecurseOctreeOperation operation, + void recurseNodeWithOperation(OctreeElement* node, RecurseOctreeOperation operation, void* extraData, int recursionCount = 0); - - void recurseNodeWithOperationDistanceSorted(OctreeElement* node, RecurseOctreeOperation operation, + + void recurseNodeWithOperationDistanceSorted(OctreeElement* node, RecurseOctreeOperation operation, const glm::vec3& point, void* extraData, int recursionCount = 0); signals: @@ -269,7 +269,7 @@ public slots: protected: void deleteOctalCodeFromTreeRecursion(OctreeElement* node, void* extraData); - int encodeTreeBitstreamRecursion(OctreeElement* node, + int encodeTreeBitstreamRecursion(OctreeElement* node, OctreePacketData* packetData, OctreeElementBag& bag, EncodeBitstreamParams& params, int& currentEncodeLevel) const; @@ -277,20 +277,20 @@ protected: OctreeElement* nodeForOctalCode(OctreeElement* ancestorNode, const unsigned char* needleCode, OctreeElement** parentOfFoundNode) const; OctreeElement* createMissingNode(OctreeElement* lastParentNode, const unsigned char* codeToReach); - int readNodeData(OctreeElement *destinationNode, const unsigned char* nodeData, + int readNodeData(OctreeElement *destinationNode, const unsigned char* nodeData, int bufferSizeBytes, ReadBitstreamToTreeParams& args); - + OctreeElement* _rootNode; - + bool _isDirty; bool _shouldReaverage; bool _stopImport; - /// Octal Codes of any subtrees currently being encoded. While any of these codes is being encoded, ancestors and + /// Octal Codes of any subtrees currently being encoded. While any of these codes is being encoded, ancestors and /// descendants of them can not be deleted. std::set _codesBeingEncoded; /// mutex lock to protect the encoding set - pthread_mutex_t _encodeSetLock; + QMutex _encodeSetLock; /// Called to indicate that a OctreeElement is in the process of being encoded. void startEncoding(OctreeElement* node); @@ -299,11 +299,11 @@ protected: /// Is the Octal Code currently being deleted? bool isEncoding(const unsigned char* codeBuffer); - /// Octal Codes of any subtrees currently being deleted. While any of these codes is being deleted, ancestors and + /// Octal Codes of any subtrees currently being deleted. While any of these codes is being deleted, ancestors and /// descendants of them can not be encoded. std::set _codesBeingDeleted; /// mutex lock to protect the deleting set - pthread_mutex_t _deleteSetLock; + QMutex _deleteSetLock; /// Called to indicate that an octal code is in the process of being deleted. void startDeleting(const unsigned char* code); @@ -313,13 +313,13 @@ protected: /// instead queued for later delete std::set _codesPendingDelete; /// mutex lock to protect the deleting set - pthread_mutex_t _deletePendingSetLock; + QMutex _deletePendingSetLock; /// Adds an Octal Code to the set of codes that needs to be deleted void queueForLaterDelete(const unsigned char* codeBuffer); /// flushes out any Octal Codes that had to be queued void emptyDeleteQueue(); - + QReadWriteLock lock; }; diff --git a/libraries/shared/src/GenericThread.cpp b/libraries/shared/src/GenericThread.cpp index 9f43473278..9b8b311ce4 100644 --- a/libraries/shared/src/GenericThread.cpp +++ b/libraries/shared/src/GenericThread.cpp @@ -14,12 +14,10 @@ GenericThread::GenericThread() : _stopThread(false), _isThreaded(false) // assume non-threaded, must call initialize() { - pthread_mutex_init(&_mutex, 0); } GenericThread::~GenericThread() { terminate(); - pthread_mutex_destroy(&_mutex); } void GenericThread::initialize(bool isThreaded) { @@ -32,30 +30,30 @@ void GenericThread::initialize(bool isThreaded) { void GenericThread::terminate() { if (_isThreaded) { _stopThread = true; - pthread_join(_thread, NULL); + pthread_join(_thread, NULL); _isThreaded = false; } } void* GenericThread::threadRoutine() { while (!_stopThread) { - + // override this function to do whatever your class actually does, return false to exit thread early if (!process()) { break; } - - // In non-threaded mode, this will break each time you call it so it's the + + // In non-threaded mode, this will break each time you call it so it's the // callers responsibility to continuously call this method if (!_isThreaded) { break; } } - + if (_isThreaded) { - pthread_exit(0); + pthread_exit(0); } - return NULL; + return NULL; } extern "C" void* GenericThreadEntry(void* arg) { diff --git a/libraries/shared/src/GenericThread.h b/libraries/shared/src/GenericThread.h index 6dea7d8cc8..d083a46dce 100644 --- a/libraries/shared/src/GenericThread.h +++ b/libraries/shared/src/GenericThread.h @@ -12,6 +12,7 @@ #define __shared__GenericThread__ #include +#include #include @@ -41,15 +42,15 @@ public: protected: /// Locks all the resources of the thread. - void lock() { pthread_mutex_lock(&_mutex); } + void lock() { _mutex.lock(); } /// Unlocks all the resources of the thread. - void unlock() { pthread_mutex_unlock(&_mutex); } + void unlock() { _mutex.unlock(); } bool isStillRunning() const { return !_stopThread; } private: - pthread_mutex_t _mutex; + QMutex _mutex; bool _stopThread; bool _isThreaded; diff --git a/libraries/shared/src/Node.cpp b/libraries/shared/src/Node.cpp index 739c1c03a9..d39ebf2765 100644 --- a/libraries/shared/src/Node.cpp +++ b/libraries/shared/src/Node.cpp @@ -7,7 +7,6 @@ // #include -#include #include #ifdef _WIN32 @@ -35,7 +34,6 @@ Node::Node(const QUuid& uuid, char type, const HifiSockAddr& publicSocket, const _isAlive(true), _clockSkewUsec(0) { - pthread_mutex_init(&_mutex, 0); } Node::~Node() { @@ -44,8 +42,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 96f16a31d4..841f0a2813 100644 --- a/libraries/shared/src/Node.h +++ b/libraries/shared/src/Node.h @@ -20,6 +20,7 @@ #include #include +#include #include "HifiSockAddr.h" #include "NodeData.h" @@ -29,39 +30,39 @@ class Node { public: Node(const QUuid& uuid, char type, const HifiSockAddr& publicSocket, const HifiSockAddr& localSocket); ~Node(); - + bool operator==(const Node& otherNode) const { return _uuid == otherNode._uuid; } bool operator!=(const Node& otherNode) const { return !(*this == otherNode); } - + char getType() const { return _type; } void setType(char type) { _type = type; } const char* getTypeName() const; - + const QUuid& getUUID() const { return _uuid; } void setUUID(const QUuid& uuid) { _uuid = uuid; } - + uint64_t getWakeMicrostamp() const { return _wakeMicrostamp; } void setWakeMicrostamp(uint64_t wakeMicrostamp) { _wakeMicrostamp = wakeMicrostamp; } - + uint64_t getLastHeardMicrostamp() const { return _lastHeardMicrostamp; } void setLastHeardMicrostamp(uint64_t lastHeardMicrostamp) { _lastHeardMicrostamp = lastHeardMicrostamp; } - + const HifiSockAddr& getPublicSocket() const { return _publicSocket; } void setPublicSocket(const HifiSockAddr& publicSocket); const HifiSockAddr& getLocalSocket() const { return _localSocket; } void setLocalSocket(const HifiSockAddr& localSocket); - + const HifiSockAddr* getActiveSocket() const { return _activeSocket; } - + void activatePublicSocket(); void activateLocalSocket(); - + NodeData* getLinkedData() const { return _linkedData; } void setLinkedData(NodeData* linkedData) { _linkedData = linkedData; } - + bool isAlive() const { return _isAlive; } void setAlive(bool isAlive) { _isAlive = isAlive; } - + void recordBytesReceived(int bytesReceived); float getAverageKilobitsPerSecond(); float getAveragePacketsPerSecond(); @@ -71,20 +72,20 @@ public: int getClockSkewUsec() const { return _clockSkewUsec; } void setClockSkewUsec(int clockSkew) { _clockSkewUsec = clockSkew; } - - void lock() { pthread_mutex_lock(&_mutex); } - + + void lock() { _mutex.lock(); } + /// 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); } + bool trylock() { return _mutex.tryLock(); } + void unlock() { _mutex.unlock(); } static void printLog(Node const&); - + private: // privatize copy and assignment operator to disallow Node copying Node(const Node &otherNode); Node& operator=(Node otherNode); - + char _type; QUuid _uuid; uint64_t _wakeMicrostamp; @@ -97,7 +98,7 @@ private: bool _isAlive; int _pingMs; int _clockSkewUsec; - pthread_mutex_t _mutex; + QMutex _mutex; }; int unpackNodeId(unsigned char *packedData, uint16_t *nodeId);