123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- #ifndef __MESG_DISPATCHER_H
- #define __MESG_DISPATCHER_H
- #include "Locks.h"
- #include "Thread.h"
- #include "Semaphore.h"
- #include "ComDef.h"
- NAMESPACE_MAS_BEGIN
- class IListerner {
- public:
- IListerner() {}
- virtual ~IListerner(){}
- public:
- void SetRegMesg(TZ_INT regvalue);
- TZ_INT GetRegMesg();
- virtual void HandleMesg(SPtr<MasMesg> & info) = 0;
- private:
- TZ_INT m_regMesg; // EN_MAS_MESG
- };
- inline void IListerner::SetRegMesg(TZ_INT regvalue)
- {
- m_regMesg = regvalue;
- }
- inline TZ_INT IListerner::GetRegMesg()
- {
- return m_regMesg;
- }
- class MesgThread : public tzc::OSThread {
- public:
- MesgThread(TZ_INT tid);
- ~MesgThread();
- public:
- void RegisterIListerner(
- IListerner * listener);
- TZ_INT PushMesg(SPtr<MasMesg> & mesg);
- TZ_INT GetTid();
- TZ_INT GetPendingCnt();
- private:
- virtual void Entry();
- SPtr<MasMesg> getFrontEvent();
- void dispatcherEvent(SPtr<MasMesg> & info);
- private:
- TZ_INT m_tid;
- tzc::Mutex m_insMapLock;
- std::map<IListerner *, TZ_INT> m_insMap;
- tzc::Mutex m_eventsLock;
- tzc::Semaphore m_eventSema;
- std::list<SPtr<MasMesg>> m_pendingMesgs;
- const TZ_Uint32 EVENT_WAIT_MSECOND = 100;
- };
- inline TZ_INT MesgThread::GetTid()
- {
- return m_tid;
- }
- inline TZ_INT MesgThread::GetPendingCnt()
- {
- tzc::ScopedLock lock(m_eventsLock);
- return m_pendingMesgs.size();
- }
- class MesgDispatcher {
- public:
- static MesgDispatcher * Instance();
- static void DestoryInstance();
- public:
- TZ_INT RegisterIListerner(
- IListerner * listener);
- // system will choose thread if the tid is zero
- // tid is 1~4
- TZ_INT PushMesg(TZ_INT tid, SPtr<MasMesg> & mesg);
- private:
- MesgDispatcher();
- ~MesgDispatcher();
- MesgThread * chooseThread();
- private:
- std::map<TZ_INT, MesgThread *> m_mesgThreads;
- static MesgDispatcher * _ins;
- static tzc::Mutex _insLock;
- };
- #define MESGDISPATCHER MesgDispatcher::Instance()
- NAMESPACE_MAS_END
- #endif
|