From e2ad02f50ac69f3789b7e475dbce469eeec29468 Mon Sep 17 00:00:00 2001 From: "madmaxoft@gmail.com" Date: Sun, 29 Jan 2012 14:29:26 +0000 Subject: ChunkGenerator: rewritten thread-locking using the new RAII CSLock class git-svn-id: http://mc-server.googlecode.com/svn/trunk@186 0a769ca7-a7f5-676a-18bf-c427514a06d6 --- source/cCriticalSection.cpp | 97 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) (limited to 'source/cCriticalSection.cpp') diff --git a/source/cCriticalSection.cpp b/source/cCriticalSection.cpp index 85f89c195..bf84b7c2f 100644 --- a/source/cCriticalSection.cpp +++ b/source/cCriticalSection.cpp @@ -1,5 +1,6 @@ #include "cCriticalSection.h" #include "cMCLogger.h" +#include #ifdef _WIN32 #include @@ -7,6 +8,13 @@ #include #endif + + + + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// cCriticalSection: + cCriticalSection::cCriticalSection() { #ifdef _WIN32 @@ -25,6 +33,10 @@ cCriticalSection::cCriticalSection() #endif } + + + + cCriticalSection::~cCriticalSection() { #ifdef _WIN32 @@ -41,6 +53,10 @@ cCriticalSection::~cCriticalSection() #endif } + + + + void cCriticalSection::Lock() { #ifdef _WIN32 @@ -50,6 +66,10 @@ void cCriticalSection::Lock() #endif } + + + + void cCriticalSection::Unlock() { #ifdef _WIN32 @@ -58,3 +78,80 @@ void cCriticalSection::Unlock() pthread_mutex_unlock( (pthread_mutex_t*)m_CriticalSectionPtr ); #endif } + + + + + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// cCSLock + +cCSLock::cCSLock(cCriticalSection * a_CS) : + m_CS(a_CS), + m_IsLocked(false) +{ + Lock(); +} + + + + + +cCSLock::~cCSLock() +{ + Unlock(); +} + + + + + +void cCSLock::Lock(void) +{ + #ifdef _DEBUG + assert(!m_IsLocked); + m_IsLocked = true; + #endif // _DEBUG + + m_CS->Lock(); +} + + + + + +void cCSLock::Unlock(void) +{ + #ifdef _DEBUG + assert(m_IsLocked); + m_IsLocked = false; + #endif // _DEBUG + + m_CS->Unlock(); +} + + + + + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// cCSUnlock: + +cCSUnlock::cCSUnlock(cCSLock & a_Lock) : + m_Lock(a_Lock) +{ + m_Lock.Unlock(); +} + + + + + +cCSUnlock::~cCSUnlock() +{ + m_Lock.Lock(); +} + + + + -- cgit v1.2.3