From 45e926536a5b835dd346ee13efa7004daec80b5d Mon Sep 17 00:00:00 2001 From: Pavel Krajcevski Date: Sun, 27 Jan 2013 14:36:19 -0500 Subject: [PATCH] Move Thread.h out of the include directory, and update the BlockStatManager to compensate. --- Core/CMakeLists.txt | 2 +- Core/include/BlockStats.h | 22 +++++++++++++++-- Core/src/BlockStats.cpp | 44 ++++++++++++++++++++++++++++++++-- Core/{include => src}/Thread.h | 0 4 files changed, 63 insertions(+), 5 deletions(-) rename Core/{include => src}/Thread.h (100%) diff --git a/Core/CMakeLists.txt b/Core/CMakeLists.txt index 68681dd..61b0045 100644 --- a/Core/CMakeLists.txt +++ b/Core/CMakeLists.txt @@ -94,7 +94,7 @@ ELSE() SET( SOURCES ${SOURCES} "src/ThreadGroup.cpp" ) SET( SOURCES ${SOURCES} "src/WorkerQueue.cpp" ) - SET( HEADERS ${HEADERS} "include/Thread.h" ) + SET( HEADERS ${HEADERS} "src/Thread.h" ) SET( HEADERS ${HEADERS} "src/ThreadGroup.h" ) SET( HEADERS ${HEADERS} "src/WorkerQueue.h" ) ENDIF() diff --git a/Core/include/BlockStats.h b/Core/include/BlockStats.h index 19a4ddd..97c6d9b 100644 --- a/Core/include/BlockStats.h +++ b/Core/include/BlockStats.h @@ -3,7 +3,19 @@ #include "TexCompTypes.h" #include "ReferenceCounter.h" -#include "Thread.h" + +/////////////////////////////////////////////////////////////////////////////// +// +// Forward declarations +// +/////////////////////////////////////////////////////////////////////////////// +class TCMutex; + +/////////////////////////////////////////////////////////////////////////////// +// +// class BlockStat +// +/////////////////////////////////////////////////////////////////////////////// struct BlockStat { friend class BlockStatManager; @@ -36,6 +48,8 @@ class BlockStatManager { public: BlockStatManager(int nBlocks); + BlockStatManager(const BlockStatManager &); + BlockStatManager &operator=(const BlockStatManager &); ~BlockStatManager(); uint32 BeginBlock(); @@ -55,6 +69,7 @@ class BlockStatManager { private: BlockStatList(const BlockStat &stat); + BlockStatList(const BlockStatList &other); BlockStat m_Stat; BlockStatList *m_Tail; @@ -63,9 +78,12 @@ class BlockStatManager { } *m_BlockStatList; uint32 m_BlockStatListSz; - TCMutex m_Mutex; + TCMutex *m_Mutex; uint32 m_NextBlock; ReferenceCounter m_Counter; + + // Note: we probably shouldn't call this... + void Copy(const BlockStatManager &); }; #endif // __BLOCK_STATS_H__ diff --git a/Core/src/BlockStats.cpp b/Core/src/BlockStats.cpp index b9f2fdb..f097a59 100644 --- a/Core/src/BlockStats.cpp +++ b/Core/src/BlockStats.cpp @@ -6,6 +6,7 @@ #include #include "FileStream.h" +#include "Thread.h" template static T max(const T &a, const T &b) { @@ -79,9 +80,36 @@ void BlockStat::ToString(CHAR *buf, int bufSz) const { // //////////////////////////////////////////////////////////////////////////////// +void BlockStatManager::Copy(const BlockStatManager &other) { + // This is a bug. If we copy the manager then all of the lists and pointers + // become shared and can cause dereferencing issues. Check to see where you're + // copying this class and make sure to actually create a new instance. + assert(!"We shouldn't be copying these in this manner!"); + + m_BlockStatList = new BlockStatList(other.m_BlockStatList); + m_BlockStatListSz = other.m_BlockStatListSz; + m_NextBlock = other.m_NextBlock; + + // If we do copy them, then make sure that we are actually using the exact same + // pointers for our synchronization primitives... otherwise we could run into + // deadlock issues. + m_Mutex = other.m_Mutex; +} + +BlockStatManager::BlockStatManager(const BlockStatManager &other) { + Copy(other); +} + +BlockStatManager &BlockStatManager::operator=(const BlockStatManager &other) { + m_Counter = other.m_Counter; + Copy(other); + return *this; +} + BlockStatManager::BlockStatManager(int nBlocks) : m_BlockStatListSz(max(nBlocks, 0)) , m_NextBlock(0) + , m_Mutex(new TCMutex) { m_BlockStatList = new BlockStatList[m_BlockStatListSz]; if(!m_BlockStatList) { @@ -95,6 +123,12 @@ BlockStatManager::~BlockStatManager() { if(m_Counter.GetRefCount() == 0) { delete [] m_BlockStatList; } + + if(m_Mutex) + { + delete m_Mutex; + m_Mutex = 0; + } } uint32 BlockStatManager::BeginBlock() { @@ -104,7 +138,7 @@ uint32 BlockStatManager::BeginBlock() { return m_NextBlock-1; } - TCLock lock (m_Mutex); + TCLock lock (*m_Mutex); return m_NextBlock++; } @@ -115,7 +149,7 @@ void BlockStatManager::AddStat(uint32 blockIdx, const BlockStat &stat) { return; } - TCLock lock (m_Mutex); + TCLock lock (*m_Mutex); m_BlockStatList[blockIdx].AddStat(stat); } @@ -168,8 +202,14 @@ BlockStatManager::BlockStatList::BlockStatList(const BlockStat &stat) : m_Tail(0) , m_Stat(stat) { + assert(!"If you're copying a block stat list then you're probably not using them properly."); } +BlockStatManager::BlockStatList::BlockStatList(const BlockStatList &other) + : m_Tail(new BlockStatList(*other.m_Tail)) + , m_Stat(other.m_Stat) +{} + BlockStatManager::BlockStatList::~BlockStatList() { if(m_Counter.GetRefCount() == 0 && m_Tail) { delete m_Tail; diff --git a/Core/include/Thread.h b/Core/src/Thread.h similarity index 100% rename from Core/include/Thread.h rename to Core/src/Thread.h