2 * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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 *****************************************************************************/
23 #include "_pbWaitforsingleobject.h"
24 #include <vehicle_service/positioning_base_library.h>
25 #include "WPF_STD_private.h"
26 #include "_pbInternalProc.h"
28 typedef struct HandleTypeTable {
29 HANDLE h_handle; /* Registration handle */
30 HANDLE_KIND l_kind; /* Type of Mutex/Semaphre/Event... */
31 struct HandleTypeTable *next;
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 */
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);
44 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
46 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
48 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
49 * _sys Internally Used APIs
50 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
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
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;
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
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? */
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
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
104 HANDLE_TYPE *p_add_list = NULL;
107 && (PB_HANDLE_INVAL < l_kind)
108 && (PB_HANDLE_KIND_MAX > l_kind)) {
110 bret = FindList(&p_add_list, h_obj);
112 /* Handle already registered */
113 if (l_kind == p_add_list->l_kind) {
114 /* re-register the same item */
117 /* An attempt was made to re-register the same handle with different type */
118 bret = FALSE; /** Consider whether to win or not later */
121 p_add_list = reinterpret_cast<HANDLE_TYPE*>(malloc(sizeof(HANDLE_TYPE)));
122 if (NULL == p_add_list) {
123 /* Memory acquisition failed */
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);
132 /* Registration failure */
144 bret = FALSE; /** Parameter error */
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
159 @retval TRUE : Normal
161 *****************************************************************************/
162 BOOL WaitObjectDel(HANDLE h_obj) { // LCOV_EXCL_START 8:dead code
163 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
165 HANDLE_TYPE *p_del_list = NULL;
169 bret = FindList(&p_del_list, h_obj);
171 /* handle already registered */
172 if (TRUE == DelList(p_del_list)) {
179 bret = FALSE; /* no handles */
185 bret = FALSE; /* Parameter error */
191 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
193 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
195 /****************************************************************************
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
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;
213 if ((NULL != h_obj) && (NULL != p_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) {
226 bret = FALSE; /** Parameter error */
233 /****************************************************************************
235 Append data to the end of the list
237 Append data to the end of the list
238 @type Completion return type
239 @param[in] HANDLE_TYPE* p_list : Data to add
242 @retval TRUE : Normal
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;
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;
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;
268 bret = FALSE; /** Parameter error */
275 /****************************************************************************
277 Remove specified data from a List
279 Remove specified data from a List
280 @type Completion return type
281 @param[in,out] HANDLE_TYPE* h_del_obj :
284 @retval TRUE : Normal
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;
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) {
300 g_pst_handle_kind_table = p_now->next;
303 pBef->next = h_del_obj->next;
312 bret = FALSE; /** Parameter error */
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
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
332 if (EOK == pthread_mutex_lock(&g_func_lock_mutex)) {
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
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
352 if (EOK == pthread_mutex_unlock(&g_func_lock_mutex)) {