]> rtime.felk.cvut.cz Git - frescor/fosa.git/blob - src_marte/tests/test_non_local_jump/test_fosa_long_jump.c
Change the makefiles of FOSA in marte which force the relocation of test
[frescor/fosa.git] / src_marte / tests / test_non_local_jump / test_fosa_long_jump.c
1 //----------------------------------------------------------------------
2 //  Copyright (C) 2006 - 2007 by the FRESCOR consortium:
3 //
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
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
17 //
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
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 // This file is part of FRSH (FRescor ScHeduler)
32 //
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.
43 //
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 #include <stdio.h>
53 #include <stdlib.h>
54 #include <unistd.h>
55 #include <time.h>
56
57 //#include <string.h>
58 //#include <errno.h>
59 //#include <pthread.h>
60 //#include <sched.h>
61 //#include <signal.h>
62 //#include <frsh.h>
63
64 #include "frsh_fosa.h"
65 #include "fosa_threads_and_signals.h"
66 #include "fosa_clocks_and_timers.h"
67
68
69 #include "timespec_operations.h"
70
71 #include "fosa_long_jump.h"
72
73 /************************/
74 /** Constants and types */
75 /************************/
76
77 #define RT_ERROR_SIGWAIT -2
78 #define RT_ERROR_TIMER   -3
79 #define RT_ERROR_HANDLER -4
80
81
82 /************************/
83 /** Global Variables    */
84 /************************/
85
86 static fosa_long_jump_context_t context;
87 static int error_status;
88
89
90 /************************/
91 /** Prototypes          */
92 /************************/
93
94 static void * thread_body(void *arg);
95
96
97 /************************************************************************/
98 /*                             Main program                             */
99 /************************************************************************/
100
101 int main()
102 {
103     //frsh_signal_t set[1];
104     frsh_thread_attr_t attr;
105
106     frsh_thread_id_t tid2;
107     int terror = 0;
108
109     // set signal mask
110     //set[0]=BUDGET_OVERRUN_SIGNAL;
111     //if (fosa_set_accepted_signals(set, 1) !=0) {
112     //  printf ("Error while setting the signal mask\n"); 
113     //  exit (1);
114     //}
115
116     // Create the thread attributes object
117     if (frsh_thread_attr_init (&attr) != 0) {
118       printf("Error while initializing the attributes\n");
119       exit(1);
120     }
121     
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");
125       exit(1);
126     }
127
128     /* create the periodic thread */
129     terror = fosa_thread_create(&tid2, &attr, thread_body, NULL);
130     if (terror) {
131         printf("pthread_create periodic thread\n");
132         exit(1);
133     }
134
135     printf("Main goes to sleep...\n");
136     sleep(20000);
137
138     return 0;
139 }
140
141 // work to be aborted if too long
142 void work() {
143   static int i=0;
144   struct timespec exec_time={1,0}; // 1 second
145
146   i++;
147   printf("start regular code %d\n",i); 
148   // regular code 
149   // eat one second of budget 
150   frsh_eat(&exec_time); 
151   // every five cycles eat an additional one second of budget */
152   if (i%5==0) { 
153     frsh_eat(&exec_time); 
154   } 
155 }
156
157
158
159 /* ------------------------------------------------------------ */
160 /** Body of periodic thread that consumes budget */
161
162 static void * thread_body(void *thread_arg)
163 {
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;
172     int jumped;
173     int err;
174
175
176     fosa_clock_get_time(FOSA_CLOCK_REALTIME,&activation_time);
177
178     if (fosa_thread_get_cputime_clock(pthread_self(),&clockid) !=0) {
179       error_status=RT_ERROR_TIMER;
180       pthread_exit ( (void*)&error_status);
181     }
182
183     // install long jump handler
184     if (fosa_long_jump_install_handler(&signal,&handler)!=0)
185         {
186           error_status=RT_ERROR_HANDLER;
187           pthread_exit ( (void*)&error_status);
188         }
189
190     // create budget timer
191     siginfo.sival_ptr=(void *)(&context);
192     if (fosa_timer_create_with_receiver 
193         (clockid,signal,siginfo,&timerid,handler) != 0) 
194       {
195         error_status=RT_ERROR_TIMER;
196         pthread_exit ( (void*)&error_status);
197       }
198
199     printf("Start periodic thread body\n");
200    
201     // main loop
202     while(1) {
203       
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);
208       }
209  
210       printf("Begin thread main loop at %d,%d\n",(int)activation_time.tv_sec, 
211              (int)(activation_time.tv_nsec/1000000));
212
213       // save context
214       
215       err=fosa_long_jump_save_context(&context);
216       if (err!=0) {
217         error_status=RT_ERROR_HANDLER;
218         pthread_exit ( (void*)&error_status);
219       }
220
221       err=fosa_long_jump_was_performed(&context,&jumped);
222       if (err!=0) {
223         error_status=RT_ERROR_HANDLER;
224         pthread_exit ( (void*)&error_status);
225       }
226       if (!jumped) { 
227         work();
228        } else { 
229         // code executed if asynchronous jump instruction invoked */
230         printf("Aborted thread OEEEEEEEEEEEEEEEEEEEE\n"); 
231       } 
232
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);
238       // sleep for a while
239       incr_timespec(&activation_time,&period);
240       clock_nanosleep(FOSA_CLOCK_REALTIME,TIMER_ABSTIME,
241                       &activation_time,&old_activation_time);
242     } // while
243 }
244
245