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 "Win32Thread.h"
00038
00039 #include "ErrorHandlerMacros.h"
00040
00041 #ifdef _MEMORY_DEBUG
00042 #define new DEBUG_NEW
00043 #define malloc DEBUG_MALLOC
00044 static char THIS_FILE[] = __FILE__;
00045 #endif
00046
00047 KOMODIA_NAMESPACE_START
00048
00049 #define CWin32Thread_Class "CWin32Thread"
00050
00051 CWin32Thread::CWin32Thread(LPGenericThreadProc pThreadProc) : CGenericThread(pThreadProc),
00052 m_hThread(0)
00053 {
00054 try
00055 {
00056
00057 SetName(CWin32Thread_Class);
00058
00059
00060 if (m_pThreadProc)
00061 {
00062
00063 m_hThread=CreateThread(NULL,
00064 0,
00065 Win32Thread,
00066 this,
00067 CREATE_SUSPENDED,
00068 &m_dwThreadID);
00069
00070
00071 if (m_hThread)
00072 SetThreadStatus(tsSuspended);
00073 }
00074 }
00075 ERROR_HANDLER("CWin32Thread")
00076 }
00077
00078 CWin32Thread::~CWin32Thread()
00079 {
00080 try
00081 {
00082
00083 Stop();
00084 }
00085 ERROR_HANDLER("~CWin32Thread")
00086 }
00087
00088 BOOL CWin32Thread::Start(LPVOID pData)
00089 {
00090 try
00091 {
00092 if (GetThreadStatus()==tsStopped)
00093 {
00094
00095
00096 m_hThread=CreateThread(NULL,
00097 0,
00098 Win32Thread,
00099 this,
00100 CREATE_SUSPENDED,
00101 &m_dwThreadID);
00102
00103
00104 if (m_hThread)
00105 SetThreadStatus(tsSuspended);
00106 else
00107
00108 return FALSE;
00109 }
00110 else if (GetThreadStatus()!=tsSuspended)
00111 return FALSE;
00112
00113
00114 CGenericThread::Start(pData);
00115
00116
00117 if (m_hThread)
00118 if (ResumeThread(m_hThread)!=-1)
00119
00120 return TRUE;
00121
00122 return FALSE;
00123 }
00124 ERROR_HANDLER_RETURN("Start",FALSE)
00125 }
00126
00127 BOOL CWin32Thread::Stop()
00128 {
00129 try
00130 {
00131
00132
00133 if (m_hThread)
00134 {
00135
00136 if (GetThreadStatus()==tsRunning &&
00137 GetBruteTermination())
00138
00139 if (!TerminateThread(m_hThread,THREAD_DO_NOTHING_EXIT_VALUE))
00140 return FALSE;
00141
00142 if (GetThreadStatus()==tsSuspended ||
00143 GetThreadStatus()==tsRunning)
00144 if (CloseHandle(m_hThread))
00145 {
00146
00147 m_hThread=NULL;
00148
00149
00150 SetThreadStatus(tsStopped);
00151
00152
00153 return TRUE;
00154 }
00155 else
00156 return FALSE;
00157 else
00158 {
00159
00160 if (!CloseHandle(m_hThread))
00161 m_hThread=NULL;
00162
00163
00164 return FALSE;
00165 }
00166 }
00167 else
00168 return FALSE;
00169 }
00170 ERROR_HANDLER_RETURN("Stop",FALSE)
00171 }
00172
00173 DWORD WINAPI CWin32Thread::Win32Thread(LPVOID lpData)
00174 {
00175 try
00176 {
00177
00178 CWin32Thread* pClass;
00179 pClass=(CWin32Thread*)lpData;
00180
00181
00182 pClass->SetThreadStatus(tsRunning);
00183
00184
00185 LPVOID pData;
00186 pData=pClass->GetData();
00187
00188
00189 LPGenericThreadProc lpProc;
00190 lpProc=pClass->GetThreadProc();
00191
00192
00193 BOOL bAutoDelete;
00194 bAutoDelete=pClass->GetAutoDelete();
00195
00196
00197 DWORD dwReturnValue;
00198
00199
00200 try
00201 {
00202 dwReturnValue=(*lpProc)(pData);
00203 }
00204 catch(...)
00205 {
00206 }
00207
00208
00209 if (bAutoDelete)
00210 {
00211
00212
00213 pClass->SetBruteTermination(FALSE);
00214
00215
00216 delete pClass;
00217 }
00218
00219 else if (dwReturnValue!=THREAD_DO_NOTHING_EXIT_VALUE)
00220
00221 pClass->SetThreadStatus(tsStopped);
00222
00223 return dwReturnValue;
00224 }
00225 ERROR_HANDLER_STATIC_RETURN(CWin32Thread_Class,"Win32Thread",THREAD_DO_NOTHING_EXIT_VALUE)
00226 }
00227
00228 BOOL CWin32Thread::SetPriority(CGenericThread::ThreadPriority aPriority)
00229 {
00230 try
00231 {
00232 static const int iThreadPriority[]={THREAD_PRIORITY_IDLE,
00233 THREAD_PRIORITY_LOWEST,
00234 THREAD_PRIORITY_BELOW_NORMAL,
00235 THREAD_PRIORITY_NORMAL,
00236 THREAD_PRIORITY_ABOVE_NORMAL,
00237 THREAD_PRIORITY_HIGHEST,
00238 THREAD_PRIORITY_TIME_CRITICAL};
00239
00240
00241 if (GetThreadStatus()==tsStopped)
00242 {
00243
00244
00245 m_hThread=CreateThread(NULL,
00246 0,
00247 Win32Thread,
00248 this,
00249 CREATE_SUSPENDED,
00250 &m_dwThreadID);
00251
00252
00253 if (m_hThread)
00254 SetThreadStatus(tsSuspended);
00255 else
00256
00257 return FALSE;
00258 }
00259
00260
00261 return SetThreadPriority(m_hThread,iThreadPriority[aPriority]);
00262 }
00263 ERROR_HANDLER_RETURN("SetPriority",FALSE)
00264 }
00265
00266 CGenericThread::ThreadPriority CWin32Thread::GetPriority()const
00267 {
00268 try
00269 {
00270
00271 if (!m_hThread)
00272 return CGenericThread::tpNormal;
00273
00274 int iPriority;
00275
00276
00277 iPriority=GetThreadPriority(m_hThread);
00278
00279
00280 switch (iPriority)
00281 {
00282 case THREAD_PRIORITY_ABOVE_NORMAL:
00283 return CGenericThread::tpAboveNormal;
00284 case THREAD_PRIORITY_BELOW_NORMAL:
00285 return CGenericThread::tpBelowNormal;
00286 case THREAD_PRIORITY_HIGHEST:
00287 return CGenericThread::tpHighest;
00288 case THREAD_PRIORITY_IDLE:
00289 return CGenericThread::tpIdle;
00290 case THREAD_PRIORITY_LOWEST:
00291 return CGenericThread::tpLowest;
00292 case THREAD_PRIORITY_NORMAL:
00293 return CGenericThread::tpNormal;
00294 case THREAD_PRIORITY_TIME_CRITICAL:
00295 return CGenericThread::tpTimeCritical;
00296 default:
00297 return CGenericThread::tpNormal;
00298 }
00299 }
00300 ERROR_HANDLER_RETURN("GetPriority",CGenericThread::tpNormal)
00301 }
00302
00303 KOMODIA_NAMESPACE_END