4a521243b12b1d49cf5b399bd33219c44517c30b
[des2015.git] / mart / ex06 / ex06c.c
1 #include <stdio.h>
2 #include <signal.h>
3 #include <unistd.h>
4 #include <sys/mman.h>
5
6 #include <native/task.h>
7 #include <native/timer.h>
8 #include <native/sem.h>
9
10 #include <rtdk.h>
11 #include <sys/io.h>
12
13 #define NTASKS 3
14
15 #define BASEPERIOD 0 // baseperiod 0 to get ns
16
17 #define EXECTIME 2e8 // execution time in ns
18 #define SPINTIME 1e7 // spin time in ns
19
20 RT_TASK demo_task[NTASKS];
21 RT_SEM mysync;
22 int switch_happened = 0;
23
24 void demo(void *arg)
25 {
26 RTIME starttime;
27 RTIME runtime;
28 int num = *(int *)arg;
29 RT_TASK *curtask;
30 RT_TASK_INFO curtaskinfo;
31
32 rt_printf("Task: %d\n", num);
33
34 rt_sem_p(&mysync, TM_INFINITE);
35
36 runtime = 0;
37 while(runtime < EXECTIME) {
38 if(num == 2 && runtime >= EXECTIME/2 && switch_happened == 0){
39 rt_task_set_priority(&demo_task[1], 51+10);
40 rt_task_set_priority(&demo_task[0], 50+10);
41 switch_happened = 1;
42 }
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);
46 }
47 rt_printf("End Task: %d\n", num);
48 }
49
50 //startup code
51 void startup()
52 {
53 int i;
54 char str[10] ;
55
56 // semaphore to sync task startup on
57 rt_sem_create(&mysync, "MySemaphore", 0, S_FIFO);
58
59 // set timing to ns
60 rt_timer_set_mode(BASEPERIOD);
61
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);
67 }
68
69 rt_printf("wake up all tasks\n");
70 rt_sem_broadcast(&mysync);
71 }
72
73 void init_xenomai() {
74 /* Avoids memory swapping for this program */
75 mlockall(MCL_CURRENT|MCL_FUTURE);
76
77 /* Perform auto-init of rt_print buffers if the task doesn't do so */
78 rt_print_auto_init(1);
79 }
80
81 int main(int argc, char* argv[])
82 {
83 printf("\nType CTRL-C to end this program\n\n" );
84
85 // code to set things to run xenomai
86 init_xenomai();
87
88 //startup code
89 startup();
90
91 // wait for CTRL-c is typed to end the program
92 pause();
93 }