1 #include "fosa_threads_and_signals.h"
2 #include "fosa_clocks_and_timers.h"
3 #include "fosa_mutexes_and_condvars.h"
4 #include "fosa_app_def_sched.h"
8 //#include <time.h> // for nanosleep
11 #define printf rtl_printf
12 void * thread_code(void *arg) {
13 struct timespec req={.tv_sec=1, .tv_nsec=0};
14 printf("Thread executing\n");
16 printf("Thread terminating\n");
20 /////////////////////////////////////////////////////////////
21 // Simple test program for FOSA
23 // It just checks that the different functions can be called
24 // and that they return appropriate values
25 /////////////////////////////////////////////////////////////
27 void *main_app(void *args) {
28 //////////////////////////////////
29 // Test clock functions
30 //////////////////////////////////
32 // -----------------------------------------------------------------------
33 // Copyright (C) 2006 - 2009 FRESCOR consortium partners:
35 // Universidad de Cantabria, SPAIN
36 // University of York, UK
37 // Scuola Superiore Sant'Anna, ITALY
38 // Kaiserslautern University, GERMANY
39 // Univ. Politécnica Valencia, SPAIN
40 // Czech Technical University in Prague, CZECH REPUBLIC
42 // Thales Communication S.A. FRANCE
43 // Visual Tools S.A. SPAIN
44 // Rapita Systems Ltd UK
47 // See http://www.frescor.org for a link to partners' websites
49 // FRESCOR project (FP6/2005/IST/5-034026) is funded
50 // in part by the European Union Sixth Framework Programme
51 // The European Union is not liable of any use that may be
55 // based on previous work (FSF) done in the FIRST project
57 // Copyright (C) 2005 Mälardalen University, SWEDEN
58 // Scuola Superiore S.Anna, ITALY
59 // Universidad de Cantabria, SPAIN
60 // University of York, UK
62 // FSF API web pages: http://marte.unican.es/fsf/docs
63 // http://shark.sssup.it/contrib/first/docs/
65 // This file is part of FOSA (Frsh Operating System Adaption)
67 // FOSA is free software; you can redistribute it and/or modify it
68 // under terms of the GNU General Public License as published by the
69 // Free Software Foundation; either version 2, or (at your option) any
70 // later version. FOSA is distributed in the hope that it will be
71 // useful, but WITHOUT ANY WARRANTY; without even the implied warranty
72 // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
73 // General Public License for more details. You should have received a
74 // copy of the GNU General Public License along with FOSA; see file
75 // COPYING. If not, write to the Free Software Foundation, 675 Mass Ave,
76 // Cambridge, MA 02139, USA.
78 // As a special exception, including FOSA header files in a file,
79 // instantiating FOSA generics or templates, or linking other files
80 // with FOSA objects to produce an executable application, does not
81 // by itself cause the resulting executable application to be covered
82 // by the GNU General Public License. This exception does not
83 // however invalidate any other reasons why the executable file might be
84 // covered by the GNU Public License.
85 // -----------------------------------------------------------------------
86 printf("test thread ids and thread creation\n");
89 struct timespec req={.tv_sec=2, .tv_nsec=0};
90 frsh_thread_id_t tid1=fosa_thread_self();
91 frsh_thread_id_t tid2=fosa_thread_self();
92 printf("equal thread comparison=%d\n",fosa_thread_equal(tid1,tid2));
94 frsh_thread_attr_t th_attr;
96 err=frsh_thread_attr_init(&th_attr);
97 printf("thread attributes object initialized, err=%d\n",err);
99 err=frsh_thread_attr_set_stacksize(&th_attr,40000);
100 printf("thread attr set stack size to 40000, err=%d\n",err);
102 err=frsh_thread_attr_get_stacksize(&th_attr,&stsize);
103 printf("thread attr get stack size=%d, err=%d\n",stsize,err);
105 err=fosa_thread_attr_set_prio(&th_attr,27);
106 printf("thread attr set prio to 27, err=%d\n",err);
108 err=fosa_thread_attr_get_prio(&th_attr,&prio);
109 printf("thread attr get prio=%d, err=%d\n",prio,err);
111 err=fosa_thread_create (&tid2, &th_attr, thread_code, NULL);
112 printf("creating thread with default attributes err=%d\n",err);
116 err=frsh_thread_attr_destroy(&th_attr);
117 printf("thread attributes object destroyed, err=%d\n",err);
119 //////////////////////////////////
121 //////////////////////////////////
123 printf("--------------------------------------------------\n");
124 printf("test signals\n");
126 frsh_signal_t received;
127 frsh_signal_info_t sigvalue, value_received;
128 struct timespec timeout;
129 frsh_signal_t sig=FOSA_SIGNAL_MIN+1;
130 frsh_signal_t timer_sig=FOSA_SIGNAL_MIN+3;
131 frsh_signal_t siglist[2];
134 siglist[1]=timer_sig;
139 sigvalue.sival_int=55;
141 err=fosa_set_accepted_signals(siglist,2);
142 printf("two signals in set of accepted signals, err=%d\n",err);
144 err=fosa_signal_timedwait(siglist,1,&received,&value_received,&timeout);
145 printf("timed wait not implemented; timeoutcode=%d\n",err);
147 err=fosa_signal_queue(sig, sigvalue,fosa_thread_self());
148 printf("signal queued with value 55, err=%d\n",err);
150 err=fosa_signal_wait(siglist,1,&received, &value_received);
151 printf("timeoutcode=%d signal received=%d value=%d\n",
152 err,received,value_received.sival_int);
155 //////////////////////////////////
156 // Test timers and signals
157 //////////////////////////////////
159 printf("--------------------------------------------------\n");
160 printf("test timers and signals\n");
162 frsh_signal_info_t timer_info;
163 fosa_timer_id_t timerid;
164 struct timespec timerval, remaining_time;
166 timer_info.sival_int=88;
168 timerval.tv_nsec=300000000;
171 err=fosa_timer_create
172 (FOSA_CLOCK_REALTIME, timer_sig, timer_info,&timerid);
173 printf("timer created, err=%d\n",err);
175 err=fosa_timer_arm(timerid,false,&timerval);
176 printf("timer armed for 1.3 secs, err=%d\n",err);
178 fosa_clock_get_time(FOSA_CLOCK_REALTIME, ¤t_time);
179 printf("current time sec=%d nsec=%d\n",
180 current_time.tv_sec, current_time.tv_nsec);
181 printf("wait for timer to expire...\n");
183 siglist[0]=timer_sig;
184 err=fosa_signal_wait(siglist,1,&received, &value_received);
185 printf("timeoutcode=%d signal received=%d value=%d\n",
186 err,received,value_received.sival_int);
188 fosa_clock_get_time(FOSA_CLOCK_REALTIME, ¤t_time);
189 printf("current time sec=%d nsec=%d\n",
190 current_time.tv_sec, current_time.tv_nsec);
194 err=fosa_timer_arm(timerid,false,&timerval);
195 printf("timer armed for 6 secs, err=%d\n",err);
198 printf("sleeping 1 second\n");
199 nanosleep(&timerval, NULL);
201 err=fosa_timer_get_remaining_time(timerid, &remaining_time);
202 printf("timer remaining time sec=%d nsec=%d, err=%d\n",
203 remaining_time.tv_sec, remaining_time.tv_nsec, err);
205 printf("sleeping 1 second\n");
206 nanosleep(&timerval, NULL);
208 err=fosa_timer_disarm(timerid,&remaining_time);
209 printf("timer disarmed, remaining time sec=%d nsec=%d, err=%d\n",
210 remaining_time.tv_sec, remaining_time.tv_nsec, err);
212 err=fosa_timer_get_remaining_time(timerid, &remaining_time);
213 printf("timer remaining time after disarm (0?) sec=%d nsec=%d, err=%d\n",
214 remaining_time.tv_sec, remaining_time.tv_nsec, err);
216 fosa_timer_delete(timerid);
219 //////////////////////////////////
220 // Test thread-specific data
221 //////////////////////////////////
223 printf("--------------------------------------------------\n");
224 printf("test thread-specific data\n");
228 frsh_thread_id_t tid=fosa_thread_self();
230 err=fosa_key_create(&key);
231 printf("key created=%d. err=%d\n",key,err);
233 fosa_thread_set_specific_data (key, tid, (void *) (&value));
234 printf("specific data set to 333. err=%d\n",err);
236 fosa_thread_get_specific_data (key, tid, &obtained);
237 printf("obtained thread specific data=%d\n",*((int *)obtained));
240 //////////////////////////////////
242 //////////////////////////////////
244 printf("--------------------------------------------------\n");
245 printf("test priorities\n");
248 err=fosa_thread_set_prio(fosa_thread_self(),14);
249 printf("priority set to 14. err=%d\n",err);
251 err=fosa_thread_get_prio(fosa_thread_self(),&prio);
252 printf("prio=%d. err=%d\n",prio,err);
255 //////////////////////////////////
257 //////////////////////////////////
259 printf("--------------------------------------------------\n");
260 printf("test mutexes\n");
265 err=fosa_mutex_init(&lock,24);
266 printf("mutex initialized with ceiling 24. err=%d\n",err);
268 err=fosa_mutex_set_prioceiling(&lock,24,&old);
269 printf("mutex priority ceiling changed to 24. old=%d. err=%d\n",old,err);
271 err=fosa_mutex_get_prioceiling(&lock,&old);
272 printf("mutex priority ceiling is=%d. err=%d\n",old,err);
274 err=fosa_mutex_lock(&lock);
275 printf("mutex locked. err=%d\n",err);
277 err=fosa_mutex_unlock(&lock);
278 printf("mutex unlocked. err=%d\n",err);
280 err=fosa_mutex_trylock(&lock);
281 printf("mutex try locked. err=%d\n",err);
283 err=fosa_mutex_unlock(&lock);
284 printf("mutex unlocked. err=%d\n",err);
286 //////////////////////////////////
287 // Test Condition variables
288 //////////////////////////////////
290 printf("--------------------------------------------------\n");
291 printf("test condition variables\n");
295 err=fosa_cond_init(&cond);
296 printf("condvar initialized. err=%d\n",err);
298 err=fosa_cond_signal(&cond);
299 printf("cond signalled. err=%d\n",err);
301 err=fosa_cond_broadcast(&cond);
302 printf("cond broadcast. err=%d\n",err);
304 fosa_clock_get_time(FOSA_CLOCK_REALTIME, ¤t_time);
305 printf("current time sec=%d nsec=%d\n",
306 current_time.tv_sec, current_time.tv_nsec);
308 current_time.tv_sec=current_time.tv_sec+2;
310 fosa_mutex_lock(&lock);
311 err=fosa_cond_timedwait(&cond,&lock,¤t_time);
312 fosa_mutex_unlock(&lock);
313 printf("cond timedwait with timeout=2 sec. err=%d\n",err);
315 fosa_clock_get_time(FOSA_CLOCK_REALTIME, ¤t_time);
316 printf("current time sec=%d nsec=%d\n",
317 current_time.tv_sec, current_time.tv_nsec);
319 err=fosa_mutex_destroy(&lock);
320 printf("mutex destroyed. err=%d\n",err);
322 err=fosa_cond_destroy(&cond);
323 printf("cond destroyed. err=%d\n",err);
325 ////////////////////////////////////////
326 // Test Application-defined scheduling
327 ///////////////////////////////////////
329 printf("--------------------------------------------------\n");
330 printf("test application-defined scheduling\n");
332 frsh_thread_attr_t th1_attr;
335 err=frsh_thread_attr_init(&th1_attr);
336 printf("thread attributes object initialized, err=%d\n",err);
338 err=fosa_thread_attr_set_appscheduled(&th1_attr,true);
339 printf("thread attr set appsched, err=%d\n",err);
341 err=fosa_thread_attr_get_appscheduled(&th1_attr,&is_appsched);
342 printf("thread attr get appsched=%d, err=%d\n",is_appsched,err);
348 static pthread_t main_thread;
350 int init_module (void) {
351 return pthread_create(&main_thread, 0, main_app, 0);
354 void cleanup_module(void) {
355 pthread_delete_np(main_thread);