Commit 4e80f972 authored by jdelange's avatar jdelange
Browse files

* include/po_hi_protected.h

   src/po_hi_protected.c
   - Introduce real wrapper for mutexes so that we can
     use generic mutex in other services whatever the underlying
     runtime is executed.



git-svn-id: https://tecsw.estec.esa.int/svn/taste/trunk/po-hi-c@3514 129961e7-ef38-4bb5-a8f7-c9a525a55882
parent 3ab95016
......@@ -6,6 +6,7 @@
* For more informations, please visit http://ocarina.enst.fr
*
* Copyright (C) 2007-2009, GET-Telecom Paris.
* Copyright (C) 2011, European Space Agency.
*/
#ifndef __PO_HI_PROTECTED_H__
......@@ -17,38 +18,130 @@
#define __PO_HI_PROTECTED_TYPE_PIP 1
#define __PO_HI_PROTECTED_TYPE_PCP 2
typedef enum
{
__PO_HI_PROTECTED_REGULAR = 1,
__PO_HI_MUTEX_REGULAR = 1,
__PO_HI_PROTECTED_PIP = 2,
__PO_HI_MUTEX_PIP = 2,
__PO_HI_PROTECTED_PCP = 3,
__PO_HI_MUTEX_PCP = 3,
__PO_HI_PROTECTED_IPCP = 4,
__PO_HI_MUTEX_IPCP = 4,
__PO_HI_PROTECTED_INVALID = 1
}__po_hi_protected_protocol_t;
typedef __po_hi_protected_protocol_t __po_hi_mutex_protocol_t;
typedef uint8_t __po_hi_protected_t;
int __po_hi_protected_lock (__po_hi_protected_t protected_id);
/*
* Lock the variable which has he id given by the argument.
* \fn __po_hi_protected_lock
*
* \brief Lock the variable which has he id given by the argument.
*
* Return __PO_HI_SUCCESS if it is successfull. If there is an error,
* it can return __PO_HI_ERROR_PTHREAD_MUTEX value
*/
int __po_hi_protected_lock (__po_hi_protected_t protected_id);
int __po_hi_protected_unlock (__po_hi_protected_t protected_id);
/*
/**
* \fn __po_hi_protected_lock
*
* Unlock the variable which has he id given
* by the argument.
* Return __PO_HI_SUCCESS if it is successfull.
* If there is an error, it can return
* __PO_HI_ERROR_PTHREAD_MUTEX value
*/
int __po_hi_protected_unlock (__po_hi_protected_t protected_id);
int __po_hi_protected_init (void);
/*
* Initialize all variables to handle protected
* objects in PolyORB-HI-C
/**
* \fn __po_hi_protected_init
*
* \brief Initialize all variables to handle protected objects in PolyORB-HI-C
*/
int __po_hi_protected_init (void);
typedef enum
typedef struct
{
__PO_HI_PROTECTED_REGULAR = 1,
__PO_HI_PROTECTED_PIP = 2,
__PO_HI_PROTECTED_PCP = 3,
__PO_HI_PROTECTED_IPCP = 4,
__PO_HI_PROTECTED_INVALID = 1
}__po_hi_protected_protocol_t;
#if defined (POSIX) || defined (RTEMS_POSIX) || defined (XENO_POSIX)
#include <stdlib.h>
#include <stdint.h>
#include <time.h>
#include <pthread.h>
pthread_mutex_t posix_mutex;
pthread_mutexattr_t posix_mutexattr;
#endif
#ifdef RTEMS_PURE
#include <rtems.h>
rtems_id rtems_mutex;
#endif
#ifdef XENO_NATIVE
#include <native/mutex.h>
RT_MUTEX xeno_mutex;
#endif
__po_hi_mutex_protocol_t protocol;
int priority;
}__po_hi_mutex_t;
/**
* \fn __po_hi_mutex_init
*
* \brief Initialize a mutex no matter the underlying executive
*
* This function allocate all the resources to the mutex so that it can be
* used with __po_hi_mutex_lock() and __po_hi_mutex_unlock(). The second
* parameter is the locking protocol of the mutex that is mapped on the
* appropriate underlyign OS directives if supported. The third argument
* is the priority ceiling used, only relevant if the protocol
* needs such an option. Otherwise, any value can be used.
*
* Upon success, the function returns __PO_HI_SUCCESS.
* It returns the following potential values:
* - __PO_HI_SUCCESS: successful operation
* - __PO_HI_TOOMANY: too many resources allocated at this time
* - __PO_HI_INVALID: supplied argument value (memory error)
*/
int __po_hi_mutex_init (__po_hi_mutex_t* mutex, const __po_hi_mutex_protocol_t protocol, const int priority);
/**
* \fn __po_hi_mutex_lock
*
* \brief Lock a mutex no matter the underlying executive
*
* This function locks the mutex so that it ensures that only one task
* acquired it. Note that if the mutex was previously acquired, the caller
* will be blocked until the mutex is released.
*
* Upon success, the function returns __PO_HI_SUCCESS.
* It returns the following potential values:
* - __PO_HI_SUCCESS: successful operation
* - __PO_HI_INVALID: supplied argument value (memory error)
* - __PO_HI_NOTINITALIZED: supplied resources was not initialized
*/
int __po_hi_mutex_lock (__po_hi_mutex_t* mutex);
/**
* \fn __po_hi_mutex_unlock
*
* \brief Unlock a mutex no matter the underlying executive
*
* This function unlocks the mutex so that other tasks can acquire it
* again.
*
* Upon success, the function returns __PO_HI_SUCCESS.
* It returns the following potential values:
* - __PO_HI_SUCCESS: successful operation
* - __PO_HI_INVALID: supplied argument value (memory error)
* - __PO_HI_NOTINITALIZED: supplied resources was not initialized
*/
int __po_hi_mutex_unlock (__po_hi_mutex_t* mutex);
#endif /* __PO_HI_PROTECTED_H__ */
......@@ -10,12 +10,12 @@
*/
#include <po_hi_config.h>
#include <po_hi_protected.h>
#include <po_hi_returns.h>
#include <po_hi_debug.h>
#include <po_hi_task.h>
#include <po_hi_types.h>
#include <po_hi_utils.h>
#include <po_hi_protected.h>
#include <deployment.h>
......@@ -25,10 +25,18 @@
#if __PO_HI_NB_PROTECTED > 0
#ifdef XENO_NATIVE
#include <native/mutex.h>
#endif
#ifdef RTEMS_PURE
#include <rtems.h>
#endif
#if defined (RTEMS_POSIX) || defined (POSIX) || defined (XENO_POSIX)
#define __USE_UNIX98 1
#include <pthread.h>
#include <po_hi_task.h>
/* Declare only needed mutexes according to the generated
* declarations. The __PO_HI_NB_PROTECTED is a generated macro that
......@@ -52,7 +60,7 @@ int __po_hi_protected_init ()
__PO_HI_DEBUG_DEBUG ("[PROTECTED] Error while initializing mutex attr\n");
}
#if defined (RTEMS_POSIX) || defined (POSIX)
#if defined (RTEMS_POSIX) || defined (POSIX) || defined (XENO_POSIX)
if (__po_hi_protected_configuration[i] == __PO_HI_PROTECTED_IPCP)
{
if (pthread_mutexattr_setprotocol (&__po_hi_protected_mutexes_attr[i], PTHREAD_PRIO_PROTECT) != 0)
......@@ -236,3 +244,138 @@ int __po_hi_protected_unlock (__po_hi_protected_t protected_id)
#endif /* __PO_HI_NB_PROTECTED > 0 */
int __po_hi_mutex_init (__po_hi_mutex_t* mutex, const __po_hi_mutex_protocol_t protocol, const int priority)
{
static int nb_mutex = 0;
if (mutex == NULL)
{
return __PO_HI_INVALID;
}
mutex->protocol = __PO_HI_MUTEX_REGULAR;
mutex->priority = 0;
#if defined (RTEMS_POSIX) || defined (POSIX) || defined (XENO_POSIX)
if (pthread_mutexattr_init (&mutex->posix_mutexattr) != 0)
{
__PO_HI_DEBUG_DEBUG ("[PROTECTED] Error when initializing the mutex\n");
}
switch (protocol)
{
case __PO_HI_MUTEX_IPCP:
{
if (pthread_mutexattr_setprotocol (&mutex->posix_mutexattr, PTHREAD_PRIO_PROTECT) != 0)
{
__PO_HI_DEBUG_DEBUG ("[PROTECTED] Error while changing mutex protocol\n");
}
if (priority == 0)
{
mutex->priority = __PO_HI_MAX_PRIORITY - 1;
}
else
{
mutex->priority = priority;
}
if (pthread_mutexattr_setprioceiling (&mutex->posix_mutexattr, mutex->priority) != 0)
{
__PO_HI_DEBUG_DEBUG ("[PROTECTED] Error while changing mutex priority\n");
}
break;
}
case __PO_HI_PROTECTED_PIP:
{
if (pthread_mutexattr_setprotocol (&mutex->posix_mutexattr, PTHREAD_PRIO_INHERIT) != 0)
{
__PO_HI_DEBUG_DEBUG ("[PROTECTED] Error while changing mutex protocol\n");
}
break;
}
}
if (pthread_mutex_init (&mutex->posix_mutex, &mutex->posix_mutexattr) != 0)
{
__PO_HI_DEBUG_DEBUG ("[PROTECTED] Error while creating mutex\n");
return __PO_HI_ERROR_UNKNOWN;
}
#endif
#ifdef XENO_NATIVE
if (rt_mutex_create (&mutex->xeno_mutex, NULL) != 0)
{
__PO_HI_DEBUG_DEBUG ("[PROTECTED] Error while creating mutex\n");
return __PO_HI_ERROR_UNKNOWN;
}
}
#endif
#ifdef RTEMS_PURE
if (rtems_semaphore_create (rtems_build_name ('P', 'S', 'E' , 'A' + (char) nb_mutex++), 1, RTEMS_BINARY_SEMAPHORE, __PO_HI_DEFAULT_PRIORITY, &mutex->rtems_mutex) != RTEMS_SUCCESSFUL)
{
__DEBUGMSG ("[PROTECTED] Cannot create RTEMS binary semaphore\n");
return __PO_HI_ERROR_PROTECTED_CREATE;
}
}
#endif
return (__PO_HI_SUCCESS);
}
int __po_hi_mutex_lock (__po_hi_mutex_t* mutex)
{
#if defined (RTEMS_POSIX) || defined (POSIX) || defined (XENO_POSIX)
if (pthread_mutex_lock (&mutex->posix_mutex) != 0 )
{
__PO_HI_DEBUG_DEBUG ("[PROTECTED] Error when trying to lock mutex\n");
return __PO_HI_ERROR_MUTEX_LOCK;
}
#endif
#ifdef RTEMS_PURE
if (rtems_semaphore_obtain (mutex->rtems_mutex, RTEMS_WAIT, RTEMS_NO_TIMEOUT) != RTEMS_SUCCESSFUL)
{
__PO_HI_DEBUG_DEBUG ("[PROTECTED] Error when trying to lock mutex\n");
return __PO_HI_ERROR_MUTEX_LOCK;
}
#endif
#ifdef XENO_NATIVE
if (rt_mutex_acquire (&mutex->xeno_mutex, TM_INFINITE) != 0 )
{
__PO_HI_DEBUG_DEBUG ("[PROTECTED] Error when trying to lock mutex\n");
return __PO_HI_ERROR_MUTEX_LOCK;
}
#endif
return __PO_HI_SUCCESS;
}
int __po_hi_mutex_unlock (__po_hi_mutex_t* mutex)
{
#if defined (RTEMS_POSIX) || defined (POSIX) || defined (XENO_POSIX)
if (pthread_mutex_unlock (&mutex->posix_mutex) != 0 )
{
__PO_HI_DEBUG_DEBUG ("[PROTECTED] Error when trying to unlock mutex\n");
return __PO_HI_ERROR_MUTEX_UNLOCK;
}
#endif
#ifdef RTEMS_PURE
if (rtems_semaphore_release (mutex->rtems_mutex) != RTEMS_SUCCESSFUL)
{
__PO_HI_DEBUG_DEBUG ("[PROTECTED] Error when trying to unlock mutex\n");
return __PO_HI_ERROR_MUTEX_UNLOCK;
}
#endif
#ifdef XENO_NATIVE
if (rt_mutex_release (&mutex->xeno_mutex) != 0 )
{
__PO_HI_DEBUG_DEBUG ("[PROTECTED] Error when trying to unlock mutex\n");
return __PO_HI_ERROR_MUTEX_UNLOCK;
}
#endif
return __PO_HI_SUCCESS;
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment