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 // -----------------------------------------------------------------------
58 #include <time.h> // for nanosleep
62 void * thread_code(void *arg) {
63 printf("Thread executing\n");
65 printf("Thread terminating\n");
69 /////////////////////////////////////////////////////////////
70 // Simple test program for FOSA
72 // It just checks that the different functions can be called
73 // and that they return appropriate values
74 /////////////////////////////////////////////////////////////
78 //////////////////////////////////
79 // Test clock functions
80 //////////////////////////////////
82 printf("--------------------------------------------------\n");
83 printf("test get_time\n");
85 fosa_abs_time_t current_time;
88 fosa_clock_id_t cpu_clock;
90 err=fosa_clock_get_time(FOSA_CLOCK_REALTIME, ¤t_time);
91 printf("fosa_clock_get_time for CLOCK_REALTIME %ld msec err=%d\n",
92 fosa_rel_time_to_msec(current_time), err);
94 fosa_thread_get_cputime_clock(fosa_thread_self(), &cpu_clock);
95 err=fosa_clock_get_time(cpu_clock, ¤t_time);
96 printf("fosa_clock_get_time for CPU-time clock %ld msec err=%d\n",
97 fosa_rel_time_to_msec(current_time), err);
99 /////////////////////////////////////////////
100 // Test thread ids, attributes and creation
101 ////////////////////////////////////////////
103 printf("--------------------------------------------------\n");
104 printf("test thread ids and thread creation\n");
108 fosa_thread_id_t tid1=fosa_thread_self();
109 fosa_thread_id_t tid2=fosa_thread_self();
110 printf("equal thread comparison=%d\n",fosa_thread_equal(tid1,tid2));
112 fosa_thread_attr_t th_attr;
114 err=fosa_thread_attr_init(&th_attr);
115 printf("thread attributes object initialized, err=%d\n",err);
117 err=fosa_thread_attr_set_stacksize(&th_attr,40000);
118 printf("thread attr set stack size to 40000, err=%d\n",err);
120 err=fosa_thread_attr_get_stacksize(&th_attr,&stsize);
121 printf("thread attr get stack size=%d, err=%d\n",stsize,err);
123 err=fosa_thread_attr_set_prio(&th_attr,27);
124 printf("thread attr set prio to 27, err=%d\n",err);
126 err=fosa_thread_attr_get_prio(&th_attr,&prio);
127 printf("thread attr get prio=%d, err=%d\n",prio,err);
129 err=fosa_thread_create (&tid2, &th_attr, thread_code, NULL);
130 printf("creating thread with default attributes err=%d\n",err);
134 err=fosa_thread_attr_destroy(&th_attr);
135 printf("thread attributes object destroyed, err=%d\n",err);
137 //////////////////////////////////
139 //////////////////////////////////
141 printf("--------------------------------------------------\n");
142 printf("test signals\n");
144 fosa_signal_t received;
145 fosa_signal_info_t sigvalue, value_received;
146 fosa_rel_time_t timeout;
147 fosa_signal_t sig=FOSA_SIGNAL_MIN+1;
148 fosa_signal_t timer_sig=FOSA_SIGNAL_MIN+3;
149 fosa_signal_t siglist[2];
152 siglist[1]=timer_sig;
154 timeout = fosa_msec_to_rel_time(1000);
156 sigvalue.sival_int=55;
158 err=fosa_set_accepted_signals(siglist,2);
159 printf("two signals in set of accepted signals, err=%d\n",err);
161 err=fosa_signal_timedwait(siglist,1,&received,&value_received,&timeout);
162 printf("timed wait not implemented; timeoutcode=%d\n",err);
164 err=fosa_signal_queue(sig, sigvalue,fosa_thread_self());
165 printf("signal queued with value 55, err=%d\n",err);
167 err=fosa_signal_wait(siglist,1,&received, &value_received);
168 printf("timeoutcode=%d signal received=%d value=%d\n",
169 err,received,value_received.sival_int);
171 //////////////////////////////////
172 // Test timers and signals
173 //////////////////////////////////
175 printf("--------------------------------------------------\n");
176 printf("test timers and signals\n");
178 fosa_signal_info_t timer_info;
179 fosa_timer_id_t timerid;
180 fosa_rel_time_t timerval, remaining_time;
182 timer_info.sival_int=88;
183 timerval = fosa_msec_to_rel_time(1300);
185 err=fosa_timer_create
186 (FOSA_CLOCK_REALTIME, timer_sig, timer_info,&timerid);
187 printf("timer created, err=%d\n",err);
189 err=fosa_rel_timer_arm(timerid, &timerval);
190 printf("timer armed for 1.3 secs, err=%d\n",err);
192 fosa_clock_get_time(FOSA_CLOCK_REALTIME, ¤t_time);
193 printf("current time %ld msec\n", fosa_rel_time_to_msec(current_time) );
194 printf("wait for timer to expire...\n");
196 siglist[0]=timer_sig;
197 err=fosa_signal_wait(siglist,1,&received, &value_received);
198 printf("timeoutcode=%d signal received=%d value=%d\n",
199 err,received,value_received.sival_int);
201 fosa_clock_get_time(FOSA_CLOCK_REALTIME, ¤t_time);
202 printf("current time: %ld msec\n", fosa_rel_time_to_msec(current_time) );
204 timerval = fosa_msec_to_rel_time(6000);
205 err=fosa_rel_timer_arm(timerid, &timerval);
206 printf("timer armed for 6 secs, err=%d\n",err);
208 struct timespec timerval_tspec = {1, 0};
209 printf("sleeping 1 second\n");
210 nanosleep(&timerval_tspec, NULL);
212 err=fosa_timer_get_remaining_time(timerid, &remaining_time);
213 printf("timer remaining time: %ld msec\n", fosa_rel_time_to_msec(remaining_time) );
215 printf("sleeping 1 second\n");
216 nanosleep(&timerval_tspec, NULL);
218 err=fosa_timer_disarm(timerid,&remaining_time);
219 printf("timer disarmed, remaining time: %ld msec, err=%d\n",
220 fosa_rel_time_to_msec(remaining_time), err);
222 err=fosa_timer_get_remaining_time(timerid, &remaining_time);
223 printf("timer remaining time after disarm (0?) %ld msec, err=%d\n",
224 fosa_rel_time_to_msec(remaining_time), err);
226 fosa_timer_delete(timerid);
229 //////////////////////////////////
230 // Test thread-specific data
231 //////////////////////////////////
233 printf("--------------------------------------------------\n");
234 printf("test thread-specific data\n");
238 fosa_thread_id_t tid=fosa_thread_self();
240 err=fosa_key_create(&key);
241 printf("key created=%d. err=%d\n",key,err);
243 fosa_thread_set_specific_data (key, tid, (void *) (&value));
244 printf("specific data set to 333. err=%d\n",err);
246 fosa_thread_get_specific_data (key, tid, &obtained);
247 printf("obtained thread specific data=%d\n",*((int *)obtained));
250 //////////////////////////////////
252 //////////////////////////////////
254 printf("--------------------------------------------------\n");
255 printf("test priorities\n");
258 err=fosa_thread_set_prio(fosa_thread_self(),14);
259 printf("priority set to 14. err=%d\n",err);
261 err=fosa_thread_get_prio(fosa_thread_self(),&prio);
262 printf("prio=%d. err=%d\n",prio,err);
265 //////////////////////////////////
267 //////////////////////////////////
269 printf("--------------------------------------------------\n");
270 printf("test mutexes\n");
275 err=fosa_mutex_init(&lock,24);
276 printf("mutex initialized with ceiling 24. err=%d\n",err);
278 err=fosa_mutex_set_prioceiling(&lock,24,&old);
279 printf("mutex priority ceiling changed to 24. old=%d. err=%d\n",old,err);
281 err=fosa_mutex_get_prioceiling(&lock,&old);
282 printf("mutex priority ceiling is=%d. err=%d\n",old,err);
284 err=fosa_mutex_lock(&lock);
285 printf("mutex locked. err=%d\n",err);
287 err=fosa_mutex_unlock(&lock);
288 printf("mutex unlocked. err=%d\n",err);
290 err=fosa_mutex_trylock(&lock);
291 printf("mutex try locked. err=%d\n",err);
293 err=fosa_mutex_unlock(&lock);
294 printf("mutex unlocked. err=%d\n",err);
296 //////////////////////////////////
297 // Test Condition variables
298 //////////////////////////////////
300 printf("--------------------------------------------------\n");
301 printf("test condition variables\n");
305 err=fosa_cond_init(&cond);
306 printf("condvar initialized. err=%d\n",err);
308 err=fosa_cond_signal(&cond);
309 printf("cond signalled. err=%d\n",err);
311 err=fosa_cond_broadcast(&cond);
312 printf("cond broadcast. err=%d\n",err);
314 fosa_clock_get_time(FOSA_CLOCK_REALTIME, ¤t_time);
315 printf("current time %ld msec\n", fosa_abs_time_to_msec(current_time) );
317 current_time = fosa_abs_time_incr(current_time, fosa_msec_to_rel_time(2000) );
319 fosa_mutex_lock(&lock);
320 err=fosa_cond_timedwait(&cond,&lock,¤t_time);
321 fosa_mutex_unlock(&lock);
322 printf("cond timedwait with timeout=2 sec. err=%d\n",err);
324 fosa_clock_get_time(FOSA_CLOCK_REALTIME, ¤t_time);
325 printf("current time %ld msec\n", fosa_abs_time_to_msec(current_time) );
327 err=fosa_mutex_destroy(&lock);
328 printf("mutex destroyed. err=%d\n",err);
330 err=fosa_cond_destroy(&cond);
331 printf("cond destroyed. err=%d\n",err);
333 ////////////////////////////////////////
334 // Test Application-defined scheduling
335 ///////////////////////////////////////
337 printf("--------------------------------------------------\n");
338 printf("test application-defined scheduling\n");
340 fosa_thread_attr_t th1_attr;
343 err=fosa_thread_attr_init(&th1_attr);
344 printf("thread attributes object initialized, err=%d\n",err);
346 err=fosa_thread_attr_set_appscheduled(&th1_attr,true);
347 printf("thread attr set appsched, err=%d\n",err);
349 err=fosa_thread_attr_get_appscheduled(&th1_attr,&is_appsched);
350 printf("thread attr get appsched=%d, err=%d\n",is_appsched,err);