Move some more common code out of boost file into its own module

This commit is contained in:
Pavel Krajcevski 2012-09-30 14:19:08 -04:00
parent 03b4f16b06
commit 4d52ea18ad
4 changed files with 81 additions and 65 deletions

View file

@ -47,6 +47,7 @@ ENDIF()
IF( NOT THREAD_API MATCHES "None")
SET( SOURCES ${SOURCES} "src/Thread.cpp" )
SET( SOURCES ${SOURCES} "src/ThreadGroup.cpp" )
SET( SOURCES ${SOURCES} "src/WorkerQueue.cpp" )

40
Core/src/Thread.cpp Normal file
View file

@ -0,0 +1,40 @@
#include "Thread.h"
#include <assert.h>
////////////////////////////////////////////////////////////////////////////////
//
// Base Implementation
//
////////////////////////////////////////////////////////////////////////////////
TCThreadBase::TCThreadBase(const TCThreadBaseImplFactory &factory)
: m_Impl(factory.CreateImpl())
{ }
TCThreadBase::TCThreadBase(const TCThreadBase &other)
: m_Impl(other.m_Impl)
{
assert(m_Impl->GetReferenceCount() > 0);
m_Impl->IncreaseReferenceCount();
}
TCThreadBase &TCThreadBase::operator=(const TCThreadBase &other) {
assert(m_Impl->GetReferenceCount() > 0);
m_Impl->DecreaseReferenceCount();
m_Impl = other.m_Impl;
m_Impl->IncreaseReferenceCount();
}
TCThreadBase::~TCThreadBase() {
if(m_Impl->GetReferenceCount() <= 1) {
assert(m_Impl->GetReferenceCount() >= 0);
delete m_Impl;
}
}
#ifndef NDEBUG
void TCThreadBase::CheckReferenceCount() {
assert(m_Impl->GetReferenceCount() > 0);
}
#endif

View file

@ -9,8 +9,31 @@
//
////////////////////////////////////////////////////////////////////////////////
class TCThreadBaseImpl;
class TCThreadBaseImplFactory;
class TCThreadBaseImpl {
friend class TCThreadBase;
private:
int m_ReferenceCount;
void IncreaseReferenceCount() { m_ReferenceCount++; }
void DecreaseReferenceCount() { m_ReferenceCount--; }
int GetReferenceCount() const { return m_ReferenceCount; }
protected:
TCThreadBaseImpl()
: m_ReferenceCount(1)
{ }
virtual ~TCThreadBaseImpl() { }
};
class TCThreadBaseImplFactory {
protected:
TCThreadBaseImplFactory() { }
virtual ~TCThreadBaseImplFactory() { }
public:
virtual TCThreadBaseImpl *CreateImpl() const = 0;
};
class TCThreadBase {
protected:
TCThreadBase(const TCThreadBaseImplFactory &);
@ -19,8 +42,19 @@ class TCThreadBase {
~TCThreadBase();
TCThreadBaseImpl *m_Impl;
#ifndef NDEBUG
void CheckReferenceCount();
#else
void CheckReferenceCount() { }
#endif
};
////////////////////////////////////////////////////////////////////////////////
//
// Thread implementation
//
////////////////////////////////////////////////////////////////////////////////
// The base class for a thread implementation
class TCCallable {
protected:
@ -30,12 +64,6 @@ class TCCallable {
virtual void operator()() = 0;
};
////////////////////////////////////////////////////////////////////////////////
//
// Thread implementation
//
////////////////////////////////////////////////////////////////////////////////
class TCThread : public TCThreadBase {
public:

View file

@ -6,59 +6,6 @@
#include <boost/thread/mutex.hpp>
#include <boost/thread/condition_variable.hpp>
////////////////////////////////////////////////////////////////////////////////
//
// Base Implementation
//
////////////////////////////////////////////////////////////////////////////////
class TCThreadBaseImpl {
int m_ReferenceCount;
public:
TCThreadBaseImpl()
: m_ReferenceCount(1)
{ }
virtual ~TCThreadBaseImpl() { }
void IncreaseReferenceCount() { m_ReferenceCount++; }
void DecreaseReferenceCount() { m_ReferenceCount--; }
int GetReferenceCount() const { return m_ReferenceCount; }
};
class TCThreadBaseImplFactory {
public:
TCThreadBaseImplFactory() { }
virtual ~TCThreadBaseImplFactory() { }
virtual TCThreadBaseImpl *CreateImpl() const = 0;
};
TCThreadBase::TCThreadBase(const TCThreadBaseImplFactory &factory)
: m_Impl(factory.CreateImpl())
{ }
TCThreadBase::TCThreadBase(const TCThreadBase &other)
: m_Impl(other.m_Impl)
{
assert(m_Impl->GetReferenceCount() > 0);
m_Impl->IncreaseReferenceCount();
}
TCThreadBase &TCThreadBase::operator=(const TCThreadBase &other) {
assert(m_Impl->GetReferenceCount() > 0);
m_Impl->DecreaseReferenceCount();
m_Impl = other.m_Impl;
m_Impl->IncreaseReferenceCount();
}
TCThreadBase::~TCThreadBase() {
if(m_Impl->GetReferenceCount() <= 1) {
assert(m_Impl->GetReferenceCount() >= 0);
delete m_Impl;
}
}
////////////////////////////////////////////////////////////////////////////////
//
// Thread Implementation
@ -105,7 +52,7 @@ TCThread::TCThread(TCCallable &callable)
{ }
void TCThread::Join() {
assert(m_Impl->GetReferenceCount() > 0);
CheckReferenceCount();
((TCThreadImpl *)m_Impl)->Join();
}
@ -213,21 +160,21 @@ TCConditionVariable::TCConditionVariable()
{ }
void TCConditionVariable::Wait(TCLock &lock) {
assert(m_Impl->GetReferenceCount() > 0);
CheckReferenceCount();
TCConditionVariableImpl *impl = (TCConditionVariableImpl *)m_Impl;
impl->Wait(lock);
}
void TCConditionVariable::NotifyOne() {
assert(m_Impl->GetReferenceCount() > 0);
CheckReferenceCount();
TCConditionVariableImpl *impl = (TCConditionVariableImpl *)m_Impl;
impl->NotifyOne();
}
void TCConditionVariable::NotifyAll() {
assert(m_Impl->GetReferenceCount() > 0);
CheckReferenceCount();
TCConditionVariableImpl *impl = (TCConditionVariableImpl *)m_Impl;
impl->NotifyAll();