Move ThreadGroup to new abstracted thread scheme.

This commit is contained in:
Pavel Krajcevski 2012-09-30 16:15:36 -04:00
parent 28af6ec225
commit d3f03a1fd3
3 changed files with 23 additions and 34 deletions

View file

@ -71,7 +71,7 @@ public:
unsigned int times = m_Times; unsigned int times = m_Times;
if(--m_ThreadCount == 0) { if(--m_ThreadCount == 0) {
m_Times = 0; m_Times++;
m_ThreadCount = m_ThreadLimit; m_ThreadCount = m_ThreadLimit;
m_CV.NotifyAll(); m_CV.NotifyAll();
return true; return true;

View file

@ -3,11 +3,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <assert.h>
#include <boost/thread/thread.hpp>
#include <boost/thread/barrier.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/condition_variable.hpp>
CmpThread::CmpThread() CmpThread::CmpThread()
: m_StartBarrier(NULL) : m_StartBarrier(NULL)
@ -34,7 +30,7 @@ void CmpThread::operator()() {
while(1) { while(1) {
// Wait for signal to start work... // Wait for signal to start work...
m_StartBarrier->wait(); m_StartBarrier->Wait();
if(*m_ParentExitFlag) { if(*m_ParentExitFlag) {
return; return;
@ -43,19 +39,19 @@ void CmpThread::operator()() {
(*m_CmpFunc)(m_InBuf, m_OutBuf, m_Width, m_Height); (*m_CmpFunc)(m_InBuf, m_OutBuf, m_Width, m_Height);
{ {
boost::lock_guard<boost::mutex> lock(*m_ParentCounterLock); TCLock lock(*m_ParentCounterLock);
(*m_ParentCounter)++; (*m_ParentCounter)++;
} }
m_FinishCV->notify_one(); m_FinishCV->NotifyOne();
} }
} }
ThreadGroup::ThreadGroup( int numThreads, const unsigned char *inBuf, unsigned int inBufSz, CompressionFunc func, unsigned char *outBuf ) ThreadGroup::ThreadGroup( int numThreads, const unsigned char *inBuf, unsigned int inBufSz, CompressionFunc func, unsigned char *outBuf )
: m_StartBarrier(new boost::barrier(numThreads + 1)) : m_StartBarrier(new TCBarrier(numThreads + 1))
, m_FinishMutex(new boost::mutex()) , m_FinishMutex(new TCMutex())
, m_FinishCV(new boost::condition_variable()) , m_FinishCV(new TCConditionVariable())
, m_NumThreads(numThreads) , m_NumThreads(numThreads)
, m_ActiveThreads(0) , m_ActiveThreads(0)
, m_Func(func) , m_Func(func)
@ -136,7 +132,7 @@ bool ThreadGroup::PrepareThreads() {
blocksProcessed += numBlocksThisThread; blocksProcessed += numBlocksThisThread;
m_ThreadHandles[m_ActiveThreads] = new boost::thread(t); m_ThreadHandles[m_ActiveThreads] = new TCThread(t);
m_ActiveThreads++; m_ActiveThreads++;
} }
@ -160,7 +156,7 @@ bool ThreadGroup::Start() {
// Last thread to activate the barrier is this one. // Last thread to activate the barrier is this one.
m_ThreadState = eThreadState_Running; m_ThreadState = eThreadState_Running;
m_StartBarrier->wait(); m_StartBarrier->Wait();
return true; return true;
} }
@ -179,11 +175,11 @@ bool ThreadGroup::CleanUpThreads() {
m_ExitFlag = true; m_ExitFlag = true;
// Hit the barrier to signal them to go. // Hit the barrier to signal them to go.
m_StartBarrier->wait(); m_StartBarrier->Wait();
// Clean up. // Clean up.
for(int i = 0; i < m_ActiveThreads; i++) { for(int i = 0; i < m_ActiveThreads; i++) {
m_ThreadHandles[i]->join(); m_ThreadHandles[i]->Join();
delete m_ThreadHandles[i]; delete m_ThreadHandles[i];
} }
@ -194,9 +190,9 @@ bool ThreadGroup::CleanUpThreads() {
void ThreadGroup::Join() { void ThreadGroup::Join() {
boost::unique_lock<boost::mutex> lock(*m_FinishMutex); TCLock lock(*m_FinishMutex);
while(m_ThreadsFinished != m_ActiveThreads) { while(m_ThreadsFinished != m_ActiveThreads) {
m_FinishCV->wait(lock); m_FinishCV->Wait(lock);
} }
m_StopWatch.Stop(); m_StopWatch.Stop();

View file

@ -2,26 +2,19 @@
#define _THREAD_GROUP_H_ #define _THREAD_GROUP_H_
#include "TexComp.h" #include "TexComp.h"
#include "Thread.h"
#include "StopWatch.h" #include "StopWatch.h"
// forward declare struct CmpThread : public TCCallable {
namespace boost {
class thread;
class mutex;
class barrier;
class condition_variable;
}
struct CmpThread {
friend class ThreadGroup; friend class ThreadGroup;
private: private:
boost::barrier *m_StartBarrier; TCBarrier *m_StartBarrier;
int *m_ParentCounter; int *m_ParentCounter;
boost::mutex *m_ParentCounterLock; TCMutex *m_ParentCounterLock;
boost::condition_variable *m_FinishCV; TCConditionVariable *m_FinishCV;
int m_Width; int m_Width;
int m_Height; int m_Height;
@ -65,10 +58,10 @@ class ThreadGroup {
}; };
private: private:
boost::barrier *const m_StartBarrier; TCBarrier *const m_StartBarrier;
boost::mutex *const m_FinishMutex; TCMutex *const m_FinishMutex;
boost::condition_variable *const m_FinishCV; TCConditionVariable *const m_FinishCV;
static const int kMaxNumThreads = 256; static const int kMaxNumThreads = 256;
const int m_NumThreads; const int m_NumThreads;
@ -77,7 +70,7 @@ class ThreadGroup {
int m_ThreadsFinished; int m_ThreadsFinished;
CmpThread m_Threads[kMaxNumThreads]; CmpThread m_Threads[kMaxNumThreads];
boost::thread *m_ThreadHandles[kMaxNumThreads]; TCThread *m_ThreadHandles[kMaxNumThreads];
// State variables. // State variables.
const unsigned int m_ImageDataSz; const unsigned int m_ImageDataSz;