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 &&
00086         dwResult<WAIT_OBJECT_0+m_ulWaitCount)
00087     {
00088         
00089         dwSignaledObject=dwResult-WAIT_OBJECT_0;
00090 
00091         
00092         if (m_bAutoRelease)
00093             if (bWaitAll)
00094                 BuildAutoRelease(0);
00095             else
00096                 BuildAutoRelease(dwSignaledObject+1);
00097 
00098         
00099         return FALSE;
00100     }
00101 
00102     
00103     return FALSE;
00104 }
00105 
00106 void CWaitList::AddObject(CWaitableObject* pObject,
00107                           BOOL bNoRelease)
00108 {
00109     if (m_ulWaitCount==MAXIMUM_WAIT_OBJECTS)
00110         return;
00111 
00112     
00113     m_aHandles[m_ulWaitCount]=pObject->GetHandle();
00114     m_aObjects[m_ulWaitCount]=pObject;
00115     m_bRelease[m_ulWaitCount]=!bNoRelease;
00116 
00117     
00118     ++m_ulWaitCount;
00119 }
00120 
00121 void CWaitList::SetAutoRelease(BOOL bRelease)
00122 {
00123     m_bAutoRelease=bRelease;
00124 }
00125 
00126 void CWaitList::BuildAutoRelease(unsigned long ulPosition)
00127 {
00128     
00129     Release();
00130 
00131     
00132     unsigned long ulItems;
00133 
00134     if (ulPosition)
00135         
00136         if (m_bRelease[ulPosition-1])
00137             ulItems=1;
00138         else
00139             
00140             return;
00141     else
00142         ulItems=m_ulWaitCount;
00143 
00144     
00145     m_ppRelease=new CWaitableObjectAutoRelease*[ulItems+1];
00146     memset(m_ppRelease,
00147            0,
00148            sizeof(CWaitableObjectAutoRelease*)*(ulItems+1));
00149 
00150     
00151     if (ulPosition)
00152         m_ppRelease[0]=new CWaitableObjectAutoRelease(m_aObjects[ulPosition-1]);
00153     else
00154     {
00155         unsigned long ulRunningCounter;
00156         ulRunningCounter=0;
00157 
00158         
00159         for (unsigned long ulCounter=0;
00160              ulCounter<m_ulWaitCount;
00161              ++ulCounter)
00162             
00163             if (m_bRelease[ulCounter])
00164                 
00165                 m_ppRelease[ulRunningCounter++]=new CWaitableObjectAutoRelease(m_aObjects[ulCounter]);
00166     }
00167 
00168     
00169 }
00170 
00171 void CWaitList::Release()
00172 {
00173     if (m_ppRelease)
00174     {
00175         
00176         CWaitableObjectAutoRelease** ppBackup;
00177         ppBackup=m_ppRelease;
00178 
00179         while (*ppBackup)
00180         {
00181             
00182             delete *ppBackup;
00183 
00184             
00185             ++ppBackup;
00186         }
00187 
00188         
00189         delete [] m_ppRelease;
00190         m_ppRelease=NULL;
00191     }
00192 }
00193 
00194 KOMODIA_NAMESPACE_END