2011-10-03 14:41:19 -04:00
|
|
|
|
2012-01-31 05:45:53 -05:00
|
|
|
// cEvent.cpp
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
2012-01-29 14:28:19 -05:00
|
|
|
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
|
|
|
|
2012-01-31 05:45:53 -05:00
|
|
|
#include "cEvent.h"
|
2012-01-29 14:28:19 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
|
|
|
|
|
2012-01-31 05:45:53 -05:00
|
|
|
cEvent::cEvent(void)
|
|
|
|
{
|
2011-10-03 14:41:19 -04:00
|
|
|
#ifdef _WIN32
|
2012-01-31 05:45:53 -05:00
|
|
|
m_Event = CreateEvent( 0, FALSE, FALSE, 0 );
|
|
|
|
if (m_Event == NULL)
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2012-01-31 05:45:53 -05:00
|
|
|
LOGERROR("cEvent: cannot create event, GLE = %d. Aborting server.", GetLastError());
|
|
|
|
abort();
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
2012-01-31 05:45:53 -05:00
|
|
|
#else // *nix
|
|
|
|
m_bIsNamed = false;
|
2012-01-31 07:09:34 -05:00
|
|
|
m_Event = new sem_t;
|
2012-01-31 05:45:53 -05:00
|
|
|
if (sem_init(m_Event, 0, 0))
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2012-03-12 09:31:34 -04:00
|
|
|
// This path is used by MacOS, because it doesn't support unnamed semaphores.
|
2012-01-31 07:09:34 -05:00
|
|
|
delete m_Event;
|
2012-01-31 05:45:53 -05:00
|
|
|
m_bIsNamed = true;
|
2011-10-03 14:41:19 -04:00
|
|
|
|
2012-02-01 08:43:47 -05:00
|
|
|
AString EventName;
|
|
|
|
Printf(EventName, "cEvent%p", this);
|
|
|
|
m_Event = sem_open(EventName.c_str(), O_CREAT, 777, 0 );
|
2012-01-31 05:45:53 -05:00
|
|
|
if (m_Event == SEM_FAILED)
|
|
|
|
{
|
|
|
|
LOGERROR("cEvent: Cannot create event, errno = %i. Aborting server.", errno);
|
|
|
|
abort();
|
|
|
|
}
|
2012-03-12 15:42:50 -04:00
|
|
|
// 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)
|
|
|
|
{
|
|
|
|
LOGWARN("ERROR: Could not unlink cEvent. (%i)", errno);
|
|
|
|
}
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
2012-01-31 05:45:53 -05:00
|
|
|
#endif // *nix
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
|
2012-01-31 05:45:53 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
cEvent::~cEvent()
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2012-01-31 05:45:53 -05:00
|
|
|
CloseHandle(m_Event);
|
2011-10-03 14:41:19 -04:00
|
|
|
#else
|
2012-01-31 05:45:53 -05:00
|
|
|
if (m_bIsNamed)
|
|
|
|
{
|
|
|
|
if (sem_close(m_Event) != 0)
|
|
|
|
{
|
|
|
|
LOGERROR("ERROR: Could not close cEvent. (%i)", errno);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sem_destroy(m_Event);
|
2012-01-31 07:09:34 -05:00
|
|
|
delete m_Event;
|
2012-01-31 05:45:53 -05:00
|
|
|
}
|
2011-10-03 14:41:19 -04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-01-31 05:45:53 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cEvent::Wait(void)
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2012-01-31 05:45:53 -05:00
|
|
|
DWORD res = WaitForSingleObject(m_Event, INFINITE);
|
|
|
|
if (res != WAIT_OBJECT_0)
|
|
|
|
{
|
|
|
|
LOGWARN("cEvent: waiting for the event failed: %d, GLE = %d. Continuing, but server may be unstable.", res, GetLastError());
|
|
|
|
}
|
2011-10-03 14:41:19 -04:00
|
|
|
#else
|
2012-01-31 05:45:53 -05:00
|
|
|
int res = sem_wait(m_Event);
|
|
|
|
if (res != 0 )
|
|
|
|
{
|
|
|
|
LOGWARN("cEvent: waiting for the event failed: %i, errno = %i. Continuing, but server may be unstable.", res, errno);
|
|
|
|
}
|
2011-10-03 14:41:19 -04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-01-31 05:45:53 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cEvent::Set(void)
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2012-02-01 04:34:15 -05:00
|
|
|
if (!SetEvent(m_Event))
|
2012-01-31 05:45:53 -05:00
|
|
|
{
|
|
|
|
LOGWARN("cEvent: Could not set cEvent: GLE = %d", GetLastError());
|
|
|
|
}
|
2011-10-03 14:41:19 -04:00
|
|
|
#else
|
2012-01-31 05:45:53 -05:00
|
|
|
int res = sem_post(m_Event);
|
|
|
|
if (res != 0)
|
|
|
|
{
|
|
|
|
LOGWARN("cEvent: Could not set cEvent: %i, errno = %d", res, errno);
|
|
|
|
}
|
2011-10-03 14:41:19 -04:00
|
|
|
#endif
|
|
|
|
}
|
2012-01-31 05:45:53 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|