void CommandChrono_Get(char *command) { switch (Chronostatus) { case stoppedInitial: if (buffered) { buffered = FALSE; } else { rt_sem_wait(¬Empty); } *command = buffer; Chronostatus = running; rt_sem_signal(¬Full); break; case running: if (rt_sem_wait_if(¬Empty) > 0) { *command = buffer; if (*command == 'E') { Chronostatus = stoppedFinal; } rt_sem_signal(¬Full); } else { *command = 'C'; } break; case stoppedFinal: if (rt_sem_wait_timed(¬Empty, fiveSeconds) >= 0) { buffered = TRUE; } *command = 'R'; Chronostatus = stoppedInitial; break; } }
int rtos_sem_wait_timed(rt_sem_t* m, NANO_TIME delay ) { int ret; CHK_LXRT_CALL(); ret = rt_sem_wait_timed(m->sem, nano2count(delay) ); #if defined(CONFIG_RTAI_VERSION_MINOR) && defined(CONFIG_RTAI_VERSION_MAJOR) # if CONFIG_RTAI_VERSION_MAJOR == 3 && CONFIG_RTAI_VERSION_MINOR > 3 return (ret == RTE_TIMOUT) ? -1 : 0; # else return (ret == SEM_TIMOUT) ? -1 : 0; # endif #else return (ret == SEM_TIMOUT) ? -1 : 0; #endif }
static void task_code(int task_no) { int i, ret; char buf[9]; buf[8] = 0; for (i = 0; i < 5; ++i) { rt_sem_wait(&sems[task_no]); TAKE_PRINT; rt_printk(strs[task_no]); GIVE_PRINT; if (task_no == NUM_TASKS-1) { rt_printk("\n"); } rt_sem_signal(&sems[(task_no + 1) % NUM_TASKS]); } rt_sem_signal(&sync_sem); rt_sem_wait(&prio_sem); TAKE_PRINT; rt_printk(strs[task_no]); GIVE_PRINT; rt_sleep(nano2count(1000000000LL)); rt_sem_wait_timed(&prio_sem, nano2count((task_no + 1)*1000000000LL)); TAKE_PRINT; rt_printk("sem timeout, task %d, %s\n", task_no, strs[task_no]); GIVE_PRINT; rt_sem_signal(&sync_sem); /* message queue stuff */ if ((ret = rt_mbx_receive(&mbx_in, buf, 8)) != 0) { rt_printk("rt_mbx_receive() failed with %d\n", ret); } TAKE_PRINT; rt_printk("\nreceived by task %d ", task_no); rt_printk(buf); GIVE_PRINT; rt_mbx_send(&mbx_out, strs[task_no], 8); /* test receive timeout */ rt_sem_wait(&sync_sem); if (rt_mbx_receive_timed(&mbx_in, buf, 8, nano2count((task_no + 1)*1000000000LL))) { TAKE_PRINT; rt_printk("mbx timeout, task %d, %s\n", task_no, strs[task_no]); GIVE_PRINT; } rt_printk("\ntask %d complete\n", task_no); }
int main(int argc, char* argv[]) { unsigned long mtsk_name = nam2num("MTSK"); unsigned long btsk_name = nam2num("BTSK"); unsigned long sem_name = nam2num("SEM"); unsigned long smbx_name = nam2num("SMBX"); unsigned long rmbx_name = nam2num("RMBX"); unsigned long msg; long long mbx_msg; long long llmsg = 0xaaaaaaaaaaaaaaaaLL; RT_TASK *mtsk, *rcvd_from; SEM *sem; MBX *smbx, *rmbx; int pid, count; if (!(mtsk = rt_task_init_schmod(mtsk_name, 0, 0, 0, SCHED_FIFO, 0x1))) { printf("CANNOT INIT MASTER TASK\n"); exit(1); } printf("MASTER TASK INIT: name = %lx, address = %p.\n", mtsk_name, mtsk); printf("MASTER TASK STARTS THE ONESHOT TIMER\n"); rt_set_oneshot_mode(); start_rt_timer(nano2count(10000000)); mlockall(MCL_CURRENT | MCL_FUTURE); rt_make_hard_real_time(); rt_sleep(1000000); printf("MASTER TASK MAKES ITSELF PERIODIC WITH A PERIOD OF 1 ms\n"); rt_task_make_periodic(mtsk, rt_get_time(), nano2count(PERIOD)); rt_sleep(nano2count(1000000000)); count = PERIODIC_LOOPS; printf("MASTER TASK LOOPS ON WAIT_PERIOD FOR %d PERIODS\n", count); while(count--) { printf("PERIOD %d\n", count); rt_task_wait_period(); } count = SLEEP_LOOPS; printf("MASTER TASK LOOPS ON SLEEP 0.1 s FOR %d PERIODS\n", count); while(count--) { printf("SLEEPING %d\n", count); rt_sleep(nano2count(DELAY)); } printf("MASTER TASK YIELDS ITSELF\n"); rt_task_yield(); printf("MASTER TASK CREATES BUDDY TASK\n"); pid = fork(); if (!pid) { execl("./slave", "./slave", NULL); } printf("MASTER TASK SUSPENDS ITSELF, TO BE RESUMED BY BUDDY TASK\n"); rt_task_suspend(mtsk); printf("MASTER TASK RESUMED BY BUDDY TASK\n"); if (!(sem = rt_sem_init(sem_name, 0))) { printf("CANNOT CREATE SEMAPHORE %lx\n", sem_name); exit(1); } printf("MASTER TASK CREATES SEM: name = %lx, address = %p.\n", sem_name, sem); printf("MASTER TASK WAIT_IF ON SEM\n"); rt_sem_wait_if(sem); printf("MASTER STEP BLOCKS WAITING ON SEM\n"); rt_sem_wait(sem); printf("MASTER TASK SIGNALLED BY BUDDY TASK WAKES UP AND BLOCKS WAIT TIMED 1 s ON SEM\n"); rt_sem_wait_timed(sem, nano2count(1000000000)); printf("MASTER TASK DELETES SEM\n"); rt_sem_delete(sem); printf("MASTER TASK BLOCKS RECEIVING FROM ANY\n"); rcvd_from = rt_receive(0, (void *)&msg); printf("MASTER TASK RECEIVED MESSAGE %lx FROM BUDDY TASK\n", msg); printf("MASTER TASK RPCS TO BUDDY TASK THE MESSAGE %lx\n", 0xabcdefL); rcvd_from = rt_rpc(rcvd_from, 0xabcdef, (void *)&msg); printf("MASTER TASK RECEIVED THE MESSAGE %lx RETURNED BY BUDDY TASK\n", msg); //exit(1); if (!(smbx = rt_mbx_init(smbx_name, 1))) { printf("CANNOT CREATE MAILBOX %lx\n", smbx_name); exit(1); } if (!(rmbx = rt_mbx_init(rmbx_name, 1))) { printf("CANNOT CREATE MAILBOX %lx\n", rmbx_name); exit(1); } printf("MASTER TASK CREATED TWO MAILBOXES %p %p %p %p \n", smbx, rmbx, &mtsk_name, &msg); count = MBX_LOOPS; while(count--) { rt_mbx_send(smbx, &llmsg, sizeof(llmsg)); printf("%d MASTER TASK SENDS THE MESSAGE %llx MBX\n", count, llmsg); mbx_msg = 0; rt_mbx_receive_timed(rmbx, &mbx_msg, sizeof(mbx_msg), nano2count(MSG_DELAY)); printf("%d MASTER TASK RECEIVED THE MESSAGE %llx FROM MBX\n", count, mbx_msg); rt_sleep(nano2count(DELAY)); } printf("MASTER TASK SENDS THE MESSAGE %lx TO BUDDY TO ALLOW ITS END\n", 0xeeeeeeeeL); rt_send(rcvd_from, 0xeeeeeeee); printf("MASTER TASK WAITS FOR BUDDY TASK END\n"); while (rt_get_adr(btsk_name)) { rt_sleep(nano2count(1000000000)); } printf("MASTER TASK STOPS THE PERIODIC TIMER\n"); stop_rt_timer(); printf("MASTER TASK DELETES MAILBOX %p\n", smbx); rt_mbx_delete(smbx); printf("MASTER TASK DELETES MAILBOX %p\n", rmbx); rt_mbx_delete(rmbx); printf("MASTER TASK DELETES ITSELF\n"); rt_task_delete(mtsk); printf("END MASTER TASK\n"); return 0; }