2012-06-14 09:06:06 -04:00
|
|
|
|
2012-09-23 17:23:33 -04:00
|
|
|
// Event.cpp
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
// Implements the cEvent object representing an OS-specific synchronization primitive that can be waited-for
|
|
|
|
// Implemented as an Event on Win and as a 1-semaphore on *nix
|
|
|
|
|
|
|
|
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
|
|
|
|
2012-09-23 17:23:33 -04:00
|
|
|
#include "Event.h"
|
2014-01-25 09:02:20 -05:00
|
|
|
#include "Errors.h"
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cEvent::cEvent(void)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2014-10-20 16:55:07 -04:00
|
|
|
m_Event = CreateEvent(nullptr, FALSE, FALSE, nullptr);
|
|
|
|
if (m_Event == nullptr)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2014-06-30 16:37:26 -04:00
|
|
|
LOGERROR("cEvent: cannot create event, GLE = %u. Aborting server.", (unsigned)GetLastError());
|
2012-06-14 09:06:06 -04:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
#else // *nix
|
|
|
|
m_bIsNamed = false;
|
|
|
|
m_Event = new sem_t;
|
|
|
|
if (sem_init(m_Event, 0, 0))
|
|
|
|
{
|
|
|
|
// This path is used by MacOS, because it doesn't support unnamed semaphores.
|
|
|
|
delete m_Event;
|
|
|
|
m_bIsNamed = true;
|
|
|
|
|
|
|
|
AString EventName;
|
|
|
|
Printf(EventName, "cEvent%p", this);
|
2014-07-21 09:19:48 -04:00
|
|
|
m_Event = sem_open(EventName.c_str(), O_CREAT, 777, 0);
|
2012-06-14 09:06:06 -04:00
|
|
|
if (m_Event == SEM_FAILED)
|
|
|
|
{
|
2014-01-25 09:02:20 -05:00
|
|
|
AString error = GetOSErrorString(errno);
|
|
|
|
LOGERROR("cEvent: Cannot create event, err = %s. Aborting server.", error.c_str());
|
2012-06-14 09:06:06 -04:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
// Unlink the semaphore immediately - it will continue to function but will not pollute the namespace
|
|
|
|
// We don't store the name, so can't call this in the destructor
|
|
|
|
if (sem_unlink(EventName.c_str()) != 0)
|
|
|
|
{
|
2014-01-25 09:02:20 -05:00
|
|
|
AString error = GetOSErrorString(errno);
|
|
|
|
LOGWARN("ERROR: Could not unlink cEvent. (%s)", error.c_str());
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // *nix
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cEvent::~cEvent()
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
CloseHandle(m_Event);
|
|
|
|
#else
|
|
|
|
if (m_bIsNamed)
|
|
|
|
{
|
|
|
|
if (sem_close(m_Event) != 0)
|
|
|
|
{
|
2014-01-25 09:02:20 -05:00
|
|
|
AString error = GetOSErrorString(errno);
|
|
|
|
LOGERROR("ERROR: Could not close cEvent. (%s)", error.c_str());
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sem_destroy(m_Event);
|
|
|
|
delete m_Event;
|
2014-10-20 16:55:07 -04:00
|
|
|
m_Event = nullptr;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cEvent::Wait(void)
|
|
|
|
{
|
2013-08-14 16:27:49 -04:00
|
|
|
#ifdef _WIN32
|
|
|
|
DWORD res = WaitForSingleObject(m_Event, INFINITE);
|
|
|
|
if (res != WAIT_OBJECT_0)
|
|
|
|
{
|
2014-06-30 16:37:26 -04:00
|
|
|
LOGWARN("cEvent: waiting for the event failed: %u, GLE = %u. Continuing, but server may be unstable.", (unsigned)res, (unsigned)GetLastError());
|
2013-08-14 16:27:49 -04:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
int res = sem_wait(m_Event);
|
2014-07-21 09:19:48 -04:00
|
|
|
if (res != 0)
|
2013-08-14 16:27:49 -04:00
|
|
|
{
|
2014-01-25 09:02:20 -05:00
|
|
|
AString error = GetOSErrorString(errno);
|
|
|
|
LOGWARN("cEvent: waiting for the event failed: %i, err = %s. Continuing, but server may be unstable.", res, error.c_str());
|
2013-08-14 16:27:49 -04:00
|
|
|
}
|
|
|
|
#endif
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-10-05 13:04:30 -04:00
|
|
|
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
|
|
|
|
// Get the current time:
|
|
|
|
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(errno).c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the specified timeout:
|
|
|
|
timeout.tv_sec += a_TimeoutMSec / 1000;
|
|
|
|
timeout.tv_nsec += (a_TimeoutMSec % 1000) * 1000000; // 1 msec = 1000000 usec
|
|
|
|
|
|
|
|
// Wait with timeout:
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
void cEvent::Set(void)
|
|
|
|
{
|
2013-08-14 16:27:49 -04:00
|
|
|
#ifdef _WIN32
|
|
|
|
if (!SetEvent(m_Event))
|
|
|
|
{
|
2014-06-30 16:37:26 -04:00
|
|
|
LOGWARN("cEvent: Could not set cEvent: GLE = %u", (unsigned)GetLastError());
|
2013-08-14 16:27:49 -04:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
int res = sem_post(m_Event);
|
|
|
|
if (res != 0)
|
|
|
|
{
|
2014-01-25 09:02:20 -05:00
|
|
|
AString error = GetOSErrorString(errno);
|
|
|
|
LOGWARN("cEvent: Could not set cEvent: %i, err = %s", res, error.c_str());
|
2013-08-14 16:27:49 -04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|