#include "basicEventHeap.h" #define LOG( X ) m_pLog->Log( "basicEventHeap", this, X ) #define LOG_ERROR( X ) m_pLog->Log( "basicEventHeap", this, _LOG_ERROR, X ) basicEventHeap* basicEventHeap::_instance = 0; /** * Returns the single instance of the object. */ basicEventHeap* basicEventHeap::getInstance() { // check if the instance has been created yet if (0 == _instance) { // if not, then create it _instance = new basicEventHeap; } // return the single instance return _instance; }; void basicEventHeap::addHandler( basicEventHandler *handler, bool atBottom ){ if (m_pRing != NULL) { if (atBottom) { m_pRing->push(handler); } else { m_pRing->add(handler); }; }; }; void basicEventHeap::removeHandler( basicEventHandler *handler ){ if( m_pRing != NULL && handler != NULL ) m_pRing->removePtr( handler ); }; void basicEventHeap::m_fThreadProc( void ){ basicEventHandler *mpHandler; basicEvent *mpEvent; EventHandlerIterator *mpIterator; //pthread_t pid = pthread_self(); //char pidBuf[16]; //sprintf(pidBuf,"pid:%d", pid); //LOG(pidBuf); m_bThreadStatus = TH_WORKING; while( m_bThreadStatus < TH_ENDED ){ if( m_bThreadStatus == TH_SLIPS ){ SLEEP( 10 ); }; if( m_bThreadStatus == TH_WORKING ){ mpEvent = NULL; m_pDispatchingMutex->lock(); if (!m_vEventQueue.empty()){ mpEvent = m_vEventQueue.front(); m_vEventQueue.pop(); }; m_pDispatchingMutex->unlock(); if( mpEvent!= NULL ){ m_pHandlerMutex->lock(); mpIterator = m_pRing->begin(); while( mpIterator != NULL ){ mpHandler = m_pRing->getAtCursor( mpIterator ); if( mpHandler != NULL ) { mpHandler->dispatchEvent( mpEvent ); }; mpIterator = mpIterator->next; }; m_pHandlerMutex->unlock(); utilizeBasicEvent( mpEvent ); } else SLEEP(3); }; }; }; void basicEventHeap::broadcast( basicEvent *Evt ){ m_pDispatchingMutex->lock(); m_vEventQueue.push( Evt ); m_pDispatchingMutex->unlock(); }; #undef LOG #undef LOG_ERROR