MesgDispatcher.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #include "MesgDispatcher.h"
  2. #include <limits>
  3. NAMESPACE_MAS_BEGIN
  4. MesgThread::MesgThread(TZ_INT tid) : m_tid(tid)
  5. {
  6. this->Start();
  7. }
  8. MesgThread::~MesgThread()
  9. {
  10. this->StopAndWait();
  11. m_eventsLock.Lock();
  12. m_pendingMesgs.clear();
  13. m_eventsLock.Unlock();
  14. m_insMapLock.Lock();
  15. m_insMap.clear();
  16. m_insMapLock.Unlock();
  17. }
  18. void MesgThread::RegisterIListerner(
  19. IListerner * listener)
  20. {
  21. tzc::ScopedLock lock(m_insMapLock);
  22. auto iter = m_insMap.find(listener);
  23. if (m_insMap.end() == iter)
  24. {
  25. m_insMap.emplace(listener, listener->GetRegMesg());
  26. }
  27. else
  28. {
  29. iter->second = listener->GetRegMesg();
  30. }
  31. }
  32. TZ_INT MesgThread::PushMesg(SPtr<MasMesg> & mesg)
  33. {
  34. m_eventsLock.Lock();
  35. m_pendingMesgs.emplace_back(mesg);
  36. m_eventsLock.Unlock();
  37. m_eventSema.Signal();
  38. return MEC_OK;
  39. }
  40. SPtr<MasMesg> MesgThread::getFrontEvent()
  41. {
  42. m_eventsLock.Lock();
  43. auto info = m_pendingMesgs.front();
  44. m_pendingMesgs.pop_front();
  45. m_eventsLock.Unlock();
  46. return info;
  47. }
  48. void MesgThread::dispatcherEvent(SPtr<MasMesg> & info)
  49. {
  50. std::list<IListerner *> eventList;
  51. m_insMapLock.Lock();
  52. for (auto mem : m_insMap)
  53. {
  54. if ((mem.second & info->MesgType) != info->MesgType)
  55. {
  56. continue;
  57. }
  58. eventList.emplace_back(mem.first);
  59. }
  60. m_insMapLock.Unlock();
  61. for (IListerner * listerner : eventList)
  62. {
  63. listerner->HandleMesg(info);
  64. }
  65. }
  66. void MesgThread::Entry()
  67. {
  68. SPtr<MasMesg> info = nullptr;
  69. while (!this->IsStop())
  70. {
  71. if (!m_eventSema.Wait(EVENT_WAIT_MSECOND)) continue;
  72. info = this->getFrontEvent();
  73. if (!info) continue;
  74. this->dispatcherEvent(info);
  75. }
  76. }
  77. MesgDispatcher * MesgDispatcher::_ins = nullptr;
  78. tzc::Mutex MesgDispatcher::_insLock;
  79. MesgDispatcher::MesgDispatcher()
  80. {
  81. for (TZ_INT i; i < 4; ++i)
  82. {
  83. auto t = new MesgThread(i + 1);
  84. m_mesgThreads.emplace(t->GetTid(), t);
  85. }
  86. }
  87. MesgDispatcher::~MesgDispatcher()
  88. {
  89. for (auto mem : m_mesgThreads)
  90. {
  91. TZ_delete(mem.second);
  92. }
  93. m_mesgThreads.clear();
  94. }
  95. MesgDispatcher * MesgDispatcher::Instance()
  96. {
  97. if (!_ins)
  98. {
  99. _insLock.Lock();
  100. if (!_ins)
  101. {
  102. _ins = new MesgDispatcher();
  103. }
  104. _insLock.Unlock();
  105. }
  106. return _ins;
  107. }
  108. void MesgDispatcher::DestoryInstance()
  109. {
  110. _insLock.Lock();
  111. TZ_delete(_ins);
  112. _insLock.Unlock();
  113. }
  114. TZ_INT MesgDispatcher::RegisterIListerner(
  115. IListerner * listener)
  116. {
  117. for (auto mem : m_mesgThreads)
  118. {
  119. mem.second->RegisterIListerner(listener);
  120. }
  121. TZLogInfo("register listerner succeed~~~");
  122. return MEC_OK;
  123. }
  124. TZ_INT MesgDispatcher::PushMesg(
  125. TZ_INT tid, SPtr<MasMesg> & mesg)
  126. {
  127. MesgThread * t = nullptr;
  128. if (!tid) t = this->chooseThread();
  129. else
  130. {
  131. auto iter = m_mesgThreads.find(tid);
  132. if (m_mesgThreads.end() == iter)
  133. {
  134. TZLogWarn("invalid tid %d!!!", tid);
  135. return 0;
  136. }
  137. t = iter->second;
  138. }
  139. t->PushMesg(mesg);
  140. return t->GetTid();
  141. }
  142. MesgThread * MesgDispatcher::chooseThread()
  143. {
  144. TZ_INT mTid = 0;
  145. TZ_INT pendingCnt = std::numeric_limits<TZ_INT>::max();
  146. for (auto mem : m_mesgThreads)
  147. {
  148. auto temp = mem.second->GetPendingCnt();
  149. if (temp <= pendingCnt)
  150. {
  151. pendingCnt = temp;
  152. mTid = mem.first;
  153. }
  154. }
  155. return m_mesgThreads[mTid];
  156. }
  157. NAMESPACE_MAS_END