6 #include <native/task.h>
7 #include <native/timer.h>
8 #include <native/sem.h>
15 #define BASEPERIOD 0 // baseperiod 0 to get ns
17 #define EXECTIME 2e8 // execution time in ns
18 #define SPINTIME 1e7 // spin time in ns
20 RT_TASK demo_task
[NTASKS
];
22 int switch_happened
[NTASKS
] = {0, 0, 0};
28 int num
= *(int *)arg
;
30 RT_TASK_INFO curtaskinfo
;
32 rt_printf("Task: %d\n", num
);
34 rt_sem_p(&mysync
, TM_INFINITE
);
37 while(runtime
< EXECTIME
) {
38 if(runtime
>= EXECTIME
/2 && switch_happened
[num
] == 0){
39 rt_task_set_priority(&demo_task
[num
],
40 num
== 0 ? 40 : (num
== 1 ? 39 : 38));
41 switch_happened
[num
] = 1;
43 rt_timer_spin(SPINTIME
); // spin cpu doing nothing
44 runtime
= runtime
+ SPINTIME
;
45 rt_printf("Running Task: %d at ms: %d\n", num
, runtime
/1000000);
47 rt_printf("End Task: %d\n", num
);
56 // semaphore to sync task startup on
57 rt_sem_create(&mysync
, "MySemaphore", 0, S_FIFO
);
60 rt_timer_set_mode(BASEPERIOD
);
62 for(i
= 0; i
< NTASKS
; i
++) {
63 rt_printf("start task: %d\n",i
);
64 sprintf(str
,"task%d",i
);
65 rt_task_create(&demo_task
[i
], str
, 0, 50+i
, 0);
66 rt_task_start(&demo_task
[i
], &demo
, &i
);
69 rt_printf("wake up all tasks\n");
70 rt_sem_broadcast(&mysync
);
74 /* Avoids memory swapping for this program */
75 mlockall(MCL_CURRENT
|MCL_FUTURE
);
77 /* Perform auto-init of rt_print buffers if the task doesn't do so */
78 rt_print_auto_init(1);
81 int main(int argc
, char* argv
[])
83 printf("\nType CTRL-C to end this program\n\n" );
85 // code to set things to run xenomai
91 // wait for CTRL-c is typed to end the program