MesgDispatcher.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #ifndef __MESG_DISPATCHER_H
  2. #define __MESG_DISPATCHER_H
  3. #include "Locks.h"
  4. #include "Thread.h"
  5. #include "Semaphore.h"
  6. #include "ComDef.h"
  7. NAMESPACE_MAS_BEGIN
  8. class IListerner {
  9. public:
  10. IListerner() {}
  11. virtual ~IListerner(){}
  12. public:
  13. void SetRegMesg(TZ_INT regvalue);
  14. TZ_INT GetRegMesg();
  15. virtual void HandleMesg(SPtr<MasMesg> & info) = 0;
  16. private:
  17. TZ_INT m_regMesg; // EN_MAS_MESG
  18. };
  19. inline void IListerner::SetRegMesg(TZ_INT regvalue)
  20. {
  21. m_regMesg = regvalue;
  22. }
  23. inline TZ_INT IListerner::GetRegMesg()
  24. {
  25. return m_regMesg;
  26. }
  27. class MesgThread : public tzc::OSThread {
  28. public:
  29. MesgThread(TZ_INT tid);
  30. ~MesgThread();
  31. public:
  32. void RegisterIListerner(
  33. IListerner * listener);
  34. TZ_INT PushMesg(SPtr<MasMesg> & mesg);
  35. TZ_INT GetTid();
  36. TZ_INT GetPendingCnt();
  37. private:
  38. virtual void Entry();
  39. SPtr<MasMesg> getFrontEvent();
  40. void dispatcherEvent(SPtr<MasMesg> & info);
  41. private:
  42. TZ_INT m_tid;
  43. tzc::Mutex m_insMapLock;
  44. std::map<IListerner *, TZ_INT> m_insMap;
  45. tzc::Mutex m_eventsLock;
  46. tzc::Semaphore m_eventSema;
  47. std::list<SPtr<MasMesg>> m_pendingMesgs;
  48. const TZ_Uint32 EVENT_WAIT_MSECOND = 100;
  49. };
  50. inline TZ_INT MesgThread::GetTid()
  51. {
  52. return m_tid;
  53. }
  54. inline TZ_INT MesgThread::GetPendingCnt()
  55. {
  56. tzc::ScopedLock lock(m_eventsLock);
  57. return m_pendingMesgs.size();
  58. }
  59. class MesgDispatcher {
  60. public:
  61. static MesgDispatcher * Instance();
  62. static void DestoryInstance();
  63. public:
  64. TZ_INT RegisterIListerner(
  65. IListerner * listener);
  66. // system will choose thread if the tid is zero
  67. // tid is 1~4
  68. TZ_INT PushMesg(TZ_INT tid, SPtr<MasMesg> & mesg);
  69. private:
  70. MesgDispatcher();
  71. ~MesgDispatcher();
  72. MesgThread * chooseThread();
  73. private:
  74. std::map<TZ_INT, MesgThread *> m_mesgThreads;
  75. static MesgDispatcher * _ins;
  76. static tzc::Mutex _insLock;
  77. };
  78. #define MESGDISPATCHER MesgDispatcher::Instance()
  79. NAMESPACE_MAS_END
  80. #endif