from BhaaL: linux compile fix for r4794, Event::Wait now supports a timeout (default to INFINITE), and it returns true when the timeout expired. fixes issue 1974

git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@4799 8ced0084-cf51-0410-be5f-012b33b47a6e
This commit is contained in:
Shawn Hoffman
2010-01-09 19:06:23 +00:00
parent f472e2904d
commit 1a5817f6fd
5 changed files with 64 additions and 33 deletions

View File

@ -139,9 +139,9 @@ void Event::Set()
SetEvent(m_hEvent);
}
void Event::Wait()
bool Event::Wait(const u32 timeout)
{
WaitForSingleObject(m_hEvent, INFINITE);
return WaitForSingleObject(m_hEvent, timeout) != WAIT_OBJECT_0;
}
inline HRESULT MsgWaitForSingleObject(HANDLE handle, DWORD timeout)
@ -400,17 +400,42 @@ void Event::Set()
}
void Event::Wait()
bool Event::Wait(const u32 timeout)
{
bool timedout = false;
struct timespec wait;
pthread_mutex_lock(&mutex_);
while (!is_set_)
if (timeout != INFINITE)
{
pthread_cond_wait(&event_, &mutex_);
struct timeval now;
gettimeofday(&now, NULL);
memset(&wait, 0, sizeof(wait));
//TODO: timespec also has nanoseconds, but do we need them?
//as consequence, waiting is limited to seconds for now.
//the following just looks ridiculous, and probably fails for
//values 429 < ms <= 999 since it overflows the long.
//wait.tv_nsec = (now.tv_usec + (timeout % 1000) * 1000) * 1000);
wait.tv_sec = now.tv_sec + (timeout / 1000);
}
while (!is_set_ && !timedout)
{
if (timeout == INFINITE)
{
pthread_cond_wait(&event_, &mutex_);
}
else
{
timedout = pthread_cond_timedwait(&event_, &mutex_, &wait) == ETIMEDOUT;
}
}
is_set_ = false;
pthread_mutex_unlock(&mutex_);
return timedout;
}
#endif