You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
1.9 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "basicEventHeap.h"
  2. #define LOG( X ) m_pLog->Log( "basicEventHeap", this, X )
  3. #define LOG_ERROR( X ) m_pLog->Log( "basicEventHeap", this, _LOG_ERROR, X )
  4. basicEventHeap* basicEventHeap::_instance = 0;
  5. /**
  6. * Returns the single instance of the object.
  7. */
  8. basicEventHeap* basicEventHeap::getInstance() {
  9. // check if the instance has been created yet
  10. if (0 == _instance) {
  11. // if not, then create it
  12. _instance = new basicEventHeap;
  13. }
  14. // return the single instance
  15. return _instance;
  16. };
  17. void basicEventHeap::addHandler( basicEventHandler *handler ){
  18. if( m_pRing != NULL ) m_pRing->add( handler );
  19. };
  20. void basicEventHeap::removeHandler( basicEventHandler *handler ){
  21. if( m_pRing != NULL && handler != NULL ) m_pRing->removePtr( handler );
  22. };
  23. void basicEventHeap::m_fThreadProc( void ){
  24. basicEventHandler *mpHandler;
  25. basicEvent *mpEvent;
  26. EventHandlerIterator *mpIterator;
  27. m_bThreadStatus = TH_WORKING;
  28. while( m_bThreadStatus < TH_ENDED ){
  29. if( m_bThreadStatus == TH_SLIPS ){
  30. SLEEP( 10 );
  31. };
  32. if( m_bThreadStatus == TH_WORKING ){
  33. mpEvent = NULL;
  34. m_pDispatchingMutex->lock();
  35. if (!m_vEventQueue.empty()){
  36. mpEvent = m_vEventQueue.front();
  37. m_vEventQueue.pop();
  38. };
  39. m_pDispatchingMutex->unlock();
  40. if( mpEvent!= NULL ){
  41. m_pHandlerMutex->lock();
  42. mpIterator = m_pRing->begin();
  43. while( mpIterator != NULL ){
  44. mpHandler = m_pRing->getAtCursor( mpIterator );
  45. if( mpHandler != NULL ) {
  46. mpHandler->dispatchEvent( mpEvent );
  47. };
  48. mpIterator = mpIterator->next;
  49. };
  50. m_pHandlerMutex->unlock();
  51. utilizeBasicEvent( mpEvent );
  52. } else SLEEP(3);
  53. };
  54. };
  55. };
  56. void basicEventHeap::broadcast( basicEvent *Evt ){
  57. m_pDispatchingMutex->lock();
  58. m_vEventQueue.push( Evt );
  59. m_pDispatchingMutex->unlock();
  60. };
  61. #undef LOG
  62. #undef LOG_ERROR