]> rtime.felk.cvut.cz Git - frescor/fosa.git/blob - src_marte/tests/test_fosa_general/test_fosa.c
8ee17ee89ad8c3197b52e7fb0e9119e076aedfd3
[frescor/fosa.git] / src_marte / tests / test_fosa_general / test_fosa.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 FOSA (Frsh Operating System Abstraction)
32 //
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.
43 //
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 // -----------------------------------------------------------------------
52
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"
57 #include <unistd.h>
58 #include <stdio.h>
59 #include <time.h> // for nanosleep
60
61 void * thread_code(void *arg) {
62   printf("Thread executing\n");
63   sleep(1);
64   printf("Thread terminating\n");
65   return NULL;
66 }
67
68 /////////////////////////////////////////////////////////////
69 // Simple test program for FOSA
70 //
71 // It just checks that the different functions can be called
72 // and that they return appropriate values
73 /////////////////////////////////////////////////////////////
74
75 int main () {
76
77   //////////////////////////////////
78   //  Test clock functions
79   //////////////////////////////////
80
81   printf("--------------------------------------------------\n");
82   printf("test get_time\n");
83
84   struct timespec current_time;
85   int err;
86   void * obtained;
87   fosa_clock_id_t cpu_clock;
88
89   err=fosa_clock_get_time(FOSA_CLOCK_REALTIME, &current_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);
92
93   fosa_thread_get_cputime_clock(fosa_thread_self(), &cpu_clock);
94   err=fosa_clock_get_time(cpu_clock, &current_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);
97
98   /////////////////////////////////////////////
99   //  Test thread ids, attributes and creation
100   ////////////////////////////////////////////
101
102   printf("--------------------------------------------------\n");
103   printf("test thread ids and thread creation\n");
104
105   int stsize, prio;
106
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));
110
111   frsh_thread_attr_t th_attr;
112
113   err=frsh_thread_attr_init(&th_attr);
114   printf("thread attributes object initialized, err=%d\n",err);
115
116   err=frsh_thread_attr_set_stacksize(&th_attr,40000);
117   printf("thread attr set stack size to 40000, err=%d\n",err);
118
119   err=frsh_thread_attr_get_stacksize(&th_attr,&stsize);
120   printf("thread attr get stack size=%d, err=%d\n",stsize,err);
121
122   err=fosa_thread_attr_set_prio(&th_attr,27);
123   printf("thread attr set prio to 27, err=%d\n",err);
124
125   err=fosa_thread_attr_get_prio(&th_attr,&prio);
126   printf("thread attr get prio=%d, err=%d\n",prio,err);
127
128   err=fosa_thread_create (&tid2, &th_attr, thread_code, NULL);
129   printf("creating thread with default attributes err=%d\n",err);
130
131   sleep(2);
132
133   err=frsh_thread_attr_destroy(&th_attr);
134   printf("thread attributes object destroyed, err=%d\n",err);
135
136   //////////////////////////////////
137   //  Test signals
138   //////////////////////////////////
139
140   printf("--------------------------------------------------\n");
141   printf("test signals\n");
142
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];
149
150   siglist[0]=sig;
151   siglist[1]=timer_sig;
152
153   timeout.tv_sec=1;
154   timeout.tv_nsec=0;
155
156   sigvalue.sival_int=55;
157
158   err=fosa_set_accepted_signals(siglist,2);
159   printf("two signals in set of accepted signals, err=%d\n",err);
160
161   err=fosa_signal_timedwait(siglist,1,&received,&value_received,&timeout);
162   printf("timed wait not implemented; timeoutcode=%d\n",err);
163
164   err=fosa_signal_queue(sig, sigvalue,fosa_thread_self());
165   printf("signal queued with value 55, err=%d\n",err);
166
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);
170
171   //////////////////////////////////
172   //  Test timers and signals
173   //////////////////////////////////
174
175   printf("--------------------------------------------------\n");
176   printf("test timers and signals\n");
177
178   frsh_signal_info_t timer_info;
179   fosa_timer_id_t timerid;
180   struct timespec timerval, remaining_time;
181
182   timer_info.sival_int=88;
183   timerval.tv_sec=1;
184   timerval.tv_nsec=300000000;
185
186   err=fosa_timer_create
187     (FOSA_CLOCK_REALTIME, timer_sig, timer_info,&timerid);
188   printf("timer created, err=%d\n",err);
189
190   err=fosa_timer_arm(timerid,false,&timerval);
191   printf("timer armed for 1.3 secs, err=%d\n",err);
192
193   fosa_clock_get_time(FOSA_CLOCK_REALTIME, &current_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");
197
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);
202
203   fosa_clock_get_time(FOSA_CLOCK_REALTIME, &current_time);
204   printf("current time sec=%d nsec=%d\n",
205          current_time.tv_sec, current_time.tv_nsec);
206
207   timerval.tv_sec=6;
208   timerval.tv_nsec=0;
209   err=fosa_timer_arm(timerid,false,&timerval);
210   printf("timer armed for 6 secs, err=%d\n",err);
211
212   timerval.tv_sec=1;
213   printf("sleeping 1 second\n");
214   nanosleep(&timerval, NULL);
215
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);
219
220   printf("sleeping 1 second\n");
221   nanosleep(&timerval, NULL);
222
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);
226
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);
230
231   fosa_timer_delete(timerid);
232
233
234   //////////////////////////////////
235   //  Test thread-specific data
236   //////////////////////////////////
237
238   printf("--------------------------------------------------\n");
239   printf("test thread-specific data\n");
240
241   int value=333;
242   int key;
243   frsh_thread_id_t tid=fosa_thread_self();
244
245   err=fosa_key_create(&key);
246   printf("key created=%d. err=%d\n",key,err);
247
248   fosa_thread_set_specific_data (key, tid, (void *) (&value));
249   printf("specific data set to 333. err=%d\n",err);
250
251   fosa_thread_get_specific_data (key, tid, &obtained);
252   printf("obtained thread specific data=%d\n",*((int *)obtained));
253
254
255   //////////////////////////////////
256   //  Test Priorities
257   //////////////////////////////////
258
259   printf("--------------------------------------------------\n");
260   printf("test priorities\n");
261
262
263   err=fosa_thread_set_prio(fosa_thread_self(),14);
264   printf("priority set to 14. err=%d\n",err);
265
266   err=fosa_thread_get_prio(fosa_thread_self(),&prio);
267   printf("prio=%d. err=%d\n",prio,err);
268
269
270   //////////////////////////////////
271   //  Test Mutexes
272   //////////////////////////////////
273
274   printf("--------------------------------------------------\n");
275   printf("test mutexes\n");
276
277   frsh_mutex_t lock;
278   int old;
279
280   err=fosa_mutex_init(&lock,24);
281   printf("mutex initialized with ceiling 24. err=%d\n",err);
282
283   err=fosa_mutex_set_prioceiling(&lock,24,&old);
284   printf("mutex priority ceiling changed to 24. old=%d. err=%d\n",old,err);
285
286   err=fosa_mutex_get_prioceiling(&lock,&old);
287   printf("mutex priority ceiling is=%d. err=%d\n",old,err);
288
289   err=fosa_mutex_lock(&lock);
290   printf("mutex locked. err=%d\n",err);
291
292   err=fosa_mutex_unlock(&lock);
293   printf("mutex unlocked. err=%d\n",err);
294
295   err=fosa_mutex_trylock(&lock);
296   printf("mutex try locked. err=%d\n",err);
297
298   err=fosa_mutex_unlock(&lock);
299   printf("mutex unlocked. err=%d\n",err);
300
301   //////////////////////////////////
302   //  Test Condition variables
303   //////////////////////////////////
304
305   printf("--------------------------------------------------\n");
306   printf("test condition variables\n");
307
308   fosa_cond_t cond;
309
310   err=fosa_cond_init(&cond);
311   printf("condvar initialized. err=%d\n",err);
312
313   err=fosa_cond_signal(&cond);
314   printf("cond signalled. err=%d\n",err);
315
316   err=fosa_cond_broadcast(&cond);
317   printf("cond broadcast. err=%d\n",err);
318
319   fosa_clock_get_time(FOSA_CLOCK_REALTIME, &current_time);
320   printf("current time sec=%d nsec=%d\n",
321          current_time.tv_sec, current_time.tv_nsec);
322
323   current_time.tv_sec=current_time.tv_sec+2;
324
325   fosa_mutex_lock(&lock);
326   err=fosa_cond_timedwait(&cond,&lock,&current_time);
327   fosa_mutex_unlock(&lock);
328   printf("cond timedwait with timeout=2 sec. err=%d\n",err);
329
330   fosa_clock_get_time(FOSA_CLOCK_REALTIME, &current_time);
331   printf("current time sec=%d nsec=%d\n",
332          current_time.tv_sec, current_time.tv_nsec);
333
334   err=fosa_mutex_destroy(&lock);
335   printf("mutex destroyed. err=%d\n",err);
336
337   err=fosa_cond_destroy(&cond);
338   printf("cond destroyed. err=%d\n",err);
339
340   ////////////////////////////////////////
341   //  Test Application-defined scheduling
342   ///////////////////////////////////////
343
344   printf("--------------------------------------------------\n");
345   printf("test application-defined scheduling\n");
346
347   frsh_thread_attr_t th1_attr;
348   bool is_appsched;
349
350   err=frsh_thread_attr_init(&th1_attr);
351   printf("thread attributes object initialized, err=%d\n",err);
352
353   err=fosa_thread_attr_set_appscheduled(&th1_attr,true);
354   printf("thread attr set appsched, err=%d\n",err);
355
356   err=fosa_thread_attr_get_appscheduled(&th1_attr,&is_appsched);
357   printf("thread attr get appsched=%d, err=%d\n",is_appsched,err);
358
359   return 0;
360 }