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 // -----------------------------------------------------------------------
59 //#include <pthread.h>
64 #include "frsh_fosa.h"
65 #include "fosa_threads_and_signals.h"
66 #include "fosa_clocks_and_timers.h"
69 #include "timespec_operations.h"
71 #include "fosa_long_jump.h"
73 /************************/
74 /** Constants and types */
75 /************************/
77 #define RT_ERROR_SIGWAIT -2
78 #define RT_ERROR_TIMER -3
79 #define RT_ERROR_HANDLER -4
82 /************************/
83 /** Global Variables */
84 /************************/
86 static fosa_long_jump_context_t context;
87 static int error_status;
90 /************************/
92 /************************/
94 static void * thread_body(void *arg);
97 /************************************************************************/
99 /************************************************************************/
103 //frsh_signal_t set[1];
104 frsh_thread_attr_t attr;
106 frsh_thread_id_t tid2;
110 //set[0]=BUDGET_OVERRUN_SIGNAL;
111 //if (fosa_set_accepted_signals(set, 1) !=0) {
112 // printf ("Error while setting the signal mask\n");
116 // Create the thread attributes object
117 if (frsh_thread_attr_init (&attr) != 0) {
118 printf("Error while initializing the attributes\n");
122 // set priority of periodic thread
123 if (fosa_thread_attr_set_prio (&attr,fosa_get_priority_min()+3) != 0) {
124 printf("Error while setting schedparam\n");
128 /* create the periodic thread */
129 terror = fosa_thread_create(&tid2, &attr, thread_body, NULL);
131 printf("pthread_create periodic thread\n");
135 printf("Main goes to sleep...\n");
141 // work to be aborted if too long
144 struct timespec exec_time={1,0}; // 1 second
147 printf("start regular code %d\n",i);
149 // eat one second of budget
150 frsh_eat(&exec_time);
151 // every five cycles eat an additional one second of budget */
153 frsh_eat(&exec_time);
159 /* ------------------------------------------------------------ */
160 /** Body of periodic thread that consumes budget */
162 static void * thread_body(void *thread_arg)
164 struct timespec period={2,500000000}; // 2.5 seconds
165 struct timespec activation_time, old_activation_time;
166 struct timespec budget ={1,400000000}; // 1.4 seconds
167 fosa_timer_id_t timerid;
168 fosa_clock_id_t clockid;
169 frsh_signal_info_t siginfo;
170 frsh_signal_t signal;
171 frsh_thread_id_t handler;
176 fosa_clock_get_time(FOSA_CLOCK_REALTIME,&activation_time);
178 if (fosa_thread_get_cputime_clock(pthread_self(),&clockid) !=0) {
179 error_status=RT_ERROR_TIMER;
180 pthread_exit ( (void*)&error_status);
183 // install long jump handler
184 if (fosa_long_jump_install_handler(&signal,&handler)!=0)
186 error_status=RT_ERROR_HANDLER;
187 pthread_exit ( (void*)&error_status);
190 // create budget timer
191 siginfo.sival_ptr=(void *)(&context);
192 if (fosa_timer_create_with_receiver
193 (clockid,signal,siginfo,&timerid,handler) != 0)
195 error_status=RT_ERROR_TIMER;
196 pthread_exit ( (void*)&error_status);
199 printf("Start periodic thread body\n");
204 // set the budget timer
205 if (fosa_timer_arm(timerid, false, &budget) != 0) {
206 error_status=RT_ERROR_TIMER;
207 pthread_exit ( (void*)&error_status);
210 printf("Begin thread main loop at %d,%d\n",(int)activation_time.tv_sec,
211 (int)(activation_time.tv_nsec/1000000));
215 err=fosa_long_jump_save_context(&context);
217 error_status=RT_ERROR_HANDLER;
218 pthread_exit ( (void*)&error_status);
221 err=fosa_long_jump_was_performed(&context,&jumped);
223 error_status=RT_ERROR_HANDLER;
224 pthread_exit ( (void*)&error_status);
229 // code executed if asynchronous jump instruction invoked */
230 printf("Aborted thread OEEEEEEEEEEEEEEEEEEEE\n");
233 printf("after abortable block\n");
234 fosa_clock_get_time(FOSA_CLOCK_REALTIME,&old_activation_time);
235 decr_timespec(&old_activation_time,&activation_time);
236 printf("End thread %6.3f\n",old_activation_time.tv_sec+(float)
237 (float)old_activation_time.tv_nsec/1000000000.0);
239 incr_timespec(&activation_time,&period);
240 clock_nanosleep(FOSA_CLOCK_REALTIME,TIMER_ABSTIME,
241 &activation_time,&old_activation_time);