just some cleanup as usual

This commit is contained in:
AJ Kelly
2011-09-07 10:56:44 -05:00
parent 7c686aa160
commit 35631c73c1
4 changed files with 69 additions and 69 deletions
+14 -12
View File
@@ -12,9 +12,10 @@ public:
virtual void Halt( bool Kill ) = 0; virtual void Halt( bool Kill ) = 0;
virtual void Resume() = 0; virtual void Resume() = 0;
/* Get the identifier for this thread. The actual meaning of this is implementation- /* Get the identifier for this thread. The actual meaning of this is
* defined, except that each thread has exactly one ID and each ID corresponds to * implementation-defined, except that each thread has exactly one ID
* one thread. (This means that Win32 thread handles are not acceptable as ThreadIds.) */ * and each ID corresponds to one thread. (This means that Win32
* thread handles are not acceptable as ThreadIds.) */
virtual uint64_t GetThreadId() const = 0; virtual uint64_t GetThreadId() const = 0;
virtual int Wait() = 0; virtual int Wait() = 0;
@@ -28,17 +29,18 @@ public:
MutexImpl( RageMutex *pParent ): m_Parent(pParent) {} MutexImpl( RageMutex *pParent ): m_Parent(pParent) {}
virtual ~MutexImpl() { } virtual ~MutexImpl() { }
/* Lock the mutex. If mutex timeouts are implemented, and the mutex times out, /* Lock the mutex. If mutex timeouts are implemented, and the mutex
* return false and do not lock the mutex. No other failure return is allowed; * times out, return false and do not lock the mutex. No other failure
* all other errors should fail with an assertion. */ * return is allowed; all other errors should fail with an assertion. */
virtual bool Lock() = 0; virtual bool Lock() = 0;
/* Non-blocking lock. If locking the mutex would block because the mutex is already /* Non-blocking lock. If locking the mutex would block because the mutex
* locked by another thread, return false; otherwise return true and lock the mutex. */ * is already locked by another thread, return false; otherwise
* return true and lock the mutex. */
virtual bool TryLock() = 0; virtual bool TryLock() = 0;
/* 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;
* may fail with an assertion if the mutex is not locked. */ * implementations may fail with an assertion if the mutex is not locked. */
virtual void Unlock() = 0; virtual void Unlock() = 0;
private: private:
@@ -74,8 +76,8 @@ EventImpl *MakeEvent( MutexImpl *pMutex );
SemaImpl *MakeSemaphore( int iInitialValue ); SemaImpl *MakeSemaphore( int iInitialValue );
uint64_t GetThisThreadId(); uint64_t GetThisThreadId();
/* Since ThreadId is implementation-defined, we can't define a universal invalid /* Since ThreadId is implementation-defined, we can't define a universal
* value. Return the invalid value for this implementation. */ * invalid value. Return the invalid value for this implementation. */
uint64_t GetInvalidThreadId(); uint64_t GetInvalidThreadId();
#endif #endif
+23 -26
View File
@@ -19,17 +19,15 @@ void ThreadImpl_Pthreads::Halt( bool Kill )
/* Linux: /* Linux:
* Send a SIGSTOP to the thread. If we send a SIGKILL, pthreads will * Send a SIGSTOP to the thread. If we send a SIGKILL, pthreads will
* "helpfully" propagate it to the other threads, and we'll get killed, too. * "helpfully" propagate it to the other threads, and we'll get killed, too.
*
* This isn't ideal, since it can cause the process to background as far as * This isn't ideal, since it can cause the process to background as far as
* the shell is concerned, so the shell prompt can display before the crash * the shell is concerned, so the shell prompt can display before the crash
* handler actually displays a message. * handler actually displays a message. */
*/
SuspendThread( threadHandle ); SuspendThread( threadHandle );
} }
void ThreadImpl_Pthreads::Resume() void ThreadImpl_Pthreads::Resume()
{ {
/* Linux: Send a SIGCONT to the thread. */ // Linux: Send a SIGCONT to the thread.
ResumeThread( threadHandle ); ResumeThread( threadHandle );
} }
@@ -66,7 +64,7 @@ static void *StartThread( void *pData )
pThis->threadHandle = GetCurrentThreadId(); pThis->threadHandle = GetCurrentThreadId();
*pThis->m_piThreadID = pThis->threadHandle; *pThis->m_piThreadID = pThis->threadHandle;
/* Tell MakeThread that we've set m_piThreadID, so it's safe to return. */ // Tell MakeThread that we've set m_piThreadID, so it's safe to return.
pThis->m_StartFinishedSem->Post(); pThis->m_StartFinishedSem->Post();
int iRet = pThis->m_pFunc( pThis->m_pData ); int iRet = pThis->m_pFunc( pThis->m_pData );
@@ -86,7 +84,7 @@ ThreadImpl *MakeThread( int (*pFunc)(void *pData), void *pData, uint64_t *piThre
int ret = pthread_create( &thread->thread, NULL, StartThread, thread ); int ret = pthread_create( &thread->thread, NULL, StartThread, thread );
ASSERT_M( ret == 0, ssprintf( "MakeThread: pthread_create: %s", strerror(errno)) ); ASSERT_M( ret == 0, ssprintf( "MakeThread: pthread_create: %s", strerror(errno)) );
/* Don't return until StartThread sets m_piThreadID. */ // Don't return until StartThread sets m_piThreadID.
thread->m_StartFinishedSem->Wait(); thread->m_StartFinishedSem->Wait();
delete thread->m_StartFinishedSem; delete thread->m_StartFinishedSem;
@@ -105,12 +103,11 @@ MutexImpl_Pthreads::~MutexImpl_Pthreads()
ASSERT_M( ret == 0, ssprintf("Error deleting mutex: %s", strerror(errno)) ); ASSERT_M( ret == 0, ssprintf("Error deleting mutex: %s", strerror(errno)) );
} }
#if defined(HAVE_PTHREAD_MUTEX_TIMEDLOCK) || defined(HAVE_PTHREAD_COND_TIMEDWAIT) #if defined(HAVE_PTHREAD_MUTEX_TIMEDLOCK) || defined(HAVE_PTHREAD_COND_TIMEDWAIT)
static bool UseTimedlock() static bool UseTimedlock()
{ {
#if defined(LINUX) #if defined(LINUX)
/* Valgrind crashes and burns on pthread_mutex_timedlock. */ // Valgrind crashes and burns on pthread_mutex_timedlock.
if( RunningUnderValgrind() ) if( RunningUnderValgrind() )
return false; return false;
#endif #endif
@@ -124,7 +121,7 @@ bool MutexImpl_Pthreads::Lock()
#if defined(HAVE_PTHREAD_MUTEX_TIMEDLOCK) #if defined(HAVE_PTHREAD_MUTEX_TIMEDLOCK)
if( UseTimedlock() ) if( UseTimedlock() )
{ {
int len = 10; /* seconds */ int len = 10; // seconds
int tries = 2; int tries = 2;
while( tries-- ) while( tries-- )
@@ -149,9 +146,9 @@ bool MutexImpl_Pthreads::Lock()
continue; continue;
case ETIMEDOUT: case ETIMEDOUT:
/* Timed out. Probably deadlocked. Try again one more time, with a smaller /* Timed out. Probably deadlocked. Try again one more time,
* timeout, just in case we're debugging and happened to stop while waiting * with a smaller timeout, just in case we're debugging
* on the mutex. */ * and happened to stop while waiting on the mutex. */
len = 1; len = 1;
break; break;
@@ -205,8 +202,8 @@ MutexImpl *MakeMutex( RageMutex *pParent )
return new MutexImpl_Pthreads( pParent ); return new MutexImpl_Pthreads( pParent );
} }
/* Check if condattr_setclock is supported, and supports the clock that RageTimer /* Check if condattr_setclock is supported, and supports the clock that
* selected. */ * RageTimer selected. */
#if defined(UNIX) #if defined(UNIX)
#include <dlfcn.h> #include <dlfcn.h>
#include "arch/ArchHooks/ArchHooks_Unix.h" #include "arch/ArchHooks/ArchHooks_Unix.h"
@@ -247,7 +244,7 @@ namespace
break; break;
} }
/* Make sure that we can set up the clock attribute. */ // Make sure that we can set up the clock attribute.
pthread_condattr_t condattr; pthread_condattr_t condattr;
pthread_condattr_init( &condattr ); pthread_condattr_init( &condattr );
@@ -314,21 +311,21 @@ bool EventImpl_Pthreads::Wait( RageTimer *pTimeout )
return true; return true;
} }
/* If the clock is not CLOCK_MONOTONIC, or we can't change the wait clock (no /* If the clock is not CLOCK_MONOTONIC, or we can't change the wait clock
* condattr_setclock), pthread_cond_timedwait has an inherent race condition: * (no condattr_setclock), pthread_cond_timedwait has an inherent race
* the system clock may change before we call it. */ * condition: the system clock may change before we call it. */
timespec abstime; timespec abstime;
if( g_CondattrSetclock != NULL || GetClock() == CLOCK_REALTIME ) if( g_CondattrSetclock != NULL || GetClock() == CLOCK_REALTIME )
{ {
/* If we support condattr_setclock, we'll set the condition to use the same /* If we support condattr_setclock, we'll set the condition to use
* clock as RageTimer and can use it directly. If the clock is CLOCK_REALTIME, * the same clock as RageTimer and can use it directly. If the
* that's the default anyway. */ * clock is CLOCK_REALTIME, that's the default anyway. */
abstime.tv_sec = pTimeout->m_secs; abstime.tv_sec = pTimeout->m_secs;
abstime.tv_nsec = pTimeout->m_us * 1000; abstime.tv_nsec = pTimeout->m_us * 1000;
} }
else else
{ {
/* The RageTimer clock is different than the wait clock; convert it. */ // The RageTimer clock is different than the wait clock; convert it.
timeval tv; timeval tv;
gettimeofday( &tv, NULL ); gettimeofday( &tv, NULL );
@@ -427,7 +424,7 @@ bool SemaImpl_Pthreads::TryWait()
return true; return true;
} }
#else #else
/* Use conditions, to work around OS X "forgetting" to implement semaphores. */ // Use conditions, to work around OS X "forgetting" to implement semaphores.
SemaImpl_Pthreads::SemaImpl_Pthreads( int iInitialValue ) SemaImpl_Pthreads::SemaImpl_Pthreads( int iInitialValue )
{ {
int ret = pthread_cond_init( &m_Cond, NULL ); int ret = pthread_cond_init( &m_Cond, NULL );
@@ -480,9 +477,9 @@ bool SemaImpl_Pthreads::Wait()
break; break;
case ETIMEDOUT: case ETIMEDOUT:
/* Timed out. Probably deadlocked. Try again one more time, with a smaller /* Timed out. Probably deadlocked. Try again one more time,
* timeout, just in case we're debugging and happened to stop while waiting * with a smaller timeout, just in case we're debugging and
* on the mutex. */ * happened to stop while waiting on the mutex. */
++ts.tv_sec; ++ts.tv_sec;
tries--; tries--;
break; break;
+4 -5
View File
@@ -12,13 +12,12 @@ public:
pthread_t thread; pthread_t thread;
/* Linux: /* Linux:
* Keep a list of child PIDs, so we can send them SIGKILL. This has an * Keep a list of child PIDs, so we can send them SIGKILL. This has
* added bonus: if this is corrupted, we'll just send signals and they'll * an added bonus: if this is corrupted, we'll just send signals and
* fail; we won't blow up (unless we're root). * they'll fail; we won't blow up (unless we're root). */
*/
uint64_t threadHandle; uint64_t threadHandle;
/* These are only used during initialization. */ // These are only used during initialization.
int (*m_pFunc)( void *pData ); int (*m_pFunc)( void *pData );
void *m_pData; void *m_pData;
uint64_t *m_piThreadID; uint64_t *m_piThreadID;
+20 -18
View File
@@ -219,9 +219,9 @@ bool MutexImpl_Win32::Lock()
if( SimpleWaitForSingleObject( mutex, len ) ) if( SimpleWaitForSingleObject( mutex, len ) )
return true; return true;
/* Timed out; probably deadlocked. Try a couple more times, with a smaller /* Timed out; probably deadlocked. Try a couple more times, with
* timeout, just in case we're debugging and happened to stop while waiting * a smaller timeout, just in case we're debugging and happened
* on the mutex. */ * to stop while waiting on the mutex. */
len = 1000; len = 1000;
} }
@@ -238,8 +238,8 @@ void MutexImpl_Win32::Unlock()
{ {
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,
* called from ASSERT. */ * which is called from ASSERT. */
if( !ret ) if( !ret )
sm_crash( werr_ssprintf( GetLastError(), "ReleaseMutex failed" ) ); sm_crash( werr_ssprintf( GetLastError(), "ReleaseMutex failed" ) );
} }
@@ -278,10 +278,11 @@ EventImpl_Win32::~EventImpl_Win32()
CloseHandle( m_WaitersDone ); CloseHandle( m_WaitersDone );
} }
/* SignalObjectAndWait is atomic, which leads to more fair event handling. However, /* SignalObjectAndWait is atomic, which leads to more fair event handling.
* we don't guarantee or depend upon fair events, and SignalObjectAndWait is only * However, we don't guarantee or depend upon fair events, and
* available in NT. I also can't find a single function to signal an object like * SignalObjectAndWait is only available in NT. I also can't find a single
* SignalObjectAndWait, so we need to know if the object is a mutex or an event. */ * function to signal an object like SignalObjectAndWait, so we need to
* know if the object is a mutex or an event. */
static bool PortableSignalObjectAndWait( HANDLE hObjectToSignal, HANDLE hObjectToWaitOn, bool bFirstParamIsMutex, unsigned iMilliseconds = INFINITE ) static bool PortableSignalObjectAndWait( HANDLE hObjectToSignal, HANDLE hObjectToWaitOn, bool bFirstParamIsMutex, unsigned iMilliseconds = INFINITE )
{ {
static bool bSignalObjectAndWaitUnavailable = false; static bool bSignalObjectAndWaitUnavailable = false;
@@ -364,8 +365,9 @@ bool EventImpl_Win32::Wait( RageTimer *pTimeout )
EnterCriticalSection( &m_iNumWaitingLock ); EnterCriticalSection( &m_iNumWaitingLock );
if( !bSuccess ) if( !bSuccess )
{ {
/* Avoid a race condition: someone may have signalled the object between PortableSignalObjectAndWait /* Avoid a race condition: someone may have signalled the object
* and EnterCriticalSection. While we hold m_iNumWaitingLock, poll (with a zero timeout) the * between PortableSignalObjectAndWait and EnterCriticalSection.
* While we hold m_iNumWaitingLock, poll (with a zero timeout) the
* object one last time. */ * object one last time. */
if( WaitForSingleObject( m_WakeupSema, 0 ) == WAIT_OBJECT_0 ) if( WaitForSingleObject( m_WakeupSema, 0 ) == WAIT_OBJECT_0 )
bSuccess = true; bSuccess = true;
@@ -374,8 +376,8 @@ bool EventImpl_Win32::Wait( RageTimer *pTimeout )
bool bLastWaiting = m_iNumWaiting == 0; bool bLastWaiting = m_iNumWaiting == 0;
LeaveCriticalSection( &m_iNumWaitingLock ); LeaveCriticalSection( &m_iNumWaitingLock );
/* If we're the last waiter to wake up, and we were actually woken by another /* If we're the last waiter to wake up, and we were actually woken by
* thread (not by timeout), wake up the signaller. */ * another thread (not by timeout), wake up the signaller. */
if( bLastWaiting && bSuccess ) if( bLastWaiting && bSuccess )
PortableSignalObjectAndWait( m_WaitersDone, m_pParent->mutex, false ); PortableSignalObjectAndWait( m_WaitersDone, m_pParent->mutex, false );
else else
@@ -416,8 +418,8 @@ void EventImpl_Win32::Broadcast()
LeaveCriticalSection( &m_iNumWaitingLock ); LeaveCriticalSection( &m_iNumWaitingLock );
/* The last waiter will touch m_WaitersDone, so we wait for all waiters to /* The last waiter will touch m_WaitersDone, so we wait for all waiters
* wake up and start waiting for the mutex before returning. */ * to wake up and start waiting for the mutex before returning. */
WaitForSingleObject( m_WaitersDone, INFINITE ); WaitForSingleObject( m_WaitersDone, INFINITE );
} }
@@ -460,9 +462,9 @@ bool SemaImpl_Win32::Wait()
return true; return true;
} }
/* Timed out; probably deadlocked. Try again a few more times, with a smaller /* Timed out; probably deadlocked. Try again a few more times,
* timeout, just in case we're debugging and happened to stop while waiting * with a smaller timeout, just in case we're debugging and
* on the mutex. */ * happened to stop while waiting on the mutex. */
len = 1000; len = 1000;
} }