#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 & 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 & mesg); TZ_INT GetTid(); TZ_INT GetPendingCnt(); private: virtual void Entry(); SPtr getFrontEvent(); void dispatcherEvent(SPtr & info); private: TZ_INT m_tid; tzc::Mutex m_insMapLock; std::map m_insMap; tzc::Mutex m_eventsLock; tzc::Semaphore m_eventSema; std::list> 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 & mesg); private: MesgDispatcher(); ~MesgDispatcher(); MesgThread * chooseThread(); private: std::map m_mesgThreads; static MesgDispatcher * _ins; static tzc::Mutex _insLock; }; #define MESGDISPATCHER MesgDispatcher::Instance() NAMESPACE_MAS_END #endif