Go to the documentation of this file.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 #include "sdkcommon.h"
00036 #include <process.h>
00037
00038 namespace rp{ namespace hal{
00039
00040 Thread Thread::create(thread_proc_t proc, void * data)
00041 {
00042 Thread newborn(proc, data);
00043
00044 newborn._handle = (_word_size_t)(
00045 _beginthreadex(NULL, 0, (unsigned int (_stdcall * )( void * ))proc,
00046 data, 0, NULL));
00047 return newborn;
00048 }
00049
00050 u_result Thread::terminate()
00051 {
00052 if (!this->_handle) return RESULT_OK;
00053 if (TerminateThread( reinterpret_cast<HANDLE>(this->_handle), -1))
00054 {
00055 CloseHandle(reinterpret_cast<HANDLE>(this->_handle));
00056 this->_handle = NULL;
00057 return RESULT_OK;
00058 }else
00059 {
00060 return RESULT_OPERATION_FAIL;
00061 }
00062 }
00063
00064 u_result Thread::setPriority( priority_val_t p)
00065 {
00066 if (!this->_handle) return RESULT_OPERATION_FAIL;
00067
00068 int win_priority = THREAD_PRIORITY_NORMAL;
00069 switch(p)
00070 {
00071 case PRIORITY_REALTIME:
00072 win_priority = THREAD_PRIORITY_TIME_CRITICAL;
00073 break;
00074 case PRIORITY_HIGH:
00075 win_priority = THREAD_PRIORITY_HIGHEST;
00076 break;
00077 case PRIORITY_NORMAL:
00078 win_priority = THREAD_PRIORITY_NORMAL;
00079 break;
00080 case PRIORITY_LOW:
00081 win_priority = THREAD_PRIORITY_LOWEST;
00082 break;
00083 case PRIORITY_IDLE:
00084 win_priority = THREAD_PRIORITY_IDLE;
00085 break;
00086 }
00087
00088 if (SetThreadPriority(reinterpret_cast<HANDLE>(this->_handle), win_priority))
00089 {
00090 return RESULT_OK;
00091 }
00092 return RESULT_OPERATION_FAIL;
00093 }
00094
00095 Thread::priority_val_t Thread::getPriority()
00096 {
00097 if (!this->_handle) return PRIORITY_NORMAL;
00098 int win_priority = ::GetThreadPriority(reinterpret_cast<HANDLE>(this->_handle));
00099
00100 if (win_priority == THREAD_PRIORITY_ERROR_RETURN)
00101 {
00102 return PRIORITY_NORMAL;
00103 }
00104
00105 if (win_priority >= THREAD_PRIORITY_TIME_CRITICAL )
00106 {
00107 return PRIORITY_REALTIME;
00108 }
00109 else if (win_priority<THREAD_PRIORITY_TIME_CRITICAL && win_priority>=THREAD_PRIORITY_ABOVE_NORMAL)
00110 {
00111 return PRIORITY_HIGH;
00112 }
00113 else if (win_priority<THREAD_PRIORITY_ABOVE_NORMAL && win_priority>THREAD_PRIORITY_BELOW_NORMAL)
00114 {
00115 return PRIORITY_NORMAL;
00116 }else if (win_priority<=THREAD_PRIORITY_BELOW_NORMAL && win_priority>THREAD_PRIORITY_IDLE)
00117 {
00118 return PRIORITY_LOW;
00119 }else if (win_priority<=THREAD_PRIORITY_IDLE)
00120 {
00121 return PRIORITY_IDLE;
00122 }
00123 return PRIORITY_NORMAL;
00124 }
00125
00126 u_result Thread::join(unsigned long timeout)
00127 {
00128 if (!this->_handle) return RESULT_OK;
00129 switch ( WaitForSingleObject(reinterpret_cast<HANDLE>(this->_handle), timeout))
00130 {
00131 case WAIT_OBJECT_0:
00132 CloseHandle(reinterpret_cast<HANDLE>(this->_handle));
00133 this->_handle = NULL;
00134 return RESULT_OK;
00135 case WAIT_ABANDONED:
00136 return RESULT_OPERATION_FAIL;
00137 case WAIT_TIMEOUT:
00138 return RESULT_OPERATION_TIMEOUT;
00139 }
00140
00141 return RESULT_OK;
00142 }
00143
00144 }}