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 // -----------------------------------------------------------------------
55 #include <time.h> // for nanosleep
59 void * thread_code(void *arg) {
60 printf("Thread executing\n");
62 printf("Thread terminating\n");
66 /////////////////////////////////////////////////////////////
67 // Simple test program for FOSA
69 // It just checks that the different functions can be called
70 // and that they return appropriate values
71 /////////////////////////////////////////////////////////////
75 //////////////////////////////////
76 // Test clock functions
77 //////////////////////////////////
79 printf("--------------------------------------------------\n");
80 printf("test get_time\n");
82 fosa_abs_time_t current_time;
85 fosa_clock_id_t cpu_clock;
87 err=fosa_clock_get_time(FOSA_CLOCK_REALTIME, ¤t_time);
88 printf("fosa_clock_get_time for CLOCK_REALTIME %ld msec err=%d\n",
89 fosa_rel_time_to_msec(current_time), err);
91 fosa_thread_get_cputime_clock(fosa_thread_self(), &cpu_clock);
92 err=fosa_clock_get_time(cpu_clock, ¤t_time);
93 printf("fosa_clock_get_time for CPU-time clock %ld msec err=%d\n",
94 fosa_rel_time_to_msec(current_time), err);
96 /////////////////////////////////////////////
97 // Test thread ids, attributes and creation
98 ////////////////////////////////////////////
100 printf("--------------------------------------------------\n");
101 printf("test thread ids and thread creation\n");
105 fosa_thread_id_t tid1=fosa_thread_self();
106 fosa_thread_id_t tid2=fosa_thread_self();
107 printf("equal thread comparison=%d\n",fosa_thread_equal(tid1,tid2));
109 fosa_thread_attr_t th_attr;
111 err=fosa_thread_attr_init(&th_attr);
112 printf("thread attributes object initialized, err=%d\n",err);
114 err=fosa_thread_attr_set_stacksize(&th_attr,40000);
115 printf("thread attr set stack size to 40000, err=%d\n",err);
117 err=fosa_thread_attr_get_stacksize(&th_attr,&stsize);
118 printf("thread attr get stack size=%d, err=%d\n",stsize,err);
120 err=fosa_thread_attr_set_prio(&th_attr,27);
121 printf("thread attr set prio to 27, err=%d\n",err);
123 err=fosa_thread_attr_get_prio(&th_attr,&prio);
124 printf("thread attr get prio=%d, err=%d\n",prio,err);
126 err=fosa_thread_create (&tid2, &th_attr, thread_code, NULL);
127 printf("creating thread with default attributes err=%d\n",err);
131 err=fosa_thread_attr_destroy(&th_attr);
132 printf("thread attributes object destroyed, err=%d\n",err);
134 //////////////////////////////////
136 //////////////////////////////////
138 printf("--------------------------------------------------\n");
139 printf("test signals\n");
141 fosa_signal_t received;
142 fosa_signal_info_t sigvalue, value_received;
143 fosa_rel_time_t timeout;
144 fosa_signal_t sig=FOSA_SIGNAL_MIN+1;
145 fosa_signal_t timer_sig=FOSA_SIGNAL_MIN+3;
146 fosa_signal_t siglist[2];
149 siglist[1]=timer_sig;
151 timeout = fosa_msec_to_rel_time(1000);
153 sigvalue.sival_int=55;
155 err=fosa_set_accepted_signals(siglist,2);
156 printf("two signals in set of accepted signals, err=%d\n",err);
158 err=fosa_signal_timedwait(siglist,1,&received,&value_received,&timeout);
159 printf("timed wait not implemented; timeoutcode=%d\n",err);
161 err=fosa_signal_queue(sig, sigvalue,fosa_thread_self());
162 printf("signal queued with value 55, err=%d\n",err);
164 err=fosa_signal_wait(siglist,1,&received, &value_received);
165 printf("timeoutcode=%d signal received=%d value=%d\n",
166 err,received,value_received.sival_int);
168 //////////////////////////////////
169 // Test timers and signals
170 //////////////////////////////////
172 printf("--------------------------------------------------\n");
173 printf("test timers and signals\n");
175 fosa_signal_info_t timer_info;
176 fosa_timer_id_t timerid;
177 fosa_rel_time_t timerval, remaining_time;
179 timer_info.sival_int=88;
180 timerval = fosa_msec_to_rel_time(1300);
182 err=fosa_timer_create
183 (FOSA_CLOCK_REALTIME, timer_sig, timer_info,&timerid);
184 printf("timer created, err=%d\n",err);
186 err=fosa_rel_timer_arm(timerid, &timerval);
187 printf("timer armed for 1.3 secs, err=%d\n",err);
189 fosa_clock_get_time(FOSA_CLOCK_REALTIME, ¤t_time);
190 printf("current time %ld msec\n", fosa_rel_time_to_msec(current_time) );
191 printf("wait for timer to expire...\n");
193 siglist[0]=timer_sig;
194 err=fosa_signal_wait(siglist,1,&received, &value_received);
195 printf("timeoutcode=%d signal received=%d value=%d\n",
196 err,received,value_received.sival_int);
198 fosa_clock_get_time(FOSA_CLOCK_REALTIME, ¤t_time);
199 printf("current time: %ld msec\n", fosa_rel_time_to_msec(current_time) );
201 timerval = fosa_msec_to_rel_time(6000);
202 err=fosa_rel_timer_arm(timerid, &timerval);
203 printf("timer armed for 6 secs, err=%d\n",err);
205 struct timespec timerval_tspec = {1, 0};
206 printf("sleeping 1 second\n");
207 nanosleep(&timerval_tspec, NULL);
209 err=fosa_timer_get_remaining_time(timerid, &remaining_time);
210 printf("timer remaining time: %ld msec\n", fosa_rel_time_to_msec(remaining_time) );
212 printf("sleeping 1 second\n");
213 nanosleep(&timerval_tspec, NULL);
215 err=fosa_timer_disarm(timerid,&remaining_time);
216 printf("timer disarmed, remaining time: %ld msec, err=%d\n",
217 fosa_rel_time_to_msec(remaining_time), err);
219 err=fosa_timer_get_remaining_time(timerid, &remaining_time);
220 printf("timer remaining time after disarm (0?) %ld msec, err=%d\n",
221 fosa_rel_time_to_msec(remaining_time), err);
223 fosa_timer_delete(timerid);
226 //////////////////////////////////
227 // Test thread-specific data
228 //////////////////////////////////
230 printf("--------------------------------------------------\n");
231 printf("test thread-specific data\n");
235 fosa_thread_id_t tid=fosa_thread_self();
237 err=fosa_key_create(&key);
238 printf("key created=%d. err=%d\n",key,err);
240 fosa_thread_set_specific_data (key, tid, (void *) (&value));
241 printf("specific data set to 333. err=%d\n",err);
243 fosa_thread_get_specific_data (key, tid, &obtained);
244 printf("obtained thread specific data=%d\n",*((int *)obtained));
247 //////////////////////////////////
249 //////////////////////////////////
251 printf("--------------------------------------------------\n");
252 printf("test priorities\n");
255 err=fosa_thread_set_prio(fosa_thread_self(),14);
256 printf("priority set to 14. err=%d\n",err);
258 err=fosa_thread_get_prio(fosa_thread_self(),&prio);
259 printf("prio=%d. err=%d\n",prio,err);
262 //////////////////////////////////
264 //////////////////////////////////
266 printf("--------------------------------------------------\n");
267 printf("test mutexes\n");
272 err=fosa_mutex_init(&lock,24);
273 printf("mutex initialized with ceiling 24. err=%d\n",err);
275 err=fosa_mutex_set_prioceiling(&lock,24,&old);
276 printf("mutex priority ceiling changed to 24. old=%d. err=%d\n",old,err);
278 err=fosa_mutex_get_prioceiling(&lock,&old);
279 printf("mutex priority ceiling is=%d. err=%d\n",old,err);
281 err=fosa_mutex_lock(&lock);
282 printf("mutex locked. err=%d\n",err);
284 err=fosa_mutex_unlock(&lock);
285 printf("mutex unlocked. err=%d\n",err);
287 err=fosa_mutex_trylock(&lock);
288 printf("mutex try locked. err=%d\n",err);
290 err=fosa_mutex_unlock(&lock);
291 printf("mutex unlocked. err=%d\n",err);
293 //////////////////////////////////
294 // Test Condition variables
295 //////////////////////////////////
297 printf("--------------------------------------------------\n");
298 printf("test condition variables\n");
302 err=fosa_cond_init(&cond);
303 printf("condvar initialized. err=%d\n",err);
305 err=fosa_cond_signal(&cond);
306 printf("cond signalled. err=%d\n",err);
308 err=fosa_cond_broadcast(&cond);
309 printf("cond broadcast. err=%d\n",err);
311 fosa_clock_get_time(FOSA_CLOCK_REALTIME, ¤t_time);
312 printf("current time %ld msec\n", fosa_abs_time_to_msec(current_time) );
314 current_time = fosa_abs_time_incr(current_time, fosa_msec_to_rel_time(2000) );
316 fosa_mutex_lock(&lock);
317 err=fosa_cond_timedwait(&cond,&lock,¤t_time);
318 fosa_mutex_unlock(&lock);
319 printf("cond timedwait with timeout=2 sec. err=%d\n",err);
321 fosa_clock_get_time(FOSA_CLOCK_REALTIME, ¤t_time);
322 printf("current time %ld msec\n", fosa_abs_time_to_msec(current_time) );
324 err=fosa_mutex_destroy(&lock);
325 printf("mutex destroyed. err=%d\n",err);
327 err=fosa_cond_destroy(&cond);
328 printf("cond destroyed. err=%d\n",err);
330 ////////////////////////////////////////
331 // Test Application-defined scheduling
332 ///////////////////////////////////////
334 printf("--------------------------------------------------\n");
335 printf("test application-defined scheduling\n");
337 fosa_thread_attr_t th1_attr;
340 err=fosa_thread_attr_init(&th1_attr);
341 printf("thread attributes object initialized, err=%d\n",err);
343 err=fosa_thread_attr_set_appscheduled(&th1_attr,true);
344 printf("thread attr set appsched, err=%d\n",err);
346 err=fosa_thread_attr_get_appscheduled(&th1_attr,&is_appsched);
347 printf("thread attr get appsched=%d, err=%d\n",is_appsched,err);