00001 00002 #include "Core/precomp.h" 00003 00004 #include "API/Core/System/thread.h" 00005 #include "API/Core/System/error.h" 00006 #include "thread_win32.h" 00007 /************************************* 00008 Generic static create-thread function 00009 *************************************/ 00010 00011 CL_Thread *CL_Thread::create(CL_Runnable *runnable) 00012 { 00013 return new CL_Thread_Win32(runnable); 00014 } 00015 00016 /************************************* 00017 class CL_Thread_Win32 00018 *************************************/ 00019 00020 00021 CL_Thread_Win32::CL_Thread_Win32(CL_Runnable *runnable) 00022 { 00023 this->runnable = runnable; 00024 thread_handle = NULL; 00025 } 00026 00027 CL_Thread_Win32::~CL_Thread_Win32() 00028 { 00029 if (thread_handle != NULL) terminate(); 00030 } 00031 #ifdef BORLAND 00032 unsigned long _stdcall CL_Thread_Win32::func_proxy(void *arg) 00033 #else 00034 unsigned long CL_Thread_Win32::func_proxy(void *arg) 00035 #endif 00036 { 00037 CL_Thread_Win32 *parent = (CL_Thread_Win32 *) arg; 00038 parent->runnable->run(); 00039 00040 return 0; 00041 } 00042 00043 void CL_Thread_Win32::start() 00044 { 00045 thread_handle = CreateThread(NULL, 0, func_proxy, this, 0, &thread_id); 00046 if (thread_handle == NULL) 00047 { 00048 throw CL_Error("Failed to create thread"); 00049 } 00050 } 00051 00052 void CL_Thread_Win32::terminate() 00053 { 00054 TerminateThread(thread_handle, 0); 00055 CloseHandle(thread_handle); 00056 thread_handle = NULL; 00057 } 00058 00059 void CL_Thread_Win32::wait() 00060 { 00061 if (thread_handle == NULL) return; 00062 00063 WaitForSingleObject(thread_handle, INFINITE); 00064 CloseHandle(thread_handle); 00065 thread_handle = NULL; 00066 }
1.2.6 written by Dimitri van Heesch,
© 1997-2001