static void *rt_BaseRate(void *args) { char name[7]; int i; static RTIME t0; for (i = 0; i < MAX_NTARGETS; i++) { sprintf(name,"BRT%d",i); if (!rt_get_adr(nam2num(name))) break; } if (!(rt_BaseRateTask = rt_task_init_schmod(nam2num(name), *((int *)args), 0, 0, SCHED_FIFO, CpuMap))) { fprintf(stderr,"Cannot init rt_BaseRateTask.\n"); return (void *)1; } sem_post(&err_sem); iopl(3); rt_task_use_fpu(rt_BaseRateTask, 1); MXmain(); grow_and_lock_stack(stackinc); if (UseHRT) { rt_make_hard_real_time(); } rt_rpc(rt_MainTask, 0, (void *)name); t0 = rt_get_cpu_time_ns(); rt_task_make_periodic(rt_BaseRateTask, rt_get_time() + rt_BaseRateTick, rt_BaseRateTick); while (!endBaseRate) { #ifdef TASKDURATION RTTSKper=rt_get_cpu_time_ns()-RTTSKinit; #endif WaitTimingEvent(TimingEventArg); if (endBaseRate) break; APPLICATION_Process_Event(TIME_EV); SIM_TIME = (rt_get_cpu_time_ns() - t0)*1.0E-9; #ifdef TASKDURATION RTTSKinit=rt_get_cpu_time_ns(); #endif } if (UseHRT) { rt_make_soft_real_time(); } rt_task_delete(rt_BaseRateTask); return 0; }
void *ThreadImplLxrt33::runThread(void *arg) { ThreadImplLxrt33 *self = static_cast<ThreadImplLxrt33*>(arg); self->m_rt_task = rt_task_init(getpid() + pthread_self_rt(), abs(self->m_priority), DEFAULT_STACK_SIZE, 0); if (self->m_rt_task == NULL) { PRINTF("ERROR: Cannot initialize LXRT task %lu!\n", self->m_thread_id); PRINTF(" Probably another thread with the same name already exists.\n"); } else { rt_task_use_fpu(self->m_rt_task, 1); if (self->m_priority < 0) { rt_make_hard_real_time(); if (!rt_is_hard_real_time(rt_buddy())) { PRINTF("ERROR: Setting thread %lu to hard real-time failed!\n", self->m_thread_id); } else { // Everything worked as expected, so no message here. } } else { // This is a soft realtime thread, so nothing additional has to // be done here. } self->m_thread->runThread(); rt_make_soft_real_time(); // TODO: Check if this is correct. The RTAI 3.5 and 3.8 // implementations leave this to a call to join(). rt_task_delete(self->m_rt_task); self->m_rt_task = NULL; } return NULL; }
INTERNAL_QUAL void* rtai_thread_wrapper( void * arg ) { RTAI_Thread* d = (RTAI_Thread*)arg; RTOS_TASK* task = d->task; void* data = d->data; int priority = d->priority; unsigned int tnum = d->tnum; void*(*wrapper)(void*) = d->wrapper; free( d ); if (!(task->rtaitask = rt_task_init(tnum, priority, 0, 0))) { std::cerr << "CANNOT INIT LXRT Thread " << task->name <<std::endl; std::cerr << "Exiting this thread." <<std::endl; exit(-1); } // Schedule in Linux' SCHED_OTHER struct sched_param param; param.sched_priority = sched_get_priority_max(SCHED_OTHER); if (param.sched_priority != -1 ) sched_setscheduler( 0, SCHED_OTHER, ¶m); // Avoid the LXRT CHANGED MODE (TRAP), PID = 4088, VEC = 14, SIGNO = 11. warning rt_task_use_fpu(task->rtaitask, 1); // New default: new threads are always hard. rt_make_hard_real_time(); data = wrapper( data ); // Exit in soft mode to avoid RTAI warnings. rt_make_soft_real_time(); // cleanup here to avoid "LXRT Releases PID" warnings. rt_task_delete(task->rtaitask); task->rtaitask = 0; // See rtos_task_delete for further cleanups. return data; }
INTERNAL_QUAL int rtos_task_create_main(RTOS_TASK* main_task) { if ( geteuid() != 0 ) { std::cerr << "You are not root. This program requires that you are root." << std::endl; exit(1); } #ifdef OROSEM_OS_LOCK_MEMORY int locktype = MCL_CURRENT; #ifdef OROSEM_OS_LOCK_MEMORY_FUTURE locktype |= MCL_FUTURE; #endif // locking of all memory for this process int rv = mlockall(locktype); if ( rv != 0 ) { perror( "rtos_task_create_main: Could not lock memory using mlockall" ); // Logger unavailable. } #endif /* check to see if rtai_lxrt module is loaded */ // struct module_info modInfo; // size_t retSize; // if ( query_module("rtai_lxrt", QM_INFO, &modInfo, // sizeof(modInfo), &retSize) != 0 ) { // std::cerr <<"It appears the rtai_lxrt module is not loaded !"<<std::endl; // exit(); // } unsigned long name = nam2num("main"); while ( rt_get_adr( name ) != 0 ) // check for existing 'MAINTHREAD' ++name; const char* tname = "main"; main_task->name = strcpy( (char*)malloc( (strlen(tname) + 1) * sizeof(char)), tname); if( !(main_task->rtaitask = rt_task_init(name, 10,0,0)) ) // priority, stack, msg_size { std::cerr << "Cannot rt_task_init() MainThread." << std::endl; exit(1); } struct sched_param param; param.sched_priority = sched_get_priority_max(SCHED_OTHER); if (param.sched_priority != -1 ) sched_setscheduler( 0, SCHED_OTHER, ¶m); // Avoid the LXRT CHANGED MODE (TRAP), PID = 4088, VEC = 14, SIGNO = 11. warning rt_task_use_fpu(main_task->rtaitask, 1); #ifdef OROSEM_OS_LXRT_PERIODIC rt_set_periodic_mode(); start_rt_timer( nano2count( NANO_TIME(ORODAT_OS_LXRT_PERIODIC_TICK*1000*1000*1000) ) ); Logger::log() << Logger::Info << "RTAI Periodic Timer ticks at "<<ORODAT_OS_LXRT_PERIODIC_TICK<<" seconds." << Logger::endl; #else // BE SURE TO SET rt_preempt_always(1) when using one shot mode rt_set_oneshot_mode(); // only call this function for RTAI 3.0 or older #if defined(CONFIG_RTAI_VERSION_MINOR) && defined(CONFIG_RTAI_VERSION_MAJOR) # if CONFIG_RTAI_VERSION_MAJOR == 3 && CONFIG_RTAI_VERSION_MINOR == 0 rt_preempt_always(1); # endif #else rt_preempt_always(1); #endif start_rt_timer(0); Logger::log() << Logger::Info << "RTAI Periodic Timer runs in preemptive 'one-shot' mode." << Logger::endl; #endif Logger::log() << Logger::Debug << "RTAI Task Created" << Logger::endl; return 0; }
int main(void) { char s[BUFSIZE]; int i, fd; long long ll; float f; double d; RT_TASK *mytask; RTIME t; struct timeval timout; struct timespec tns; if (!(mytask = rt_task_init_schmod(nam2num("HRTSK"), 0, 0, 0, SCHED_FIFO, 0x1))) { printf("CANNOT INIT TEST BUDDY TASK\n"); exit(1); } rt_sync_async_linux_syscall_server_create(NULL, SYNC_LINUX_SYSCALL, NULL, NRECS + 1); rt_grow_and_lock_stack(40000); rt_make_hard_real_time(); rt_task_use_fpu(mytask, 1); #if 1 printf("(SCANF) Input a: string, integer, long long, float, double: "); scanf("%s %i %lld %f %lf", s, &i, &ll, &f, &d); printf("(SELECT) Got string, integer, long long, now we wait %d s using select.\n", WAITIME); timout.tv_sec = WAITIME; timout.tv_usec = 0; select(1, NULL, NULL, NULL, &timout); printf("(PRINTF) Select expired and we print what we read: %s %i %lld %f %lf\n", s, i, ll, f, d); printf("(OPEN) Open a file.\n"); fd = open("rtfile", O_RDWR | O_CREAT | O_TRUNC, 0666); printf("(WRITE) Write a %d bytes header of 'B's.\n", HDRSIZE); memset(s, 'B', HDRSIZE); write(fd, s, HDRSIZE); memset(s, 'A', BUFSIZE); printf("(WRITE) Write %d records of %d 'A's to the opened file.\n", NRECS, BUFSIZE); #if SYNC_ASYNC == ASYNC_LINUX_SYSCALL printf("!!!!! The writing will be executed asynchronously !!!!!\n"); #else printf("!!!!! The writing will be executed synchronously !!!!!\n"); #endif rt_set_linux_syscall_mode(SYNC_ASYNC, NULL); t = rt_get_cpu_time_ns(); for (i = 0; i < NRECS; i++) { write(fd, s, BUFSIZE); } rt_set_linux_syscall_mode(SYNC_LINUX_SYSCALL, NULL); printf("(PRINTF) WRITE TIME: %lld (ms).\n", (rt_get_cpu_time_ns() - t + 500000)/1000000); printf("(WRITE) Write a %d bytes trailer of 'E's.\n", HDRSIZE); memset(s, 'E', HDRSIZE); write(fd, s, HDRSIZE); printf("(SYNC) Sync file to disk.\n"); sync(); printf("(LSEEK) Position file at its beginning.\n"); lseek(fd, 0, SEEK_SET); printf("(POLL) Got the beginning, now we wait %d s using poll.\n", WAITIME); poll(0, 0, WAITIME*1000); printf("(READ) Poll expired, read the first %d bytes (header + first 'A').\n", HDRSIZE + 1); read(fd, s, HDRSIZE + 1); s[HDRSIZE + 1] = 0; printf("(READ) Here is the header %s.\n", s); lseek(fd, -1, SEEK_CUR); printf("(READ) Read the written %d MB of 'A's back.\n", BUFSIZE*NRECS); #if SYNC_ASYNC == ASYNC_LINUX_SYSCALL printf("!!!!! The reading will be executed asynchronously !!!!!\n"); #else printf("!!!!! The reading will be executed asynchronously !!!!!\n"); #endif rt_set_linux_syscall_mode(ASYNC_LINUX_SYSCALL, NULL); t = rt_get_cpu_time_ns(); for (i = 0; i < NRECS; i++) { read(fd, s, BUFSIZE); } rt_set_linux_syscall_mode(SYNC_LINUX_SYSCALL, NULL); printf("(PRINTF) READ TIME %lld (ms).\n", (rt_get_cpu_time_ns() - t + 500000)/1000000); lseek(fd, -1, SEEK_CUR); printf("(READ) Read the last %d bytes (last 'A' + trailer).\n", HDRSIZE + 1); read(fd, s, HDRSIZE + 1); s[HDRSIZE + 1] = 0; printf("(READ) Here is the trailer %s.\n", s); printf("(CLOSE) Close the file and end the test.\n"); close(fd); printf("(NANOWAITIME) File closed, let's wait %d s using nanosleep.\n", WAITIME); tns.tv_sec = WAITIME; tns.tv_nsec = 0; nanosleep(&tns, NULL); printf("(PRINTF) Test done, exiting.\n"); #endif rt_make_soft_real_time(); rt_task_delete(mytask); return 0; }