int main(void) { RT_TASK *task; RTIME now; int cnt=0; // make main thread LXRT soft realtime task = rt_task_init_schmod(nam2num("MYTASK"), 9, 0, 0, SCHED_FIFO, 0xF); mlockall(MCL_CURRENT | MCL_FUTURE); // start realtime timer and scheduler //rt_set_oneshot_mode(); rt_set_periodic_mode(); start_rt_timer(0); now = rt_get_time() + 10*PERIOD; rt_task_make_periodic(task, now, PERIOD); printf("Init mutex and cond.\n"); mutex = rt_sem_init(nam2num("MUTEX"), 1); if (mutex==0) printf("Error init mutex\n"); cond = rt_cond_init(nam2num("CONDITION")); if (cond==0) printf("Error init cond\n"); thread0 = rt_thread_create(fun0, NULL, 10000); //thread1 = rt_thread_create(fun1, NULL, 20000); //rt_sleep(PERIOD); while (cnt < THRESHOLD) { rt_task_wait_period(); rt_printk("main: Hello World %d!\n",cnt); rt_sem_wait(mutex); //now the mutex should have value 0 if (instance_cnt==0) { rt_sem_signal(mutex); //now the mutex should have vaule 1 rt_printk("worker thread busy!\n"); } else { instance_cnt++; rt_cond_signal(cond); rt_sem_signal(mutex); //now the mutex should have vaule 1 rt_printk("signaling worker thread to start!\n"); } cnt++; } // wait for end of program printf("TYPE <ENTER> TO TERMINATE\n"); getchar(); // cleanup stop_rt_timer(); return 0; }
int init_module(void) { printk("Conditional semaphore test program.\n"); printk("Wait for all tasks to end, then type: ./rem.\n\n"); start_rt_timer(nano2count(TICK)); rt_cond_init(&cond); rt_mutex_init(&mtx); rt_task_init(&task1, task_func1, 0, RT_STACK, 0, 0, 0); rt_task_init(&task2, task_func2, 0, RT_STACK, 1, 0, 0); rt_task_init(&task3, task_func3, 0, RT_STACK, 2, 0, 0); rt_task_init(&task4, task_func4, 0, RT_STACK, 3, 0, 0); rt_task_resume(&task1); rt_task_resume(&task2); rt_task_resume(&task3); rt_task_resume(&task4); printk("Do not panic, wait 2 s, till task3 times out.\n\n"); return 0; }
int rtos_cond_init(rt_cond_t *cond) { CHK_LXRT_CALL(); cond->cond = rt_cond_init(0); return cond->cond == 0 ? -1 : 0; }