1
0

Use std::recusive_mutex

This commit is contained in:
Tiger Wang 2014-10-20 21:26:18 +01:00
parent 2334c8dd9d
commit a324333c11
3 changed files with 9 additions and 52 deletions

View File

@ -1,6 +1,5 @@
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules #include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
#include "IsThread.h"
@ -9,41 +8,12 @@
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// cCriticalSection: // cCriticalSection:
#ifdef _DEBUG
cCriticalSection::cCriticalSection() cCriticalSection::cCriticalSection()
{ {
#ifdef _WIN32 m_IsLocked = 0;
InitializeCriticalSection(&m_CriticalSection);
#else
pthread_mutexattr_init(&m_Attributes);
pthread_mutexattr_settype(&m_Attributes, PTHREAD_MUTEX_RECURSIVE);
if (pthread_mutex_init(&m_CriticalSection, &m_Attributes) != 0)
{
LOGERROR("Could not initialize Critical Section!");
}
#endif
#ifdef _DEBUG
m_IsLocked = 0;
#endif // _DEBUG
}
cCriticalSection::~cCriticalSection()
{
#ifdef _WIN32
DeleteCriticalSection(&m_CriticalSection);
#else
if (pthread_mutex_destroy(&m_CriticalSection) != 0)
{
LOGWARNING("Could not destroy Critical Section!");
}
pthread_mutexattr_destroy(&m_Attributes);
#endif
} }
#endif // _DEBUG
@ -51,11 +21,7 @@ cCriticalSection::~cCriticalSection()
void cCriticalSection::Lock() void cCriticalSection::Lock()
{ {
#ifdef _WIN32 m_Mutex.lock();
EnterCriticalSection(&m_CriticalSection);
#else
pthread_mutex_lock(&m_CriticalSection);
#endif
#ifdef _DEBUG #ifdef _DEBUG
m_IsLocked += 1; m_IsLocked += 1;
@ -74,11 +40,7 @@ void cCriticalSection::Unlock()
m_IsLocked -= 1; m_IsLocked -= 1;
#endif // _DEBUG #endif // _DEBUG
#ifdef _WIN32 m_Mutex.unlock();
LeaveCriticalSection(&m_CriticalSection);
#else
pthread_mutex_unlock(&m_CriticalSection);
#endif
} }

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <mutex>
@ -8,8 +9,6 @@
class cCriticalSection class cCriticalSection
{ {
public: public:
cCriticalSection(void);
~cCriticalSection();
void Lock(void); void Lock(void);
void Unlock(void); void Unlock(void);
@ -17,6 +16,7 @@ public:
// IsLocked/IsLockedByCurrentThread are only used in ASSERT statements, but because of the changes with ASSERT they must always be defined // IsLocked/IsLockedByCurrentThread are only used in ASSERT statements, but because of the changes with ASSERT they must always be defined
// The fake versions (in Release) will not effect the program in any way // The fake versions (in Release) will not effect the program in any way
#ifdef _DEBUG #ifdef _DEBUG
cCriticalSection(void);
bool IsLocked(void); bool IsLocked(void);
bool IsLockedByCurrentThread(void); bool IsLockedByCurrentThread(void);
#else #else
@ -30,12 +30,7 @@ private:
std::thread::id m_OwningThreadID; std::thread::id m_OwningThreadID;
#endif // _DEBUG #endif // _DEBUG
#ifdef _WIN32 std::recursive_mutex m_Mutex;
CRITICAL_SECTION m_CriticalSection;
#else // _WIN32
pthread_mutex_t m_CriticalSection;
pthread_mutexattr_t m_Attributes;
#endif // else _WIN32
} ALIGN_8; } ALIGN_8;

View File

@ -63,7 +63,7 @@ void cIsThread::Stop(void)
bool cIsThread::Wait(void) bool cIsThread::Wait(void)
{ {
#ifdef LOGD // ProtoProxy doesn't have LOGD #ifdef LOGD // ProtoProxy doesn't have LOGD
LOGD("Waiting for thread %s to finish", m_ThreadName.c_str()); LOGD("Waiting for thread %s to finish", m_ThreadName.c_str());
#endif // LOGD #endif // LOGD