po_hi_protected.h 4.59 KB
Newer Older
1
2
3
4
5
/*
 * 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.
 *
yoogx's avatar
yoogx committed
6
 * For more informations, please visit http://taste.tuxfamily.org/wiki
7
 *
8
 * Copyright (C) 2007-2009 Telecom ParisTech, 2010-2018 ESA & ISAE.
9
10
11
12
 */

#ifndef __PO_HI_PROTECTED_H__
#define __PO_HI_PROTECTED_H__
13

14

15
16
17
#include <stdint.h>
#include <deployment.h>

18
19
20
#define __PO_HI_PROTECTED_TYPE_REGULAR    0
#define __PO_HI_PROTECTED_TYPE_PIP        1
#define __PO_HI_PROTECTED_TYPE_PCP        2
21

22
23
24
25
26
27
#if defined (POSIX) || defined (RTEMS_POSIX) || defined (XENO_POSIX)
   #include <stdlib.h>
   #include <stdint.h>
   #include <time.h>
   #include <pthread.h>
#endif
jdelange's avatar
jdelange committed
28

29
#if defined (__PO_HI_RTEMS_CLASSIC_API)
30
31
   #include <rtems.h>
#endif
jdelange's avatar
jdelange committed
32

33
34
35
36
#if defined (XENO_NATIVE)
   #include <native/mutex.h>
#endif

jdelange's avatar
jdelange committed
37
38
39
40
#ifdef _WIN32
#include <windows.h>
#endif

jdelange's avatar
jdelange committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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;

56
57
58
59
60
61
62
63
typedef struct
{
   __po_hi_mutex_protocol_t   protocol;
   int                        priority;
#if defined (POSIX) || defined (RTEMS_POSIX) || defined (XENO_POSIX)
   pthread_mutex_t      posix_mutex;
   pthread_mutexattr_t  posix_mutexattr;
#endif
64
#if defined (__PO_HI_RTEMS_CLASSIC_API)
65
66
67
68
69
   rtems_id             rtems_mutex;
#endif
#if defined (XENO_NATIVE)
   RT_MUTEX             xeno_mutex;
#endif
jdelange's avatar
jdelange committed
70
71
72
#if defined (_WIN32)
   HANDLE               win32_mutex;
#endif
73
74
}__po_hi_mutex_t;

75
76
77
typedef uint8_t __po_hi_protected_t;

/*
jdelange's avatar
jdelange committed
78
79
80
81
 * \fn __po_hi_protected_lock
 *
 * \brief Lock the variable which has he id given by the argument.
 *
82
83
84
 * Return __PO_HI_SUCCESS if it is successfull.  If there is an error,
 * it can return __PO_HI_ERROR_PTHREAD_MUTEX value
 */
jdelange's avatar
jdelange committed
85
int __po_hi_protected_lock (__po_hi_protected_t protected_id);
86

jdelange's avatar
jdelange committed
87
88
89
/**
 * \fn __po_hi_protected_lock
 *
yoogx's avatar
yoogx committed
90
 * Unlock the variable which has he id given
91
92
 * by the argument.
 * Return __PO_HI_SUCCESS if it is successfull.
yoogx's avatar
yoogx committed
93
 * If there is an error, it can return
94
95
 * __PO_HI_ERROR_PTHREAD_MUTEX value
 */
jdelange's avatar
jdelange committed
96
int __po_hi_protected_unlock (__po_hi_protected_t protected_id);
97

jdelange's avatar
jdelange committed
98
99
100
101
/**
 * \fn __po_hi_protected_init
 *
 * \brief Initialize all variables to handle protected objects in PolyORB-HI-C
102
 */
jdelange's avatar
jdelange committed
103
int __po_hi_protected_init (void);
104

jdelange's avatar
jdelange committed
105
106
107
108

/**
 * \fn __po_hi_mutex_init
 *
yoogx's avatar
yoogx committed
109
 * \brief Initialize a mutex no matter the underlying executive
jdelange's avatar
jdelange committed
110
111
112
 *
 * 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
yoogx's avatar
yoogx committed
113
 * parameter is the locking protocol of the mutex that is mapped on the
jdelange's avatar
jdelange committed
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
 * 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
 *
yoogx's avatar
yoogx committed
129
 * \brief Lock a mutex no matter the underlying executive
jdelange's avatar
jdelange committed
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
 *
 * 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
 *
yoogx's avatar
yoogx committed
146
 * \brief Unlock a mutex no matter the underlying executive
jdelange's avatar
jdelange committed
147
148
149
150
151
152
153
154
155
156
157
158
159
 *
 * 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);

160

161
#endif /*  __PO_HI_PROTECTED_H__ */