1
0

Critical sections aren't heap-allocated in linux anymore.

git-svn-id: http://mc-server.googlecode.com/svn/trunk@1012 0a769ca7-a7f5-676a-18bf-c427514a06d6
This commit is contained in:
madmaxoft@gmail.com 2012-10-26 15:03:44 +00:00
parent 403ef6bcdc
commit 31a326382a
2 changed files with 26 additions and 30 deletions

View File

@ -11,19 +11,17 @@
cCriticalSection::cCriticalSection() cCriticalSection::cCriticalSection()
{ {
#ifdef _WIN32 #ifdef _WIN32
InitializeCriticalSection( &m_CriticalSection ); InitializeCriticalSection(&m_CriticalSection);
#else #else
m_Attributes = new pthread_mutexattr_t; pthread_mutexattr_init(&m_Attributes);
pthread_mutexattr_init((pthread_mutexattr_t*)m_Attributes); pthread_mutexattr_settype(&m_Attributes, PTHREAD_MUTEX_RECURSIVE);
pthread_mutexattr_settype((pthread_mutexattr_t*)m_Attributes, PTHREAD_MUTEX_RECURSIVE);
m_CriticalSectionPtr = new pthread_mutex_t; if (pthread_mutex_init(&m_CriticalSection, &m_Attributes) != 0)
if( pthread_mutex_init( (pthread_mutex_t*)m_CriticalSectionPtr, (pthread_mutexattr_t*)m_Attributes ) != 0 ) {
{ LOGERROR("Could not initialize Critical Section!");
LOG("ERROR: Could not initialize Critical Section!"); }
} #endif
#endif
} }
@ -32,17 +30,15 @@ cCriticalSection::cCriticalSection()
cCriticalSection::~cCriticalSection() cCriticalSection::~cCriticalSection()
{ {
#ifdef _WIN32 #ifdef _WIN32
DeleteCriticalSection( &m_CriticalSection ); DeleteCriticalSection(&m_CriticalSection);
#else #else
if( pthread_mutex_destroy( (pthread_mutex_t*)m_CriticalSectionPtr ) != 0 ) if (pthread_mutex_destroy(&m_CriticalSection) != 0)
{ {
LOG("ERROR: Could not destroy Critical Section!"); LOGWARNING("Could not destroy Critical Section!");
} }
delete (pthread_mutex_t*)m_CriticalSectionPtr; pthread_mutexattr_destroy(&m_Attributes);
pthread_mutexattr_destroy( (pthread_mutexattr_t*)m_Attributes ); #endif
delete (pthread_mutexattr_t*)m_Attributes;
#endif
} }
@ -52,9 +48,9 @@ cCriticalSection::~cCriticalSection()
void cCriticalSection::Lock() void cCriticalSection::Lock()
{ {
#ifdef _WIN32 #ifdef _WIN32
EnterCriticalSection( &m_CriticalSection ); EnterCriticalSection(&m_CriticalSection);
#else #else
pthread_mutex_lock( (pthread_mutex_t*)m_CriticalSectionPtr ); pthread_mutex_lock(&m_CriticalSection);
#endif #endif
#ifdef _DEBUG #ifdef _DEBUG
@ -74,9 +70,9 @@ void cCriticalSection::Unlock()
#endif // _DEBUG #endif // _DEBUG
#ifdef _WIN32 #ifdef _WIN32
LeaveCriticalSection( &m_CriticalSection ); LeaveCriticalSection(&m_CriticalSection);
#else #else
pthread_mutex_unlock( (pthread_mutex_t*)m_CriticalSectionPtr ); pthread_mutex_unlock(&m_CriticalSection);
#endif #endif
} }
@ -96,7 +92,7 @@ bool cCriticalSection::IsLocked(void)
bool cCriticalSection::IsLockedByCurrentThread(void) bool cCriticalSection::IsLockedByCurrentThread(void)
{ {
return m_IsLocked && (m_OwningThreadID == cIsThread::GetCurrentID()); return (m_IsLocked && (m_OwningThreadID == cIsThread::GetCurrentID()));
} }
#endif // _DEBUG #endif // _DEBUG

View File

@ -28,8 +28,8 @@ private:
#ifdef _WIN32 #ifdef _WIN32
CRITICAL_SECTION m_CriticalSection; CRITICAL_SECTION m_CriticalSection;
#else // _WIN32 #else // _WIN32
void* m_CriticalSectionPtr ALIGN_8; // Pointer to a CRITICAL_SECTION object pthread_mutex_t m_CriticalSection;
void* m_Attributes ALIGN_8; pthread_mutexattr_t m_Attributes;
#endif // else _WIN32 #endif // else _WIN32
} ALIGN_8; } ALIGN_8;