static int init_module(void) { int i, srvport; while ((srvport = rt_request_hard_port(tasknode)) <= 0) { msleep(100); } printf("START TASK GOT SYNC TASKNODE PORT %lx, %d\n", tasknode, srvport); rt_make_hard_real_time(); while (!RT_get_adr(tasknode, srvport, task[NUM_TASKS - 1])) { msleep(100); } rt_make_soft_real_time(); printf("START TASK REL SYNC TASKNODE PORT %lx, %d\n", tasknode, srvport); rt_release_port(tasknode, srvport); srvport = rt_request_hard_port(comnode); printf("START TASK GOT INIT COMNODE PORT %lx, %d\n", comnode, srvport); rt_make_hard_real_time(); print_sem = RT_get_adr(comnode, srvport, "PRTSEM"); sync_sem = RT_get_adr(comnode, srvport, "SYNCSM"); prio_sem = RT_get_adr(comnode, srvport, "PRIOSM"); end_sem = RT_get_adr(comnode, srvport, "ENDSEM"); mbx_in = RT_get_adr(comnode, srvport, "MBXIN"); mbx_out = RT_get_adr(comnode, srvport, "MBXOUT"); for (i = 0; i < NUM_TASKS; ++i) { sems[i] = RT_get_adr(comnode, srvport, sem[i]); } printf("START TASK REL INIT COMNODE PORT %lx, %d\n", comnode, srvport); rt_make_soft_real_time(); rt_release_port(comnode, srvport); thread = rt_thread_create(start_task_code, NULL, 10000); return 0; }
int main(void) { int i, srq, count = 0, nextcount = 0, repeat; rt_thread_init(nam2num("MNTSK"), 100, 0, SCHED_FIFO, 0x1); rt_printk("\nTESTING THE SCHEDULER WITH SRQs [%d LOOPs].\n", LOOPS); repeat = 1000000000LL/((long long)DELAY*(long long)PRINT_FREQ); srq = rtai_open_srq(0xcacca); start_rt_timer(0); rt_grow_and_lock_stack(100000); #ifdef MAKE_HARD MAKE_HARD(); #endif for (i = 0; i < LOOPS; i++) { rtai_srq(srq, (unsigned long)nano2count(DELAY)); if (++count > nextcount) { nextcount += repeat; rt_printk(">>> %d.\n", nextcount); } } rt_make_soft_real_time(); stop_rt_timer(); rt_task_delete(NULL); rt_printk("END SCHEDULER TEST WITH SRQs.\n\n"); return 0; }
int main(void) { RT_TASK *spktsk, *plrtsk; RTIME period; char buf[BUFSIZE], data, temp; unsigned int msg, i, len; printf("\n\nGENERIC RECEIVE\n"); ioperm(PORT_ADR, 1, 1); iopl(3); if (!(spktsk = rt_task_init_schmod(nam2num("SPKTSK"), 1, 0, 0, SCHED_FIFO, 0xF))) { printf("CANNOT INIT SPEAKER TASK\n"); exit(1); } rt_set_oneshot_mode(); start_rt_timer(0); mlockall(MCL_CURRENT | MCL_FUTURE); printf("\nSPEAKER TASK RUNNING\n"); rt_make_hard_real_time(); period = nano2count(PERIOD); rt_task_make_periodic(spktsk, rt_get_time() + 5*period, period); rt_returnx(rt_receivex(0, &msg, sizeof(int), &len), &msg, 1); for (i = 0; i < 100; i++) { rt_returnx(rt_receivex(0, &msg, sizeof(int), &len), &msg, 1); } len = 0; while(1) { if (len) { data = filter(buf[i++]); temp = inb(PORT_ADR); temp &= 0xfd; temp |= (data & 1) << 1; outb(temp, PORT_ADR); len--; } else { if (rt_evdrpx(0, buf, BUFSIZE, &i)) { // rt_printk("EVDRP %d\n", i); } if ((plrtsk = rt_receivex_if(0, buf, BUFSIZE, &len))) { rt_returnx(plrtsk, &len, sizeof(int)); if (len == sizeof(int) && ((int *)buf)[0] == 0xFFFFFFFF) { break; } i = 0; } } rt_task_wait_period(); } rt_sleep(nano2count(100000000)); rt_make_soft_real_time(); stop_rt_timer(); rt_task_delete(spktsk); printf("\nSPEAKER TASK STOPS\n"); return 0; }
int main(int argc, char *argv[]) { int fifo, period, skip, average = 0; RT_TASK *task; RTIME expected; if ((fifo = open("/dev/rtf0", O_WRONLY)) < 0) { printf("Error opening FIFO0 in UCAL\n"); exit(1); } if (!(task = rt_task_init_schmod(nam2num("UCAL"), 0, 0, 0, SCHED_FIFO, 0xF))) { printf("Cannot init UCAL\n"); exit(1); } rt_set_oneshot_mode(); period = start_rt_timer(nano2count(atoi(argv[1]))); mlockall(MCL_CURRENT | MCL_FUTURE); rt_make_hard_real_time(); expected = rt_get_time() + 100*period; rt_task_make_periodic(task, expected, period); for (skip = 0; skip < atoi(argv[2]); skip++) { expected += period; rt_task_wait_period(); average += (int)count2nano(rt_get_time() - expected); } rt_make_soft_real_time(); stop_rt_timer(); rt_task_delete(task); write(fifo, &average, sizeof(average)); close(fifo); exit(0); }
static void bfun(int t) { RT_TASK *mytask; RT_MSGQ *smbx, *rmbx[NTASKS]; int msg[MAXSIZ + 1], mtype, i, n; mytask = rt_thread_init(nam2num("BFUN"), 1, 0, SCHED_FIFO, 0xF); smbx = rt_msgq_init(nam2num("SMSG"), NTASKS, 0); for (i = 0; i < NTASKS; i++) { char mname[6] = "RMBX"; mname[4] = i + '0'; mname[5] = 0; rmbx[i] = rt_msgq_init(nam2num(mname), 1, 0); } mlockall(MCL_CURRENT | MCL_FUTURE); rt_make_hard_real_time(); while (!end) { rt_msg_receive(smbx, msg, sizeof(msg), &mtype); n = 0; for (i = 1; i < MAXSIZ; i++) { n += msg[i]; } if (msg[MAXSIZ] != n) { rt_printk("SERVER RECEIVED AN UNKNOWN MSG.\n"); goto prem; } msg[1] = 0xFFFFFFFF; rt_msg_send(rmbx[msg[0]], msg, 2*sizeof(int), 1); } prem: rt_make_soft_real_time(); rt_task_delete(mytask); printf("SERVER TASK ENDS.\n"); }
//---------------------------------------------------------------------------- // real time task void writing_task_proc(void *arg) { #ifdef RTAI writing_task = rt_task_init_schmod(nam2num("WRTSK"),2, 0, 0, SCHED_FIFO, 0xF); rt_make_hard_real_time(); #endif while (1) { std::list<TPCANMsg>::iterator iter; for (iter = List->begin(); iter != List->end(); iter++) { // test for standard frames only if ((nExtended == CAN_INIT_TYPE_EX) || !(iter->MSGTYPE & MSGTYPE_EXTENDED)) { // send the message if ((errno = CAN_Write(h, &(*iter)))) shutdownnow = 1; } if (shutdownnow == 1) break; } if (shutdownnow == 1) break; } #ifdef RTAI rt_make_soft_real_time(); rt_task_delete(writing_task); #endif }
int main(void) { RT_TASK *task; signal(SIGTERM, catch_signal); signal(SIGINT, catch_signal); if (!(task = rt_thread_init(nam2num("SWITCH"), 0, 0, SCHED_FIFO, 0xF))) { printf("CANNOT INIT SWITCH TASK SIGNAL\n"); exit(1); } start_rt_timer(0); rt_request_signal(SWITCH_SIGNAL, switch_handler); rt_task_signal_handler(task, (void *)SWITCH_SIGNAL); rt_make_hard_real_time(); while (!end) { rt_sleep(nano2count(PERIOD)); } rt_task_signal_handler(task, NULL); rt_release_signal(SWITCH_SIGNAL, task); rt_make_soft_real_time(); stop_rt_timer(); rt_task_delete(task); return 0; }
static void *fast_fun(void *arg) { int jit, period; RTIME expected; if (!(Fast_Task = rt_thread_init(nam2num("FSTSK"), 2, 0, SCHED_FIFO, CPUMAP))) { printf("CANNOT INIT FAST TASK\n"); exit(1); } mlockall(MCL_CURRENT | MCL_FUTURE); rt_make_hard_real_time(); rt_sem_wait_barrier(barrier); period = nano2count(FASTMUL*TICK_TIME); expected = start + 6*nano2count(TICK_TIME); rt_task_make_periodic(Fast_Task, expected, period); while (!end) { jit = abs(count2nano(rt_get_time() - expected)); if (jit > fastjit) { fastjit = jit; } rt_busy_sleep((FASTMUL*TICK_TIME*USEDFRAC)/100); expected += period; END("FE\n"); rt_task_wait_period(); BEGIN("FB\n"); } rt_sem_wait_barrier(barrier); rt_make_soft_real_time(); rt_thread_delete(Fast_Task); return 0; }
void *signalSan(void *arg) { RT_TASK *Task_3; unsigned long Task_3_name = nam2num("TSK_3") + i++; time_t aclock; time_t clockNow; int timeEx, begin_3; int count = 0; struct tm *newtime; Task_3 = rt_task_init(Task_3_name, 0, 0, 0); // if(!(Task_3 = rt_task_init_schmod(Task_3_name,3,0,0,SCHED_FIFO,1))) { // printf("CANNOT INIT HANDLER TASK > Task 3 <\n"); // exit(1); // } rt_allow_nonroot_hrt(); rt_make_hard_real_time(); rt_task_make_periodic(Task_3, rt_get_time(), sampling * 30); rt_change_prio(Task_3, 3); begin_3 = begin; while (count < 20) { rt_sem_wait(rMutex); time(&aclock); // Pega tempo em segundos. newtime = localtime(&aclock); printf(" Signal 3 =======> %s", asctime(newtime)); sleep(1); time(&aclock); // Pega tempo em segundos. newtime = localtime(&aclock); printf(" Signal 3 after Sleep =======> %s", asctime(newtime)); timeEx = 3600 * newtime->tm_hour + 60 * newtime->tm_min + newtime->tm_sec; if( (timeEx - begin_3) > 15 ) printf(" Time Failure of the Signal 3\n"); else printf(" Time Correct of the Signal 3\n"); begin_3 = timeEx + (15 - (timeEx-begin)%15); rt_sem_signal(rMutex); rt_task_wait_period(); count++; } rt_make_soft_real_time(); rt_task_delete(Task_3); return 0; }
void *ClockChrono_Clock(void *args) { RT_TASK *mytask; const int hundredthes = FALSE; MenageHmsh_tHour hour; MenageHmsh_tChain11 hourChain; char command; BOOLEAN display; if (!(mytask = rt_thread_init(nam2num("CLOCK"), 1, 0, SCHED_FIFO, 0xF))) { printf("CANNOT INIT TASK ClockChronoClock\n"); exit(1); } printf("INIT TASK ClockChronoClock %p.\n", mytask); mlockall(MCL_CURRENT | MCL_FUTURE); rt_make_hard_real_time(); sem_wait(&sync_sem); MenageHmsh_Initialise(&hour); while(1) { CommandClock_Get(&command); switch(command) { case 'R': rt_fractionated_sleep(OneUnit); MenageHmsh_PlusOneUnit(&hour, &display); break; case 'T': MenageHmsh_InitialiseHundredthes(&hour); display = FALSE; break; case 'H': MenageHmsh_AdvanceHours(&hour); display = TRUE; break; case 'M': MenageHmsh_AdvanceMinutes(&hour); display = TRUE; break; case 'S': MenageHmsh_AdvanceSeconds(&hour); display = TRUE; break; case 'F': goto end; } if (display) { MenageHmsh_Convert(hour, hundredthes, &hourChain); Display_PutHour(hourChain); } } end: rt_make_soft_real_time(); hourChain.chain[1] = 'e'; Display_PutHour(hourChain); rt_task_delete(mytask); printf("END TASK ClockChronoClock %p.\n", mytask); return 0; }
int main(int argc, char *argv[]) { unsigned int player, msg, spknode, spkport, i; RT_TASK *plrtsk, *spktsk; struct sockaddr_in addr; char data[BUFSIZE]; thread = rt_thread_create(endme, NULL, 2000); if ((player = open("../../../share/linux.au", O_RDONLY)) < 0) { printf("ERROR OPENING SOUND FILE (linux.au)\n"); exit(1); } if (!(plrtsk = rt_task_init_schmod(nam2num("PLRTSK"), 2, 0, 0, SCHED_FIFO, 0xF))) { printf("CANNOT INIT PLAYER TASK\n"); exit(1); } spknode = 0; if (argc == 2 && strstr(argv[1], "SpkNode=")) { inet_aton(argv[1] + 8, &addr.sin_addr); spknode = addr.sin_addr.s_addr; } if (!spknode) { inet_aton("127.0.0.1", &addr.sin_addr); spknode = addr.sin_addr.s_addr; } while ((spkport = rt_request_hard_port(spknode)) <= 0 && spkport != -EINVAL); rt_make_hard_real_time(); spktsk = RT_get_adr(spknode, spkport, "SPKTSK"); RT_rpcx(spknode, spkport, spktsk, &i, &msg, sizeof(int), sizeof(int)); for (i = 0; i < 100; i++) { RT_rpcx(spknode, spkport, spktsk, &i, &msg, sizeof(int), sizeof(int)); } mlockall(MCL_CURRENT | MCL_FUTURE); printf("\nPLAYER TASK RUNNING\n(TYPE ENTER TO END EVERYTHING)\n"); while (!end) { lseek(player, 0, SEEK_SET); while(!end && (i = read(player, data, BUFSIZE)) > 0) { RT_rpcx(spknode, spkport, spktsk, data, &msg, i, sizeof(int)); if (msg != i) { printf("SPEAKER RECEIVED LESS THAN SENT BY PLAYER\n"); } } } msg = 0xFFFFFFFF; RT_rpcx(spknode, spkport, spktsk, &msg, &msg, sizeof(int), 1); rt_make_soft_real_time(); rt_release_port(spknode, spkport); rt_task_delete(plrtsk); close(player); printf("PLAYER TASK STOPS\n"); return 0; }
int main(void) { double omega = (2.0*M_PI*SIN_FREQ*SAMP_TIME)/1.0E9; RTIME until; RT_TASK *task; lsampl_t data[NCHAN*2]; long k, sinewave, retval = 0; signal(SIGKILL, endme); signal(SIGTERM, endme); start_rt_timer(0); task = rt_task_init_schmod(nam2num("MYTASK"), 1, 0, 0, SCHED_FIFO, 0xF); printf("COMEDI CMD TEST BEGINS: SAMPLING FREQ: %d, RUN TIME: %d.\n", SAMP_FREQ, RUN_TIME); if (init_board()) {; printf("Board initialization failed.\n"); return 1; } do_cmd(); mlockall(MCL_CURRENT | MCL_FUTURE); rt_make_hard_real_time(); until = rt_get_cpu_time_ns() + (long long)RUN_TIME*1000000000; for (k = 0; k < SAMP_FREQ*RUN_TIME && !end; k++) { sinewave = (long)(maxdata/4*sin(k*omega)); data[0] = (lsampl_t)( sinewave + maxdata/2); data[1] = (lsampl_t)(- sinewave + maxdata/2); while (rt_comedi_command_data_write(dev, subdev, NCHAN, data) != NCHAN) { rt_sleep(nano2count(SAMP_TIME/2)); } if (k == TRIGSAMP) { rt_comedi_trigger(dev, subdev); } } while (until > rt_get_cpu_time_ns()) { rt_sleep(nano2count(100000)); } comedi_cancel(dev, subdev); comedi_close(dev); comedi_data_write(dev, subdev, 0, 0, AREF_GROUND, 2048); comedi_data_write(dev, subdev, 1, 0, AREF_GROUND, 2048); printf("COMEDI TEST ENDS.\n"); if (retval < 0) { printf("rt_comedi_wait_timed overruns: %d\n", abs(retval)); } stop_rt_timer(); rt_make_soft_real_time(); rt_task_delete(task); return 0; }
void *thread_fun(void *arg) { RTIME start_time, period; RTIME t0, t; SEM *sem; RT_TASK *mytask; unsigned long mytask_name; int mytask_indx, jit, maxj, maxjp, count; mytask_indx = *((int *)arg); mytask_name = taskname(mytask_indx); cpus_allowed = 1 - cpus_allowed; if (!(mytask = rt_task_init_schmod(mytask_name, 1, 0, 0, SCHED_FIFO, 1 << cpus_allowed))) { printf("CANNOT INIT TASK %lu\n", mytask_name); exit(1); } printf("THREAD INIT: index = %d, name = %lu, address = %p.\n", mytask_indx, mytask_name, mytask); mlockall(MCL_CURRENT | MCL_FUTURE); if (!(mytask_indx%2)) { rt_make_hard_real_time(); } rt_receive(0, (unsigned long *)((void *)&sem)); period = nano2count(PERIOD); start_time = rt_get_time() + nano2count(10000000); rt_task_make_periodic(mytask, start_time + (mytask_indx + 1)*period, ntasks*period); // start of task body { count = maxj = 0; t0 = rt_get_cpu_time_ns(); while(count++ < LOOPS) { rt_task_wait_period(); t = rt_get_cpu_time_ns(); if ((jit = t - t0 - ntasks*(RTIME)PERIOD) < 0) { jit = -jit; } if (count > 1 && jit > maxj) { maxj = jit; } t0 = t; // rtai_print_to_screen("THREAD: index = %d, count %d\n", mytask_indx, count); } maxjp = (maxj + 499)/1000; } // end of task body rt_sem_signal(sem); if (!(mytask_indx%2)) { rt_make_soft_real_time(); } rt_task_delete(mytask); printf("THREAD %lu ENDS, LOOPS: %d MAX JIT: %d (us)\n", mytask_name, count, maxjp); return 0; }
static void *latency_fun(void *arg) { struct sample { long min, max, avrg, jitters[2]; } samp; int diff; int skip; int average; int min_diff; int max_diff; int period; RT_TASK *chktsk; RTIME expected; min_diff = 1000000000; max_diff = -1000000000; if (!(Latency_Task = rt_thread_init(nam2num("PRETSK"), 0, 0, SCHED_FIFO, CPUMAP))) { printf("CANNOT INIT LATENCY TASK\n"); exit(1); } mlockall(MCL_CURRENT | MCL_FUTURE); rt_make_hard_real_time(); rt_sem_wait_barrier(barrier); period = nano2count(TICK_TIME); expected = start + 3*period; rt_task_make_periodic(Latency_Task, expected, period); while (!end) { average = 0; for (skip = 0; skip < NAVRG && !end; skip++) { expected += period; END("HE\n"); rt_task_wait_period(); BEGIN("HB\n"); diff = count2nano(rt_get_time() - expected); if (diff < min_diff) { min_diff = diff; } if (diff > max_diff) { max_diff = diff; } average += diff; } samp.min = min_diff; samp.max = max_diff; samp.avrg = average/NAVRG; samp.jitters[0] = fastjit; samp.jitters[1] = slowjit; if ((chktsk = rt_get_adr(nam2num("PRECHK")))) { rt_sendx_if(chktsk, &samp, sizeof(samp)); } } rt_sem_wait_barrier(barrier); rt_make_soft_real_time(); rt_thread_delete(Latency_Task); return 0; }
void delete_tasks(void) { rt_make_soft_real_time(); rt_task_delete(Task_1); rt_task_delete(Task_2); rt_task_delete(Task_3); rt_task_delete(Task_4); printf("\nFim do Escalonamento\n"); }
void *ThreadImplLxrt35::runThread(void *arg) { ThreadImplLxrt35 *self = static_cast<ThreadImplLxrt35*>(arg); if (self->m_rt_start_sync == NULL) { // Technically, this can never happen because this condition is // already checked in the Start() function. But who knows! PRINTF("ERROR: NULL thread start barrier!\n"); } else { 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"); // Let the thread, which started us, continue! pthread_barrier_wait_rt(self->m_rt_start_sync); } else { 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. } pthread_barrier_wait_rt(self->m_rt_start_sync); self->m_thread->runThread(); // Remark: It does not hurt to call this in a soft realtime // thread, so just skip the hard realtime test. rt_make_soft_real_time(); } } return NULL; }
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 delete_tasks(void) { int i; rt_make_soft_real_time(); for (i = 0; i < NTASKS; i++) { rt_task_delete(arrayTasks[i]); } printf("\nFim do Escalonamento %s\n", texto_preto); }
int main(void) { RT_TASK *spktsk, *plrtsk; RTIME period; MBX *mbx; char data, temp; unsigned int msg, i; // ioperm(PORT_ADR, 1, 1); iopl(3); if (!(spktsk = rt_task_init_schmod(nam2num("SPKTSK"), 1, 0, 0, SCHED_FIFO, 0xF))) { printf("CANNOT INIT SPEAKER TASK\n"); exit(1); } mbx = rt_mbx_init(nam2num("SNDMBX"), 4000); printf("\nSPEAKER TASK RUNNING\n"); rt_set_oneshot_mode(); start_rt_timer(0); mlockall(MCL_CURRENT | MCL_FUTURE); rt_make_hard_real_time(); period = nano2count(PERIOD); rt_task_make_periodic(spktsk, rt_get_time() + 5*period, period); for (i = 0; i < 100; i++) { plrtsk = rt_receive(0, &msg); rt_return(plrtsk, msg); } while(1) { if (!rt_mbx_receive_if(mbx, &data, 1)) { data = filter(data); temp = inb(PORT_ADR); temp &= 0xfd; temp |= (data & 1) << 1; outb(temp, PORT_ADR); } rt_task_wait_period(); if ((plrtsk = rt_receive_if(0, &msg))) { rt_return(plrtsk, msg); break; } } rt_sleep(nano2count(100000000)); rt_make_soft_real_time(); rt_mbx_delete(mbx); stop_rt_timer(); rt_task_delete(spktsk); printf("\nSPEAKER TASK STOPS\n"); return 0; }
INTERNAL_QUAL int rtos_task_set_scheduler(RTOS_TASK* t, int s) { if ( t->rtaitask == 0 || t->rtaitask != rt_buddy() ) { return -1; } if (rtos_task_check_scheduler(&s) == -1) return -1; if (s == SCHED_LXRT_HARD) rt_make_hard_real_time(); else if ( s == SCHED_LXRT_SOFT) rt_make_soft_real_time(); return 0; }
int main(int argc, char *argv[]) { unsigned long rcvnode; RT_TASK *sndtsk, *rcvtsk; MBX *mbx; long rcvport, i; struct sockaddr_in addr; thread = rt_thread_create(endme, NULL, 2000); if (!(sndtsk = rt_task_init_schmod(nam2num("SNDTSK"), 1, 0, 0, SCHED_FIFO, 0xF))) { printf("CANNOT INIT SENDER TASK\n"); exit(1); } rcvnode = 0; if (argc == 2 && strstr(argv[1], "RcvNode=")) { inet_aton(argv[1] + 8, &addr.sin_addr); rcvnode = addr.sin_addr.s_addr; } if (!rcvnode) { inet_aton("127.0.0.1", &addr.sin_addr); rcvnode = addr.sin_addr.s_addr; } mbx = rt_mbx_init(nam2num("SNDMBX"), 500); while ((rcvport = rt_request_port(rcvnode)) <= 0 && rcvport != -EINVAL); printf("\nSENDER TASK RUNNING\n"); mlockall(MCL_CURRENT | MCL_FUTURE); rt_make_hard_real_time(); for (i = 0; i < MAXLOOPS && !end; i++) { rt_mbx_send(mbx, &i, sizeof(long)); rt_printk("SENT %ld\n", i); RT_sleep(rcvnode, rcvport, 200000000); } i = -1; rt_mbx_send(mbx, &i, sizeof(long)); rt_make_soft_real_time(); while (!(rcvtsk = RT_get_adr(rcvnode, rcvport, "RCVTSK"))) { RT_sleep(rcvnode, rcvport, 100000000); } RT_rpc(rcvnode, rcvport, rcvtsk, i, &i); rt_release_port(rcvnode, rcvport); rt_mbx_delete(mbx); rt_task_delete(sndtsk); printf("\nSENDER TASK STOPS\n"); return 0; }
static void *intr_handler(void *args) { RT_TASK *mytask, *master; RTIME period; MBX *mbx; char data = 'G'; char temp; unsigned int msg; pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); // rt_allow_nonroot_hrt(); ioperm(PORT_ADR, 1, 1); if (!(mytask = rt_task_init_schmod(nam2num("SOUND"), 1, 0, 0, SCHED_FIFO, 0xF))) { printf("CANNOT INIT SOUND TASK\n"); exit(1); } mbx = rt_get_adr(nam2num("SNDMBX")); mlockall(MCL_CURRENT | MCL_FUTURE); printf("\nINIT SOUND TASK %p\n", mytask); rt_make_hard_real_time(); period = nano2count(PERIOD); rt_mbx_send(mbx, &data, 1); rt_task_make_periodic(mytask, rt_get_time() + 100*period, period); while(1) { if (!rt_mbx_receive_if(mbx, &data, 1)) { data = filter(data); temp = inb(PORT_ADR); temp &= 0xfd; temp |= (data & 1) << 1; outb(temp, PORT_ADR); } rt_task_wait_period(); if ((master = rt_receive_if(0, &msg))) { rt_return(master, msg); break; } } rt_make_soft_real_time(); rt_task_delete(mytask); printf("\nEND SOUND TASK %p\n", mytask); return 0; }
int main(int argc, char *argv[]) { RT_TASK *task; struct sockaddr_in addr; int i, srvport; if (!(task = rt_task_init(nam2num("STRTSK"), 0, 0, 0))) { printf("CANNOT INIT START_TASK TASK\n"); exit(1); } comnode = tasknode = 0; for (i = 0; i < argc; i++) { if (strstr(argv[i], "ComNode=")) { inet_aton(argv[i] + 8, &addr.sin_addr); comnode = addr.sin_addr.s_addr; argv[i] = 0; continue; } if (strstr(argv[i], "TaskNode=")) { inet_aton(argv[i] + 9, &addr.sin_addr); tasknode = addr.sin_addr.s_addr; argv[i] = 0; continue; } } if (!comnode) { inet_aton("127.0.0.1", &addr.sin_addr); comnode = addr.sin_addr.s_addr; } if (!tasknode) { inet_aton("127.0.0.1", &addr.sin_addr); tasknode = addr.sin_addr.s_addr; } rt_grow_and_lock_stack(100000); init_module(); rt_thread_join(thread); while ((srvport = rt_request_hard_port(comnode)) <= 0) { msleep(100); } rt_make_hard_real_time(); RT_sem_signal(comnode, srvport, end_sem); rt_make_soft_real_time(); rt_release_port(comnode, srvport); rt_task_delete(task); exit(0); }
static void mfun(int t) { char tname[6] = "MFUN", mname[6] = "RMBX"; RT_TASK *mytask; int msg[MAXSIZ + 1], mtype, i; void *smbx, *rmbx; randu(); tname[4] = mname[4] = t + '0'; tname[5] = mname[5] = 0; mytask = rt_thread_init(nam2num(tname), t + 1, 0, SCHED_FIFO, 0xF); smbx = rt_msgq_init(nam2num("SMSG"), 0, 0); rmbx = rt_msgq_init(nam2num(mname), 0, 0); mlockall(MCL_CURRENT | MCL_FUTURE); rt_make_hard_real_time(); msg[0] = t; rt_sem_wait_barrier(barrier); while (end < t) { msg[MAXSIZ] = 0; for (i = 1; i < MAXSIZ; i++) { msg[MAXSIZ] += (msg[i] = MAXSIZ*randu()); } if (rt_msg_send(smbx, msg, sizeof(msg), 1)) { rt_printk("SEND FAILED, TASK: %d\n", t); goto prem; } msg[0] = msg[1] = 0; if (rt_msg_receive(rmbx, msg, sizeof(msg), &mtype) < 0) { rt_printk("RECEIVE FAILED, TASK: %d\n", t); goto prem; } if (msg[0] != t || msg[1] != 0xFFFFFFFF || mtype != RETPRIO) { rt_printk("WRONG REPLY TO TASK: %d.\n", t); goto prem; } cnt[t]++; // rt_printk("TASK: %d, OK (%d).\n", t, cnt[t]); rt_sleep(nano2count(SLEEP_TIME)); } prem: rt_msgq_delete(rmbx); rt_msgq_delete(smbx); rt_make_soft_real_time(); rt_task_delete(mytask); printf("TASK %d ENDS.\n", t); }
bool ThreadImplLxrt35::setHardRealtime(bool hard_realtime) { if (hard_realtime && !os::isThisHRT()) { rt_make_hard_real_time(); return os::isThisHRT(); } else if (!hard_realtime && os::isThisHRT()) { rt_make_soft_real_time(); return !os::isThisHRT(); } else { return false; } }
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; }
static void *thread_fun(void *arg) { int mytask_indx; unsigned long msg; pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); mytask_indx = ((int *)arg)[0]; if (!(mytask[mytask_indx] = rt_thread_init(taskname(mytask_indx), 0, 0, SCHED_FIFO, 0x1))) { printf("CANNOT INIT TASK %u\n", taskname(mytask_indx)); exit(1); } mlockall(MCL_CURRENT | MCL_FUTURE); rt_make_hard_real_time(); hrt[mytask_indx] = 1; while (!end) { switch (change) { case 0: rt_task_suspend(mytask[mytask_indx]); break; case 1: rt_sem_wait(sem); break; case 2: rt_receive(NULL, &msg); break; case 3: rt_return(rt_receive(NULL, &msg), 0); break; } } rt_make_soft_real_time(); rt_task_delete(mytask[mytask_indx]); hrt[mytask_indx] = 0; return (void*)0; }
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; }
static void bfun(int t) { RT_TASK *mytask; int smbx, rmbx[NTASKS], msg[MAXSIZ + 1], mtype, i, n; mytask = rt_thread_init(nam2num("BFUN"), 1, 0, SCHED_FIFO, 0xF); smbx = rt_msgget(nam2num("SMSG"), 0); for (i = 0; i < NTASKS; i++) { char mname[6] = "RMBX"; mname[4] = i + '0'; mname[5] = 0; rmbx[i] = rt_msgget(nam2num(mname), 0); } mlockall(MCL_CURRENT | MCL_FUTURE); rt_make_hard_real_time(); rt_sem_wait_barrier(barrier); while (end < NTASKS) { rt_msgrcv_nu(smbx, &mtype, msg, sizeof(msg), 1, 0); n = 0; for (i = 1; i < MAXSIZ; i++) { n += msg[i]; } if (msg[MAXSIZ] != n) { rt_printk("SERVER RECEIVED AN UNKNOWN MSG.\n"); // goto prem; } if(end >= NTASKS) goto prem; msg[1] = 0xFFFFFFFF; rt_msgsnd_nu(rmbx[msg[0]], 1, msg, 2*sizeof(int), 0); } prem: rt_make_soft_real_time(); rt_task_delete(mytask); printf("SERVER TASK ENDS.\n"); }
static void* rt_system_thread(void * arg) { struct timeval tv; int64_t ts1, ts2; SEM * shm_sem; SEM * sync_sem; RT_TASK *task; M3EcSystemShm * sys = (M3EcSystemShm *)arg; printf("Starting real-time thread\n",0); RTIME t_last; int cntr=0; task = rt_task_init_schmod(nam2num("M3SYSP"), 0, 0, 0, SCHED_FIFO, 0xF); rt_allow_nonroot_hrt(); if (task==NULL) { printf("Failed to create RT-TASK M3SYSP\n",0); return 0; } shm_sem=(SEM*)rt_get_adr(nam2num(SEMNAM_M3LSHM)); if (!shm_sem) { printf("Unable to find the SEMNAM_M3LSHM semaphore.\n",0); rt_task_delete(task); return 0; } //else // printf("Allocated shm_sem semaphore %08x \n",shm_sem); sync_sem=(SEM*)rt_get_adr(nam2num(SEMNAM_M3SYNC)); if (!sync_sem) { printf("Unable to find the SEMNAM_M3SYNC semaphore.\n",0); rt_task_delete(task); rt_sem_delete(shm_sem); return 0; } //else // printf("Allocated sync_sem semaphore %08x \n",sync_sem); RTIME tick_period = nano2count(RT_TIMER_TICKS_NS); RTIME now = rt_get_time(); rt_task_make_periodic(task, now + tick_period, tick_period); mlockall(MCL_CURRENT | MCL_FUTURE); rt_make_hard_real_time(); t_last=now; sys_thread_active=1; uint64_t tl; while(!sys_thread_end) { rt_sem_wait(sync_sem); rt_sem_wait(shm_sem); if (cntr%200==0) { now=rt_get_time_ns(); float dt = (now-t_last)/1000000.0; count2timeval(nano2count(rt_get_real_time_ns()), &tv); printf("\n\nM3 Cycle: %d: 200 cycles in %4.3f ms. EC cycles: %d\n", cntr,dt, sys->counter); printf("DT: timestamp_dt (uS) : %lld\n",(sys->timestamp_ns-tl)/1000); t_last=now; SysEcShmPrettyPrint(sys); } tl=sys->timestamp_ns; cntr++; rt_sem_signal(shm_sem); rt_task_wait_period(); } printf("Exiting RealTime Thread...\n",0); rt_make_soft_real_time(); rt_task_delete(task); sys_thread_active=0; return 0; }