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