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