00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "Common/Assert.h"
00012 #include "Common/Thread.h"
00013
00014
00015 KThread::KThread()
00016 {
00017 m_hThread = NULL;
00018 m_hStopEvent = NULL;
00019 }
00020
00021
00022 bool KThread::IsThreadRunning()
00023 {
00024 return ( WaitForSingleObject( m_hStopEvent, 0 ) != WAIT_OBJECT_0 );
00025 }
00026
00027
00028 DWORD WINAPI KThread::StaticThreadProc( LPVOID lpParam )
00029 {
00030 return ((KThread*)lpParam)->ThreadProc();
00031 }
00032
00033
00034 bool KThread::StartThread()
00035 {
00036 DWORD ThreadId;
00037
00038 if( m_hThread )
00039 StopThread();
00040
00041 KASSERT( !m_hThread );
00042
00043
00044 m_hStopEvent = CreateEvent( NULL, FALSE, FALSE, NULL );
00045 if( !m_hStopEvent )
00046 return false;
00047
00048
00049 m_hThread = CreateThread( NULL, 0, StaticThreadProc, this, 0, &ThreadId );
00050 if( !m_hThread )
00051 {
00052 CloseHandle( m_hStopEvent );
00053 return false;
00054 }
00055
00056 return true;
00057 }
00058
00059
00060 bool KThread::StopThread()
00061 {
00062
00063 SetEvent( m_hStopEvent );
00064
00065
00066 WaitForSingleObject( m_hThread, INFINITE );
00067
00068 CloseHandle( m_hThread );
00069 CloseHandle( m_hStopEvent );
00070
00071 m_hThread = NULL;
00072 m_hStopEvent = NULL;
00073
00074 return true;
00075 }
00076
00077
00078 void KThread::SetPriority( KTHREADPRIORITY Priority )
00079 {
00080 int ThreadPriority = KCTP_NORMAL;
00081
00082 switch( Priority )
00083 {
00084 case KCTP_ABOVE_NORMAL: ThreadPriority = THREAD_PRIORITY_ABOVE_NORMAL;
00085 case KCTP_BELOW_NORMAL: ThreadPriority = THREAD_PRIORITY_BELOW_NORMAL;
00086 case KCTP_HIGHEST: ThreadPriority = THREAD_PRIORITY_HIGHEST;
00087 case KCTP_IDLE: ThreadPriority = THREAD_PRIORITY_IDLE;
00088 case KCTP_LOWEST: ThreadPriority = THREAD_PRIORITY_LOWEST;
00089 case KCTP_NORMAL: ThreadPriority = THREAD_PRIORITY_NORMAL;
00090 case KCTP_TIME_CRITICAL: ThreadPriority = THREAD_PRIORITY_TIME_CRITICAL;
00091 }
00092
00093 SetThreadPriority( m_hThread, ThreadPriority );
00094 }