simplify (handle recursive locking in RageMutex)
This commit is contained in:
@@ -49,10 +49,6 @@ public:
|
|||||||
/* Unlock the mutex. This must only be called when the mutex is locked; implementations
|
/* Unlock the mutex. This must only be called when the mutex is locked; implementations
|
||||||
* may fail with an assertion if the mutex is not locked. */
|
* may fail with an assertion if the mutex is not locked. */
|
||||||
virtual void Unlock() = 0;
|
virtual void Unlock() = 0;
|
||||||
|
|
||||||
/* Return the thread ID of the thread that has this mutex locked. If no thread
|
|
||||||
* holds the mutex, return GetInvalidThreadId. */
|
|
||||||
virtual uint64_t GetLockedByThreadId() const = 0;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/* These functions must be implemented by the thread implementation. */
|
/* These functions must be implemented by the thread implementation. */
|
||||||
|
|||||||
@@ -114,8 +114,6 @@ MutexImpl_Pthreads::MutexImpl_Pthreads( RageMutex *pParent ):
|
|||||||
MutexImpl( pParent )
|
MutexImpl( pParent )
|
||||||
{
|
{
|
||||||
pthread_mutex_init( &mutex, NULL );
|
pthread_mutex_init( &mutex, NULL );
|
||||||
LockedBy = GetInvalidThreadId();
|
|
||||||
LockCnt = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
MutexImpl_Pthreads::~MutexImpl_Pthreads()
|
MutexImpl_Pthreads::~MutexImpl_Pthreads()
|
||||||
@@ -128,12 +126,6 @@ MutexImpl_Pthreads::~MutexImpl_Pthreads()
|
|||||||
|
|
||||||
bool MutexImpl_Pthreads::Lock()
|
bool MutexImpl_Pthreads::Lock()
|
||||||
{
|
{
|
||||||
if( LockedBy == (uint64_t) GetThisThreadId() )
|
|
||||||
{
|
|
||||||
++LockCnt;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
#if defined(HAVE_PTHREAD_MUTEX_TIMEDLOCK) && defined(CRASH_HANDLER)
|
#if defined(HAVE_PTHREAD_MUTEX_TIMEDLOCK) && defined(CRASH_HANDLER)
|
||||||
int len = 10; /* seconds */
|
int len = 10; /* seconds */
|
||||||
int tries = 2;
|
int tries = 2;
|
||||||
@@ -151,7 +143,6 @@ bool MutexImpl_Pthreads::Lock()
|
|||||||
switch( ret )
|
switch( ret )
|
||||||
{
|
{
|
||||||
case 0:
|
case 0:
|
||||||
LockedBy = GetCurrentThreadId();
|
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
case ETIMEDOUT:
|
case ETIMEDOUT:
|
||||||
@@ -171,7 +162,6 @@ bool MutexImpl_Pthreads::Lock()
|
|||||||
int ret = pthread_mutex_lock( &mutex );
|
int ret = pthread_mutex_lock( &mutex );
|
||||||
if( ret )
|
if( ret )
|
||||||
RageException::Throw( "pthread_mutex_lock failed: %s", strerror(ret) );
|
RageException::Throw( "pthread_mutex_lock failed: %s", strerror(ret) );
|
||||||
LockedBy = GetThisThreadId();
|
|
||||||
return true;
|
return true;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
@@ -179,21 +169,9 @@ bool MutexImpl_Pthreads::Lock()
|
|||||||
|
|
||||||
void MutexImpl_Pthreads::Unlock()
|
void MutexImpl_Pthreads::Unlock()
|
||||||
{
|
{
|
||||||
if( LockCnt )
|
|
||||||
{
|
|
||||||
--LockCnt;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
LockedBy = GetInvalidThreadId();
|
|
||||||
pthread_mutex_unlock( &mutex );
|
pthread_mutex_unlock( &mutex );
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t MutexImpl_Pthreads::GetLockedByThreadId() const
|
|
||||||
{
|
|
||||||
return LockedBy;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint64_t GetThisThreadId()
|
uint64_t GetThisThreadId()
|
||||||
{
|
{
|
||||||
#if defined(PID_BASED_THREADS)
|
#if defined(PID_BASED_THREADS)
|
||||||
|
|||||||
@@ -38,9 +38,6 @@ public:
|
|||||||
|
|
||||||
struct MutexImpl_Pthreads: public MutexImpl
|
struct MutexImpl_Pthreads: public MutexImpl
|
||||||
{
|
{
|
||||||
uint64_t LockedBy;
|
|
||||||
volatile int LockCnt;
|
|
||||||
|
|
||||||
pthread_mutex_t mutex;
|
pthread_mutex_t mutex;
|
||||||
|
|
||||||
MutexImpl_Pthreads( RageMutex *parent );
|
MutexImpl_Pthreads( RageMutex *parent );
|
||||||
@@ -48,7 +45,6 @@ struct MutexImpl_Pthreads: public MutexImpl
|
|||||||
|
|
||||||
bool Lock();
|
bool Lock();
|
||||||
void Unlock();
|
void Unlock();
|
||||||
uint64_t GetLockedByThreadId() const;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -87,8 +87,6 @@ MutexImpl_Win32::MutexImpl_Win32( RageMutex *pParent ):
|
|||||||
{
|
{
|
||||||
mutex = CreateMutex( NULL, false, NULL );
|
mutex = CreateMutex( NULL, false, NULL );
|
||||||
ASSERT_M( mutex != NULL, werr_ssprintf(GetLastError(), "CreateMutex") );
|
ASSERT_M( mutex != NULL, werr_ssprintf(GetLastError(), "CreateMutex") );
|
||||||
LockedBy = GetInvalidThreadId();
|
|
||||||
LockCnt = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
MutexImpl_Win32::~MutexImpl_Win32()
|
MutexImpl_Win32::~MutexImpl_Win32()
|
||||||
@@ -98,12 +96,6 @@ MutexImpl_Win32::~MutexImpl_Win32()
|
|||||||
|
|
||||||
bool MutexImpl_Win32::Lock()
|
bool MutexImpl_Win32::Lock()
|
||||||
{
|
{
|
||||||
if( LockedBy == GetCurrentThreadId() )
|
|
||||||
{
|
|
||||||
++LockCnt;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
int len = 15000;
|
int len = 15000;
|
||||||
int tries = 2;
|
int tries = 2;
|
||||||
|
|
||||||
@@ -120,7 +112,6 @@ bool MutexImpl_Win32::Lock()
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case WAIT_OBJECT_0:
|
case WAIT_OBJECT_0:
|
||||||
LockedBy = GetCurrentThreadId();
|
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
case WAIT_TIMEOUT:
|
case WAIT_TIMEOUT:
|
||||||
@@ -140,13 +131,6 @@ bool MutexImpl_Win32::Lock()
|
|||||||
|
|
||||||
void MutexImpl_Win32::Unlock()
|
void MutexImpl_Win32::Unlock()
|
||||||
{
|
{
|
||||||
if( LockCnt )
|
|
||||||
{
|
|
||||||
--LockCnt;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
LockedBy = GetInvalidThreadId();
|
|
||||||
const bool ret = !!ReleaseMutex( mutex );
|
const bool ret = !!ReleaseMutex( mutex );
|
||||||
|
|
||||||
/* We can't ASSERT here, since this is called from checkpoints, which is
|
/* We can't ASSERT here, since this is called from checkpoints, which is
|
||||||
@@ -155,11 +139,6 @@ void MutexImpl_Win32::Unlock()
|
|||||||
sm_crash( werr_ssprintf( GetLastError(), "ReleaseMutex failed" ) );
|
sm_crash( werr_ssprintf( GetLastError(), "ReleaseMutex failed" ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t MutexImpl_Win32::GetLockedByThreadId() const
|
|
||||||
{
|
|
||||||
return LockedBy;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint64_t GetThisThreadId()
|
uint64_t GetThisThreadId()
|
||||||
{
|
{
|
||||||
return GetCurrentThreadId();
|
return GetCurrentThreadId();
|
||||||
|
|||||||
@@ -22,9 +22,6 @@ public:
|
|||||||
|
|
||||||
struct MutexImpl_Win32: public MutexImpl
|
struct MutexImpl_Win32: public MutexImpl
|
||||||
{
|
{
|
||||||
uint64_t LockedBy;
|
|
||||||
volatile int LockCnt;
|
|
||||||
|
|
||||||
HANDLE mutex;
|
HANDLE mutex;
|
||||||
|
|
||||||
MutexImpl_Win32( RageMutex *parent );
|
MutexImpl_Win32( RageMutex *parent );
|
||||||
@@ -32,7 +29,6 @@ struct MutexImpl_Win32: public MutexImpl
|
|||||||
|
|
||||||
bool Lock();
|
bool Lock();
|
||||||
void Unlock();
|
void Unlock();
|
||||||
uint64_t GetLockedByThreadId() const;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user