From 4d52ea18ad84c9abaa9b4d1a0d5963ec371d20e8 Mon Sep 17 00:00:00 2001 From: Pavel Krajcevski Date: Sun, 30 Sep 2012 14:19:08 -0400 Subject: [PATCH] Move some more common code out of boost file into its own module --- Core/CMakeLists.txt | 1 + Core/src/Thread.cpp | 40 ++++++++++++++++++++++++++ Core/src/Thread.h | 44 +++++++++++++++++++++++------ Core/src/ThreadBoost.cpp | 61 +++------------------------------------- 4 files changed, 81 insertions(+), 65 deletions(-) create mode 100644 Core/src/Thread.cpp diff --git a/Core/CMakeLists.txt b/Core/CMakeLists.txt index c1d51bb..27d9dac 100644 --- a/Core/CMakeLists.txt +++ b/Core/CMakeLists.txt @@ -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" ) diff --git a/Core/src/Thread.cpp b/Core/src/Thread.cpp new file mode 100644 index 0000000..044a1cb --- /dev/null +++ b/Core/src/Thread.cpp @@ -0,0 +1,40 @@ +#include "Thread.h" + +#include + +//////////////////////////////////////////////////////////////////////////////// +// +// 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 diff --git a/Core/src/Thread.h b/Core/src/Thread.h index e0075e1..5cea1ac 100644 --- a/Core/src/Thread.h +++ b/Core/src/Thread.h @@ -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: diff --git a/Core/src/ThreadBoost.cpp b/Core/src/ThreadBoost.cpp index 1dcceb2..9c7dcc9 100644 --- a/Core/src/ThreadBoost.cpp +++ b/Core/src/ThreadBoost.cpp @@ -6,59 +6,6 @@ #include #include -//////////////////////////////////////////////////////////////////////////////// -// -// 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();