00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #ifndef _ResourceQueue_H_
00037 #define _ResourceQueue_H_
00038
00039 #include "ErrorHandler.h"
00040 #include "OSManager.h"
00041 #include "GenericCriticalSection.h"
00042 #include "GenericSemaphore.h"
00043 #include "ErrorHandlerMacros.h"
00044
00045 #include <deque>
00046 #include <algorithm>
00047
00048 KOMODIA_NAMESPACE_START
00049
00050 template<class T>
00051 class CResourceQueue : public CErrorHandler
00052 {
00053 public:
00054
00055 typedef void (*DeleteProc)(T& rItem);
00056 typedef DeleteProc LPDeleteProc;
00057 public:
00058
00059 unsigned long GetDataCount()const;
00060
00061
00062 virtual BOOL RequestResource(T& rResource,
00063 unsigned long ulTimeout=INFINITE);
00064
00065
00066 virtual void AddResource(T& rResource);
00067
00068
00069 CResourceQueue(LPDeleteProc pCleanUPProc=NULL,
00070 unsigned long ulMaxItems=0);
00071 virtual ~CResourceQueue();
00072 private:
00073
00074 typedef std::deque<T> ItemsQueue;
00075 private:
00076
00077 CResourceQueue(const CResourceQueue& rQueue);
00078
00079
00080 CResourceQueue& operator=(const CResourceQueue& rQueue);
00081
00082
00083 ItemsQueue m_aItems;
00084
00085
00086 CGenericCriticalSection* m_pCSection;
00087
00088
00089 CGenericSemaphore* m_pSemaphore;
00090
00091
00092 LPDeleteProc m_pProc;
00093
00094
00095 unsigned long m_ulMaxItems;
00096 };
00097
00098 #define CResourceQueue_Class "CResourceQueue"
00099
00100 template<class T>
00101 CResourceQueue<T>::CResourceQueue(LPDeleteProc pCleanUPProc,
00102 unsigned long ulMaxItems) : CErrorHandler(),
00103 m_pCSection(NULL),
00104 m_pSemaphore(NULL),
00105 m_pProc(pCleanUPProc),
00106 m_ulMaxItems(ulMaxItems)
00107 {
00108 try
00109 {
00110
00111 SetName(CResourceQueue_Class);
00112
00113
00114 m_pCSection=COSManager::CreateCriticalSection();
00115
00116
00117 m_pSemaphore=COSManager::CreateSemaphore(0,
00118 INFINITE/3);
00119 }
00120 ERROR_HANDLER("CResourceQueue")
00121 }
00122
00123 template<class T>
00124 CResourceQueue<T>::~CResourceQueue()
00125 {
00126 try
00127 {
00128
00129 delete m_pCSection;
00130
00131
00132 delete m_pSemaphore;
00133
00134
00135 if (m_pProc)
00136
00137 std::for_each(m_aItems.begin(),
00138 m_aItems.end(),
00139 m_pProc);
00140 }
00141 ERROR_HANDLER("~CResourceQueue")
00142 }
00143
00144 template<class T>
00145 BOOL CResourceQueue<T>::RequestResource(T& rResource,
00146 unsigned long ulTimeout)
00147 {
00148 try
00149 {
00150
00151 if (m_pSemaphore->Aquire(ulTimeout))
00152
00153 return FALSE;
00154
00155
00156 CCriticalAutoRelease aRelease(m_pCSection);
00157
00158
00159 rResource=m_aItems.front();
00160 m_aItems.pop_front();
00161
00162
00163 return TRUE;
00164 }
00165 ERROR_HANDLER_RETURN("RequestResource",FALSE)
00166 }
00167
00168 template<class T>
00169 void CResourceQueue<T>::AddResource(T& rResource)
00170 {
00171 try
00172 {
00173 {
00174 while (1)
00175 {
00176
00177 CCriticalAutoRelease aRelease(m_pCSection);
00178
00179
00180 if (!m_ulMaxItems ||
00181 m_aItems.size()<m_ulMaxItems)
00182 {
00183
00184 m_aItems.push_back(rResource);
00185
00186
00187 break;
00188 }
00189 else
00190 Sleep(1);
00191 }
00192 }
00193
00194
00195 m_pSemaphore->Release();
00196 }
00197 ERROR_HANDLER_RETHROW("AddResource")
00198 }
00199
00200 template<class T>
00201 unsigned long CResourceQueue<T>::GetDataCount()const
00202 {
00203 try
00204 {
00205
00206 CCriticalAutoRelease aRelease(m_pCSection);
00207
00208
00209 return m_aItems.size();
00210 }
00211 ERROR_HANDLER_RETURN("GetDataCount",0)
00212 }
00213
00214 KOMODIA_NAMESPACE_END
00215
00216 #endif