]> rtime.felk.cvut.cz Git - frescor/fosa.git/blob - src_marte/tests/test_fosa_general/test_fosa.c
Updating marte_non_local_jump to new MaRTE OS file organisation
[frescor/fosa.git] / src_marte / tests / test_fosa_general / test_fosa.c
1 // -----------------------------------------------------------------------
2 //  Copyright (C) 2006 - 2008 FRESCOR consortium partners:
3 //
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
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 for a link to partners' websites
17 //
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
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 //   FSF API web pages: http://marte.unican.es/fsf/docs
32 //                      http://shark.sssup.it/contrib/first/docs/
33 //
34 //   This file is part of FOSA (Frsh Operating System Adaption)
35 //
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.
46 //
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 // -----------------------------------------------------------------------
55
56 #include <unistd.h>
57 #include <stdio.h>
58 #include <time.h> // for nanosleep
59
60 #include "fosa.h"
61
62 void * thread_code(void *arg) {
63   printf("Thread executing\n");
64   sleep(1);
65   printf("Thread terminating\n");
66   return NULL;
67 }
68
69 /////////////////////////////////////////////////////////////
70 // Simple test program for FOSA
71 //
72 // It just checks that the different functions can be called
73 // and that they return appropriate values
74 /////////////////////////////////////////////////////////////
75
76 int main () {
77
78   //////////////////////////////////
79   //  Test clock functions
80   //////////////////////////////////
81
82   printf("--------------------------------------------------\n");
83   printf("test get_time\n");
84
85   fosa_abs_time_t current_time;
86   int err;
87   void * obtained;
88   fosa_clock_id_t cpu_clock;
89
90   err=fosa_clock_get_time(FOSA_CLOCK_REALTIME, &current_time);
91   printf("fosa_clock_get_time for CLOCK_REALTIME %ld msec err=%d\n",
92          fosa_rel_time_to_msec(current_time), err);
93
94   fosa_thread_get_cputime_clock(fosa_thread_self(), &cpu_clock);
95   err=fosa_clock_get_time(cpu_clock, &current_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);
98
99   /////////////////////////////////////////////
100   //  Test thread ids, attributes and creation
101   ////////////////////////////////////////////
102
103   printf("--------------------------------------------------\n");
104   printf("test thread ids and thread creation\n");
105
106   int stsize, prio;
107
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));
111
112   fosa_thread_attr_t th_attr;
113
114   err=fosa_thread_attr_init(&th_attr);
115   printf("thread attributes object initialized, err=%d\n",err);
116
117   err=fosa_thread_attr_set_stacksize(&th_attr,40000);
118   printf("thread attr set stack size to 40000, err=%d\n",err);
119
120   err=fosa_thread_attr_get_stacksize(&th_attr,&stsize);
121   printf("thread attr get stack size=%d, err=%d\n",stsize,err);
122
123   err=fosa_thread_attr_set_prio(&th_attr,27);
124   printf("thread attr set prio to 27, err=%d\n",err);
125
126   err=fosa_thread_attr_get_prio(&th_attr,&prio);
127   printf("thread attr get prio=%d, err=%d\n",prio,err);
128
129   err=fosa_thread_create (&tid2, &th_attr, thread_code, NULL);
130   printf("creating thread with default attributes err=%d\n",err);
131
132   sleep(2);
133
134   err=fosa_thread_attr_destroy(&th_attr);
135   printf("thread attributes object destroyed, err=%d\n",err);
136
137   //////////////////////////////////
138   //  Test signals
139   //////////////////////////////////
140
141   printf("--------------------------------------------------\n");
142   printf("test signals\n");
143
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];
150
151   siglist[0]=sig;
152   siglist[1]=timer_sig;
153
154   timeout = fosa_msec_to_rel_time(1000);
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   fosa_signal_info_t timer_info;
179   fosa_timer_id_t timerid;
180   fosa_rel_time_t timerval, remaining_time;
181
182   timer_info.sival_int=88;
183   timerval = fosa_msec_to_rel_time(1300);
184
185   err=fosa_timer_create
186     (FOSA_CLOCK_REALTIME, timer_sig, timer_info,&timerid);
187   printf("timer created, err=%d\n",err);
188
189   err=fosa_rel_timer_arm(timerid, &timerval);
190   printf("timer armed for 1.3 secs, err=%d\n",err);
191
192   fosa_clock_get_time(FOSA_CLOCK_REALTIME, &current_time);
193   printf("current time %ld msec\n", fosa_rel_time_to_msec(current_time) );
194   printf("wait for timer to expire...\n");
195
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);
200
201   fosa_clock_get_time(FOSA_CLOCK_REALTIME, &current_time);
202   printf("current time: %ld msec\n", fosa_rel_time_to_msec(current_time) );
203
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);
207
208   struct timespec timerval_tspec = {1, 0};
209   printf("sleeping 1 second\n");
210   nanosleep(&timerval_tspec, NULL);
211
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) );
214
215   printf("sleeping 1 second\n");
216   nanosleep(&timerval_tspec, NULL);
217
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);
221
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);
225
226   fosa_timer_delete(timerid);
227
228
229   //////////////////////////////////
230   //  Test thread-specific data
231   //////////////////////////////////
232
233   printf("--------------------------------------------------\n");
234   printf("test thread-specific data\n");
235
236   int value=333;
237   int key;
238   fosa_thread_id_t tid=fosa_thread_self();
239
240   err=fosa_key_create(&key);
241   printf("key created=%d. err=%d\n",key,err);
242
243   fosa_thread_set_specific_data (key, tid, (void *) (&value));
244   printf("specific data set to 333. err=%d\n",err);
245
246   fosa_thread_get_specific_data (key, tid, &obtained);
247   printf("obtained thread specific data=%d\n",*((int *)obtained));
248
249
250   //////////////////////////////////
251   //  Test Priorities
252   //////////////////////////////////
253
254   printf("--------------------------------------------------\n");
255   printf("test priorities\n");
256
257
258   err=fosa_thread_set_prio(fosa_thread_self(),14);
259   printf("priority set to 14. err=%d\n",err);
260
261   err=fosa_thread_get_prio(fosa_thread_self(),&prio);
262   printf("prio=%d. err=%d\n",prio,err);
263
264
265   //////////////////////////////////
266   //  Test Mutexes
267   //////////////////////////////////
268
269   printf("--------------------------------------------------\n");
270   printf("test mutexes\n");
271
272   fosa_mutex_t lock;
273   int old;
274
275   err=fosa_mutex_init(&lock,24);
276   printf("mutex initialized with ceiling 24. err=%d\n",err);
277
278   err=fosa_mutex_set_prioceiling(&lock,24,&old);
279   printf("mutex priority ceiling changed to 24. old=%d. err=%d\n",old,err);
280
281   err=fosa_mutex_get_prioceiling(&lock,&old);
282   printf("mutex priority ceiling is=%d. err=%d\n",old,err);
283
284   err=fosa_mutex_lock(&lock);
285   printf("mutex locked. err=%d\n",err);
286
287   err=fosa_mutex_unlock(&lock);
288   printf("mutex unlocked. err=%d\n",err);
289
290   err=fosa_mutex_trylock(&lock);
291   printf("mutex try locked. err=%d\n",err);
292
293   err=fosa_mutex_unlock(&lock);
294   printf("mutex unlocked. err=%d\n",err);
295
296   //////////////////////////////////
297   //  Test Condition variables
298   //////////////////////////////////
299
300   printf("--------------------------------------------------\n");
301   printf("test condition variables\n");
302
303   fosa_cond_t cond;
304
305   err=fosa_cond_init(&cond);
306   printf("condvar initialized. err=%d\n",err);
307
308   err=fosa_cond_signal(&cond);
309   printf("cond signalled. err=%d\n",err);
310
311   err=fosa_cond_broadcast(&cond);
312   printf("cond broadcast. err=%d\n",err);
313
314   fosa_clock_get_time(FOSA_CLOCK_REALTIME, &current_time);
315   printf("current time %ld msec\n", fosa_abs_time_to_msec(current_time) );
316
317   current_time = fosa_abs_time_incr(current_time, fosa_msec_to_rel_time(2000) );
318
319   fosa_mutex_lock(&lock);
320   err=fosa_cond_timedwait(&cond,&lock,&current_time);
321   fosa_mutex_unlock(&lock);
322   printf("cond timedwait with timeout=2 sec. err=%d\n",err);
323
324   fosa_clock_get_time(FOSA_CLOCK_REALTIME, &current_time);
325   printf("current time %ld msec\n", fosa_abs_time_to_msec(current_time) );
326
327   err=fosa_mutex_destroy(&lock);
328   printf("mutex destroyed. err=%d\n",err);
329
330   err=fosa_cond_destroy(&cond);
331   printf("cond destroyed. err=%d\n",err);
332
333   ////////////////////////////////////////
334   //  Test Application-defined scheduling
335   ///////////////////////////////////////
336
337   printf("--------------------------------------------------\n");
338   printf("test application-defined scheduling\n");
339
340   fosa_thread_attr_t th1_attr;
341   bool is_appsched;
342
343   err=fosa_thread_attr_init(&th1_attr);
344   printf("thread attributes object initialized, err=%d\n",err);
345
346   err=fosa_thread_attr_set_appscheduled(&th1_attr,true);
347   printf("thread attr set appsched, err=%d\n",err);
348
349   err=fosa_thread_attr_get_appscheduled(&th1_attr,&is_appsched);
350   printf("thread attr get appsched=%d, err=%d\n",is_appsched,err);
351
352   return 0;
353 }