]> rtime.felk.cvut.cz Git - frescor/fosa.git/blob - src_rtlinux/test_fosa.c
Makefile: Add missing header file
[frescor/fosa.git] / src_rtlinux / test_fosa.c
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"
5 #include <rtl.h>
6 //#include <unistd.h>
7 //#include <stdio.h>
8 //#include <time.h> // for nanosleep
9
10
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");
15     nanosleep(&req, 0);
16     printf("Thread terminating\n");
17     return NULL;
18 }
19
20 /////////////////////////////////////////////////////////////
21 // Simple test program for FOSA
22 //
23 // It just checks that the different functions can be called
24 // and that they return appropriate values
25 /////////////////////////////////////////////////////////////
26
27 void *main_app(void *args) {
28   //////////////////////////////////
29   //  Test clock functions
30   //////////////////////////////////
31
32 // -----------------------------------------------------------------------
33 //  Copyright (C) 2006 - 2009 FRESCOR consortium partners:
34 //
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
41 //    ENEA                                   SWEDEN
42 //    Thales Communication S.A.              FRANCE
43 //    Visual Tools S.A.                      SPAIN
44 //    Rapita Systems Ltd                     UK
45 //    Evidence                               ITALY
46 //
47 //    See http://www.frescor.org for a link to partners' websites
48 //
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
52 //        made of this code.
53 //
54 //
55 //  based on previous work (FSF) done in the FIRST project
56 //
57 //   Copyright (C) 2005  Mälardalen University, SWEDEN
58 //                       Scuola Superiore S.Anna, ITALY
59 //                       Universidad de Cantabria, SPAIN
60 //                       University of York, UK
61 //
62 //   FSF API web pages: http://marte.unican.es/fsf/docs
63 //                      http://shark.sssup.it/contrib/first/docs/
64 //
65 //   This file is part of FOSA (Frsh Operating System Adaption)
66 //
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.
77 //
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");
87
88   int stsize, prio;
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));
93
94   frsh_thread_attr_t th_attr;
95
96   err=frsh_thread_attr_init(&th_attr);
97   printf("thread attributes object initialized, err=%d\n",err);
98
99   err=frsh_thread_attr_set_stacksize(&th_attr,40000);
100   printf("thread attr set stack size to 40000, err=%d\n",err);
101
102   err=frsh_thread_attr_get_stacksize(&th_attr,&stsize);
103   printf("thread attr get stack size=%d, err=%d\n",stsize,err);
104
105   err=fosa_thread_attr_set_prio(&th_attr,27);
106   printf("thread attr set prio to 27, err=%d\n",err);
107
108   err=fosa_thread_attr_get_prio(&th_attr,&prio);
109   printf("thread attr get prio=%d, err=%d\n",prio,err);
110
111   err=fosa_thread_create (&tid2, &th_attr, thread_code, NULL);
112   printf("creating thread with default attributes err=%d\n",err);
113
114   nanosleep(&req,0);
115
116   err=frsh_thread_attr_destroy(&th_attr);
117   printf("thread attributes object destroyed, err=%d\n",err);
118
119   //////////////////////////////////
120   //  Test signals
121   //////////////////////////////////
122
123   printf("--------------------------------------------------\n");
124   printf("test signals\n");
125
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];
132
133   siglist[0]=sig;
134   siglist[1]=timer_sig;
135
136   timeout.tv_sec=1;
137   timeout.tv_nsec=0;
138
139   sigvalue.sival_int=55;
140
141   err=fosa_set_accepted_signals(siglist,2);
142   printf("two signals in set of accepted signals, err=%d\n",err);
143
144   err=fosa_signal_timedwait(siglist,1,&received,&value_received,&timeout);
145   printf("timed wait not implemented; timeoutcode=%d\n",err);
146
147   err=fosa_signal_queue(sig, sigvalue,fosa_thread_self());
148   printf("signal queued with value 55, err=%d\n",err);
149
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);
153
154   
155   //////////////////////////////////
156   //  Test timers and signals
157   //////////////////////////////////
158
159   printf("--------------------------------------------------\n");
160   printf("test timers and signals\n");
161
162   frsh_signal_info_t timer_info;
163   fosa_timer_id_t timerid;
164   struct timespec timerval, remaining_time;
165
166   timer_info.sival_int=88;
167   timerval.tv_sec=1;
168   timerval.tv_nsec=300000000;
169
170
171   err=fosa_timer_create
172     (FOSA_CLOCK_REALTIME, timer_sig, timer_info,&timerid);
173   printf("timer created, err=%d\n",err);
174   
175   err=fosa_timer_arm(timerid,false,&timerval);
176   printf("timer armed for 1.3 secs, err=%d\n",err);
177   
178   fosa_clock_get_time(FOSA_CLOCK_REALTIME, &current_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");
182   
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);
187   
188   fosa_clock_get_time(FOSA_CLOCK_REALTIME, &current_time);
189   printf("current time sec=%d nsec=%d\n",
190          current_time.tv_sec, current_time.tv_nsec);
191   
192   timerval.tv_sec=6;
193   timerval.tv_nsec=0;
194   err=fosa_timer_arm(timerid,false,&timerval);
195   printf("timer armed for 6 secs, err=%d\n",err);
196
197   timerval.tv_sec=1;
198   printf("sleeping 1 second\n");
199   nanosleep(&timerval, NULL);
200   
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);
204
205   printf("sleeping 1 second\n");
206   nanosleep(&timerval, NULL);
207   
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);
211
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);
215
216   fosa_timer_delete(timerid);
217   
218
219   //////////////////////////////////
220   //  Test thread-specific data
221   //////////////////////////////////
222
223   printf("--------------------------------------------------\n");
224   printf("test thread-specific data\n");
225
226   int value=333;
227   int key;
228   frsh_thread_id_t tid=fosa_thread_self();
229
230   err=fosa_key_create(&key);
231   printf("key created=%d. err=%d\n",key,err);
232
233   fosa_thread_set_specific_data (key, tid, (void *) (&value));
234   printf("specific data set to 333. err=%d\n",err);
235
236   fosa_thread_get_specific_data (key, tid, &obtained);
237   printf("obtained thread specific data=%d\n",*((int *)obtained));
238  
239
240   //////////////////////////////////
241   //  Test Priorities
242   //////////////////////////////////
243
244   printf("--------------------------------------------------\n");
245   printf("test priorities\n");
246
247
248   err=fosa_thread_set_prio(fosa_thread_self(),14);
249   printf("priority set to 14. err=%d\n",err);
250
251   err=fosa_thread_get_prio(fosa_thread_self(),&prio);
252   printf("prio=%d. err=%d\n",prio,err);
253
254
255   //////////////////////////////////
256   //  Test Mutexes
257   //////////////////////////////////
258
259   printf("--------------------------------------------------\n");
260   printf("test mutexes\n");
261
262   frsh_mutex_t lock;
263   int old;
264
265   err=fosa_mutex_init(&lock,24);
266   printf("mutex initialized with ceiling 24. err=%d\n",err);
267   
268   err=fosa_mutex_set_prioceiling(&lock,24,&old);
269   printf("mutex priority ceiling changed to 24. old=%d. err=%d\n",old,err);
270
271   err=fosa_mutex_get_prioceiling(&lock,&old);
272   printf("mutex priority ceiling is=%d. err=%d\n",old,err);
273
274   err=fosa_mutex_lock(&lock);
275   printf("mutex locked. err=%d\n",err);
276
277   err=fosa_mutex_unlock(&lock);
278   printf("mutex unlocked. err=%d\n",err);
279
280   err=fosa_mutex_trylock(&lock);
281   printf("mutex try locked. err=%d\n",err);
282
283   err=fosa_mutex_unlock(&lock);
284   printf("mutex unlocked. err=%d\n",err);
285   
286   //////////////////////////////////
287   //  Test Condition variables
288   //////////////////////////////////
289
290   printf("--------------------------------------------------\n");
291   printf("test condition variables\n");
292
293   fosa_cond_t cond;
294
295   err=fosa_cond_init(&cond);
296   printf("condvar initialized. err=%d\n",err);
297   
298   err=fosa_cond_signal(&cond);
299   printf("cond signalled. err=%d\n",err);
300
301   err=fosa_cond_broadcast(&cond);
302   printf("cond broadcast. err=%d\n",err);
303
304   fosa_clock_get_time(FOSA_CLOCK_REALTIME, &current_time);
305   printf("current time sec=%d nsec=%d\n",
306          current_time.tv_sec, current_time.tv_nsec);
307
308   current_time.tv_sec=current_time.tv_sec+2;
309
310   fosa_mutex_lock(&lock);
311   err=fosa_cond_timedwait(&cond,&lock,&current_time);
312   fosa_mutex_unlock(&lock);
313   printf("cond timedwait with timeout=2 sec. err=%d\n",err);
314
315   fosa_clock_get_time(FOSA_CLOCK_REALTIME, &current_time);
316   printf("current time sec=%d nsec=%d\n",
317          current_time.tv_sec, current_time.tv_nsec);
318
319   err=fosa_mutex_destroy(&lock);
320   printf("mutex destroyed. err=%d\n",err);
321
322   err=fosa_cond_destroy(&cond);
323   printf("cond destroyed. err=%d\n",err);
324   
325   ////////////////////////////////////////
326   //  Test Application-defined scheduling
327   ///////////////////////////////////////
328   /*
329   printf("--------------------------------------------------\n");
330   printf("test application-defined scheduling\n");
331
332   frsh_thread_attr_t th1_attr;
333   bool is_appsched;
334
335   err=frsh_thread_attr_init(&th1_attr);
336   printf("thread attributes object initialized, err=%d\n",err);
337
338   err=fosa_thread_attr_set_appscheduled(&th1_attr,true);
339   printf("thread attr set appsched, err=%d\n",err);
340
341   err=fosa_thread_attr_get_appscheduled(&th1_attr,&is_appsched);
342   printf("thread attr get appsched=%d, err=%d\n",is_appsched,err);
343   */
344   return 0;
345
346 }
347
348 static pthread_t main_thread;
349
350 int init_module (void) {
351     return pthread_create(&main_thread, 0, main_app, 0);
352 }
353
354 void cleanup_module(void) {
355   pthread_delete_np(main_thread);
356 }