]> rtime.felk.cvut.cz Git - frescor/fosa.git/blobdiff - src_marte_os/tests/test_fosa_general/test_fosa.c
Renaming fosa/src_marte to fosa/src_marte_os
[frescor/fosa.git] / src_marte_os / tests / test_fosa_general / test_fosa.c
diff --git a/src_marte_os/tests/test_fosa_general/test_fosa.c b/src_marte_os/tests/test_fosa_general/test_fosa.c
new file mode 100644 (file)
index 0000000..e15f226
--- /dev/null
@@ -0,0 +1,353 @@
+// -----------------------------------------------------------------------
+//  Copyright (C) 2006 - 2008 FRESCOR consortium partners:
+//
+//    Universidad de Cantabria,              SPAIN
+//    University of York,                    UK
+//    Scuola Superiore Sant'Anna,            ITALY
+//    Kaiserslautern University,             GERMANY
+//    Univ. Politécnica  Valencia,           SPAIN
+//    Czech Technical University in Prague,  CZECH REPUBLIC
+//    ENEA                                   SWEDEN
+//    Thales Communication S.A.              FRANCE
+//    Visual Tools S.A.                      SPAIN
+//    Rapita Systems Ltd                     UK
+//    Evidence                               ITALY
+//
+//    See http://www.frescor.org for a link to partners' websites
+//
+//           FRESCOR project (FP6/2005/IST/5-034026) is funded
+//        in part by the European Union Sixth Framework Programme
+//        The European Union is not liable of any use that may be
+//        made of this code.
+//
+//
+//  based on previous work (FSF) done in the FIRST project
+//
+//   Copyright (C) 2005  Mälardalen University, SWEDEN
+//                       Scuola Superiore S.Anna, ITALY
+//                       Universidad de Cantabria, SPAIN
+//                       University of York, UK
+//
+//   FSF API web pages: http://marte.unican.es/fsf/docs
+//                      http://shark.sssup.it/contrib/first/docs/
+//
+//   This file is part of FOSA (Frsh Operating System Adaption)
+//
+//  FOSA is free software; you can redistribute it and/or modify it
+//  under terms of the GNU General Public License as published by the
+//  Free Software Foundation; either version 2, or (at your option) any
+//  later version.  FOSA is distributed in the hope that it will be
+//  useful, but WITHOUT ANY WARRANTY; without even the implied warranty
+//  of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+//  General Public License for more details. You should have received a
+//  copy of the GNU General Public License along with FOSA; see file
+//  COPYING. If not, write to the Free Software Foundation, 675 Mass Ave,
+//  Cambridge, MA 02139, USA.
+//
+//  As a special exception, including FOSA header files in a file,
+//  instantiating FOSA generics or templates, or linking other files
+//  with FOSA objects to produce an executable application, does not
+//  by itself cause the resulting executable application to be covered
+//  by the GNU General Public License. This exception does not
+//  however invalidate any other reasons why the executable file might be
+//  covered by the GNU Public License.
+// -----------------------------------------------------------------------
+
+#include <unistd.h>
+#include <stdio.h>
+#include <time.h> // for nanosleep
+
+#include "fosa.h"
+
+void * thread_code(void *arg) {
+  printf("Thread executing\n");
+  sleep(1);
+  printf("Thread terminating\n");
+  return NULL;
+}
+
+/////////////////////////////////////////////////////////////
+// Simple test program for FOSA
+//
+// It just checks that the different functions can be called
+// and that they return appropriate values
+/////////////////////////////////////////////////////////////
+
+int main () {
+
+  //////////////////////////////////
+  //  Test clock functions
+  //////////////////////////////////
+
+  printf("--------------------------------------------------\n");
+  printf("test get_time\n");
+
+  fosa_abs_time_t current_time;
+  int err;
+  void * obtained;
+  fosa_clock_id_t cpu_clock;
+
+  err=fosa_clock_get_time(FOSA_CLOCK_REALTIME, &current_time);
+  printf("fosa_clock_get_time for CLOCK_REALTIME %ld msec err=%d\n",
+         fosa_rel_time_to_msec(current_time), err);
+
+  fosa_thread_get_cputime_clock(fosa_thread_self(), &cpu_clock);
+  err=fosa_clock_get_time(cpu_clock, &current_time);
+  printf("fosa_clock_get_time for CPU-time clock %ld msec err=%d\n",
+         fosa_rel_time_to_msec(current_time), err);
+
+  /////////////////////////////////////////////
+  //  Test thread ids, attributes and creation
+  ////////////////////////////////////////////
+
+  printf("--------------------------------------------------\n");
+  printf("test thread ids and thread creation\n");
+
+  int stsize, prio;
+
+  fosa_thread_id_t tid1=fosa_thread_self();
+  fosa_thread_id_t tid2=fosa_thread_self();
+  printf("equal thread comparison=%d\n",fosa_thread_equal(tid1,tid2));
+
+  fosa_thread_attr_t th_attr;
+
+  err=fosa_thread_attr_init(&th_attr);
+  printf("thread attributes object initialized, err=%d\n",err);
+
+  err=fosa_thread_attr_set_stacksize(&th_attr,40000);
+  printf("thread attr set stack size to 40000, err=%d\n",err);
+
+  err=fosa_thread_attr_get_stacksize(&th_attr,&stsize);
+  printf("thread attr get stack size=%d, err=%d\n",stsize,err);
+
+  err=fosa_thread_attr_set_prio(&th_attr,27);
+  printf("thread attr set prio to 27, err=%d\n",err);
+
+  err=fosa_thread_attr_get_prio(&th_attr,&prio);
+  printf("thread attr get prio=%d, err=%d\n",prio,err);
+
+  err=fosa_thread_create (&tid2, &th_attr, thread_code, NULL);
+  printf("creating thread with default attributes err=%d\n",err);
+
+  sleep(2);
+
+  err=fosa_thread_attr_destroy(&th_attr);
+  printf("thread attributes object destroyed, err=%d\n",err);
+
+  //////////////////////////////////
+  //  Test signals
+  //////////////////////////////////
+
+  printf("--------------------------------------------------\n");
+  printf("test signals\n");
+
+  fosa_signal_t received;
+  fosa_signal_info_t sigvalue,  value_received;
+  fosa_rel_time_t timeout;
+  fosa_signal_t sig=FOSA_SIGNAL_MIN+1;
+  fosa_signal_t timer_sig=FOSA_SIGNAL_MIN+3;
+  fosa_signal_t siglist[2];
+
+  siglist[0]=sig;
+  siglist[1]=timer_sig;
+
+  timeout = fosa_msec_to_rel_time(1000);
+
+  sigvalue.sival_int=55;
+
+  err=fosa_set_accepted_signals(siglist,2);
+  printf("two signals in set of accepted signals, err=%d\n",err);
+
+  err=fosa_signal_timedwait(siglist,1,&received,&value_received,&timeout);
+  printf("timed wait not implemented; timeoutcode=%d\n",err);
+
+  err=fosa_signal_queue(sig, sigvalue,fosa_thread_self());
+  printf("signal queued with value 55, err=%d\n",err);
+
+  err=fosa_signal_wait(siglist,1,&received, &value_received);
+  printf("timeoutcode=%d signal received=%d value=%d\n",
+        err,received,value_received.sival_int);
+
+  //////////////////////////////////
+  //  Test timers and signals
+  //////////////////////////////////
+
+  printf("--------------------------------------------------\n");
+  printf("test timers and signals\n");
+
+  fosa_signal_info_t timer_info;
+  fosa_timer_id_t timerid;
+  fosa_rel_time_t timerval, remaining_time;
+
+  timer_info.sival_int=88;
+  timerval = fosa_msec_to_rel_time(1300);
+
+  err=fosa_timer_create
+    (FOSA_CLOCK_REALTIME, timer_sig, timer_info,&timerid);
+  printf("timer created, err=%d\n",err);
+
+  err=fosa_rel_timer_arm(timerid, &timerval);
+  printf("timer armed for 1.3 secs, err=%d\n",err);
+
+  fosa_clock_get_time(FOSA_CLOCK_REALTIME, &current_time);
+  printf("current time %ld msec\n", fosa_rel_time_to_msec(current_time) );
+  printf("wait for timer to expire...\n");
+
+  siglist[0]=timer_sig;
+  err=fosa_signal_wait(siglist,1,&received, &value_received);
+  printf("timeoutcode=%d signal received=%d value=%d\n",
+        err,received,value_received.sival_int);
+
+  fosa_clock_get_time(FOSA_CLOCK_REALTIME, &current_time);
+  printf("current time: %ld msec\n", fosa_rel_time_to_msec(current_time) );
+
+  timerval = fosa_msec_to_rel_time(6000);
+  err=fosa_rel_timer_arm(timerid, &timerval);
+  printf("timer armed for 6 secs, err=%d\n",err);
+
+  struct timespec timerval_tspec = {1, 0};
+  printf("sleeping 1 second\n");
+  nanosleep(&timerval_tspec, NULL);
+
+  err=fosa_timer_get_remaining_time(timerid, &remaining_time);
+  printf("timer remaining time: %ld msec\n", fosa_rel_time_to_msec(remaining_time) );
+
+  printf("sleeping 1 second\n");
+  nanosleep(&timerval_tspec, NULL);
+
+  err=fosa_timer_disarm(timerid,&remaining_time);
+  printf("timer disarmed, remaining time: %ld msec, err=%d\n",
+         fosa_rel_time_to_msec(remaining_time), err);
+
+  err=fosa_timer_get_remaining_time(timerid, &remaining_time);
+  printf("timer remaining time after disarm (0?) %ld msec, err=%d\n",
+         fosa_rel_time_to_msec(remaining_time), err);
+
+  fosa_timer_delete(timerid);
+
+
+  //////////////////////////////////
+  //  Test thread-specific data
+  //////////////////////////////////
+
+  printf("--------------------------------------------------\n");
+  printf("test thread-specific data\n");
+
+  int value=333;
+  int key;
+  fosa_thread_id_t tid=fosa_thread_self();
+
+  err=fosa_key_create(&key);
+  printf("key created=%d. err=%d\n",key,err);
+
+  fosa_thread_set_specific_data (key, tid, (void *) (&value));
+  printf("specific data set to 333. err=%d\n",err);
+
+  fosa_thread_get_specific_data (key, tid, &obtained);
+  printf("obtained thread specific data=%d\n",*((int *)obtained));
+
+
+  //////////////////////////////////
+  //  Test Priorities
+  //////////////////////////////////
+
+  printf("--------------------------------------------------\n");
+  printf("test priorities\n");
+
+
+  err=fosa_thread_set_prio(fosa_thread_self(),14);
+  printf("priority set to 14. err=%d\n",err);
+
+  err=fosa_thread_get_prio(fosa_thread_self(),&prio);
+  printf("prio=%d. err=%d\n",prio,err);
+
+
+  //////////////////////////////////
+  //  Test Mutexes
+  //////////////////////////////////
+
+  printf("--------------------------------------------------\n");
+  printf("test mutexes\n");
+
+  fosa_mutex_t lock;
+  int old;
+
+  err=fosa_mutex_init(&lock,24);
+  printf("mutex initialized with ceiling 24. err=%d\n",err);
+
+  err=fosa_mutex_set_prioceiling(&lock,24,&old);
+  printf("mutex priority ceiling changed to 24. old=%d. err=%d\n",old,err);
+
+  err=fosa_mutex_get_prioceiling(&lock,&old);
+  printf("mutex priority ceiling is=%d. err=%d\n",old,err);
+
+  err=fosa_mutex_lock(&lock);
+  printf("mutex locked. err=%d\n",err);
+
+  err=fosa_mutex_unlock(&lock);
+  printf("mutex unlocked. err=%d\n",err);
+
+  err=fosa_mutex_trylock(&lock);
+  printf("mutex try locked. err=%d\n",err);
+
+  err=fosa_mutex_unlock(&lock);
+  printf("mutex unlocked. err=%d\n",err);
+
+  //////////////////////////////////
+  //  Test Condition variables
+  //////////////////////////////////
+
+  printf("--------------------------------------------------\n");
+  printf("test condition variables\n");
+
+  fosa_cond_t cond;
+
+  err=fosa_cond_init(&cond);
+  printf("condvar initialized. err=%d\n",err);
+
+  err=fosa_cond_signal(&cond);
+  printf("cond signalled. err=%d\n",err);
+
+  err=fosa_cond_broadcast(&cond);
+  printf("cond broadcast. err=%d\n",err);
+
+  fosa_clock_get_time(FOSA_CLOCK_REALTIME, &current_time);
+  printf("current time %ld msec\n", fosa_abs_time_to_msec(current_time) );
+
+  current_time = fosa_abs_time_incr(current_time, fosa_msec_to_rel_time(2000) );
+
+  fosa_mutex_lock(&lock);
+  err=fosa_cond_timedwait(&cond,&lock,&current_time);
+  fosa_mutex_unlock(&lock);
+  printf("cond timedwait with timeout=2 sec. err=%d\n",err);
+
+  fosa_clock_get_time(FOSA_CLOCK_REALTIME, &current_time);
+  printf("current time %ld msec\n", fosa_abs_time_to_msec(current_time) );
+
+  err=fosa_mutex_destroy(&lock);
+  printf("mutex destroyed. err=%d\n",err);
+
+  err=fosa_cond_destroy(&cond);
+  printf("cond destroyed. err=%d\n",err);
+
+  ////////////////////////////////////////
+  //  Test Application-defined scheduling
+  ///////////////////////////////////////
+
+  printf("--------------------------------------------------\n");
+  printf("test application-defined scheduling\n");
+
+  fosa_thread_attr_t th1_attr;
+  bool is_appsched;
+
+  err=fosa_thread_attr_init(&th1_attr);
+  printf("thread attributes object initialized, err=%d\n",err);
+
+  err=fosa_thread_attr_set_appscheduled(&th1_attr,true);
+  printf("thread attr set appsched, err=%d\n",err);
+
+  err=fosa_thread_attr_get_appscheduled(&th1_attr,&is_appsched);
+  printf("thread attr get appsched=%d, err=%d\n",is_appsched,err);
+
+  return 0;
+}