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.

73 lines
2.0 KiB

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