1 // -----------------------------------------------------------------------
2 // Copyright (C) 2006 - 2008 FRESCOR consortium partners:
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
11 // Thales Communication S.A. FRANCE
12 // Visual Tools S.A. SPAIN
13 // Rapita Systems Ltd UK
16 // See http://www.frescor.org for a link to partners' websites
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
24 // based on previous work (FSF) done in the FIRST project
26 // Copyright (C) 2005 Mälardalen University, SWEDEN
27 // Scuola Superiore S.Anna, ITALY
28 // Universidad de Cantabria, SPAIN
29 // University of York, UK
31 // FSF API web pages: http://marte.unican.es/fsf/docs
32 // http://shark.sssup.it/contrib/first/docs/
34 // This file is part of FOSA (Frsh Operating System Adaption)
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.
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 // -----------------------------------------------------------------------
58 #include <time.h> // for nanosleep
61 #include <stdlib.h> // for exit in assert
62 #include <string.h> // for memset
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
72 #define SIGNAL_TIMER FOSA_SIGNAL_MAX - 1
74 /***************************/
75 /* P R O T O T Y P E S */
76 /***************************/
77 static void * thread_body(void *thread_arg);
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;
89 fosa_thread_attr_t attr;
92 fosa_signal_t signal_received;
93 fosa_signal_info_t info_received;
95 fosa_rel_time_t work_interval = fosa_msec_to_rel_time(3200);
96 fosa_rel_time_t elapsed_time;
98 fosa_signal_t signal_set[1];
100 /* Set the signal mask */
101 /***********************/
102 signal_set[0] = SIGNAL_TIMER;
103 if (fosa_set_accepted_signals(signal_set, 1) !=0)
105 printf ("Error while setting the signal mask\n");
109 /* Create the thread attributes and define its priority */
111 /* At the same time ensure that the main priority is */
113 /********************************************************/
114 if (fosa_thread_attr_init (&attr) != 0) {
115 printf("Error while initializing the attributes\n");
119 if (fosa_thread_attr_set_prio (&attr,fosa_get_priority_min()+3) != 0) {
120 printf("Error while setting schedparam\n");
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);
129 printf("pthread_create failed thread\n");
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);
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);
145 fosa_clock_get_time(FOSA_CLOCK_REALTIME, &signal_reception);
146 elapsed_time = fosa_abs_time_extract_interval(start_execution, signal_reception);
148 printf("signal received=%d value=%d (42?), err=%d\n",
149 signal_received, info_received.sival_int, err);
151 printf("Elapsed time between sigwait and timer expiration: %ld msecs\n",
152 fosa_rel_time_to_msec(elapsed_time) );
158 // ----------------------------------------------------------------
160 static void * thread_body(void *thread_arg)
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);
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;
172 fosa_signal_info_t info_programmed;
176 /* Get the thread's cputime clock */
177 /**********************************/
178 if (fosa_thread_get_cputime_clock(fosa_thread_self(), &clockid) !=0)
180 exit(RT_ERROR_TIMER);
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);
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);
195 fosa_clock_get_time(FOSA_CLOCK_REALTIME, &start_execution);
199 err = fosa_clock_get_time(FOSA_CLOCK_REALTIME, &before_work_time);
201 printf("Start periodic work at %ld msecs\n", fosa_abs_time_to_msec(before_work_time) );
203 fosa_eat(&work_interval);
205 err = fosa_clock_get_time(FOSA_CLOCK_REALTIME, &after_work_time);
208 printf("End periodic work at %ld msecs\n", fosa_abs_time_to_msec(after_work_time) );
210 elapsed_time = fosa_abs_time_extract_interval(before_work_time, after_work_time);
212 printf("Elapsed time: %ld msec\n", fosa_rel_time_to_msec(elapsed_time) );