]> rtime.felk.cvut.cz Git - frescor/fosa.git/blobdiff - src_marte/fosa_threads_and_signals.c
Time abstraction added to FOSA to replace timespec_operations
[frescor/fosa.git] / src_marte / fosa_threads_and_signals.c
index 8476307d4358ca8044b23e4e1ff88a1e811215b8..25f319bde39a6324c3c3a49d4c1c89a78be3f803 100644 (file)
@@ -1,5 +1,5 @@
-// -----------------------------------------------------------------------
-//  Copyright (C) 2006 - 2007 FRESCOR consortium partners:
+//----------------------------------------------------------------------
+//  Copyright (C) 2006 - 2007 by the FRESCOR consortium:
 //
 //    Universidad de Cantabria,              SPAIN
 //    University of York,                    UK
 //    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
+//    See http://www.frescor.org
+//
+//        The 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.
 //
-//  This file is part of the FRSH implementation
 //
-//  FRSH is free software; you can  redistribute it and/or  modify
-//  it under the terms of  the GNU General Public License as published by
-//  the Free Software Foundation;  either  version 2, or (at  your option)
-//  any later version.
+//  based on previous work (FSF) done in the FIRST project
 //
-//  FRSH  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.
+//   Copyright (C) 2005  Mälardalen University, SWEDEN
+//                       Scuola Superiore S.Anna, ITALY
+//                       Universidad de Cantabria, SPAIN
+//                       University of York, UK
 //
-//  You should have  received a  copy of  the  GNU  General Public License
-//  distributed  with  FRSH;  see file COPYING.   If not,  write to the
-//  Free Software  Foundation,  59 Temple Place  -  Suite 330,  Boston, MA
-//  02111-1307, USA.
+// This file is part of FOSA (Frsh Operating System Abstraction)
 //
+// 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.
 // -----------------------------------------------------------------------
 //fosa_threads_and_signals.c
 //==============================================
@@ -108,12 +119,12 @@ void init_keys() {
     key_in_use[i]=false;
   }
   // Initialize the mutex
-  pthread_mutexattr_init(&attr);
+  CHK(pthread_mutexattr_init(&attr));
   // we use the priority inheritance protocol because we don't know which
   // tasks will be using the mutex
-  pthread_mutexattr_setprotocol(&attr,PTHREAD_PRIO_INHERIT);
-  pthread_mutex_init(&key_lock,&attr);
-  pthread_mutexattr_destroy(&attr);
+  CHK(pthread_mutexattr_setprotocol(&attr,PTHREAD_PRIO_INHERIT));
+  CHK(pthread_mutex_init(&key_lock,&attr));
+  CHK(pthread_mutexattr_destroy(&attr));
 }
 
 /*************************
@@ -126,7 +137,7 @@ void init_keys() {
  * Compare two thread identifiers to determine if they refer to the 
  * same thread
  **/
-bool fosa_thread_equal(frsh_thread_id_t t1, frsh_thread_id_t t2)
+bool fosa_thread_equal(fosa_thread_id_t t1, fosa_thread_id_t t2)
 {
   return pthread_equal(t1,t2);
 }
@@ -137,11 +148,103 @@ bool fosa_thread_equal(frsh_thread_id_t t1, frsh_thread_id_t t2)
  *
  * Return the thread id of the calling thread
  **/
-frsh_thread_id_t fosa_thread_self()
+fosa_thread_id_t fosa_thread_self()
 {
   return pthread_self();
 }
 
+/*************************
+ * Thread attributes
+ *************************/ 
+
+/**
+ * fosa_thread_attr_init()
+ *
+ * Initialize a thread attributes object
+ *
+ * This function initializes the object pointed to by attr to all 
+ * the default values defined by FRSH
+ *
+ * @return 0 if successful; otherwise it returns \n
+ *   FOSA_ENOMEM: insufficient memory exists to initialize the thread 
+ *           attributes object
+ **/
+int fosa_thread_attr_init(fosa_thread_attr_t *attr)
+{
+  int ret_value;
+
+  ret_value=pthread_attr_init(attr);
+  if (ret_value==0) {
+    // set the default values
+
+    // detachstate = detached thread (no join operation allowed)
+    CHK(pthread_attr_setdetachstate(attr,PTHREAD_CREATE_DETACHED));
+
+    // inheritsched = explicit, so that we can explicitly set the attributes
+    CHK(pthread_attr_setinheritsched(attr,PTHREAD_EXPLICIT_SCHED));
+
+    // schedpolicy = fixed priorities
+    CHK(pthread_attr_setschedpolicy(attr,SCHED_FIFO));
+
+    // detachstate = detached thread (no join operation allowed)
+    CHK(pthread_attr_setdetachstate(attr,PTHREAD_CREATE_DETACHED));
+
+  }
+  return ret_value;
+}
+
+/**
+ * fosa_thread_attr_destroy()
+ *
+ * Destroy a thread attributes object
+ *
+ * This function is used to destroy the thread attributes object,
+ * pointed to by attr, and deallocate any system resources allocated for it
+ * 
+ * Returns 0
+ */
+int fosa_thread_attr_destroy(fosa_thread_attr_t *attr)
+{
+  return pthread_attr_destroy(attr);
+}
+
+/**
+ * fosa_thread_attr_set_stacksize()
+ *
+ * Set the thread minimum stack size in a thread attributes object
+ *
+ * This function sets the minimum stack size of the thread attributes
+ * object attr to the value given by stacksize, in bytes. This
+ * function has no runtime effect on the stack size, except when the
+ * attributes object is used to create a thread, when it will be
+ * created with the specified minimum stack size
+ * 
+ * @return 0 if successful, or the following error code:
+ *    FOSA_EINVAL: the specified stacksize  value is not supported in
+ *            this implementation
+ */
+int fosa_thread_attr_set_stacksize
+     (fosa_thread_attr_t *attr, size_t stacksize)
+{
+  return pthread_attr_setstacksize(attr,stacksize);
+}
+
+/**
+ * fosa_thread_attr_get_stacksize()
+ *
+ * Get the thread minimum stack size from a thread attributes object
+ *
+ * This function sets the variable pointed to by stacksize to the
+ * minimum stack size stored in the thread attributes object attr.
+ * 
+ * @return 0
+ */
+int fosa_thread_attr_get_stacksize
+      (const fosa_thread_attr_t *attr, size_t *stacksize)
+{
+  return pthread_attr_getstacksize(attr,stacksize);
+}
+
 
 /*************************
  * Thread creation and termination
@@ -173,8 +276,8 @@ frsh_thread_id_t fosa_thread_self()
  *
  **/
  int fosa_thread_create
-    (frsh_thread_id_t *tid, const frsh_thread_attr_t *attr, 
-     frsh_thread_code_t code, void * arg)
+    (fosa_thread_id_t *tid, const fosa_thread_attr_t *attr, 
+     fosa_thread_code_t code, void * arg)
 {
   return pthread_create(tid,attr,code,arg);
 }
@@ -215,6 +318,7 @@ int fosa_key_create(int *key)
   int i,ret_value;
   bool found=false;
 
+  ret_value = 0;
   // initialize the keys data structure if needed
   CHK(pthread_once(&keys_initialized, init_keys));
 
@@ -285,7 +389,7 @@ int fosa_key_destroy(int key)
  * implementation and dependant applications
  **/
  int fosa_thread_set_specific_data
-       (int key, frsh_thread_id_t tid, const void * value)
+       (int key, fosa_thread_id_t tid, const void * value)
 {
   return pthread_setspecific_for(key_list[key],tid,value);
 }
@@ -305,7 +409,7 @@ int fosa_key_destroy(int key)
  * notify it to the system console and then terminate the FRSH
  * implementation and dependant applications
  **/
-int fosa_thread_get_specific_data(int key, frsh_thread_id_t tid, 
+int fosa_thread_get_specific_data(int key, fosa_thread_id_t tid, 
                                   void ** value)
 {
   return pthread_getspecific_from(key_list[key],tid,value);
@@ -362,7 +466,7 @@ int fosa_get_priority_min()
  * notify it to the system console and then terminate the FRSH
  * implementation and dependant applications
  **/
-int fosa_thread_attr_set_prio(frsh_thread_attr_t *attr, int prio)
+int fosa_thread_attr_set_prio(fosa_thread_attr_t *attr, int prio)
 {
   struct sched_param param;
 
@@ -381,7 +485,7 @@ int fosa_thread_attr_set_prio(frsh_thread_attr_t *attr, int prio)
  * Returns 0
  **/
 int fosa_thread_attr_get_prio
-          (const frsh_thread_attr_t *attr, int *prio)
+          (const fosa_thread_attr_t *attr, int *prio)
 {
   struct sched_param param;
   int ret_value;
@@ -409,12 +513,14 @@ int fosa_thread_attr_get_prio
  * notify it to the system console and then terminate the FRSH
  * implementation and dependant applications
  **/
-int fosa_thread_set_prio(frsh_thread_id_t tid, int prio)
+int fosa_thread_set_prio(fosa_thread_id_t tid, int prio)
 {
   struct sched_param param;
-  int policy;
+  int policy, ret_value;
+
+  ret_value=pthread_getschedparam(tid,&policy,&param);
+  if (ret_value!=0) return ret_value;
 
-  CHK(pthread_getschedparam(tid,&policy,&param));
   param.sched_priority=prio;
   return pthread_setschedparam(tid,policy,&param);
 }
@@ -429,7 +535,7 @@ int fosa_thread_set_prio(frsh_thread_id_t tid, int prio)
  * 
  * Returns 0
  **/
-int fosa_thread_get_prio (frsh_thread_id_t tid, int *prio)
+int fosa_thread_get_prio (fosa_thread_id_t tid, int *prio)
 {
   struct sched_param param;
   int policy, ret_value;
@@ -451,7 +557,7 @@ int fosa_thread_get_prio (frsh_thread_id_t tid, int *prio)
  * synchronously wait. Signals carry an associated piece of
  * information (an integer or a pointer) and are queued until they are
  * accepted.  Signals are identified by an integer signal number (of
- * the type frsh_signal_t) in the range FOSA_SIGNAL_MIN,
+ * the type fosa_signal_t) in the range FOSA_SIGNAL_MIN,
  * FOSA_SIGNAL_MAX.  This range is required to have at least <tbd>
  * values.
  *******************************************************************/
@@ -474,15 +580,27 @@ int fosa_thread_get_prio (frsh_thread_id_t tid, int *prio)
  * notify it to the system console and then terminate the FRSH
  * implementation and dependant applications
  **/
-int fosa_set_accepted_signals(frsh_signal_t set[], int size)
+int fosa_set_accepted_signals(fosa_signal_t set[], int size)
 {
   sigset_t signalset;
   int i;
+  struct sigaction act;
 
+  // empty the signal set
   CHKE(sigemptyset(&signalset));
+
+  // configure the signal action to make the signal a real-time
+  //   signal that can be queued
+  act.sa_handler=SIG_DFL;
+  act.sa_mask=signalset;
+  act.sa_flags=SA_SIGINFO;
+  act.sa_sigaction=NULL;
+
+    // loop for all signals in set
   for(i=0;i<size;i++) {
     CHKE(sigaddset(&signalset,set[i]));
-    // falta configurar la señal para que se pueda encolar........
+    // Configure the signal so that it can be queued with data
+    CHKE(sigaction(set[i],&act,NULL));
   } 
   return pthread_sigmask(SIG_BLOCK,&signalset,NULL);
 }
@@ -515,15 +633,16 @@ int fosa_set_accepted_signals(frsh_signal_t set[], int size)
  * notify it to the system console and then terminate the FRSH
  * implementation and dependant applications
  **/
- int fosa_signal_queue
-       (frsh_signal_t signal, frsh_signal_info_t info,
-        frsh_thread_id_t receiver)
+int fosa_signal_queue
+       (fosa_signal_t signal, fosa_signal_info_t info,
+        fosa_thread_id_t receiver)
 {
   // note: in MaRTE OS the signal is sent to any interested thread
   pid_t pid=1; // dummy value; the pid is ignored in MaRTE OS
   int err;
-
-  err=sigqueue(pid,signal, *( (union sigval *) &info) );
+  err=sigqueue(pid,signal,*((union sigval *)(&info)));
+  // the above casting construct is used to overcome the compiler
+  // restriction that does not allow casts between unions
   if (err==0) {
     return 0;
   } else {
@@ -556,9 +675,9 @@ int fosa_set_accepted_signals(frsh_signal_t set[], int size)
  * notify it to the system console and then terminate the FRSH
  * implementation and dependant applications
  **/
- int fosa_signal_wait
-      (frsh_signal_t set[], int size, frsh_signal_t *signal_received, 
-       frsh_signal_info_t *info)
+int fosa_signal_wait
+      (fosa_signal_t set[], int size, fosa_signal_t *signal_received, 
+       fosa_signal_info_t *info)
 {
   int err,i;
   sigset_t signalset;
@@ -572,7 +691,7 @@ int fosa_set_accepted_signals(frsh_signal_t set[], int size)
   err=sigwaitinfo(&signalset,&siginfo);
   if (err!=-1) {
     *signal_received=siginfo.si_signo;
-    *info= *( (frsh_signal_info_t *) &siginfo.si_value);
+    *info=*((fosa_signal_info_t *)(&siginfo.si_value));
     return 0;
   } else {
     return errno;
@@ -599,8 +718,8 @@ int fosa_set_accepted_signals(frsh_signal_t set[], int size)
  * FRSH implementation and dependant applications
  **/
  int fosa_signal_timedwait
-      (frsh_signal_t set[], int size, frsh_signal_t *signal_received, 
-       frsh_signal_info_t *info, const struct timespec *timeout)
+      (fosa_signal_t set[], int size, fosa_signal_t *signal_received, 
+       fosa_signal_info_t *info, const fosa_rel_time_t *timeout)
 {
 
   // the implementation of sigtimedwait is not yet available in MaRTE OS