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 "arch/linux/arch_linux.h"
00038
00039 #include <sched.h>
00040
00041 namespace rp{ namespace hal{
00042
00043 Thread Thread::create(thread_proc_t proc, void * data)
00044 {
00045 Thread newborn(proc, data);
00046
00047
00048 assert( sizeof(newborn._handle) >= sizeof(pthread_t));
00049
00050 pthread_create((pthread_t *)&newborn._handle, NULL, (void * (*)(void *))proc, data);
00051
00052 return newborn;
00053 }
00054
00055 u_result Thread::terminate()
00056 {
00057 if (!this->_handle) return RESULT_OK;
00058
00059 return pthread_cancel((pthread_t)this->_handle)==0?RESULT_OK:RESULT_OPERATION_FAIL;
00060 }
00061
00062 u_result Thread::setPriority( priority_val_t p)
00063 {
00064 if (!this->_handle) return RESULT_OPERATION_FAIL;
00065
00066
00067
00068 int current_policy;
00069 struct sched_param current_param;
00070 int ans;
00071 if (pthread_getschedparam( (pthread_t) this->_handle, ¤t_policy, ¤t_param))
00072 {
00073
00074 return RESULT_OPERATION_FAIL;
00075 }
00076
00077
00078
00079 switch(p)
00080 {
00081 case PRIORITY_REALTIME:
00082
00083 current_policy = SCHED_RR;
00084 break;
00085 case PRIORITY_HIGH:
00086
00087 current_policy = SCHED_RR;
00088 break;
00089 case PRIORITY_NORMAL:
00090 case PRIORITY_LOW:
00091 case PRIORITY_IDLE:
00092
00093 current_policy = SCHED_OTHER;
00094 break;
00095 }
00096
00097 current_param.__sched_priority = current_policy;
00098 if ( (ans = pthread_setschedparam( (pthread_t) this->_handle, current_policy, ¤t_param)) )
00099 {
00100 return RESULT_OPERATION_FAIL;
00101 }
00102 return RESULT_OK;
00103 }
00104
00105 Thread::priority_val_t Thread::getPriority()
00106 {
00107 if (!this->_handle) return PRIORITY_NORMAL;
00108
00109 int current_policy;
00110 struct sched_param current_param;
00111 if (pthread_getschedparam( (pthread_t) this->_handle, ¤t_policy, ¤t_param))
00112 {
00113
00114 return PRIORITY_NORMAL;
00115 }
00116
00117 int pthread_priority_max = sched_get_priority_max(SCHED_RR);
00118 int pthread_priority_min = sched_get_priority_min(SCHED_RR);
00119
00120 if (current_param.__sched_priority ==(pthread_priority_max ))
00121 {
00122 return PRIORITY_REALTIME;
00123 }
00124 if (current_param.__sched_priority >=(pthread_priority_max + pthread_priority_min)/2)
00125 {
00126 return PRIORITY_HIGH;
00127 }
00128 return PRIORITY_NORMAL;
00129 }
00130
00131 u_result Thread::join(unsigned long timeout)
00132 {
00133 if (!this->_handle) return RESULT_OK;
00134
00135 pthread_join((pthread_t)(this->_handle), NULL);
00136 return RESULT_OK;
00137 }
00138
00139 }}