Commit d74a8e63 authored by jdelange's avatar jdelange
Browse files

* po-hi-c/include/po_hi_protected.h

   po-hi-c/src/po_hi_protected.c
   - Factorisation of existing codem avoiding redundant mutex wrappers.



git-svn-id: https://tecsw.estec.esa.int/svn/taste/trunk/po-hi-c@3518 129961e7-ef38-4bb5-a8f7-c9a525a55882
parent cd2c47a8
......@@ -11,6 +11,16 @@
#ifndef __PO_HI_PROTECTED_H__
#define __PO_HI_PROTECTED_H__
#include <po_hi_config.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 <stdint.h>
#include <deployment.h>
......
......@@ -23,7 +23,10 @@
#define __PO_HI_NB_PROTECTED 0
#endif
#if __PO_HI_NB_PROTECTED > 0
#if defined (RTEMS_POSIX) || defined (POSIX) || defined (XENO_POSIX)
#define __USE_UNIX98 1
#include <pthread.h>
#endif
#ifdef XENO_NATIVE
#include <native/mutex.h>
......@@ -33,18 +36,14 @@
#include <rtems.h>
#endif
#if defined (RTEMS_POSIX) || defined (POSIX) || defined (XENO_POSIX)
#define __USE_UNIX98 1
#include <pthread.h>
#if __PO_HI_NB_PROTECTED > 0
/* Declare only needed mutexes according to the generated
* declarations. The __PO_HI_NB_PROTECTED is a generated macro that
* represents the needed number of mutexes in the system.
*/
pthread_mutex_t __po_hi_protected_mutexes[__PO_HI_NB_PROTECTED];
pthread_mutexattr_t __po_hi_protected_mutexes_attr[__PO_HI_NB_PROTECTED];
__po_hi_mutex_t __po_hi_protected_mutexes[__PO_HI_NB_PROTECTED];
extern __po_hi_protected_protocol_t __po_hi_protected_configuration[__PO_HI_NB_PROTECTED];
extern __po_hi_uint8_t __po_hi_protected_priorities[__PO_HI_NB_PROTECTED];
......@@ -55,49 +54,10 @@ int __po_hi_protected_init ()
for (i = 0 ; i < __PO_HI_NB_PROTECTED ; i++ )
{
if (pthread_mutexattr_init (&__po_hi_protected_mutexes_attr[i]) != 0)
{
__PO_HI_DEBUG_DEBUG ("[PROTECTED] Error while initializing mutex attr\n");
}
#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)
{
__PO_HI_DEBUG_DEBUG ("[PROTECTED] Error while changing mutex protocol\n");
}
prio = __po_hi_protected_priorities[i];
if (prio == 0)
{
prio = __PO_HI_MAX_PRIORITY - 1;
}
if (pthread_mutexattr_setprioceiling (&__po_hi_protected_mutexes_attr[i], prio) != 0)
{
__PO_HI_DEBUG_DEBUG ("[PROTECTED] Error while changing mutex priority\n");
}
}
#endif
if (__po_hi_protected_configuration[i] == __PO_HI_PROTECTED_PIP)
{
if (pthread_mutexattr_setprotocol (&__po_hi_protected_mutexes_attr[i], PTHREAD_PRIO_INHERIT) != 0)
{
__PO_HI_DEBUG_DEBUG ("[PROTECTED] Error while changing mutex protocol\n");
}
}
if (pthread_mutex_init (&__po_hi_protected_mutexes[i], &__po_hi_protected_mutexes_attr[i]) != 0)
if (__po_hi_mutex_init (&__po_hi_protected_mutexes[i], __po_hi_protected_configuration[i], __po_hi_protected_priorities[i]) != __PO_HI_SUCCESS)
{
__PO_HI_DEBUG_DEBUG ("[PROTECTED] Error while creating mutex %d\n", i);
return __PO_HI_ERROR_PROTECTED_CREATE;
__PO_HI_DEBUG_DEBUG ("[PROTECTED] Error when initialize protected resource %d\n", i);
}
__PO_HI_DEBUG_DEBUG ("[PROTECTED] Successfully create mutex %d\n", i);
}
return (__PO_HI_SUCCESS);
}
......@@ -105,7 +65,7 @@ int __po_hi_protected_init ()
int __po_hi_protected_lock (__po_hi_protected_t protected_id)
{
__PO_HI_INSTRUMENTATION_VCD_WRITE("1w%d\n", protected_id);
if (pthread_mutex_lock (&__po_hi_protected_mutexes[protected_id]) != 0 )
if (__po_hi_mutex_lock (&__po_hi_protected_mutexes[protected_id]) != __PO_HI_SUCCESS )
{
__PO_HI_INSTRUMENTATION_VCD_WRITE("0w%d\n", protected_id);
__PO_HI_DEBUG_DEBUG ("[PROTECTED] Error when lock protected resource %d\n", protected_id);
......@@ -121,118 +81,7 @@ int __po_hi_protected_unlock (__po_hi_protected_t protected_id)
{
__PO_HI_INSTRUMENTATION_VCD_WRITE("0l%d\n", protected_id);
if (pthread_mutex_unlock (&__po_hi_protected_mutexes[protected_id]) != 0 )
{
__PO_HI_DEBUG_DEBUG ("[PROTECTED] Error when unlock protected resource %d\n", protected_id);
return __PO_HI_ERROR_PROTECTED_UNLOCK;
}
return __PO_HI_SUCCESS;
}
#endif /* POSIX or RTEMS_POSIX or XENO_POSIX*/
#if defined (RTEMS_PURE)
/* Declare only needed mutexes according to the generated
* declarations. The __PO_HI_NB_PROTECTED is a generated macro that
* represents the needed number of mutexes in the system.
*/
#include <rtems.h>
rtems_id __po_hi_protected_mutexes[__PO_HI_NB_PROTECTED];
int __po_hi_protected_init ()
{
__po_hi_uint8_t i;
rtems_status_code ret;
for (i = 0 ; i < __PO_HI_NB_PROTECTED ; i++ )
{
/*FIXME : handle different locking protocols !!! */
ret = rtems_semaphore_create (rtems_build_name ('P', 'S', 'E' , 'A' + (char) i), 1, RTEMS_BINARY_SEMAPHORE, __PO_HI_DEFAULT_PRIORITY, &(__po_hi_protected_mutexes[i]));
if (ret != RTEMS_SUCCESSFUL)
{
__DEBUGMSG ("[PROTECTED] Cannot create binary semaphore, error code=%d\n", ret);
return __PO_HI_ERROR_PROTECTED_CREATE;
}
}
return (__PO_HI_SUCCESS);
}
int __po_hi_protected_lock (__po_hi_protected_t protected_id)
{
rtems_status_code ret;
__DEBUGMSG ("[PROTECTED] Try to obtain binary semaphore for protected object %d\n", protected_id);
ret = rtems_semaphore_obtain (__po_hi_protected_mutexes[protected_id], RTEMS_WAIT, RTEMS_NO_TIMEOUT);
if (ret != RTEMS_SUCCESSFUL)
{
__DEBUGMSG ("[PROTECTED] Cannot obtain binary semaphore in __po_hi_protected_lock()\n");
return __PO_HI_ERROR_PROTECTED_LOCK;
}
return __PO_HI_SUCCESS;
}
int __po_hi_protected_unlock (__po_hi_protected_t protected_id)
{
rtems_status_code ret;
__DEBUGMSG ("[PROTECTED] Try to release protected object %d\n", protected_id);
ret = rtems_semaphore_release (__po_hi_protected_mutexes[protected_id]);
if (ret != RTEMS_SUCCESSFUL)
{
__DEBUGMSG ("[PROTECTED] Cannot release semaphore in __po_hi_protected_unlock()\n");
return __PO_HI_ERROR_PROTECTED_UNLOCK;
}
return __PO_HI_SUCCESS;
}
#endif /* RTEMS_PURE */
#if defined (XENO_NATIVE)
#include <native/mutex.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
* represents the needed number of mutexes in the system.
*/
RT_MUTEX __po_hi_protected_mutexes[__PO_HI_NB_PROTECTED];
int __po_hi_protected_init ()
{
__po_hi_uint8_t i;
for (i = 0 ; i < __PO_HI_NB_PROTECTED ; i++ )
{
if (rt_mutex_create (&__po_hi_protected_mutexes[i], NULL) != 0)
{
__PO_HI_DEBUG_DEBUG ("[PROTECTED] Error while creating mutex %d\n", i);
return __PO_HI_ERROR_PROTECTED_CREATE;
}
__PO_HI_DEBUG_DEBUG ("[PROTECTED] Successfully create mutex %d\n", i);
}
return (__PO_HI_SUCCESS);
}
int __po_hi_protected_lock (__po_hi_protected_t protected_id)
{
if (rt_mutex_acquire (&__po_hi_protected_mutexes[protected_id], TM_INFINITE) != 0 )
{
__PO_HI_DEBUG_DEBUG ("[PROTECTED] Error when lock protected resource %d\n", protected_id);
return __PO_HI_ERROR_PROTECTED_LOCK;
}
return __PO_HI_SUCCESS;
}
int __po_hi_protected_unlock (__po_hi_protected_t protected_id)
{
if (rt_mutex_release (&__po_hi_protected_mutexes[protected_id]) != 0 )
if (__po_hi_mutex_unlock (&__po_hi_protected_mutexes[protected_id]) != __PO_HI_SUCCESS )
{
__PO_HI_DEBUG_DEBUG ("[PROTECTED] Error when unlock protected resource %d\n", protected_id);
return __PO_HI_ERROR_PROTECTED_UNLOCK;
......@@ -240,12 +89,10 @@ int __po_hi_protected_unlock (__po_hi_protected_t protected_id)
return __PO_HI_SUCCESS;
}
#endif /* XENO_NATIVE */
#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;
......
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