switch from pthread mutex to QMutex

This commit is contained in:
Brad Hefta-Gaub 2014-01-10 18:01:36 -08:00
parent 2c30b8b5b4
commit 9bc4a3df41
12 changed files with 111 additions and 131 deletions

View file

@ -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();
}

View file

@ -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<glBufferIndex> _freeIndexes;
pthread_mutex_t _freeIndexLock;
QMutex _freeIndexLock;
void freeBufferIndex(glBufferIndex index);
void clearFreeBufferIndexes();

View file

@ -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() {

View file

@ -10,12 +10,12 @@
#define __interface__LogDialog__
#include <QDialog>
#include <QMutex>
#include <QPlainTextEdit>
#include <QLineEdit>
#include <QPushButton>
#include <QCheckBox>
#include <QSyntaxHighlighter>
#include <pthread.h>
#include "AbstractLoggerInterface.h"
@ -60,7 +60,7 @@ private:
QCheckBox* _extraDebuggingBox;
QPushButton* _revealLogButton;
QPlainTextEdit* _logTextBox;
pthread_mutex_t _mutex;
QMutex _mutex;
QString _searchTerm;
KeywordHighlighter* _highlighter;

View file

@ -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);
}

View file

@ -12,6 +12,7 @@
#define __shared__JurisdictionSender__
#include <queue>
#include <QMutex>
#include <PacketSender.h>
#include <ReceivedPacketProcessor.h>
@ -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<QUuid> _nodesRequestingJurisdictions;
NODE_TYPE _nodeType;

View file

@ -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<unsigned char*> 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<const unsigned char*>::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() {

View file

@ -290,7 +290,7 @@ protected:
/// descendants of them can not be deleted.
std::set<const unsigned char*> _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);
@ -303,7 +303,7 @@ protected:
/// descendants of them can not be encoded.
std::set<const unsigned char*> _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,7 +313,7 @@ protected:
/// instead queued for later delete
std::set<const unsigned char*> _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);

View file

@ -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) {

View file

@ -12,6 +12,7 @@
#define __shared__GenericThread__
#include <QtCore/QObject>
#include <QMutex>
#include <pthread.h>
@ -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;

View file

@ -7,7 +7,6 @@
//
#include <cstring>
#include <pthread.h>
#include <stdio.h>
#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

View file

@ -20,6 +20,7 @@
#include <QtCore/QDebug>
#include <QtCore/QUuid>
#include <QMutex>
#include "HifiSockAddr.h"
#include "NodeData.h"
@ -72,11 +73,11 @@ 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&);
@ -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);