1 //----------------------------------------------------------------------
2 // Copyright (C) 2006 - 2007 by the FRESCOR consortium:
4 // Universidad de Cantabria, SPAIN
5 // University of York, UK
6 // Scuola Superiore Sant'Anna, ITALY
7 // Kaiserslautern University, GERMANY
8 // Univ. Politecnica 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
18 // The 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 // This file is part of FRSH (FRescor ScHeduler)
33 // FRSH is free software; you can redistribute it and/or modify it
34 // under terms of the GNU General Public License as published by the
35 // Free Software Foundation; either version 2, or (at your option) any
36 // later version. FRSH is distributed in the hope that it will be
37 // useful, but WITHOUT ANY WARRANTY; without even the implied warranty
38 // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
39 // General Public License for more details. You should have received a
40 // copy of the GNU General Public License along with FRSH; see file
41 // COPYING. If not, write to the Free Software Foundation, 675 Mass Ave,
42 // Cambridge, MA 02139, USA.
44 // As a special exception, including FRSH header files in a file,
45 // instantiating FRSH generics or templates, or linking other files
46 // with FRSH objects to produce an executable application, does not
47 // by itself cause the resulting executable application to be covered
48 // by the GNU General Public License. This exception does not
49 // however invalidate any other reasons why the executable file might be
50 // covered by the GNU Public License.
51 // -----------------------------------------------------------------------
52 // 11-Jul-2007 SANGORRIN change calibrate and eat because it didn't work for
53 // MaRTE OS linux_arch
54 // 13-Jul-2007 SANGORRIN substitute calibrate for a new eat that I have added
55 // -----------------------------------------------------------------------
63 //#include <pthread.h>
70 #include "marte_non_local_jmp.h"
72 /************************/
73 /** Constants and types */
74 /************************/
76 #define BUDGET_OVERRUN_SIGNAL SIGRTMIN+3
77 #define RT_ERROR_SIGWAIT -2
78 #define RT_ERROR_TIMER -3
82 marte_nonlocaljmp_context_t *context_ptr;
84 } budget_timer_info_t;
87 /************************/
88 /** Global Variables */
89 /************************/
91 static marte_nonlocaljmp_context_t context;
92 static int error_status;
95 /************************/
97 /************************/
99 static void * thread_body(void *arg);
100 static void * signal_handler_thread (void * arg);
103 /************************************************************************/
105 /************************************************************************/
109 fosa_signal_t set[1];
110 fosa_thread_attr_t attr;
112 fosa_thread_id_t tid1, tid2;
116 set[0]=BUDGET_OVERRUN_SIGNAL;
117 if (fosa_set_accepted_signals(set, 1) !=0) {
118 printf ("Error while setting the signal mask\n");
122 // Create the thread attributes object
123 if (fosa_thread_attr_init (&attr) != 0) {
124 printf("Error while initializing the attributes\n");
128 if (fosa_thread_attr_set_prio (&attr,fosa_get_priority_max()-2) != 0) {
129 printf("Error while setting schedparam\n");
134 /* create the signal handler thread */
135 terror = fosa_thread_create(&tid1, &attr, signal_handler_thread, NULL);
137 printf("pthread_create signal handler\n");
141 // set priority of periodic thread
142 if (fosa_thread_attr_set_prio (&attr,fosa_get_priority_min()+3) != 0) {
143 printf("Error while setting schedparam\n");
147 /* create the periodic thread */
148 terror = fosa_thread_create(&tid2, &attr, thread_body, NULL);
150 printf("pthread_create periodic thread\n");
154 printf("Main goes to sleep...\n");
160 // work to be aborted if too long
163 fosa_rel_time_t exec_time = fosa_msec_to_rel_time(1000); // 1 second
166 printf("start regular code %d\n",i);
168 // eat one second of budget
169 fosa_eat(&exec_time);
170 // every five cycles eat an additional one second of budget */
173 fosa_eat(&exec_time);
174 printf("aqui tambien\n");
180 /* ------------------------------------------------------------ */
181 /** Body of periodic thread that consumes budget */
183 static void * thread_body(void *thread_arg)
185 fosa_rel_time_t period = fosa_msec_to_rel_time(2500);
186 fosa_rel_time_t budget = fosa_msec_to_rel_time(1400);
187 fosa_abs_time_t activation_time, after_activation_time;
188 fosa_rel_time_t elapsed_time;
189 struct timespec activation_time_tspec;
191 fosa_timer_id_t timerid;
192 fosa_clock_id_t clockid;
193 budget_timer_info_t timerinfo;
194 fosa_signal_info_t siginfo;
197 timerinfo.tid=pthread_self();
198 timerinfo.context_ptr=&context;
200 fosa_clock_get_time(FOSA_CLOCK_REALTIME,&activation_time);
202 if (fosa_thread_get_cputime_clock(timerinfo.tid,&clockid) !=0) {
203 error_status=RT_ERROR_TIMER;
204 pthread_exit ( (void*)&error_status);
207 // create budget timer
208 siginfo.sival_ptr=(void *)(&timerinfo);
209 if (fosa_timer_create (clockid,BUDGET_OVERRUN_SIGNAL,siginfo,&timerid)
212 error_status=RT_ERROR_TIMER;
213 pthread_exit ( (void*)&error_status);
216 printf("Start periodic thread body\n");
221 // set the budget timer
222 if (fosa_rel_timer_arm(timerid, &budget) != 0) {
223 error_status=RT_ERROR_TIMER;
224 pthread_exit ( (void*)&error_status);
227 printf("Begin thread %ld msec\n", fosa_abs_time_to_msec(activation_time) );
233 marte_nonlocaljmp_savecontext(&context);
234 printf ("salvado\n");
236 if (marte_nonlocaljmp_afterjmp(&context)==0) {
237 printf("start regular code\n");
240 // code executed if asynchronous jump instruction invoked */
241 printf("Aborted thread\n");
244 //printf("after block %d\n",marte_nonlocaljmp_afterjmp(&context));
245 printf("after block\n");
246 fosa_clock_get_time(FOSA_CLOCK_REALTIME, &after_activation_time);
247 elapsed_time = fosa_abs_time_extract_interval(activation_time, after_activation_time);
248 printf("End thread %ld msec\n", fosa_abs_time_to_msec(after_activation_time) );
251 activation_time = fosa_abs_time_incr(activation_time, period);
252 activation_time_tspec = fosa_abs_time_to_timespec(activation_time);
254 clock_nanosleep(FOSA_CLOCK_REALTIME,TIMER_ABSTIME,
255 &activation_time_tspec, NULL);
260 /*----------------------------------------------------------------------*/
262 /** Body of signal handler thread
263 * The signal mask is assumed to be inherited from the parent
266 void * signal_handler_thread (void * arg) {
268 fosa_signal_t set[1];
270 fosa_signal_info_t siginfo;
272 budget_timer_info_t *info;
274 // Wait for a budget overrun signal
275 set[0]=BUDGET_OVERRUN_SIGNAL;
277 if ((fosa_signal_wait(set, 1, &sig, &siginfo)) == -1) {
278 error_status=RT_ERROR_SIGWAIT;
279 pthread_exit ( (void*)&error_status);
281 // Restore periodic thread's context
282 printf("About to restore\n");
283 info=((budget_timer_info_t*) (siginfo.sival_ptr));
284 marte_nonlocaljmp_restorecontext(info->tid,info->context_ptr);
285 printf("Restored thread %d\n",info->dummy);