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.

79 lines
2.2 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. // check if the instance has been created yet
  11. if (0 == _instance) {
  12. // if not, then create it
  13. _instance = new basicEventHeap;
  14. }
  15. // return the single instance
  16. return _instance;
  17. };
  18. void basicEventHeap::addHandler( basicEventHandler *handler, bool atBottom ){
  19. m_vHandlers.push_back( handler );
  20. };
  21. void basicEventHeap::removeHandler( basicEventHandler *handler ){
  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. };
  29. void basicEventHeap::m_fThreadProc( void ){
  30. basicEventHandler *mpHandler;
  31. EVENT_CONTAINER *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. for( V_EVT_HANDLER::iterator idx = m_vHandlers.begin(); idx< m_vHandlers.end();idx++) {
  53. mpHandler = (*idx);
  54. if( mpHandler != NULL ) {
  55. mpHandler->dispatchEvent( mpEvent );
  56. };
  57. };
  58. m_pHandlerMutex->unlock();
  59. utilizeBasicEvent( mpEvent );
  60. } else SLEEP(1);
  61. };
  62. };
  63. };
  64. void basicEventHeap::broadcast( EVENT_CONTAINER *Evt ){
  65. m_pDispatchingMutex->lock();
  66. m_vEventQueue.push( Evt );
  67. m_pDispatchingMutex->unlock();
  68. };
  69. #undef LOG
  70. #undef LOG_ERROR