Permalink
Browse files
Added a cEvent::Wait() with timeout.
- Loading branch information...
Showing
with
45 additions
and
0 deletions.
-
+41
−0
src/OSSupport/Event.cpp
-
+4
−0
src/OSSupport/Event.h
|
|
@@ -102,6 +102,47 @@ void cEvent::Wait(void) |
|
|
|
|
|
|
|
|
|
|
|
bool cEvent::Wait(int a_TimeoutMSec)
|
|
|
{
|
|
|
#ifdef _WIN32
|
|
|
DWORD res = WaitForSingleObject(m_Event, (DWORD)a_TimeoutMSec);
|
|
|
switch (res)
|
|
|
case WAIT_OBJECT_0: return true; // Regular event signalled
|
|
|
case WAIT_TIMEOUT: return false; // Regular event timeout
|
|
|
default:
|
|
|
{
|
|
|
LOGWARN("cEvent: waiting for the event failed: %u, GLE = %u. Continuing, but server may be unstable.", (unsigned)res, (unsigned)GetLastError());
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
#else
|
|
|
timespec timeout;
|
|
|
if (clock_gettime(CLOCK_REALTIME, &timeout) == -1)
|
|
|
{
|
|
|
LOGWARN("cEvent: Getting current time failed: %i, err = %s. Continuing, but the server may be unstable.", errno, GetOSErrorString().c_str());
|
|
|
return false;
|
|
|
}
|
|
|
timeout.tv_sec += a_TimeoutMSec / 1000;
|
|
|
timeout.tv_nsecs += (a_TimeoutMsec % 1000) * 1000000; // 1 msec = 1000000 usec
|
|
|
int res = sem_timedwait(m_Event, &timeout);
|
|
|
switch (res)
|
|
|
{
|
|
|
case 0: return true; // Regular event signalled
|
|
|
case ETIMEDOUT: return false; // Regular even timeout
|
|
|
default:
|
|
|
{
|
|
|
AString error = GetOSErrorString(errno);
|
|
|
LOGWARN("cEvent: waiting for the event failed: %i, err = %s. Continuing, but server may be unstable.", res, error.c_str());
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
#endif
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cEvent::Set(void)
|
|
|
{
|
|
|
#ifdef _WIN32
|
|
|
|
|
|
@@ -24,6 +24,10 @@ class cEvent |
|
|
|
|
|
void Wait(void);
|
|
|
void Set (void);
|
|
|
|
|
|
/** Waits for the event until either it is signalled, or the (relative) timeout is passed.
|
|
|
Returns true if the event was signalled, false if the timeout was hit or there was an error. */
|
|
|
bool Wait(int a_TimeoutMSec);
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
0 comments on commit
5403032