#ifndef _EVENTPOOL_H #define _EVENTPOOL_H #pragma once #include " .CircularLinkList.h " #include " .EventElement.h " class __declspec(dllexport) CEventPool ... {public: CEventPool(void); ~CEventPool(void); void Initial(void); void RaiseEvent(US, US, CEventPara*); bool GetEvent(CNote**); void CancelEvent(CNote*); void Free(void);protected: void RaiseEvent(US, US, CEventPara*, CCircularLinkList*);private: CCircularLinkList m_LowEventList; CCircularLinkList m_MiddleEventList; CCircularLinkList m_HighEventList; CCriticalSection m_CriticalSection;} ; #endif /* _EVENTPOOL_H */ #include " StdAfx.h " #include " .EventPool.h " CEventPool::CEventPool( void ) ... {} CEventPool:: ~ CEventPool( void ) ... { /**/////Free();} void CEventPool::Initial( void ) ... {} void CEventPool::RaiseEvent(US rank, US trig, CEventPara * para_p) ... { m_CriticalSection.Lock(); switch (rank)...{ case TASK_RANK_LOW: RaiseEvent(rank,trig,para_p,&m_LowEventList); break; case TASK_RANK_MIDDLE: RaiseEvent(rank,trig,para_p,&m_MiddleEventList); break; case TASK_RANK_HIGH: RaiseEvent(rank,trig,para_p,&m_HighEventList); break; default: break; } m_CriticalSection.Unlock();} bool CEventPool::GetEvent(CNote ** node_dp) ... { bool result = false; m_CriticalSection.Lock(); result = m_HighEventList.GetHeader(node_dp); if (!result)...{ result = m_MiddleEventList.GetHeader(node_dp); if (!result)...{ result = m_LowEventList.GetHeader(node_dp); } } return result; m_CriticalSection.Lock();} void CEventPool::CancelEvent(CNote * node_p) ... { m_CriticalSection.Lock(); m_HighEventList.Remove(node_p); m_MiddleEventList.Remove(node_p); m_LowEventList.Remove(node_p); m_CriticalSection.Lock();} void CEventPool::Free( void ) ... { m_CriticalSection.Lock(); m_HighEventList.Free(); m_MiddleEventList.Free(); m_LowEventList.Free(); m_CriticalSection.Lock();} void CEventPool::RaiseEvent(US rank, US trig, CEventPara * para_p, CCircularLinkList * list_p) ... { CEventElement* eventElement_p; CMyEvent* myEvent_p; myEvent_p = new CMyEvent(); eventElement_p = new CEventElement(); myEvent_p->m_Trigger = trig; myEvent_p->m_Priority = rank; myEvent_p->parameter_p->Copy(¶_p); eventElement_p->Set(myEvent_p); list_p->AppendTail(eventElement_p);} #ifndef _LISTENERPOOL_H #define _LISTENERPOOL_H #pragma once #include " .CircularLinkList.h " #include " .HashTable.h " #include " .ListElement.h " #include " .ListenerElement.h " class __declspec(dllexport) CListenerPool ... {public: CListenerPool(void); ~CListenerPool(void); void Initial(void); void RaiseListener(CListener*); void CancelListener(US); void CancelListener(CListener*); bool FetchTrigListener(US, CListElement**); bool FetchTrigListener(US, CNote**); void RaiseRunningInstance(OBJECT, CNote**); void CancelRunningInstance(OBJECT); void CancelRunningInstance(CNote*); bool IsRunningInstance(OBJECT); void Free(void);private: CHashTable m_ListenerListTable; CCircularLinkList m_InstanceList; CCriticalSection m_CriticalSection;} ; #endif /* _LISTENERPOOL_H */ #include " StdAfx.h " #include " .ListenerPool.h " CListenerPool::CListenerPool( void ) ... {} CListenerPool:: ~ CListenerPool( void ) ... { Free();} void CListenerPool::Initial( void ) ... {} void CListenerPool::RaiseListener(CListener * listener_p) ... { US trig; CListenerElement* listenerElement_p; CListElement* listElement_p; CCircularLinkList* circularLinkList_p; CNote* note_p; m_CriticalSection.Lock(); if (NULL != listener_p)...{ listenerElement_p = new CListenerElement(); trig = listener_p->GetTrig(); listenerElement_p->Set(listener_p); if (m_ListenerListTable.Search(trig,¬e_p))...{ /**//*finded listener list*/ listElement_p = (CListElement*)note_p->element_p->Get(); circularLinkList_p = listElement_p->Get(); circularLinkList_p->AppendTail((CTokenElement*)listenerElement_p); } else...{ /**//*not find*/ circularLinkList_p = new CCircularLinkList(); listElement_p = new CListElement(); circularLinkList_p->AppendTail((CTokenElement*)listenerElement_p); listElement_p->Set(circularLinkList_p); this->m_ListenerListTable.Raise(trig,(CTokenElement*)listElement_p); } } m_CriticalSection.Unlock();} void CListenerPool::CancelListener(US trig) ... { m_CriticalSection.Lock(); m_ListenerListTable.Remove(trig); m_CriticalSection.Unlock();} void CListenerPool::CancelListener(CListener * listener_p) ... { US trig; CCircularLinkList* listenerlist_p; CNote* note_p; m_CriticalSection.Lock(); if (NULL != listener_p)...{ /**//*↓↓↓↓↓↓↓↓↓*/ trig = listener_p->GetTrig(); /**//*↑↑↑↑↑↑↑↑↑*/ if (m_ListenerListTable.Search(trig,¬e_p))...{ /**//*Get listener list*/ listenerlist_p = (CCircularLinkList*)(note_p->element_p->Get()); /**//*delete listener In Link List*/ listenerlist_p->Remove((OBJECT)listener_p); } } m_CriticalSection.Unlock();} bool CListenerPool::FetchTrigListener(US trig, CListElement ** listener_dp) ... { bool result = false; m_CriticalSection.Lock(); if (m_ListenerListTable.Search(trig,(CTokenElement**)listener_dp))...{ result = true; } m_CriticalSection.Unlock(); return result;} bool CListenerPool::FetchTrigListener(US trig, CNote ** node_dp) ... { bool result = false; m_CriticalSection.Lock(); if (m_ListenerListTable.Search(trig,node_dp))...{ result = true; } m_CriticalSection.Unlock(); return result;} void CListenerPool::RaiseRunningInstance(OBJECT instance_p, CNote ** node_dp) ... { CTokenElement* tokenElement_p; m_CriticalSection.Lock(); tokenElement_p = new CTokenElement(); tokenElement_p->Set(instance_p); m_InstanceList.AppendTail(tokenElement_p); m_InstanceList.GetTail(node_dp); m_CriticalSection.Unlock();} void CListenerPool::CancelRunningInstance(OBJECT instance_p) ... { m_CriticalSection.Lock(); m_InstanceList.Remove(instance_p); m_CriticalSection.Unlock();} void CListenerPool::CancelRunningInstance(CNote * node_p) ... { m_CriticalSection.Lock(); m_InstanceList.Remove(node_p); m_CriticalSection.Unlock();} bool CListenerPool::IsRunningInstance(OBJECT instance_p) ... { CNote* note_p; bool result = false; m_CriticalSection.Lock(); if (m_InstanceList.GetHeader(¬e_p))...{ while ( NULL != note_p)...{ /**//*Check Instance*/ if (instance_p == note_p->element_p->Get())...{ result = true; break; } /**//*Next Node*/ m_InstanceList.GetNext(note_p); } } m_CriticalSection.Unlock(); return result;} void CListenerPool::Free( void ) ... { CNote* node_p; m_CriticalSection.Lock(); while (NULL != m_ListenerListTable.SearchAllNode(&node_p))...{ /**/////delete node_p->element_p; ////delete node_p; m_ListenerListTable.Remove(node_p->key); } m_ListenerListTable.Free(); m_InstanceList.Free(); m_CriticalSection.Unlock();} 哈希表 #ifndef _HASHTABLE_H #define _HASHTABLE_H #pragma once #include " .OrdinalLinkList.h " class __declspec(dllexport) CHashTable ... {public: CHashTable(void); ~CHashTable(void); void Raise(US, CTokenElement*); void Remove(US); bool Search(US, CNote**); bool Search(US, CTokenElement**); bool SearchAllNode(CNote**); void Free(void);protected: US HashFunc(US);private: COrdinalLinkList hashtable_t[HASHSIZE];} ; #endif /* _HASHTABLE_H */ #include " StdAfx.h " #include " .HashTable.h " CHashTable::CHashTable( void ) ... {} CHashTable:: ~ CHashTable( void ) ... {/**///// Free();} void CHashTable::Raise(US key, CTokenElement * element_p) ... { hashtable_t[HashFunc(key)].Insert(key,element_p);} void CHashTable::Remove(US key) ... { hashtable_t[HashFunc(key)].Remove(key);} bool CHashTable::Search(US key, CNote ** node_dp) ... { bool result = false; CNote* note_p; if(hashtable_t[HashFunc(key)].Search(key,¬e_p))...{ *node_dp = note_p; result = true; } return result;} bool CHashTable::Search(US key, CTokenElement ** element_dp) ... { bool result = false; CNote* note_p; if(hashtable_t[HashFunc(key)].Search(key,¬e_p))...{ *element_dp = note_p->element_p; result = true; } return result;} bool CHashTable::SearchAllNode(CNote ** node_dp) ... { int i; bool result = false; for (i = 0; i < HASHSIZE; i++)...{ result = hashtable_t[i].GetHeader(node_dp); if (result)...{ break;/**//*find the node break this method*/ } } return result;} void CHashTable::Free( void ) ... { int i; for (i = 0; i<HASHSIZE; i++)...{ hashtable_t[i].Free(); }} US CHashTable::HashFunc(US key) ... { return (key % HASHSIZE);}