]> rtime.felk.cvut.cz Git - frescor/fosa.git/blob - src_marte_os/tests/test_cpu_clocks/test_cpu_clocks_sigwait_from_different_thread.c
68beac59b0928adc5bdb20b8eca97b46e828f619
[frescor/fosa.git] / src_marte_os / tests / test_cpu_clocks / test_cpu_clocks_sigwait_from_different_thread.c
1 // -----------------------------------------------------------------------
2 //  Copyright (C) 2006 - 2008 FRESCOR consortium partners:
3 //
4 //    Universidad de Cantabria,              SPAIN
5 //    University of York,                    UK
6 //    Scuola Superiore Sant'Anna,            ITALY
7 //    Kaiserslautern University,             GERMANY
8 //    Univ. Politécnica  Valencia,           SPAIN
9 //    Czech Technical University in Prague,  CZECH REPUBLIC
10 //    ENEA                                   SWEDEN
11 //    Thales Communication S.A.              FRANCE
12 //    Visual Tools S.A.                      SPAIN
13 //    Rapita Systems Ltd                     UK
14 //    Evidence                               ITALY
15 //
16 //    See http://www.frescor.org for a link to partners' websites
17 //
18 //           FRESCOR project (FP6/2005/IST/5-034026) is funded
19 //        in part by the European Union Sixth Framework Programme
20 //        The European Union is not liable of any use that may be
21 //        made of this code.
22 //
23 //
24 //  based on previous work (FSF) done in the FIRST project
25 //
26 //   Copyright (C) 2005  Mälardalen University, SWEDEN
27 //                       Scuola Superiore S.Anna, ITALY
28 //                       Universidad de Cantabria, SPAIN
29 //                       University of York, UK
30 //
31 //   FSF API web pages: http://marte.unican.es/fsf/docs
32 //                      http://shark.sssup.it/contrib/first/docs/
33 //
34 //   This file is part of FOSA (Frsh Operating System Adaption)
35 //
36 //  FOSA is free software; you can redistribute it and/or modify it
37 //  under terms of the GNU General Public License as published by the
38 //  Free Software Foundation; either version 2, or (at your option) any
39 //  later version.  FOSA is distributed in the hope that it will be
40 //  useful, but WITHOUT ANY WARRANTY; without even the implied warranty
41 //  of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
42 //  General Public License for more details. You should have received a
43 //  copy of the GNU General Public License along with FOSA; see file
44 //  COPYING. If not, write to the Free Software Foundation, 675 Mass Ave,
45 //  Cambridge, MA 02139, USA.
46 //
47 //  As a special exception, including FOSA header files in a file,
48 //  instantiating FOSA generics or templates, or linking other files
49 //  with FOSA objects to produce an executable application, does not
50 //  by itself cause the resulting executable application to be covered
51 //  by the GNU General Public License. This exception does not
52 //  however invalidate any other reasons why the executable file might be
53 //  covered by the GNU Public License.
54 // -----------------------------------------------------------------------
55
56 #include <unistd.h>
57 #include <stdio.h>
58 #include <time.h> // for nanosleep
59
60 #include <assert.h>
61 #include <stdlib.h> // for exit in assert
62 #include <string.h> // for memset
63
64 #include "fosa.h"
65
66 /*****************************/
67 /*   D E F I N I T I O N S   */
68 /*****************************/
69 #define RT_ERROR_SIGWAIT -2
70 #define RT_ERROR_TIMER   -3
71
72 #define SIGNAL_TIMER  FOSA_SIGNAL_MAX - 1
73
74 /***************************/
75 /*   P R O T O T Y P E S   */
76 /***************************/
77 static void * thread_body(void *thread_arg);
78
79 /*************************************/
80 /*  S T A T I C   V A R I A B L E S  */
81 /*************************************/
82 static fosa_abs_time_t start_execution;
83 static fosa_abs_time_t signal_reception;
84
85 int main ()
86 {
87     int err = -1;
88
89     fosa_thread_attr_t attr;
90     fosa_thread_id_t tid;
91
92     fosa_signal_t signal_received;
93     fosa_signal_info_t info_received;
94
95     fosa_rel_time_t work_interval = fosa_msec_to_rel_time(3200);
96     fosa_rel_time_t elapsed_time;
97
98     fosa_signal_t signal_set[1];
99
100     /* Set the signal mask */
101     /***********************/
102     signal_set[0] = SIGNAL_TIMER;
103     if (fosa_set_accepted_signals(signal_set, 1) !=0)
104     {
105         printf ("Error while setting the signal mask\n");
106         exit (1);
107     }
108
109     /* Create the thread attributes and define its priority */
110     /*                                                      */
111     /* At the same time ensure that the main priority is    */
112     /* higher  */
113     /********************************************************/
114     if (fosa_thread_attr_init (&attr) != 0) {
115         printf("Error while initializing the attributes\n");
116         exit(1);
117     }
118
119     if (fosa_thread_attr_set_prio (&attr,fosa_get_priority_min()+3) != 0) {
120         printf("Error while setting schedparam\n");
121         exit(1);
122     }
123
124     /* create the periodic thread.  It won't execute yet */
125     /* because it has lower priority than main().        */
126     /*****************************************************/
127     err = fosa_thread_create(&tid, &attr, thread_body, NULL);
128     if (err) {
129         printf("pthread_create failed thread\n");
130         exit(1);
131     }
132
133
134     /* We execute a little bit to ensure that execution time */
135     /* and real time differ                                  */
136     /*********************************************************/
137     printf("Main works for some time...\n");
138     fosa_eat(&work_interval);
139
140     /* Now we do the wait in order to allow the thread to run */
141     /**********************************************************/
142     printf("About to do the wait\n");
143     err = fosa_signal_wait(signal_set, 1 ,&signal_received, &info_received);
144
145     fosa_clock_get_time(FOSA_CLOCK_REALTIME, &signal_reception);
146     elapsed_time = fosa_abs_time_extract_interval(start_execution, signal_reception);
147
148     printf("signal received=%d value=%d (42?), err=%d\n",
149            signal_received, info_received.sival_int, err);
150
151     printf("Elapsed time between sigwait and timer expiration: %ld msecs\n",
152            fosa_rel_time_to_msec(elapsed_time) );
153
154     return 0;
155 }
156
157
158 // ----------------------------------------------------------------
159
160 static void * thread_body(void *thread_arg)
161 {
162     fosa_abs_time_t before_work_time;
163     fosa_abs_time_t after_work_time;
164     fosa_rel_time_t work_interval = fosa_msec_to_rel_time(1400);
165
166
167     fosa_clock_id_t clockid;
168     fosa_timer_id_t timerid;
169     fosa_rel_time_t budget;
170     fosa_rel_time_t elapsed_time;
171
172     fosa_signal_info_t info_programmed;
173     int err;
174
175
176     /* Get the thread's cputime clock */
177     /**********************************/
178     if (fosa_thread_get_cputime_clock(fosa_thread_self(), &clockid) !=0)
179     {
180         exit(RT_ERROR_TIMER);
181     }
182
183     /* Create a timer and arm it with a given budget */
184     /*************************************************/
185     info_programmed.sival_int = 42;
186     err = fosa_timer_create(clockid, SIGNAL_TIMER, info_programmed, &timerid);
187     printf("timer created, err=%d\n", err);
188     assert(err == 0);
189
190     budget = fosa_msec_to_rel_time(2500);
191     err = fosa_rel_timer_arm(timerid, &budget);
192     printf("timer armed for 2.5 secs, err=%d\n", err);
193     assert(err == 0);
194
195     fosa_clock_get_time(FOSA_CLOCK_REALTIME, &start_execution);
196
197     while(1)
198     {
199         err = fosa_clock_get_time(FOSA_CLOCK_REALTIME, &before_work_time);
200         assert(err == 0);
201         printf("Start periodic work  at %ld msecs\n", fosa_abs_time_to_msec(before_work_time) );
202
203         fosa_eat(&work_interval);
204
205         err = fosa_clock_get_time(FOSA_CLOCK_REALTIME, &after_work_time);
206         assert(err == 0);
207
208         printf("End periodic work  at %ld msecs\n", fosa_abs_time_to_msec(after_work_time) );
209
210         elapsed_time = fosa_abs_time_extract_interval(before_work_time, after_work_time);
211
212         printf("Elapsed time:  %ld msec\n", fosa_rel_time_to_msec(elapsed_time) );
213
214     } // while
215
216     return NULL;
217 }
218