1 // -----------------------------------------------------------------------
2 // Copyright (C) 2006 - 2009 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 // -----------------------------------------------------------------------
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);
80 static fosa_abs_time_t start_execution;
81 static fosa_abs_time_t signal_reception;
87 fosa_thread_attr_t attr;
90 fosa_signal_t signal_received;
91 fosa_signal_info_t info_programmed, info_received;
92 fosa_rel_time_t work_interval = fosa_msec_to_rel_time(3200); // 3.2 seconds
93 fosa_clock_id_t clockid;
94 fosa_timer_id_t timerid;
96 fosa_rel_time_t budget;
97 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 */
110 /********************************************************/
111 if (fosa_thread_attr_init (&attr) != 0) {
112 printf("Error while initializing the attributes\n");
116 if (fosa_thread_attr_set_prio (&attr,fosa_get_priority_min()+3) != 0) {
117 printf("Error while setting schedparam\n");
121 /* create the periodic thread. It won't execute yet */
122 /* because it has lower priority than main(). */
123 /*****************************************************/
124 err = fosa_thread_create(&tid, &attr, thread_body, NULL);
126 printf("pthread_create failed thread\n");
130 /* Get the thread's cputime clock */
131 /**********************************/
132 if (fosa_thread_get_cputime_clock(tid, &clockid) !=0)
134 exit(RT_ERROR_TIMER);
137 /* Create a timer and arm it with a given budget */
138 /*************************************************/
139 info_programmed.sival_int = 42;
140 err = fosa_timer_create(clockid, SIGNAL_TIMER, info_programmed, &timerid);
141 printf("timer created, err=%d\n", err);
144 budget = fosa_msec_to_rel_time(2500);
145 err = fosa_rel_timer_arm(timerid, &budget);
146 printf("timer armed for 2.5 secs, err=%d\n", err);
149 /* We execute a little bit to ensure that execution time */
150 /* and real time differ */
151 /*********************************************************/
152 printf("Main works for some time...\n");
153 fosa_eat(&work_interval);
155 /* Now we do the wait in order to allow the thread to run */
156 /**********************************************************/
157 printf("About to do the wait\n");
158 err = fosa_signal_wait(signal_set, 1 ,&signal_received, &info_received);
160 fosa_clock_get_time(FOSA_CLOCK_REALTIME, &signal_reception);
161 elapsed_time = fosa_abs_time_extract_interval(start_execution, signal_reception);
163 printf("signal received=%d value=%d (42?), err=%d\n",
164 signal_received, info_received.sival_int, err);
166 printf("Elapsed time between sigwait and timer expiration: %ld msecs\n",
167 fosa_rel_time_to_msec(elapsed_time) );
173 // ----------------------------------------------------------------
175 static void * thread_body(void *thread_arg)
177 fosa_abs_time_t before_work_time;
178 fosa_abs_time_t after_work_time;
179 fosa_rel_time_t work_interval = fosa_msec_to_rel_time(1400);
180 fosa_rel_time_t elapsed_time;
183 fosa_clock_get_time(FOSA_CLOCK_REALTIME, &start_execution);
187 err = fosa_clock_get_time(FOSA_CLOCK_REALTIME, &before_work_time);
189 printf("Start periodic work at %ld msecs\n", fosa_abs_time_to_msec(before_work_time) );
191 fosa_eat(&work_interval);
193 err = fosa_clock_get_time(FOSA_CLOCK_REALTIME, &after_work_time);
196 printf("End periodic work at %ld msecs\n", fosa_abs_time_to_msec(after_work_time) );
197 elapsed_time = fosa_abs_time_extract_interval(before_work_time, after_work_time);
199 printf("Elapsed time: %ld msecs\n", fosa_rel_time_to_msec(elapsed_time) );