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.

81 lines
2.1 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
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, bool atBottom ){
  18. if (m_pRing != NULL) {
  19. if (atBottom) {
  20. m_pRing->push(handler);
  21. } else {
  22. m_pRing->add(handler);
  23. };
  24. };
  25. };
  26. void basicEventHeap::removeHandler( basicEventHandler *handler ){
  27. if( m_pRing != NULL && handler != NULL ) m_pRing->removePtr( handler );
  28. };
  29. void basicEventHeap::m_fThreadProc( void ){
  30. basicEventHandler *mpHandler;
  31. basicEvent *mpEvent;
  32. EventHandlerIterator *mpIterator;
  33. //pthread_t pid = pthread_self();
  34. //char pidBuf[16];
  35. //sprintf(pidBuf,"pid:%d", pid);
  36. //LOG(pidBuf);
  37. m_bThreadStatus = TH_WORKING;
  38. while( m_bThreadStatus < TH_ENDED ){
  39. if( m_bThreadStatus == TH_SLIPS ){
  40. SLEEP( 10 );
  41. };
  42. if( m_bThreadStatus == TH_WORKING ){
  43. mpEvent = NULL;
  44. m_pDispatchingMutex->lock();
  45. if (!m_vEventQueue.empty()){
  46. mpEvent = m_vEventQueue.front();
  47. m_vEventQueue.pop();
  48. };
  49. m_pDispatchingMutex->unlock();
  50. if( mpEvent!= NULL ){
  51. m_pHandlerMutex->lock();
  52. mpIterator = m_pRing->begin();
  53. while( mpIterator != NULL ){
  54. mpHandler = m_pRing->getAtCursor( mpIterator );
  55. if( mpHandler != NULL ) {
  56. mpHandler->dispatchEvent( mpEvent );
  57. };
  58. mpIterator = mpIterator->next;
  59. };
  60. m_pHandlerMutex->unlock();
  61. utilizeBasicEvent( mpEvent );
  62. } else SLEEP(3);
  63. };
  64. };
  65. };
  66. void basicEventHeap::broadcast( basicEvent *Evt ){
  67. m_pDispatchingMutex->lock();
  68. m_vEventQueue.push( Evt );
  69. m_pDispatchingMutex->unlock();
  70. };
  71. #undef LOG
  72. #undef LOG_ERROR