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 #include "stdafx.h"
00037 #include "WaitList.h"
00038
00039 #include "ErrorHandlerMacros.h"
00040 #include "WaitableObject.h"
00041 #include "WaitableObjectAutoRelease.h"
00042
00043 #ifdef _MEMORY_DEBUG
00044 #define new DEBUG_NEW
00045 #define malloc DEBUG_MALLOC
00046 static char THIS_FILE[] = __FILE__;
00047 #endif
00048
00049 KOMODIA_NAMESPACE_START
00050
00051 CWaitList::CWaitList() : m_ulWaitCount(0),
00052 m_bAutoRelease(FALSE),
00053 m_ppRelease(NULL)
00054 {
00055 }
00056
00057 CWaitList::~CWaitList()
00058 {
00059 Release();
00060 }
00061
00062 BOOL CWaitList::Wait(BOOL bWaitAll,
00063 DWORD& dwSignaledObject,
00064 unsigned long ulTimeout)
00065 {
00066
00067 dwSignaledObject=MAXIMUM_WAIT_OBJECTS;
00068
00069
00070 if (!m_ulWaitCount)
00071 return FALSE;
00072
00073
00074 DWORD dwResult;
00075 dwResult=WaitForMultipleObjects(m_ulWaitCount,
00076 m_aHandles,
00077 bWaitAll,
00078 ulTimeout);
00079
00080
00081 if (dwResult==WAIT_TIMEOUT)
00082 return TRUE;
00083
00084
00085 if (dwResult<=WAIT_OBJECT_0+m_ulWaitCount-1)
00086 {
00087
00088 dwSignaledObject=dwResult-WAIT_OBJECT_0;
00089
00090
00091 if (m_bAutoRelease)
00092 if (bWaitAll)
00093 BuildAutoRelease(0);
00094 else
00095 BuildAutoRelease(dwSignaledObject+1);
00096
00097
00098 return FALSE;
00099 }
00100
00101
00102 return FALSE;
00103 }
00104
00105 void CWaitList::AddObject(CWaitableObject* pObject,
00106 BOOL bNoRelease)
00107 {
00108 if (m_ulWaitCount==MAXIMUM_WAIT_OBJECTS)
00109 return;
00110
00111
00112 m_aHandles[m_ulWaitCount]=pObject->GetHandle();
00113 m_aObjects[m_ulWaitCount]=pObject;
00114 m_bRelease[m_ulWaitCount]=!bNoRelease;
00115
00116
00117 ++m_ulWaitCount;
00118 }
00119
00120 CWaitList& CWaitList::operator<<(CWaitableObject* pObject)
00121 {
00122
00123 AddObject(pObject);
00124
00125
00126 return *this;
00127 }
00128
00129 void CWaitList::SetAutoRelease(BOOL bRelease)
00130 {
00131 m_bAutoRelease=bRelease;
00132 }
00133
00134 void CWaitList::BuildAutoRelease(unsigned long ulPosition)
00135 {
00136
00137 Release();
00138
00139
00140 unsigned long ulItems;
00141
00142 if (ulPosition)
00143
00144 if (m_bRelease[ulPosition-1])
00145 ulItems=1;
00146 else
00147
00148 return;
00149 else
00150 ulItems=m_ulWaitCount;
00151
00152
00153 m_ppRelease=new CWaitableObjectAutoRelease*[ulItems+1];
00154 memset(m_ppRelease,
00155 0,
00156 sizeof(CWaitableObjectAutoRelease*)*(ulItems+1));
00157
00158
00159 if (ulPosition)
00160 m_ppRelease[0]=new CWaitableObjectAutoRelease(m_aObjects[ulPosition-1]);
00161 else
00162 {
00163 unsigned long ulRunningCounter;
00164 ulRunningCounter=0;
00165
00166
00167 for (unsigned long ulCounter=0;
00168 ulCounter<m_ulWaitCount;
00169 ++ulCounter)
00170
00171 if (m_bRelease[ulCounter])
00172
00173 m_ppRelease[ulRunningCounter++]=new CWaitableObjectAutoRelease(m_aObjects[ulCounter]);
00174 }
00175
00176
00177 }
00178
00179 void CWaitList::Release()
00180 {
00181 if (m_ppRelease)
00182 {
00183
00184 CWaitableObjectAutoRelease** ppBackup;
00185 ppBackup=m_ppRelease;
00186
00187 while (*ppBackup)
00188 {
00189
00190 delete *ppBackup;
00191
00192
00193 ++ppBackup;
00194 }
00195
00196
00197 delete [] m_ppRelease;
00198 m_ppRelease=NULL;
00199 }
00200 }
00201
00202 KOMODIA_NAMESPACE_END