mirror of
https://foundry.openuru.org/gitblit/r/CWE-ou-minkata.git
synced 2025-07-18 11:19:10 +00:00
Replace hsMutex with std::mutex
This commit is contained in:
@ -57,14 +57,14 @@ hsReaderWriterLock::hsReaderWriterLock(Callback * cb)
|
||||
void hsReaderWriterLock::LockForReading()
|
||||
{
|
||||
if ( fCallback )
|
||||
fCallback->OnLockingForRead( this );
|
||||
fReaderCountLock.Lock();
|
||||
fReaderLock.Lock();
|
||||
fReaderCount++;
|
||||
if ( fReaderCount==1 )
|
||||
fWriterSema.Wait();
|
||||
fReaderLock.Unlock();
|
||||
fReaderCountLock.Unlock();
|
||||
fCallback->OnLockingForRead(this);
|
||||
{
|
||||
std::lock_guard<std::mutex> lock_count(fReaderCountLock);
|
||||
std::lock_guard<std::mutex> lock(fReaderLock);
|
||||
fReaderCount++;
|
||||
if (fReaderCount == 1)
|
||||
fWriterSema.Wait();
|
||||
}
|
||||
if ( fCallback )
|
||||
fCallback->OnLockedForRead( this );
|
||||
}
|
||||
@ -72,12 +72,13 @@ void hsReaderWriterLock::LockForReading()
|
||||
void hsReaderWriterLock::UnlockForReading()
|
||||
{
|
||||
if ( fCallback )
|
||||
fCallback->OnUnlockingForRead( this );
|
||||
fReaderLock.Lock();
|
||||
fReaderCount--;
|
||||
if ( fReaderCount==0 )
|
||||
fWriterSema.Signal();
|
||||
fReaderLock.Unlock();
|
||||
fCallback->OnUnlockingForRead(this);
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(fReaderLock);
|
||||
fReaderCount--;
|
||||
if (fReaderCount == 0)
|
||||
fWriterSema.Signal();
|
||||
}
|
||||
if ( fCallback )
|
||||
fCallback->OnUnlockedForRead( this );
|
||||
}
|
||||
@ -86,7 +87,7 @@ void hsReaderWriterLock::LockForWriting()
|
||||
{
|
||||
if ( fCallback )
|
||||
fCallback->OnLockingForWrite( this );
|
||||
fReaderCountLock.Lock();
|
||||
fReaderCountLock.lock();
|
||||
fWriterSema.Wait();
|
||||
hsAssert( fReaderCount==0, "Locked for writing, but fReaderCount>0" );
|
||||
if ( fCallback )
|
||||
@ -98,7 +99,7 @@ void hsReaderWriterLock::UnlockForWriting()
|
||||
if ( fCallback )
|
||||
fCallback->OnUnlockingForWrite( this );
|
||||
fWriterSema.Signal();
|
||||
fReaderCountLock.Unlock();
|
||||
fReaderCountLock.unlock();
|
||||
if ( fCallback )
|
||||
fCallback->OnUnlockedForWrite( this );
|
||||
}
|
||||
|
@ -43,6 +43,7 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com
|
||||
#define hsThread_Defined
|
||||
|
||||
#include "HeadSpin.h"
|
||||
#include <mutex>
|
||||
|
||||
typedef uint32_t hsMilliseconds;
|
||||
|
||||
@ -106,44 +107,6 @@ public:
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
class hsMutex {
|
||||
#if HS_BUILD_FOR_WIN32
|
||||
HANDLE fMutexH;
|
||||
#elif HS_BUILD_FOR_UNIX
|
||||
pthread_mutex_t fPMutex;
|
||||
#endif
|
||||
public:
|
||||
hsMutex();
|
||||
virtual ~hsMutex();
|
||||
|
||||
#ifdef HS_BUILD_FOR_WIN32
|
||||
HANDLE GetHandle() const { return fMutexH; }
|
||||
#endif
|
||||
|
||||
void Lock();
|
||||
bool TryLock();
|
||||
void Unlock();
|
||||
};
|
||||
|
||||
class hsTempMutexLock {
|
||||
hsMutex* fMutex;
|
||||
public:
|
||||
hsTempMutexLock(hsMutex* mutex) : fMutex(mutex)
|
||||
{
|
||||
fMutex->Lock();
|
||||
}
|
||||
hsTempMutexLock(hsMutex& mutex) : fMutex(&mutex)
|
||||
{
|
||||
fMutex->Lock();
|
||||
}
|
||||
~hsTempMutexLock()
|
||||
{
|
||||
fMutex->Unlock();
|
||||
}
|
||||
};
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
class hsSemaphore {
|
||||
#if HS_BUILD_FOR_WIN32
|
||||
HANDLE fSemaH;
|
||||
@ -181,8 +144,8 @@ class hsEvent
|
||||
#else
|
||||
enum { kRead, kWrite };
|
||||
int fFds[2];
|
||||
hsMutex fWaitLock;
|
||||
hsMutex fSignalLock;
|
||||
std::mutex fWaitLock;
|
||||
std::mutex fSignalLock;
|
||||
#endif // PSEUDO_EVENT
|
||||
#elif HS_BUILD_FOR_WIN32
|
||||
HANDLE fEvent;
|
||||
@ -231,8 +194,8 @@ public:
|
||||
|
||||
private:
|
||||
int fReaderCount;
|
||||
hsMutex fReaderCountLock;
|
||||
hsMutex fReaderLock;
|
||||
std::mutex fReaderCountLock;
|
||||
std::mutex fReaderLock;
|
||||
hsSemaphore fWriterSema;
|
||||
Callback * fCallback;
|
||||
};
|
||||
|
@ -115,32 +115,6 @@ void hsThread::ThreadYield()
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
hsMutex::hsMutex()
|
||||
{
|
||||
OSStatus status = ::MPCreateCriticalRegion(&fCriticalRegion);
|
||||
hsThrowIfOSErr(status);
|
||||
}
|
||||
|
||||
hsMutex::~hsMutex()
|
||||
{
|
||||
OSStatus status = ::MPDeleteCriticalRegion(fCriticalRegion);
|
||||
hsThrowIfOSErr(status);
|
||||
}
|
||||
|
||||
void hsMutex::Lock()
|
||||
{
|
||||
OSStatus status = ::MPEnterCriticalRegion(fCriticalRegion, kDurationForever);
|
||||
hsThrowIfOSErr(status);
|
||||
}
|
||||
|
||||
void hsMutex::Unlock()
|
||||
{
|
||||
OSStatus status = ::MPExitCriticalRegion(fCriticalRegion);
|
||||
hsThrowIfOSErr(status);
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
hsSemaphore::hsSemaphore(int initialValue)
|
||||
{
|
||||
OSStatus status = MPCreateSemaphore(kPosInfinity32, initialValue, &fSemaId);
|
||||
|
@ -190,83 +190,6 @@ static void InitEventLoggingFile()
|
||||
|
||||
#endif
|
||||
|
||||
hsMutex::hsMutex()
|
||||
{
|
||||
|
||||
#ifdef MUTEX_TIMING
|
||||
InitMutexTimerFile();
|
||||
#endif
|
||||
|
||||
// create mutex attributes
|
||||
pthread_mutexattr_t attr;
|
||||
int status = ::pthread_mutexattr_init(&attr);
|
||||
hsThrowIfOSErr(status);
|
||||
|
||||
// make the mutex attributes recursive
|
||||
status = ::pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_RECURSIVE);
|
||||
hsThrowIfOSErr(status);
|
||||
|
||||
//init the mutex
|
||||
status = ::pthread_mutex_init(&fPMutex, &attr);
|
||||
hsThrowIfOSErr(status);
|
||||
|
||||
// destroy the attributes
|
||||
status = ::pthread_mutexattr_destroy(&attr);
|
||||
hsThrowIfOSErr(status);
|
||||
}
|
||||
|
||||
hsMutex::~hsMutex()
|
||||
{
|
||||
int status = ::pthread_mutex_destroy(&fPMutex);
|
||||
hsThrowIfOSErr(status);
|
||||
}
|
||||
|
||||
void hsMutex::Lock()
|
||||
{
|
||||
#ifdef MUTEX_TIMING
|
||||
# ifndef HS_DEBUGGING
|
||||
timeval tv;
|
||||
hsWide start;
|
||||
gettimeofday( &tv, nil );
|
||||
start.Mul( tv.tv_sec, 1000000 )->Add( tv.tv_usec );
|
||||
# endif
|
||||
#endif
|
||||
|
||||
int status = ::pthread_mutex_lock(&fPMutex);
|
||||
hsThrowIfOSErr(status);
|
||||
|
||||
#ifdef MUTEX_TIMING
|
||||
# ifndef HS_DEBUGGING
|
||||
hsWide diff;
|
||||
gettimeofday( &tv, nil );
|
||||
diff.Mul( tv.tv_sec, 1000000 )->Add( tv.tv_usec )->Sub( &start )->Div( 1000000 );
|
||||
double duration = diff.AsDouble();
|
||||
if ( gMutexTimerFile && duration>0.005 )
|
||||
{
|
||||
time_t t;
|
||||
time( &t );
|
||||
struct tm *now = localtime( &t );
|
||||
char tmp[30];
|
||||
strftime( tmp, 30, "%c", now );
|
||||
fprintf( gMutexTimerFile, "[%s] [%lu:%lu] %f\n", tmp, getpid(), hsThread::GetMyThreadId(), duration );
|
||||
}
|
||||
# endif
|
||||
#endif
|
||||
}
|
||||
|
||||
bool hsMutex::TryLock()
|
||||
{
|
||||
int status = ::pthread_mutex_trylock(&fPMutex);
|
||||
hsThrowIfOSErr(status);
|
||||
return status==EBUSY?false:true;
|
||||
}
|
||||
|
||||
void hsMutex::Unlock()
|
||||
{
|
||||
int status = ::pthread_mutex_unlock(&fPMutex);
|
||||
hsThrowIfOSErr(status);
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
hsSemaphore::hsSemaphore(int initialValue, const char* name)
|
||||
@ -538,7 +461,7 @@ hsEvent::~hsEvent()
|
||||
|
||||
bool hsEvent::Wait( hsMilliseconds timeToWait )
|
||||
{
|
||||
hsTempMutexLock lock( fWaitLock );
|
||||
std::lock_guard<std::mutex> lock(fWaitLock);
|
||||
|
||||
fd_set fdset;
|
||||
FD_ZERO( &fdset );
|
||||
@ -572,7 +495,7 @@ bool hsEvent::Wait( hsMilliseconds timeToWait )
|
||||
|
||||
void hsEvent::Signal()
|
||||
{
|
||||
hsTempMutexLock lock( fSignalLock );
|
||||
std::lock_guard<std::mutex> lock(fSignalLock);
|
||||
write( fFds[kWrite], "*", 1 );
|
||||
}
|
||||
|
||||
|
@ -135,42 +135,6 @@ void hsThread::ThreadYield()
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
hsMutex::hsMutex()
|
||||
{
|
||||
fMutexH = ::CreateMutex(nil, false, nil);
|
||||
if (fMutexH == nil)
|
||||
throw hsOSException(-1);
|
||||
}
|
||||
|
||||
hsMutex::~hsMutex()
|
||||
{
|
||||
::CloseHandle(fMutexH);
|
||||
}
|
||||
|
||||
void hsMutex::Lock()
|
||||
{
|
||||
DWORD state = ::WaitForSingleObject(fMutexH, INFINITE);
|
||||
hsAssert(state != WAIT_FAILED,"hsMutex::Lock -> Wait Failed");
|
||||
hsAssert(state != WAIT_ABANDONED,"hsMutex::Lock -> Abandoned Mutex");
|
||||
hsAssert(state != WAIT_TIMEOUT,"hsMutex::Lock -> Infinite Timeout expired?");
|
||||
}
|
||||
|
||||
bool hsMutex::TryLock()
|
||||
{
|
||||
DWORD state = ::WaitForSingleObject(fMutexH, 0);
|
||||
hsAssert(state != WAIT_ABANDONED,"hsMutex::TryLock -> Abandoned Mutex");
|
||||
return state == WAIT_OBJECT_0?true:false;
|
||||
}
|
||||
|
||||
void hsMutex::Unlock()
|
||||
{
|
||||
BOOL result = ::ReleaseMutex(fMutexH);
|
||||
hsAssert(result != 0, "hsMutex::Unlock Failed!");
|
||||
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
hsSemaphore::hsSemaphore(int initialValue, const char *name)
|
||||
{
|
||||
fSemaH = ::CreateSemaphore(nil, initialValue, kPosInfinity32, name);
|
||||
|
Reference in New Issue
Block a user