common_library: gettid is multiple declaration in cl_error
[staging/basesystem.git] / video_in_hal / vehicleservice / positioning_base_library / library / src / _pbWaitforsingleobject.cpp
1 /*
2  * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /****************************************************************************
18 @file            waitforsingleobject.cpp
19 @detail            Functions that implement waitforsingleobject in PosixBasedOS001<BR>
20                 Functions to register/delete a table to determine the handle and its type
21 *****************************************************************************/
22
23 #include "_pbWaitforsingleobject.h"
24 #include <vehicle_service/positioning_base_library.h>
25 #include "WPF_STD_private.h"
26 #include "_pbInternalProc.h"
27
28 typedef struct HandleTypeTable {
29     HANDLE        h_handle;   /* Registration handle */
30     HANDLE_KIND    l_kind;   /*  Type of Mutex/Semaphre/Event... */
31     struct HandleTypeTable *next;
32 } HANDLE_TYPE;
33
34 static HANDLE_TYPE *g_pst_handle_kind_table = NULL;
35 static pthread_mutex_t g_func_lock_mutex = PTHREAD_MUTEX_INITIALIZER;   /* Consider replacing it later */
36
37 /* Prototype declarations */
38 static BOOL FindList(HANDLE_TYPE **p_list, HANDLE h_obj);
39 static BOOL AddList(HANDLE_TYPE *p_add_list);
40 static BOOL DelList(HANDLE_TYPE *h_del_obj);
41 static BOOL FunctionLock(void);
42 static BOOL FunctionUnlock(void);
43
44 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
45 * Public APIs
46 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
47
48 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
49 * _sys Internally Used APIs
50 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
51
52 /****************************************************************************
53 @brief            WaitObjectInit<BR>
54                 Initialization processing for each process
55 @outline        WaitObjectInit<BR>
56                 Initialization processing for each process
57 @type            Completion return type
58 @return            BOOL
59 @retval            TRUE    :    Normal
60 @retval            FALSE    :    Error
61 *****************************************************************************/
62 BOOL WaitObjectInit(void) {  // LCOV_EXCL_START 8:dead code
63     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
64     g_pst_handle_kind_table = NULL;
65     return TRUE;
66 }
67 // LCOV_EXCL_STOP
68
69 /****************************************************************************
70 @brief            WaitObjectTerm<BR>
71                 Termination processing for each process
72 @outline        WaitObjectTerm<BR>
73                 Termination processing for each process
74 @type            Completion return type
75 @return            BOOL
76 @retval            TRUE    :    Normal
77 @retval            FALSE    :    Error
78 *****************************************************************************/
79 BOOL WaitObjectTerm(void) {  // LCOV_EXCL_START 8:dead code
80     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
81     if (NULL != g_pst_handle_kind_table) {
82         /* Should be Deleted a List? */
83     }
84     return TRUE;
85 }
86 // LCOV_EXCL_STOP
87
88 /****************************************************************************
89 @brief            WaitObjectAdd<BR>
90                 Register a handle and a handle-type in a list
91 @outline        WaitObjectAdd<BR>
92                 Register a handle and a handle-type in a list
93 @type            Completion return type
94 @param[in]    HANDLE        h_obj    :    Handle to register
95 @param[in]    HANDLE_KIND    l_kind    :    Handle type
96
97 @return            BOOL
98 @retval            TRUE    :    Normal
99                 FALSE    :    Error
100 *****************************************************************************/
101 BOOL WaitObjectAdd(HANDLE h_obj, HANDLE_KIND l_kind) {  // LCOV_EXCL_START 8:dead code
102     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
103     BOOL bret = FALSE;
104     HANDLE_TYPE *p_add_list = NULL;
105
106     if ((NULL != h_obj)
107             && (PB_HANDLE_INVAL < l_kind)
108             && (PB_HANDLE_KIND_MAX > l_kind)) {
109         FunctionLock();
110         bret = FindList(&p_add_list, h_obj);
111         if (TRUE == bret) {
112             /* Handle already registered */
113             if (l_kind == p_add_list->l_kind) {
114                 /* re-register the same item */
115                 bret = TRUE;
116             } else {
117                 /* An attempt was made to re-register the same handle with different type */
118                 bret = FALSE;   /** Consider whether to win or not later */
119             }
120         } else {
121             p_add_list = reinterpret_cast<HANDLE_TYPE*>(malloc(sizeof(HANDLE_TYPE)));
122             if (NULL == p_add_list) {
123                 /* Memory acquisition failed */
124                 bret = FALSE;
125             } else {
126                 memset(p_add_list, 0, sizeof(HANDLE_TYPE));
127                 p_add_list->h_handle = h_obj;
128                 p_add_list->l_kind = l_kind;
129                 p_add_list->next = NULL;
130                 bret = AddList(p_add_list);
131                 if (FALSE == bret) {
132                     /* Registration failure */
133                     free(p_add_list);
134                     bret = FALSE;
135                 } else {
136                     bret = TRUE;
137                 }
138             }
139         }
140
141         FunctionUnlock();
142
143     } else {
144         bret = FALSE;   /** Parameter error */
145     }
146     return bret;
147 }
148 // LCOV_EXCL_STOP
149
150 /****************************************************************************
151 @brief            WaitObjectDel<BR>
152                 Remove handle and handle-type from the list
153 @outline        WaitObjectDel<BR>
154                 Remove handle and handle-type from the list
155 @type            Completion return type
156 @param[in]    HANDLE        h_obj    :    Handle to delete
157
158 @return            BOOL
159 @retval            TRUE    :    Normal
160                 FALSE    :    Error
161 *****************************************************************************/
162 BOOL WaitObjectDel(HANDLE h_obj) {  // LCOV_EXCL_START 8:dead code
163     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
164     BOOL bret = FALSE;
165     HANDLE_TYPE *p_del_list = NULL;
166
167     if (NULL != h_obj) {
168         FunctionLock();
169         bret = FindList(&p_del_list, h_obj);
170         if (TRUE == bret) {
171             /* handle already registered */
172             if (TRUE == DelList(p_del_list)) {
173                 free(p_del_list);
174                 bret = TRUE;
175             } else {
176                 bret = FALSE;
177             }
178         } else {
179             bret = FALSE;   /*  no handles */
180         }
181
182         FunctionUnlock();
183
184     } else {
185         bret = FALSE;   /* Parameter error */
186     }
187     return bret;
188 }
189 // LCOV_EXCL_STOP
190
191 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
192 * Private API
193 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
194
195 /****************************************************************************
196 @brief            FindList<BR>
197                 Searching for a Handle in the List
198 @outline        FindList<BR>
199                 Searching for a Handle in the List
200 @type            Completion return type
201 @param[out]        HANDLE_TYPE**    p_list    :    Found list pointer
202 @param[in]        HANDLE            h_obj    :    Handle to look for
203
204 @return            BOOL
205 @retval            TRUE    :    Normal (p_list != NULL)
206                 FALSE    :    Error (p_list == NULL)
207 *****************************************************************************/
208 static BOOL FindList(HANDLE_TYPE **p_list, HANDLE h_obj) {  // LCOV_EXCL_START 8:dead code
209     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
210     HANDLE_TYPE *p_now = NULL;
211     BOOL bret = FALSE;
212
213     if ((NULL != h_obj) && (NULL != p_list)) {
214         /* Search list */
215         p_now = g_pst_handle_kind_table;
216         while (NULL != p_now) {
217             /* h_obj and p_now->h_handle are pointer type.*/
218             if ((int64_t)h_obj == (int64_t)p_now->h_handle) {
219                 *p_list = p_now;
220                 bret = TRUE;
221                 break;
222             }
223             p_now = p_now->next;
224         }
225     } else {
226         bret = FALSE;   /** Parameter error */
227     }
228
229     return bret;
230 }
231 // LCOV_EXCL_STOP
232
233 /****************************************************************************
234 @brief            AddList<BR>
235                 Append data to the end of the list
236 @outline        AddList<BR>
237                 Append data to the end of the list
238 @type            Completion return type
239 @param[in]        HANDLE_TYPE*    p_list    :    Data to add
240
241 @return            BOOL
242 @retval            TRUE    :    Normal
243                 FALSE    :    Error
244 *****************************************************************************/
245 static BOOL AddList(HANDLE_TYPE *p_add_list) {  // LCOV_EXCL_START 8:dead code
246     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
247     HANDLE_TYPE *p_now = NULL;
248     BOOL bret = FALSE;
249
250     if (NULL != p_add_list) {
251         /* Add unregistered data */
252         if (NULL == g_pst_handle_kind_table) {
253             g_pst_handle_kind_table = p_add_list;
254             bret = TRUE;
255         } else {
256             /* Add to end of list */
257             p_now = g_pst_handle_kind_table;
258             while (NULL != p_now) {
259                 if (NULL == p_now->next) {
260                     p_now->next = p_add_list;
261                     bret = TRUE;
262                     break;
263                 }
264                 p_now = p_now->next;
265             }
266         }
267     } else {
268         bret = FALSE;   /** Parameter error */
269     }
270
271     return bret;
272 }
273 // LCOV_EXCL_STOP
274
275 /****************************************************************************
276 @brief            DelList<BR>
277                 Remove specified data from a List
278 @outline        DelList<BR>
279                 Remove specified data from a List
280 @type            Completion return type
281 @param[in,out]    HANDLE_TYPE*    h_del_obj    :
282
283 @return            BOOL
284 @retval            TRUE    :    Normal
285                 FALSE    :    End
286 *****************************************************************************/
287 static BOOL DelList(HANDLE_TYPE *h_del_obj) {  // LCOV_EXCL_START 8:dead code
288     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
289     HANDLE_TYPE *p_now = NULL;
290     HANDLE_TYPE *pBef = NULL;
291     BOOL bret = FALSE;
292
293     if (NULL != h_del_obj) {
294         /* Add to end of list */
295         p_now = g_pst_handle_kind_table;
296         while (NULL != p_now) {
297             if (h_del_obj == p_now) {
298                 if (NULL == pBef) {
299                     /* Delete first */
300                     g_pst_handle_kind_table = p_now->next;
301                 } else {
302                     /* Others */
303                     pBef->next = h_del_obj->next;
304                 }
305                 bret = TRUE;
306                 break;
307             }
308             pBef = p_now;
309             p_now = p_now->next;
310         }
311     } else {
312         bret = FALSE;   /** Parameter error */
313     }
314
315     return bret;
316 }
317 // LCOV_EXCL_STOP
318
319 /****************************************************************************
320 @brief            FunctionLock<BR>
321                 Start locking of g_pst_handle_kind_table
322 @outline        FunctionLock<BR>
323                 Start locking of g_pst_handle_kind_table
324 @type            Completion return type
325 @return            BOOL
326 @retval            TRUE    :    Normal
327 @retval            FALSE    :    Error
328 *****************************************************************************/
329 static BOOL FunctionLock(void) {  // LCOV_EXCL_START 8:dead code
330     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
331     BOOL bret = FALSE;
332     if (EOK == pthread_mutex_lock(&g_func_lock_mutex)) {
333         bret = TRUE;
334     }
335     return bret;
336 }
337 // LCOV_EXCL_STOP
338
339 /****************************************************************************
340 @brief            FunctionUnlock<BR>
341                 Terminate locking of g_pst_handle_kind_table
342 @outline        FunctionUnlock<BR>
343                 Terminate locking of g_pst_handle_kind_table
344 @type            Completion return type
345 @return            BOOL
346 @retval            TRUE    :    Normal
347 @retval            FALSE    :    Error
348 *****************************************************************************/
349 static BOOL FunctionUnlock(void) {  // LCOV_EXCL_START 8:dead code
350     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
351     BOOL bret = FALSE;
352     if (EOK == pthread_mutex_unlock(&g_func_lock_mutex)) {
353         bret = TRUE;
354     }
355     return bret;
356 }
357 // LCOV_EXCL_STOP
358