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