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 FOSA (Frsh Operating System Abstraction)
33 // FOSA 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. FOSA 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 FOSA; 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 FOSA header files in a file,
45 // instantiating FOSA generics or templates, or linking other files
46 // with FOSA 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 // -----------------------------------------------------------------------
53 #include "fosa_threads_and_signals.h"
54 #include "fosa_clocks_and_timers.h"
55 #include "fosa_mutexes_and_condvars.h"
56 #include "fosa_app_def_sched.h"
59 #include <time.h> // for nanosleep
61 void * thread_code(void *arg) {
62 printf("Thread executing\n");
64 printf("Thread terminating\n");
68 /////////////////////////////////////////////////////////////
69 // Simple test program for FOSA
71 // It just checks that the different functions can be called
72 // and that they return appropriate values
73 /////////////////////////////////////////////////////////////
77 //////////////////////////////////
78 // Test clock functions
79 //////////////////////////////////
81 printf("--------------------------------------------------\n");
82 printf("test get_time\n");
84 struct timespec current_time;
87 fosa_clock_id_t cpu_clock;
89 err=fosa_clock_get_time(FOSA_CLOCK_REALTIME, ¤t_time);
90 printf("fosa_clock_get_time for CLOCK_REALTIME sec=%d nsec=%d err=%d\n",
91 current_time.tv_sec, current_time.tv_nsec,err);
93 fosa_thread_get_cputime_clock(fosa_thread_self(), &cpu_clock);
94 err=fosa_clock_get_time(cpu_clock, ¤t_time);
95 printf("fosa_clock_get_time for CPU-time clock sec=%d nsec=%d err=%d\n",
96 current_time.tv_sec, current_time.tv_nsec,err);
98 /////////////////////////////////////////////
99 // Test thread ids, attributes and creation
100 ////////////////////////////////////////////
102 printf("--------------------------------------------------\n");
103 printf("test thread ids and thread creation\n");
107 frsh_thread_id_t tid1=fosa_thread_self();
108 frsh_thread_id_t tid2=fosa_thread_self();
109 printf("equal thread comparison=%d\n",fosa_thread_equal(tid1,tid2));
111 frsh_thread_attr_t th_attr;
113 err=frsh_thread_attr_init(&th_attr);
114 printf("thread attributes object initialized, err=%d\n",err);
116 err=frsh_thread_attr_set_stacksize(&th_attr,40000);
117 printf("thread attr set stack size to 40000, err=%d\n",err);
119 err=frsh_thread_attr_get_stacksize(&th_attr,&stsize);
120 printf("thread attr get stack size=%d, err=%d\n",stsize,err);
122 err=fosa_thread_attr_set_prio(&th_attr,27);
123 printf("thread attr set prio to 27, err=%d\n",err);
125 err=fosa_thread_attr_get_prio(&th_attr,&prio);
126 printf("thread attr get prio=%d, err=%d\n",prio,err);
128 err=fosa_thread_create (&tid2, &th_attr, thread_code, NULL);
129 printf("creating thread with default attributes err=%d\n",err);
133 err=frsh_thread_attr_destroy(&th_attr);
134 printf("thread attributes object destroyed, err=%d\n",err);
136 //////////////////////////////////
138 //////////////////////////////////
140 printf("--------------------------------------------------\n");
141 printf("test signals\n");
143 frsh_signal_t received;
144 frsh_signal_info_t sigvalue, value_received;
145 struct timespec timeout;
146 frsh_signal_t sig=FOSA_SIGNAL_MIN+1;
147 frsh_signal_t timer_sig=FOSA_SIGNAL_MIN+3;
148 frsh_signal_t siglist[2];
151 siglist[1]=timer_sig;
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 frsh_signal_info_t timer_info;
179 fosa_timer_id_t timerid;
180 struct timespec timerval, remaining_time;
182 timer_info.sival_int=88;
184 timerval.tv_nsec=300000000;
186 err=fosa_timer_create
187 (FOSA_CLOCK_REALTIME, timer_sig, timer_info,&timerid);
188 printf("timer created, err=%d\n",err);
190 err=fosa_timer_arm(timerid,false,&timerval);
191 printf("timer armed for 1.3 secs, err=%d\n",err);
193 fosa_clock_get_time(FOSA_CLOCK_REALTIME, ¤t_time);
194 printf("current time sec=%d nsec=%d\n",
195 current_time.tv_sec, current_time.tv_nsec);
196 printf("wait for timer to expire...\n");
198 siglist[0]=timer_sig;
199 err=fosa_signal_wait(siglist,1,&received, &value_received);
200 printf("timeoutcode=%d signal received=%d value=%d\n",
201 err,received,value_received.sival_int);
203 fosa_clock_get_time(FOSA_CLOCK_REALTIME, ¤t_time);
204 printf("current time sec=%d nsec=%d\n",
205 current_time.tv_sec, current_time.tv_nsec);
209 err=fosa_timer_arm(timerid,false,&timerval);
210 printf("timer armed for 6 secs, err=%d\n",err);
213 printf("sleeping 1 second\n");
214 nanosleep(&timerval, NULL);
216 err=fosa_timer_get_remaining_time(timerid, &remaining_time);
217 printf("timer remaining time sec=%d nsec=%d, err=%d\n",
218 remaining_time.tv_sec, remaining_time.tv_nsec, err);
220 printf("sleeping 1 second\n");
221 nanosleep(&timerval, NULL);
223 err=fosa_timer_disarm(timerid,&remaining_time);
224 printf("timer disarmed, remaining time sec=%d nsec=%d, err=%d\n",
225 remaining_time.tv_sec, remaining_time.tv_nsec, err);
227 err=fosa_timer_get_remaining_time(timerid, &remaining_time);
228 printf("timer remaining time after disarm (0?) sec=%d nsec=%d, err=%d\n",
229 remaining_time.tv_sec, remaining_time.tv_nsec, err);
231 fosa_timer_delete(timerid);
234 //////////////////////////////////
235 // Test thread-specific data
236 //////////////////////////////////
238 printf("--------------------------------------------------\n");
239 printf("test thread-specific data\n");
243 frsh_thread_id_t tid=fosa_thread_self();
245 err=fosa_key_create(&key);
246 printf("key created=%d. err=%d\n",key,err);
248 fosa_thread_set_specific_data (key, tid, (void *) (&value));
249 printf("specific data set to 333. err=%d\n",err);
251 fosa_thread_get_specific_data (key, tid, &obtained);
252 printf("obtained thread specific data=%d\n",*((int *)obtained));
255 //////////////////////////////////
257 //////////////////////////////////
259 printf("--------------------------------------------------\n");
260 printf("test priorities\n");
263 err=fosa_thread_set_prio(fosa_thread_self(),14);
264 printf("priority set to 14. err=%d\n",err);
266 err=fosa_thread_get_prio(fosa_thread_self(),&prio);
267 printf("prio=%d. err=%d\n",prio,err);
270 //////////////////////////////////
272 //////////////////////////////////
274 printf("--------------------------------------------------\n");
275 printf("test mutexes\n");
280 err=fosa_mutex_init(&lock,24);
281 printf("mutex initialized with ceiling 24. err=%d\n",err);
283 err=fosa_mutex_set_prioceiling(&lock,24,&old);
284 printf("mutex priority ceiling changed to 24. old=%d. err=%d\n",old,err);
286 err=fosa_mutex_get_prioceiling(&lock,&old);
287 printf("mutex priority ceiling is=%d. err=%d\n",old,err);
289 err=fosa_mutex_lock(&lock);
290 printf("mutex locked. err=%d\n",err);
292 err=fosa_mutex_unlock(&lock);
293 printf("mutex unlocked. err=%d\n",err);
295 err=fosa_mutex_trylock(&lock);
296 printf("mutex try locked. err=%d\n",err);
298 err=fosa_mutex_unlock(&lock);
299 printf("mutex unlocked. err=%d\n",err);
301 //////////////////////////////////
302 // Test Condition variables
303 //////////////////////////////////
305 printf("--------------------------------------------------\n");
306 printf("test condition variables\n");
310 err=fosa_cond_init(&cond);
311 printf("condvar initialized. err=%d\n",err);
313 err=fosa_cond_signal(&cond);
314 printf("cond signalled. err=%d\n",err);
316 err=fosa_cond_broadcast(&cond);
317 printf("cond broadcast. err=%d\n",err);
319 fosa_clock_get_time(FOSA_CLOCK_REALTIME, ¤t_time);
320 printf("current time sec=%d nsec=%d\n",
321 current_time.tv_sec, current_time.tv_nsec);
323 current_time.tv_sec=current_time.tv_sec+2;
325 fosa_mutex_lock(&lock);
326 err=fosa_cond_timedwait(&cond,&lock,¤t_time);
327 fosa_mutex_unlock(&lock);
328 printf("cond timedwait with timeout=2 sec. err=%d\n",err);
330 fosa_clock_get_time(FOSA_CLOCK_REALTIME, ¤t_time);
331 printf("current time sec=%d nsec=%d\n",
332 current_time.tv_sec, current_time.tv_nsec);
334 err=fosa_mutex_destroy(&lock);
335 printf("mutex destroyed. err=%d\n",err);
337 err=fosa_cond_destroy(&cond);
338 printf("cond destroyed. err=%d\n",err);
340 ////////////////////////////////////////
341 // Test Application-defined scheduling
342 ///////////////////////////////////////
344 printf("--------------------------------------------------\n");
345 printf("test application-defined scheduling\n");
347 frsh_thread_attr_t th1_attr;
350 err=frsh_thread_attr_init(&th1_attr);
351 printf("thread attributes object initialized, err=%d\n",err);
353 err=fosa_thread_attr_set_appscheduled(&th1_attr,true);
354 printf("thread attr set appsched, err=%d\n",err);
356 err=fosa_thread_attr_get_appscheduled(&th1_attr,&is_appsched);
357 printf("thread attr get appsched=%d, err=%d\n",is_appsched,err);