po_hi_protected.c 7.26 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
/*
 * This is a part of PolyORB-HI-C distribution, a minimal
 * middleware written for generated code from AADL models.
 * You should use it with the Ocarina toolsuite.
 *
 * For more informations, please visit http://ocarina.enst.fr
 *
 * Copyright (C) 2007-2008, GET-Telecom Paris.
 */

#include <po_hi_config.h>
#include <po_hi_protected.h>
#include <po_hi_returns.h>
14
15
#include <po_hi_debug.h>
#include <po_hi_task.h>
16
17
18
19
20
21
22
23
24
#include <po_hi_types.h>
#include <deployment.h>

#ifndef __PO_HI_NB_PROTECTED
#define __PO_HI_NB_PROTECTED 0
#endif

#if __PO_HI_NB_PROTECTED > 0

25
#if defined (RTEMS_POSIX) || defined (POSIX) || defined (XENO_POSIX)
26
#define __USE_UNIX98 1
27
#include <pthread.h>
28
#include <po_hi_task.h>
29

30
31
32
33
34
/* 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.
 */

35
36
37
38
pthread_mutex_t                        __po_hi_protected_mutexes[__PO_HI_NB_PROTECTED];
pthread_mutexattr_t                    __po_hi_protected_mutexes_attr[__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];
39
40
41

int __po_hi_protected_init ()
{
42
   __po_hi_uint8_t i;
43
   __po_hi_uint8_t prio;
44

45
46
   for (i = 0 ; i < __PO_HI_NB_PROTECTED ; i++ )
   {
47
48
49
50
51
      if (pthread_mutexattr_init (&__po_hi_protected_mutexes_attr[i]) != 0)
      {
         __PO_HI_DEBUG_DEBUG ("[PROTECTED] Error while initializing mutex attr\n");
      }

52
#if defined (RTEMS_POSIX) || defined (POSIX)
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
      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");
         }

      }
73
#endif
74
75

      if (__po_hi_protected_configuration[i] == __PO_HI_PROTECTED_PIP)
76
      {
77
78
79
80
81
82
83
84
85
         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)
      {
         __PO_HI_DEBUG_DEBUG ("[PROTECTED] Error while creating mutex %d\n", i);
86
87
         return __PO_HI_ERROR_PROTECTED_CREATE;
      }
88
89

      __PO_HI_DEBUG_DEBUG ("[PROTECTED] Successfully create mutex %d\n", i);
90
91
   }
   return (__PO_HI_SUCCESS);
92
93
94
95
}

int __po_hi_protected_lock (__po_hi_protected_t protected_id)
{
96
97
   if (pthread_mutex_lock (&__po_hi_protected_mutexes[protected_id]) != 0 )
   {
98
      __PO_HI_DEBUG_DEBUG ("[PROTECTED] Error when lock protected resource %d\n", protected_id);
99
100
      return __PO_HI_ERROR_PROTECTED_LOCK;
   }
101
102

   __PO_HI_DEBUG_DEBUG ("[PROTECTED] Successfully lock protected resource %d\n", protected_id);
103
104
105
106
107
108
   return __PO_HI_SUCCESS;
}

int __po_hi_protected_unlock (__po_hi_protected_t protected_id)
{
  if (pthread_mutex_unlock (&__po_hi_protected_mutexes[protected_id]) != 0 )
109
    {
110
      __PO_HI_DEBUG_DEBUG ("[PROTECTED] Error when unlock protected resource %d\n", protected_id);
111
      return __PO_HI_ERROR_PROTECTED_UNLOCK;
112
    }
113
114

  __PO_HI_DEBUG_DEBUG ("[PROTECTED] Successfully unlock protected resource %d\n", protected_id);
115
116
  return __PO_HI_SUCCESS;
}
117
#endif /* POSIX or RTEMS_POSIX or XENO_POSIX*/
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137


#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++ )
   {
138
      /*FIXME : handle different locking protocols !!! */
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
      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;
}
162
163
164

int __po_hi_protected_unlock (__po_hi_protected_t protected_id)
{
165
166
167
168
169
170
171
172
173
   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;
   }

174
175
  return __PO_HI_SUCCESS;
}
176
177
#endif /* RTEMS_PURE */

178

179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#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;
   }

   __PO_HI_DEBUG_DEBUG ("[PROTECTED] Successfully lock protected resource %d\n", protected_id);
   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 )
    {
      __PO_HI_DEBUG_DEBUG ("[PROTECTED] Error when unlock protected resource %d\n", protected_id);
      return __PO_HI_ERROR_PROTECTED_UNLOCK;
    }

  __PO_HI_DEBUG_DEBUG ("[PROTECTED] Successfully unlock protected resource %d\n", protected_id);
  return __PO_HI_SUCCESS;
}
#endif /* XENO_NATIVE */
231
232

#endif /* __PO_HI_NB_PROTECTED > 0 */
233