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