1
0

cEvent class cleanup (single-event only, since multi-event wasn't used and wouldn't work on *nix anyway)

git-svn-id: http://mc-server.googlecode.com/svn/trunk@199 0a769ca7-a7f5-676a-18bf-c427514a06d6
This commit is contained in:
madmaxoft@gmail.com 2012-01-31 10:45:53 +00:00
parent 16f92b2901
commit 8baa234541
2 changed files with 124 additions and 82 deletions

View File

@ -1,107 +1,120 @@
// 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
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules #include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
#include "cEvent.h"
cEvent::cEvent( unsigned int a_NumEvents /* = 1 */ )
: m_NumEvents( a_NumEvents ) cEvent::cEvent(void)
#ifndef _WIN32
, m_bNamed( false )
#endif
{ {
if( m_NumEvents < 1 ) m_NumEvents = 1;
#ifdef _WIN32 #ifdef _WIN32
m_Handle = new HANDLE[ m_NumEvents ]; m_Event = CreateEvent( 0, FALSE, FALSE, 0 );
for( unsigned int i = 0; i < m_NumEvents; i++) if (m_Event == NULL)
{ {
((HANDLE*)m_Handle)[i] = CreateEvent( 0, FALSE, FALSE, 0 ); LOGERROR("cEvent: cannot create event, GLE = %d. Aborting server.", GetLastError());
abort();
} }
#else #else // *nix
m_Handle = new sem_t*[ m_NumEvents ]; m_bIsNamed = false;
for( unsigned int i = 0; i < m_NumEvents; i++) if (sem_init(m_Event, 0, 0))
{ {
LOGWARN("WARNING cEvent: Could not create unnamed semaphore, fallback to named.");
sem_t* & HandlePtr = ((sem_t**)m_Handle)[i]; // _X: I'm unconvinced about using sem_unlink() just after a successful sem_open(), it seems wrong - why destroy the object just after creating?
HandlePtr = new sem_t; assert(!"This codepath is really weird, if it is ever used, please check that everything works.");
if( sem_init( HandlePtr, 0, 0 ) ) m_bIsNamed = true;
{
LOG("WARNING cEvent: Could not create unnamed semaphore, fallback to named.");
m_bNamed = true;
delete HandlePtr; // named semaphores return their own address
char c_Str[32]; char c_Str[64];
sprintf( c_Str, "cEvent%p", &HandlePtr ); sprintf(c_Str, "cEvent%p", this);
HandlePtr = sem_open( c_Str, O_CREAT, 777, 0 ); m_Event = sem_open( c_Str, O_CREAT, 777, 0 );
if( HandlePtr == SEM_FAILED ) if (m_Event == SEM_FAILED)
LOG("ERROR: Could not create Event. (%i)", errno); {
else LOGERROR("cEvent: Cannot create event, errno = %i. Aborting server.", errno);
if( sem_unlink( c_Str ) != 0 ) abort();
LOG("ERROR: Could not unlink cEvent. (%i)", errno); }
} else
{
if( sem_unlink( c_Str ) != 0 )
{
LOGWARN("ERROR: Could not unlink cEvent. (%i)", errno);
}
}
} }
#endif #endif // *nix
} }
cEvent::~cEvent() cEvent::~cEvent()
{ {
#ifdef _WIN32 #ifdef _WIN32
for( unsigned int i = 0; i < m_NumEvents; i++ ) CloseHandle(m_Event);
{
CloseHandle( ((HANDLE*)m_Handle)[i] );
}
delete [] (HANDLE*)m_Handle;
#else #else
for( unsigned int i = 0; i < m_NumEvents; i++ ) if (m_bIsNamed)
{ {
if( m_bNamed ) if (sem_close(m_Event) != 0)
{ {
sem_t* & HandlePtr = ((sem_t**)m_Handle)[i]; LOGERROR("ERROR: Could not close cEvent. (%i)", errno);
char c_Str[32]; }
sprintf( c_Str, "cEvent%p", &HandlePtr ); }
// LOG("Closing event: %s", c_Str ); else
// LOG("Sem ptr: %p", HandlePtr ); {
if( sem_close( HandlePtr ) != 0 ) sem_destroy(m_Event);
{ }
LOG("ERROR: Could not close cEvent. (%i)", errno);
}
}
else
{
sem_destroy( ((sem_t**)m_Handle)[i] );
delete ((sem_t**)m_Handle)[i];
}
}
delete [] (sem_t**)m_Handle; m_Handle = 0;
#endif #endif
} }
void cEvent::Wait()
void cEvent::Wait(void)
{ {
#ifdef _WIN32 #ifdef _WIN32
WaitForMultipleObjects( m_NumEvents, (HANDLE*)m_Handle, true, INFINITE ); 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());
}
#else #else
for(unsigned int i = 0; i < m_NumEvents; i++) int res = sem_wait(m_Event);
{ if (res != 0 )
if( sem_wait( ((sem_t**)m_Handle)[i] ) != 0 ) {
{ LOGWARN("cEvent: waiting for the event failed: %i, errno = %i. Continuing, but server may be unstable.", res, errno);
LOG("ERROR: Could not wait for cEvent. (%i)", errno); }
}
}
#endif #endif
} }
void cEvent::Set(unsigned int a_EventNum /* = 0 */)
void cEvent::Set(void)
{ {
#ifdef _WIN32 #ifdef _WIN32
SetEvent( ((HANDLE*)m_Handle)[a_EventNum] ); if (SetEvent(m_Event))
{
LOGWARN("cEvent: Could not set cEvent: GLE = %d", GetLastError());
}
#else #else
if( sem_post( ((sem_t**)m_Handle)[a_EventNum] ) != 0 ) int res = sem_post(m_Event);
{ if (res != 0)
LOG("ERROR: Could not set cEvent. (%i)", errno); {
} LOGWARN("cEvent: Could not set cEvent: %i, errno = %d", res, errno);
}
#endif #endif
} }

View File

@ -1,18 +1,47 @@
// cEvent.h
// Interfaces to 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
#pragma once #pragma once
#ifndef CEVENT_H_INCLUDED
#define CEVENT_H_INCLUDED
class cEvent class cEvent
{ {
public: public:
cEvent( unsigned int a_NumEvents = 1 ); cEvent(void);
~cEvent(); ~cEvent();
void Wait(); void Wait(void);
void Set(unsigned int a_EventNum = 0); void Set (void);
private:
unsigned int m_NumEvents; private:
void* m_Handle; // HANDLE[] pointer
#ifdef _WIN32
HANDLE m_Event;
#else
sem_t * m_Event;
bool m_bIsNamed;
#endif
} ;
#endif // CEVENT_H_INCLUDED
#ifndef _WIN32
bool m_bNamed;
#endif
};