39 #ifndef OS_FOSI_INTERNAL_HPP 40 #define OS_FOSI_INTERNAL_HPP 41 #define OROBLD_OS_LXRT_INTERNAL 47 #include "../fosi_internal_interface.hpp" 50 #include <sys/types.h> 51 #include "../../rtt-config.h" 56 #include "../../Logger.hpp" 64 if ( geteuid() != 0 ) {
65 std::cerr <<
"You are not root. This program requires that you are root." <<
std::endl;
69 #ifdef OROSEM_OS_LOCK_MEMORY 70 int locktype = MCL_CURRENT;
71 #ifdef OROSEM_OS_LOCK_MEMORY_FUTURE 72 locktype |= MCL_FUTURE;
75 int rv = mlockall(locktype);
77 perror(
"rtos_task_create_main: Could not lock memory using mlockall" );
89 unsigned long name = nam2num(
"main");
90 while ( rt_get_adr( name ) != 0 )
94 const char* tname =
"main";
95 main_task->
name = strcpy( (
char*)malloc( (strlen(tname) + 1) *
sizeof(
char)), tname);
97 if( !(main_task->
rtaitask = rt_task_init(name, 10,0,0)) )
99 std::cerr <<
"Cannot rt_task_init() MainThread." <<
std::endl;
103 struct sched_param param;
105 param.sched_priority = sched_get_priority_max(SCHED_OTHER);
106 if (param.sched_priority != -1 )
107 sched_setscheduler( 0, SCHED_OTHER, ¶m);
110 rt_task_use_fpu(main_task->
rtaitask, 1);
112 #ifdef OROSEM_OS_LXRT_PERIODIC 113 rt_set_periodic_mode();
114 start_rt_timer( nano2count(
NANO_TIME(ORODAT_OS_LXRT_PERIODIC_TICK*1000*1000*1000) ) );
118 rt_set_oneshot_mode();
120 #if defined(CONFIG_RTAI_VERSION_MINOR) && defined(CONFIG_RTAI_VERSION_MAJOR) 121 # if CONFIG_RTAI_VERSION_MAJOR == 3 && CONFIG_RTAI_VERSION_MINOR == 0 122 rt_preempt_always(1);
125 rt_preempt_always(1);
138 rt_task_delete(main_task->
rtaitask);
139 free(main_task->
name);
140 main_task->
name = NULL;
147 void *(*wrapper)(
void*);
160 void*(*wrapper)(
void*) = d->
wrapper;
163 if (!(task->
rtaitask = rt_task_init(tnum, priority, 0, 0))) {
164 std::cerr <<
"CANNOT INIT LXRT Thread " << task->
name <<
std::endl;
165 std::cerr <<
"Exiting this thread." <<
std::endl;
170 struct sched_param param;
171 param.sched_priority = sched_get_priority_max(SCHED_OTHER);
172 if (param.sched_priority != -1 )
173 sched_setscheduler( 0, SCHED_OTHER, ¶m);
179 rt_make_hard_real_time();
184 rt_make_soft_real_time();
194 unsigned cpu_affinity,
198 void * (*start_routine)(
void *),
202 if ( strlen(name) == 0 )
204 strncpy(taskName, name, 7);
205 unsigned long task_num = nam2num( taskName );
206 if ( rt_get_adr(nam2num( taskName )) != 0 ) {
207 unsigned long nname = nam2num( taskName );
208 while ( rt_get_adr( nname ) != 0 )
210 num2nam( nname, taskName);
216 task->
name = strcpy( (
char*)malloc( (strlen(name)+1)*
sizeof(
char) ), name);
231 int retv = pthread_create(&(task->
thread), 0,
235 while ( task->
rtaitask == 0 && ++timeout < 20)
237 return timeout < 20 ? retv : -1;
248 RT_TASK*
self = rt_buddy();
264 log(
Error) <<
"Unknown scheduler type." <<endlog();
278 rt_make_hard_real_time();
280 rt_make_soft_real_time();
285 if ( rt_is_hard_real_time( t->
rtaitask ) )
294 if (rt_buddy() == mytask->
rtaitask) {
304 rt_task_suspend( mytask->
rtaitask );
309 rt_task_suspend( mytask->
rtaitask );
310 rt_task_make_periodic_relative_ns(mytask->
rtaitask, 0, nanosecs);
319 rt_set_period(mytask->
rtaitask, 0 );
321 rt_set_period(mytask->
rtaitask, nano2count( nanosecs ));
335 rt_task_wait_period();
340 if ( pthread_join((mytask->
thread),0) != 0 )
343 free( mytask->
name );
357 log(
Warning) <<
"Forcing priority ("<<*priority<<
") of thread to 0." <<endlog();
361 if (*priority > 255){
362 log(
Warning) <<
"Forcing priority ("<<*priority<<
") of thread to 255." <<endlog();
377 rv = rt_change_prio( mytask->
rtaitask, priority);
387 return t->
name ? t->
name :
"(destroyed)";
int rtos_task_is_self(const RTOS_TASK *task)
Returns 1 when task is the task struct of the thread calling this function, 0 otherwise.
int rtos_task_get_priority(const RTOS_TASK *task)
Return the priority of a thread.
INTERNAL_QUAL void rtos_task_make_periodic(RTOS_TASK *mytask, NANO_TIME nanosecs)
This function is to inform the RTOS that a thread is switching between periodic or non-periodic execu...
INTERNAL_QUAL int rtos_task_check_priority(int *scheduler, int *priority)
This function checks (and corrects) a given priority within a given scheduler type.
INTERNAL_QUAL void * rtai_thread_wrapper(void *arg)
unsigned int rtos_task_get_pid(const RTOS_TASK *task)
Returns the process ID the OS gave to the task task.
INTERNAL_QUAL int rtos_task_delete_main(RTOS_TASK *main_task)
Cleanup the main thread.
int rtos_task_set_scheduler(RTOS_TASK *t, int sched_type)
Set the scheduler of a given task t to a the type sched_type.
int usleep(unsigned int us)
int rtos_task_set_cpu_affinity(RTOS_TASK *task, unsigned cpu_affinity)
Set the cpu affinity of a thread.
unsigned rtos_task_get_cpu_affinity(const RTOS_TASK *task)
Return the cpu affinity of a thread.
basic_ostreams & endl(basic_ostreams &s)
Flush and newline.
void *(* wrapper)(void *)
A thread which is being run.
void rtos_task_set_wait_period_policy(RTOS_TASK *task, int policy)
Set the wait policy of a thread.
void rtos_task_delete(RTOS_TASK *mytask)
This function must join the thread created with rtos_task_create and then clean up the RTOS_TASK stru...
int rtos_task_set_priority(RTOS_TASK *task, int priority)
Set the priority of a thread.
static std::ostream & endl(std::ostream &__os)
const char * rtos_task_get_name(const RTOS_TASK *task)
Returns the name by which a task is known in the RTOS.
int rtos_task_check_scheduler(int *sched_type)
This function checks (and corrects) if the given sched_type is valid for this RTOS.
int rtos_task_wait_period(RTOS_TASK *task)
This function is called by a periodic thread which wants to go to sleep and wake up the next period...
INTERNAL_QUAL void rtos_task_yield(RTOS_TASK *)
Yields the current thread.
static Logger & log()
As Instance(), but more userfriendly.
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
INTERNAL_QUAL int rtos_task_create_main(RTOS_TASK *main_task)
Initialise the main thread.
void rtos_task_set_period(RTOS_TASK *mytask, NANO_TIME nanosecs)
Change the period of a periodic RTOS task.
RTOS_RTAI_TASK * rtaitask
INTERNAL_QUAL int rtos_task_create(RTOS_TASK *task, int priority, unsigned cpu_affinity, const char *name, int sched_type, size_t stack_size, void *(*start_routine)(void *), ThreadInterface *obj)
Create a thread.
int rtos_task_get_scheduler(const RTOS_TASK *t)
Returns the current scheduler set for task t.