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
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_vHandlers.push_back( handler );
  17. };
  18. void basicEventHeap::removeHandler( basicEventHandler *handler ){
  19. for( V_EVT_HANDLER::iterator idx = m_vHandlers.begin(); idx< m_vHandlers.end();idx++) {
  20. if( (*idx) == handler ) {
  21. m_vHandlers.erase( idx );
  22. break;
  23. };
  24. };
  25. };
  26. void basicEventHeap::m_fThreadProc( void ){
  27. basicEventHandler *mpHandler = NULL;
  28. EVENT_CONTAINER *mpEvent = NULL;
  29. m_bThreadStatus = TH_WORKING;
  30. while( m_bThreadStatus < TH_ENDED ){
  31. if( m_bThreadStatus == TH_SLIPS ){
  32. SLEEP( 10 );
  33. };
  34. if( m_bThreadStatus == TH_WORKING ){
  35. mpEvent = NULL;
  36. m_pDispatchingMutex->lock();
  37. if (!m_vEventQueue.empty()){
  38. mpEvent = m_vEventQueue.front();
  39. m_vEventQueue.pop();
  40. };
  41. m_pDispatchingMutex->unlock();
  42. if( mpEvent!= NULL ){
  43. m_pHandlerMutex->lock();
  44. for( V_EVT_HANDLER::iterator idx = m_vHandlers.begin(); idx< m_vHandlers.end();idx++) {
  45. mpHandler = (*idx);
  46. if( mpHandler != NULL ) {
  47. mpHandler->dispatchEvent( mpEvent );
  48. };
  49. };
  50. m_pHandlerMutex->unlock();
  51. utilizeBasicEvent( mpEvent );
  52. } else SLEEP(1);
  53. };
  54. };
  55. };
  56. void basicEventHeap::broadcast( EVENT_CONTAINER *Evt ){
  57. m_pDispatchingMutex->lock();
  58. m_vEventQueue.push( Evt );
  59. m_pDispatchingMutex->unlock();
  60. };
  61. #undef LOG
  62. #undef LOG_ERROR