implement RageEvent::Wait timeouts
This commit is contained in:
@@ -666,14 +666,18 @@ RageEvent::~RageEvent()
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* For each of these calls, the mutex must be locked, and must not be locked recursively. */
|
/* For each of these calls, the mutex must be locked, and must not be locked recursively. */
|
||||||
void RageEvent::Wait()
|
bool RageEvent::Wait( RageTimer *pTimeout )
|
||||||
{
|
{
|
||||||
ASSERT( IsLockedByThisThread() );
|
ASSERT( IsLockedByThisThread() );
|
||||||
ASSERT( m_LockCnt == 0 );
|
ASSERT( m_LockCnt == 0 );
|
||||||
|
|
||||||
m_pEvent->Wait();
|
/* A zero RageTimer also means no timeout. */
|
||||||
|
if( pTimeout != NULL && pTimeout->IsZero() )
|
||||||
|
pTimeout = NULL;
|
||||||
|
bool bRet = m_pEvent->Wait( pTimeout );
|
||||||
|
|
||||||
m_LockedBy = GetThisThreadId();
|
m_LockedBy = GetThisThreadId();
|
||||||
|
return bRet;
|
||||||
}
|
}
|
||||||
|
|
||||||
void RageEvent::Signal()
|
void RageEvent::Signal()
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
#define RAGE_THREADS_H
|
#define RAGE_THREADS_H
|
||||||
|
|
||||||
struct ThreadSlot;
|
struct ThreadSlot;
|
||||||
|
class RageTimer;
|
||||||
class RageThread
|
class RageThread
|
||||||
{
|
{
|
||||||
ThreadSlot *m_pSlot;
|
ThreadSlot *m_pSlot;
|
||||||
@@ -137,7 +138,13 @@ public:
|
|||||||
RageEvent( CString name );
|
RageEvent( CString name );
|
||||||
~RageEvent();
|
~RageEvent();
|
||||||
|
|
||||||
void Wait();
|
/*
|
||||||
|
* If pTimeout is non-NULL, the event will be automatically signalled at the given
|
||||||
|
* time. Note that implementing this timeout is optional; not all archs support it.
|
||||||
|
* If false is returned, the wait timed out (and the mutex is locked, as if the
|
||||||
|
* event had been signalled).
|
||||||
|
*/
|
||||||
|
bool Wait( RageTimer *pTimeout = NULL );
|
||||||
void Signal();
|
void Signal();
|
||||||
void Broadcast();
|
void Broadcast();
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
/* This is the low-level implementation; you probably want RageThreads. */
|
/* This is the low-level implementation; you probably want RageThreads. */
|
||||||
class RageMutex;
|
class RageMutex;
|
||||||
|
class RageTimer;
|
||||||
|
|
||||||
class ThreadImpl
|
class ThreadImpl
|
||||||
{
|
{
|
||||||
@@ -46,7 +46,7 @@ class EventImpl
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
virtual ~EventImpl() { }
|
virtual ~EventImpl() { }
|
||||||
virtual void Wait() = 0;
|
virtual bool Wait( RageTimer *pTimeout ) = 0;
|
||||||
virtual void Signal() = 0;
|
virtual void Signal() = 0;
|
||||||
virtual void Broadcast() = 0;
|
virtual void Broadcast() = 0;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -215,9 +215,19 @@ EventImpl_Pthreads::~EventImpl_Pthreads()
|
|||||||
pthread_cond_destroy( &m_Cond );
|
pthread_cond_destroy( &m_Cond );
|
||||||
}
|
}
|
||||||
|
|
||||||
void EventImpl_Pthreads::Wait()
|
bool EventImpl_Pthreads::Wait( RageTimer *pTimeout )
|
||||||
{
|
{
|
||||||
pthread_cond_wait( &m_Cond, &m_pParent->mutex );
|
if( pTimeout == NULL )
|
||||||
|
{
|
||||||
|
pthread_cond_wait( &m_Cond, &m_pParent->mutex );
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
timespec abstime;
|
||||||
|
timeout.tv_sec = pTimeout->m_secs;
|
||||||
|
timeout.tv_nsec = pTimeout->m_us * 1000;
|
||||||
|
int iRet = pthread_cond_timedwait( &m_Cond, &m_pParent->mutex );
|
||||||
|
return iRet != ETIMEDOUT;
|
||||||
}
|
}
|
||||||
|
|
||||||
void EventImpl_Pthreads::Signal()
|
void EventImpl_Pthreads::Signal()
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ public:
|
|||||||
EventImpl_Pthreads( MutexImpl_Pthreads *pParent );
|
EventImpl_Pthreads( MutexImpl_Pthreads *pParent );
|
||||||
~EventImpl_Pthreads();
|
~EventImpl_Pthreads();
|
||||||
|
|
||||||
void Wait();
|
bool Wait( RageTimer *pTimeout );
|
||||||
void Signal();
|
void Signal();
|
||||||
void Broadcast();
|
void Broadcast();
|
||||||
|
|
||||||
|
|||||||
@@ -321,8 +321,9 @@ static void PortableSignalObjectAndWait( HANDLE hObjectToSignal, HANDLE hObjectT
|
|||||||
WaitForSingleObject( hObjectToWaitOn, INFINITE );
|
WaitForSingleObject( hObjectToWaitOn, INFINITE );
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Event logic from http://www.cs.wustl.edu/~schmidt/win32-cv-1.html. */
|
/* Event logic from http://www.cs.wustl.edu/~schmidt/win32-cv-1.html.
|
||||||
void EventImpl_Win32::Wait()
|
* pTimeout is not currently implemented. */
|
||||||
|
bool EventImpl_Win32::Wait( RageTimer *pTimeout )
|
||||||
{
|
{
|
||||||
EnterCriticalSection( &m_iNumWaitingLock );
|
EnterCriticalSection( &m_iNumWaitingLock );
|
||||||
++m_iNumWaiting;
|
++m_iNumWaiting;
|
||||||
@@ -342,6 +343,8 @@ void EventImpl_Win32::Wait()
|
|||||||
PortableSignalObjectAndWait( m_WaitersDone, m_pParent->mutex, false );
|
PortableSignalObjectAndWait( m_WaitersDone, m_pParent->mutex, false );
|
||||||
else
|
else
|
||||||
WaitForSingleObject( m_pParent->mutex, INFINITE );
|
WaitForSingleObject( m_pParent->mutex, INFINITE );
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void EventImpl_Win32::Signal()
|
void EventImpl_Win32::Signal()
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ public:
|
|||||||
EventImpl_Win32( MutexImpl_Win32 *pParent );
|
EventImpl_Win32( MutexImpl_Win32 *pParent );
|
||||||
~EventImpl_Win32();
|
~EventImpl_Win32();
|
||||||
|
|
||||||
void Wait();
|
bool Wait( RageTimer *pTimeout );
|
||||||
void Signal();
|
void Signal();
|
||||||
void Broadcast();
|
void Broadcast();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user