8 #include <native/task.h>
9 #include <native/timer.h>
10 #include <native/sem.h>
17 #define HIGH 52 /* high priority */
18 #define MID 51 /* medium priority */
19 #define LOW 50 /* low priority */
21 RT_TASK demo_task
[NTASKS
];
24 #define BASEPERIOD 0 // baseperiod 0 to get ns
26 #define EXECTIME 2e8 // execution time in ns
27 #define SPINTIME 1e7 // spin time in ns
31 RTIME starttime
, runtime
;
34 RT_TASK_INFO curtaskinfo
;
36 curtask
=rt_task_self();
37 rt_printf("Task : %d\n",num
);
39 rt_sem_p(&mysync
,TM_INFINITE
);
42 while(runtime
< EXECTIME
) {
43 rt_timer_spin(SPINTIME
); // spin cpu doing nothing
45 runtime
= runtime
+ SPINTIME
;
47 rt_printf("Running Task : %d at ms : %d\n",num
,runtime
/1000000);
48 if(runtime
== (EXECTIME
/2)){
50 rt_task_set_priority(&demo_task
[1],MID
+10);
51 rt_task_set_priority(&demo_task
[0],LOW
+10);
55 rt_printf("End Task : %d\n",num
);
64 // semaphore to sync task startup on
65 rt_sem_create(&mysync
,"MySemaphore",0,S_FIFO
);
68 rt_timer_set_mode(BASEPERIOD
);
70 for(i
=0; i
< NTASKS
; i
++) {
71 rt_printf("start task : %d\n",i
);
72 sprintf(str
,"task%d",i
);
73 rt_task_create(&demo_task
[i
], str
, 0, 50, 0);
74 rt_task_start(&demo_task
[i
], &demo
, &i
);
76 // assign priorities to tasks
77 // (or in creation use 50+i)
78 rt_task_set_priority(&demo_task
[0],LOW
);
79 rt_task_set_priority(&demo_task
[1],MID
);
80 rt_task_set_priority(&demo_task
[2],HIGH
);
82 rt_printf("wake up all tasks\n");
83 rt_sem_broadcast(&mysync
);
87 /* Avoids memory swapping for this program */
88 mlockall(MCL_CURRENT
|MCL_FUTURE
);
90 /* Perform auto-init of rt_print buffers if the task doesn't do so */
91 rt_print_auto_init(1);
94 int main(int argc
, char* argv
[])
96 printf("\nType CTRL-C to end this program\n\n" );
98 // code to set things to run xenomai
104 // wait for CTRL-c is typed to end the program