]> rtime.felk.cvut.cz Git - frescor/fosa.git/blobdiff - src_aquosa/fosa_mutexes_and_condvars.c
Updating header text in FOSA files for the incoming final project
[frescor/fosa.git] / src_aquosa / fosa_mutexes_and_condvars.c
index 163765a8ac3959d1e6b04e7cc8adc91f784f930c..514e38c5137b5f3185567fb894f72c1ed7579f32 100644 (file)
@@ -1,18 +1,18 @@
 // -----------------------------------------------------------------------
-//  Copyright (C) 2006 - 2007 FRESCOR consortium partners:
+//  Copyright (C) 2006 - 2009 FRESCOR consortium partners:
 //
 //    Universidad de Cantabria,              SPAIN
 //    University of York,                    UK
 //    Scuola Superiore Sant'Anna,            ITALY
 //    Kaiserslautern University,             GERMANY
-//    Univ. Politecnica  Valencia,           SPAIN
+//    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
 //        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
+//
+//   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/
 //
-//  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.
+//   This file is part of FOSA (Frsh Operating System Adaption)
 //
-//  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.
+//  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, if you include this header file into source
-//  files to be compiled, this header file does not by itself cause
-//  the resulting executable 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 General
-//  Public License.
+//  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_mutexes_and_condvars.h
 //==============================================
 // FOSA(Frescor Operating System Adaptation layer)
 //================================================
 
-#include <fosa.h>
+#include "fosa_time.h"
+#include "fosa_configuration_parameters.h"
+#include "fosa_mutexes_and_condvars.h"
 
+#ifdef OMK_FOR_USER            /* If compiled by OMK, use the config */
+#include "fosa_config.h"
+#endif
 
 /*******************************************************
- * Mutexes with priority ceiling
+ * Mutexes with priority/bandwidth inheritance
  ******************************************************/
 
-
-int fosa_mutex_init(frsh_mutex_t *mutex, int prioceiling)
+/**
+ * fosa_mutex_init()
+ *
+ * Initialize a frsh mutex
+ *
+ * The mutex pointed to by mutex is initialized as a mutex using
+ * the priority ceiling protocol. A priority ceiling of prioceiling
+ * is assigned to this mutex.
+ *
+ * Returns 0 if successful; otherwise it returns an error code:
+ *    EINVAL: the value of prioceiling is invalid
+ *    EAGAIN: the system lacked the necessary resources to create the mutex
+ *    ENOMEM: Insufficient memory exists to initialize the mutex
+ *    EBUSY:  The system has detected an attempt to reinitialize the mutex
+ **/
+int fosa_mutex_init(fosa_mutex_t *mutex, int prioceiling)
 {
-       /* priority ceiling if, for now, ignored */
-       return pthread_mutex_init(mutex, NULL);
+       int ret;
+       pthread_mutexattr_t attr;
+
+       ret = pthread_mutexattr_init(&attr);
+       if (ret) return errno;
+
+#ifndef CONFIG_NO_PRIO_INHERIT /* Valgrind doesn't support this attribute */
+       ret = pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT);
+       if (ret) return errno;
+#endif
+
+       ret = pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
+       if (ret) return errno;
+
+       return pthread_mutex_init(mutex, &attr);
 }
 
-int fosa_mutex_destroy(frsh_mutex_t *mutex)
+/**
+ * fosa_mutex_destroy()
+ *
+ * Destroy a frsh mutex
+ *
+ * The mutex pointed to by mutex is destroyed
+ *
+ * Returns 0 if successful; otherwise it returns an error code:
+ *    EINVAL: the value of mutex is invalid
+ *    EBUSY:  The mutex is in use (is locked)
+ **/
+int fosa_mutex_destroy(fosa_mutex_t *mutex)
 {
        return pthread_mutex_destroy(mutex);
 }
 
-int fosa_mutex_set_prioceiling(frsh_mutex_t *mutex,
-       int new_ceiling,
-       int *old_ceiling)
+/**
+ * fosa_mutex_set_prioceiling()
+ *
+ * Dynamically set the priority ceiling of a mutex
+ *
+ * Since in this implementation we use BandWidth Inheritance defining the
+ * ceiling of a mutex is meaningless, and so the function always returns
+ * EINVAL
+ **/
+int fosa_mutex_set_prioceiling(fosa_mutex_t *mutex,
+                              int new_ceiling,
+                              int *old_ceiling)
 {
-       return pthread_mutex_setprioceiling(mutex, new_ceiling, old_ceiling);
+       return FOSA_EINVAL;
 }
 
-int fosa_mutex_get_prioceiling(const frsh_mutex_t *mutex, int *ceiling)
+/**
+ * fosa_mutex_get_prioceiling()
+ *
+ * Dynamically get the priority ceiling of a mutex
+ *
+ * Since in this implementation we use BandWidth Inheritance defining the
+ * ceiling of a mutex is meaningless, and so the function always returns
+ * EINVAL
+ **/
+int fosa_mutex_get_prioceiling(const fosa_mutex_t *mutex, int *ceiling)
 {
-       return pthread_mutex_getprioceiling(mutex, ceiling);
+       return FOSA_EINVAL;
 }
 
-int fosa_mutex_lock(frsh_mutex_t *mutex)
+/**
+ * fosa_mutex_lock()
+ *
+ * Lock a mutex
+ *
+ * This function locks the mutex specified by mutex. If it is already
+ * locked, the calling thread blocks until the mutex becomes
+ * available. The operation returns with the mutex in the locked
+ * state, with the calling thread as its owner.
+ *
+ * Returns 0 if successful; otherwise it returns an error code:
+ *    EINVAL: the value of mutex is invalid, or the priority of the
+ *            calling thread is higher than the priority ceiling of the mutex
+ *    EDEADLK: the current thread already owns this mutex
+ **/
+int fosa_mutex_lock(fosa_mutex_t *mutex)
 {
        return pthread_mutex_lock(mutex);
 }
 
-int fosa_mutex_trylock(frsh_mutex_t *mutex)
+/**
+ * fosa_mutex_trylock()
+ *
+ * Try locking a mutex
+ *
+ * This function is identical to fosa_mutex_lock() except that if the
+ * mutex is already locked the call returns immediately with an error
+ * indication.
+ *
+ * Returns 0 if successful; otherwise it returns an error code:
+ *    EINVAL: the value of mutex is invalid, or the priority of the
+ *            calling thread is higher than the priority ceiling of the mutex
+ *    EBUSY: the mutex was already locked
+ **/
+int fosa_mutex_trylock(fosa_mutex_t *mutex)
 {
        return pthread_mutex_trylock(mutex);
 }
 
-int fosa_mutex_unlock(frsh_mutex_t *mutex)
+/**
+ * fosa_mutex_unlock()
+ *
+ * Unlock a mutex
+ *
+ * This function must be called by the owner of the mutex referenced
+ * by mutex, to unlock it. If there are threads blocked on the mutex
+ * the mutex becomes available and the highest priority thread is
+ * awakened to acquire the mutex.
+ *
+ * Returns 0 if successful; otherwise it returns an error code:
+ *     EINVAL: the value of mutex is invalid
+ *     EPERM: the calling thread is not the owner of the mutex
+ **/
+int fosa_mutex_unlock(fosa_mutex_t *mutex)
 {
        return pthread_mutex_unlock(mutex);
 }
@@ -108,35 +220,129 @@ int fosa_mutex_unlock(frsh_mutex_t *mutex)
  * Condition variables
  *********************/
 
+/**
+ * fosa_cond_init()
+ *
+ * Initiatize a condition variable
+ *
+ * The condition variable referenced by cond is initialized with
+ * the attributes required by the FOSA implementation.
+ *
+ *  Returns 0 if successful; otherwise it returns an error code:
+ *     EAGAIN: the system lacked the necessary resources to create the
+ *             condition variable
+ *     ENOMEM: Insufficient memory exists to initialize the condition variable
+ *     EBUSY:  The system has detected an attempt to reinitialize the
+ *             condition variable
+ **/
 int fosa_cond_init(fosa_cond_t *cond)
 {
-       return pthread_cond_init(cond, NULL);
+       int ret;
+       pthread_condattr_t attr;
+
+       ret = pthread_condattr_init(&attr);
+       if (ret) return errno;
+
+       ret = pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
+       if (ret) return errno;
+
+       return pthread_cond_init(cond, &attr);
 }
 
+/**
+ * fosa_cond_destroy()
+ *
+ * Destroy a condition variable
+ *
+ * The condition variable pointed to by cond is destroyed
+ *
+ * Returns 0 if successful; otherwise it returns an error code:
+ *     EINVAL: the value of cond is invalid
+ *     EBUSY:  The condition variable is in use (a thread is waiting on it)
+ **/
 int fosa_cond_destroy(fosa_cond_t *cond)
 {
        return pthread_cond_destroy(cond);
 }
 
+/**
+ * fosa_cond_signal()
+ *
+ * Signal a condition variable
+ *
+ * This call unblocks at least one of the threads that are waiting on
+ * the condition variable referenced by cond. If there are no threads
+ * waiting, the function has no effect
+ *
+ * Returns 0 if successful; otherwise it returns an error code:
+ *     EINVAL: the value of cond is invalid
+ **/
 int fosa_cond_signal(fosa_cond_t *cond)
 {
        return pthread_cond_signal(cond);
 }
 
+/**
+ * fosa_cond_broadcast()
+ *
+ * Broadcast a condition variable
+ *
+ * This call unblocks all of the threads that are waiting on the
+ * condition variable referenced by cond. If there are no threads
+ * waiting, the function has no effect.
+ *
+ * Returns 0 if successful; otherwise it returns an error code:
+ *     EINVAL: the value of cond is invalid
+ **/
 int fosa_cond_broadcast(fosa_cond_t *cond)
 {
        return pthread_cond_broadcast(cond);
 }
 
-int fosa_cond_wait(fosa_cond_t *cond, frsh_mutex_t *mutex)
+/**
+ * fosa_cond_wait()
+ *
+ * Wait at a condition variable
+ *
+ * This call is used to block on the condition variable referenced by
+ * cond. It shall be called with the mutex referenced by mutex
+ * locked. The function releases the mutex and blocks the calling
+ * thread until the condition is signalled by some other thread and
+ * the calling thread is awakened. Then it locks the mutex and
+ * returns with the mutex locked by the calling thread.
+ *
+ * Returns 0 if successful; otherwise it returns an error code:
+ *    EINVAL: the value of cond or mutex is invalid, or different
+ *            mutexes were used for concurrent wait operations on cond, or
+ *            the mutex was not owned by the calling thread
+ **/
+int fosa_cond_wait(fosa_cond_t *cond, fosa_mutex_t *mutex)
 {
        return pthread_cond_wait(cond, mutex);
 }
 
+/**
+ * fosa_cond_timedwait()
+ *
+ * Wait at a condition variable, with a timeout
+ *
+ * This function is equal to fosa_cond_wait(), except that the maximum
+ * wait time is limited to the absolute time referenced by abstime, as
+ * measured by the FOSA_CLOCK_ABSOLUTE clock.
+ *
+ * Returns 0 if successful; otherwise it returns an error code:
+ *     EINVAL: the value of cond or mutex or abstime is invalid, or different
+ *             mutexes were used for concurrent wait operations on cond, or
+ *             the mutex was not owned by the calling thread
+ *     ETIMEDOUT: the timeout expired
+ **/
 int fosa_cond_timedwait(fosa_cond_t *cond,
-       frsh_mutex_t *mutex,
-       const struct timespec *abstime)
+       fosa_mutex_t *mutex,
+       const fosa_abs_time_t *abstime)
 {
-       return pthread_cond_timedwait(cond, mutex, abstime);
+       struct timespec abstime_tspec;
+
+       abstime_tspec = fosa_abs_time_to_timespec(*abstime);
+       return pthread_cond_timedwait(cond, mutex, &abstime_tspec);
 }