]> rtime.felk.cvut.cz Git - frescor/fosa.git/blob - src_marte/tests/test_cpu_clocks/test_cpu_clocks_sigwait_from_different_thread.c
254675a067b3c4ca6e6037a271dee38f5905c871
[frescor/fosa.git] / src_marte / tests / test_cpu_clocks / test_cpu_clocks_sigwait_from_different_thread.c
1 #include <unistd.h>
2 #include <stdio.h>
3 #include <time.h> // for nanosleep
4
5 #include <assert.h>
6 #include <stdlib.h> // for exit in assert
7 #include <string.h> // for memset
8
9 #include "fosa.h"
10
11 /*****************************/
12 /*   D E F I N I T I O N S   */
13 /*****************************/
14 #define RT_ERROR_SIGWAIT -2
15 #define RT_ERROR_TIMER   -3
16
17 #define SIGNAL_TIMER  FOSA_SIGNAL_MAX - 1
18
19 /***************************/
20 /*   P R O T O T Y P E S   */
21 /***************************/
22 static void * thread_body(void *thread_arg);
23
24 /*************************************/
25 /*  S T A T I C   V A R I A B L E S  */
26 /*************************************/
27 static fosa_abs_time_t start_execution;
28 static fosa_abs_time_t signal_reception;
29
30 int main () 
31 {
32     int err = -1;
33
34     fosa_thread_attr_t attr;
35     fosa_thread_id_t tid;
36
37     fosa_signal_t signal_received;
38     fosa_signal_info_t info_received;
39
40     fosa_rel_time_t work_interval = fosa_msec_to_rel_time(3200);
41     fosa_rel_time_t elapsed_time;
42
43     fosa_signal_t signal_set[1];
44
45     /* Set the signal mask */
46     /***********************/
47     signal_set[0] = SIGNAL_TIMER;
48     if (fosa_set_accepted_signals(signal_set, 1) !=0) 
49     {
50         printf ("Error while setting the signal mask\n"); 
51         exit (1);
52     }
53
54     /* Create the thread attributes and define its priority */
55     /*                                                      */
56     /* At the same time ensure that the main priority is    */
57     /* higher  */
58     /********************************************************/
59     if (fosa_thread_attr_init (&attr) != 0) {
60         printf("Error while initializing the attributes\n");
61         exit(1);
62     }
63
64     if (fosa_thread_attr_set_prio (&attr,fosa_get_priority_min()+3) != 0) {
65         printf("Error while setting schedparam\n");
66         exit(1);
67     }
68
69     /* create the periodic thread.  It won't execute yet */
70     /* because it has lower priority than main().        */
71     /*****************************************************/
72     err = fosa_thread_create(&tid, &attr, thread_body, NULL);
73     if (err) {
74         printf("pthread_create failed thread\n");
75         exit(1);
76     }
77
78
79     /* We execute a little bit to ensure that execution time */
80     /* and real time differ                                  */
81     /*********************************************************/
82     printf("Main works for some time...\n");
83     fosa_eat(&work_interval);
84
85     /* Now we do the wait in order to allow the thread to run */
86     /**********************************************************/
87     printf("About to do the wait\n");
88     err = fosa_signal_wait(signal_set, 1 ,&signal_received, &info_received);
89
90     fosa_clock_get_time(FOSA_CLOCK_REALTIME, &signal_reception);
91     elapsed_time = fosa_abs_time_extract_interval(start_execution, signal_reception);
92
93     printf("signal received=%d value=%d (42?), err=%d\n",
94            signal_received, info_received.sival_int, err);
95
96     printf("Elapsed time between sigwait and timer expiration: %ld msecs\n", 
97            fosa_rel_time_to_msec(elapsed_time) );
98
99     return 0;
100 }
101
102
103 // ----------------------------------------------------------------
104
105 static void * thread_body(void *thread_arg)
106 {
107     fosa_abs_time_t before_work_time;
108     fosa_abs_time_t after_work_time;
109     fosa_rel_time_t work_interval = fosa_msec_to_rel_time(1400);
110
111
112     fosa_clock_id_t clockid;
113     fosa_timer_id_t timerid;
114     fosa_rel_time_t budget;
115     fosa_rel_time_t elapsed_time;
116
117     fosa_signal_info_t info_programmed;
118     int err;
119
120
121     /* Get the thread's cputime clock */
122     /**********************************/
123     if (fosa_thread_get_cputime_clock(fosa_thread_self(), &clockid) !=0) 
124     {
125         exit(RT_ERROR_TIMER);
126     }
127
128     /* Create a timer and arm it with a given budget */
129     /*************************************************/
130     info_programmed.sival_int = 42;
131     err = fosa_timer_create(clockid, SIGNAL_TIMER, info_programmed, &timerid);
132     printf("timer created, err=%d\n", err);
133     assert(err == 0);
134
135     budget = fosa_msec_to_rel_time(2500);
136     err = fosa_rel_timer_arm(timerid, &budget);
137     printf("timer armed for 2.5 secs, err=%d\n", err);
138     assert(err == 0);
139
140     fosa_clock_get_time(FOSA_CLOCK_REALTIME, &start_execution);
141
142     while(1) 
143     {
144         err = fosa_clock_get_time(FOSA_CLOCK_REALTIME, &before_work_time);
145         assert(err == 0);
146         printf("Start periodic work  at %ld msecs\n", fosa_abs_time_to_msec(before_work_time) );
147
148         fosa_eat(&work_interval);
149
150         err = fosa_clock_get_time(FOSA_CLOCK_REALTIME, &after_work_time);
151         assert(err == 0);
152
153         printf("End periodic work  at %ld msecs\n", fosa_abs_time_to_msec(after_work_time) );
154
155         elapsed_time = fosa_abs_time_extract_interval(before_work_time, after_work_time);
156
157         printf("Elapsed time:  %ld msec\n", fosa_rel_time_to_msec(elapsed_time) );
158
159     } // while
160
161     return NULL;
162 }
163