common_library: gettid is multiple declaration in cl_error
[staging/basesystem.git] / video_in_hal / vehicleservice / positioning_base_library / library / src / _pbMutex.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
19  *    _pbMutex.cpp
20  */
21
22 /*---------------------------------------------------------------------------------*
23  * Include Files                                                                   *
24  *---------------------------------------------------------------------------------*/
25
26 #ifndef PT_PB_MUTEX_STUB__CWORD71_
27
28 #include <vehicle_service/positioning_base_library.h>
29 #include "_pbInternalProc.h"
30 #include <native_service/cl_lock.h>
31 #include <native_service/cl_lockid.h>
32 #include "WPF_STD_private.h"
33 #include "tchar.h"
34
35 /*---------------------------------------------------------------------------------*
36  * Define                                                                          *
37  *---------------------------------------------------------------------------------*/
38 /* Mutex control table name (Shared Memory) */
39 #define MUTEX_CTRL_TBL_NAME        __TEXT("POS_BASE_MUTEX_TABLE")
40
41 /*
42    Max. available Lock ID
43   Note : Lock ID application is 1 (for BaseAPI initialization) + 1 (for Mutex function control) + 30 (for provision) = 32
44 */
45 #define MAX_CTRL_MUTEX_NUM        (30)
46
47 #define FULL_CTRL_MUTEX_NUM (MAX_CTRL_MUTEX_NUM - 4)  /** Mutex control data threshold (no free) */
48 #define WARN_CTRL_MUTEX_NUM (MAX_CTRL_MUTEX_NUM - 10) /** Mutex control data threshold (warning) */
49
50 /*
51     ID needs to be aplicated
52
53     If you increase or decrease the IDs, define maximum number of managed LOCK IDs and
54     the default setting of "Lock ID Resource List" must also be changed.
55 */
56 #define MUTEX_LOCK_ID_0        LOCK_POS_MTX_2 /* Mutex management-information-exclusive control */
57 #define MUTEX_LOCK_ID_1        LOCK_POS_MTX_3
58 #define MUTEX_LOCK_ID_2        LOCK_POS_MTX_4
59 #define MUTEX_LOCK_ID_3        LOCK_POS_MTX_5
60 #define MUTEX_LOCK_ID_4        LOCK_POS_MTX_6
61 #define MUTEX_LOCK_ID_5        LOCK_POS_MTX_7
62 #define MUTEX_LOCK_ID_6        LOCK_POS_MTX_8
63 #define MUTEX_LOCK_ID_7        LOCK_POS_MTX_9
64 #define MUTEX_LOCK_ID_8        LOCK_POS_MTX_10
65 #define MUTEX_LOCK_ID_9        LOCK_POS_MTX_11
66 #define MUTEX_LOCK_ID_10    LOCK_POS_MTX_12
67 #define MUTEX_LOCK_ID_11    LOCK_POS_MTX_13
68 #define MUTEX_LOCK_ID_12    LOCK_POS_MTX_14
69 #define MUTEX_LOCK_ID_13    LOCK_POS_MTX_15
70 #define MUTEX_LOCK_ID_14    LOCK_POS_MTX_16
71 #define MUTEX_LOCK_ID_15    LOCK_POS_MTX_17
72 #define MUTEX_LOCK_ID_16    LOCK_POS_MTX_18
73 #define MUTEX_LOCK_ID_17     LOCK_POS_MTX_19
74 #define MUTEX_LOCK_ID_18    LOCK_POS_MTX_20
75 #define MUTEX_LOCK_ID_19    LOCK_POS_MTX_21
76 #define MUTEX_LOCK_ID_20    LOCK_POS_MTX_22
77 #define MUTEX_LOCK_ID_21    LOCK_POS_MTX_23
78 #define MUTEX_LOCK_ID_22    LOCK_POS_MTX_24
79 #define MUTEX_LOCK_ID_23    LOCK_POS_MTX_25
80 #define MUTEX_LOCK_ID_24    LOCK_POS_MTX_26
81 #define MUTEX_LOCK_ID_25    LOCK_POS_MTX_27
82 #define MUTEX_LOCK_ID_26    LOCK_POS_MTX_28
83 #define MUTEX_LOCK_ID_27    LOCK_POS_MTX_29
84 #define MUTEX_LOCK_ID_28    LOCK_POS_MTX_30
85 #define MUTEX_LOCK_ID_29    LOCK_POS_MTX_31
86 #define MUTEX_LOCK_ID_30    LOCK_POS_MTX_32
87
88 typedef int32 LOCK_ID;
89
90 /*---------------------------------------------------------------------------------*
91  * Structure                                                                       *
92  *---------------------------------------------------------------------------------*/
93 /*!
94   @brief Mutex control information(Interprocess common-details)
95  */
96 typedef struct {
97     LOCK_ID      lock_id;            /**< Lock ID                                       */
98     TCHAR        name[NAME_MAX]; /**< Mutex Name                                       */
99     int32        ref_cnt;            /**< Reference Counter                               */
100     uint32_t    thread_id;       /**< Thread IDs while Lock is being acquired (Initial value:0) Note: For debugging */
101     BOOL        is_rel_fail;      /**< Release failure information (Failed:TRUE) Note: For debugging  */
102     BOOL        is_forbid_access; /**< Deletion occurred during Lock acquisition (Deletion occurred:TRUE) Note: For debugging  */
103 } MUTEX_CTRL_SHARED_DETAIL;
104
105 /*!
106   @brief Mutex control information (Interprocess common)
107  */
108 typedef struct {
109     MUTEX_CTRL_SHARED_DETAIL   detail[MAX_CTRL_MUTEX_NUM]; /**< Common Infomation  */
110     uint32_t    use_cnt;                                    /**< Use Counter        */
111     uint32_t    rsv_cnt;                                    /**< Reserve Counter    */
112 } MUTEX_CTRL_SHARED_INFO;
113
114 /*!
115    @brief Mutex control information
116 */
117 typedef struct {
118     HANDLE    handle[MAX_CTRL_MUTEX_NUM];    /**< Mutex Handle        */
119     int32    ref_cnt[MAX_CTRL_MUTEX_NUM];    /**< Reference Counter     */
120     MUTEX_CTRL_SHARED_INFO* info;        /**< Common Infomation    */
121 } MUTEX_CTRL_INFO;
122
123 /*---------------------------------------------------------------------------------*
124  * Grobal Value                                                                    *
125  *---------------------------------------------------------------------------------*/
126 /**
127     Mutex control table pointer (Partial shared memory)
128     Note: Access to this instance shall be made through the operation module.
129  */
130 static MUTEX_CTRL_INFO g_mutex_ctrl_tbl;   // NOLINT(readability/nolint)  global class instance
131
132 static HANDLE    g_h_mtx; /** Mutex control-information-locking Mutex handle */
133 static HANDLE    g_h_shm; /** Shared memory handle */   // Coverity CID: 18788 compliant
134
135
136 /** Lock ID Resource List */
137 static const LOCK_ID kLockIdList[MAX_CTRL_MUTEX_NUM] = {
138     MUTEX_LOCK_ID_1,
139     MUTEX_LOCK_ID_2,
140     MUTEX_LOCK_ID_3,
141     MUTEX_LOCK_ID_4,
142     MUTEX_LOCK_ID_5,
143     MUTEX_LOCK_ID_6,
144     MUTEX_LOCK_ID_7,
145     MUTEX_LOCK_ID_8,
146     MUTEX_LOCK_ID_9,
147     MUTEX_LOCK_ID_10,
148     MUTEX_LOCK_ID_11,
149     MUTEX_LOCK_ID_12,
150     MUTEX_LOCK_ID_13,
151     MUTEX_LOCK_ID_14,
152     MUTEX_LOCK_ID_15,
153     MUTEX_LOCK_ID_16,
154     MUTEX_LOCK_ID_17,
155     MUTEX_LOCK_ID_18,
156     MUTEX_LOCK_ID_19,
157     MUTEX_LOCK_ID_20,
158     MUTEX_LOCK_ID_21,
159     MUTEX_LOCK_ID_22,
160     MUTEX_LOCK_ID_23,
161     MUTEX_LOCK_ID_24,
162     MUTEX_LOCK_ID_25,
163     MUTEX_LOCK_ID_26,
164     MUTEX_LOCK_ID_27,
165     MUTEX_LOCK_ID_28,
166     MUTEX_LOCK_ID_29,
167     MUTEX_LOCK_ID_30,
168 };
169
170 /*
171    Mutex property information (Information for each process)
172    _pb_CreateMutex calls -> 1
173    DeleteMutex calls -> 0
174    Only "1" can be used in the process.
175  */
176 BOOL g_is_mutex_owner_tbl[MAX_CTRL_MUTEX_NUM];
177
178 /*---------------------------------------------------------------------------------*
179  * Local Function Prototype                                                        *
180  *---------------------------------------------------------------------------------*/
181 /* Mutex Control Table Manipulation Functions */
182 static void        MutexSetLockIdOfCtrlTbl(u_int32 idx, LOCK_ID l_id);         /* Set Lock ID     */
183 static LOCK_ID    MutexGetLockIdOfCtrlTbl(u_int32 idx);                        /* Get Lock ID     */
184 static void        MutexSetMutexNameOfCtrlTbl(u_int32 idx, LPCTSTR name);        /* Set Mutex name        */
185 static void        MutexSetMutexHandleOfCtrlTbl(u_int32 idx, HANDLE handle);     /* Set Mutex handle  */
186 static HANDLE    MutexGetMutexHandleOfCtrlTbl(u_int32 idx);                     /* Get Mutex Handle  */
187 static void     MutexSetTidOfCtrlTbl(uint32_t idx, uint32_t tid);          /* Set thread ID while Lock is being acquired     */
188 static uint32_t MutexGetTidOfCtrlTbl(uint32_t idx);                        /* Get thread ID while Lock is being acquired     */
189 static void     MutexSetIsRelFailOfCtrlTbl(uint32_t idx, BOOL flag);       /* Set release failure information        */
190 static void     MutexSetIsForbidAccessOfCtrlTbl(uint32_t idx, BOOL flag);  /* Set deletion occurrence while Lock is being acquired   */
191 static u_int32    MutexGetIdxOfCtrlTbl(HANDLE h_mutex);       /* Get index for access                   */
192 static void        MutexIncRefCntOfCtrlTbl(u_int32 idx);         /* Increment Mutex reference counter     */
193 static void        MutexDecRefCntOfCtrlTbl(u_int32 idx);         /* Decrement Mutex reference counter       */
194 static int32    MutexGetRefCntOfCtrlTbl(u_int32 idx);         /* Get Mutex reference counter            */
195 static int32    MutexGetRefCntLocalOfCtrlTbl(u_int32 idx);      /* Get mutex reference counter (in-process) */
196 static u_int32    MutexSearchNameOfCtrlTbl(LPCTSTR name);      /* Search mutex name       */
197 static u_int32    MutexSearchEmptyOfCtrlTbl(void);              /* Search unused area            */
198 static void     MutexIncUseCntOfCtrlInfo(void);             /* Increment Mutex using counter     */
199 static void     MutexDecUseCntOfCtrlInfo(void);             /* Decrement Mutex using counter     */
200 static void     MutexIncRsvCntOfCtrlInfo(void);             /* Increment Mutex reserved counter     */
201 static void     MutexDecRsvCntOfCtrlInfo(void);             /* Decrement Mutex reserved counter     */
202
203 /* Mutex ownership-information manipulation functions */
204 static void    MutexSetFlagOfOwnerTbl(u_int32 idx, BOOL flag); /* Set ownership-information */
205 static BOOL MutexGetFlagOfOwnerTbl(u_int32 idx);             /* Get ownership-information */
206
207 /* Mutex manipulation functions for accessing Mutex control tables */
208 static void MutexCreateMutex(void);    /* Create Mutex */
209 static void MutexLockMutex(void);        /* Get Mutex */
210 static void MutexUnlockMutex(void);    /* Release Mutex */
211 static void MutexDeleteMutex(void);    /* Delete Mutex */
212
213 /*---------------------------------------------------------------------------------*
214  * Function                                                                                        *
215  *---------------------------------------------------------------------------------*/
216 /**
217  * @brief
218  *   Initialize the mutex function
219  *
220  *   Call the in-process initialization API for locking between processes that provide CLS.<br>
221  *   Create shared memory for a Mutex control table.<br>
222  *   Create a mutex for accessing Mutex control info.<br>
223  *   If an error occurs during internal processing of this API, subsequent normal operations cannot be performed, so call _pb_Exit().
224  *
225  * @return    RET_NORMAL
226  */
227 RET_API MutexInit(void) {
228     RET_API ret_api = RET_NORMAL;
229     MUTEX_CTRL_SHARED_INFO **pp_tbl;
230     u_int32    idx;
231
232     memset(g_mutex_ctrl_tbl.handle, 0, sizeof(g_mutex_ctrl_tbl.handle));
233     memset(g_mutex_ctrl_tbl.ref_cnt, 0, sizeof(g_mutex_ctrl_tbl.ref_cnt));
234
235     /* Set pointer to Mutex control table */
236     pp_tbl = &(g_mutex_ctrl_tbl.info);
237
238     MutexCreateMutex(); /* Create Mutex for accessing Mutex control info */
239
240     MutexLockMutex(); /* Get Mutex for accessing Mutex control info */
241
242     /* Open the Mutex control table shared memory */
243     g_h_shm = OpenSharedMemory((TCHAR*)(MUTEX_CTRL_TBL_NAME),  // NOLINT(readability/casting)
244             sizeof(MUTEX_CTRL_SHARED_INFO));
245
246     /* If called for the first time within all processes, an error occurs and the following processing is performed. */
247     if (g_h_shm == NULL) {  // LCOV_EXCL_BR_LINE 200: can not be other val
248         /* Create shared memory for Mutex control table */
249         // LCOV_EXCL_BR_LINE 200: can not return NULL
250         g_h_shm = CreateSharedMemory((TCHAR*)MUTEX_CTRL_TBL_NAME,  // NOLINT(readability/casting)
251                                     sizeof(MUTEX_CTRL_SHARED_INFO));
252         if (g_h_shm == NULL) /* In case of an error */ {  // LCOV_EXCL_BR_LINE 200: can not return NULL
253             FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "_pb_CreateShareData ERROR [hShm:%p]", g_h_shm);
254             AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
255             _pb_Exit();  // LCOV_EXCL_LINE 200: cannot return NULL
256             /* don't arrive here. */
257         }
258     }
259
260     MutexUnlockMutex(); /* Release of Mutex for accessing Mutex control info */
261
262     /* Set addresses of the acquired shared memory as a pointer of the Mutex control table. */
263     *pp_tbl = reinterpret_cast<MUTEX_CTRL_SHARED_INFO*>(GetSharedMemoryPtr(g_h_shm));
264
265     /* Initialize various information of control table */
266     for (idx = 0; idx < MAX_CTRL_MUTEX_NUM; idx++) {
267         /* Set Lock ID in Mutex control table(Overwrite from the second process onwards) */
268         MutexSetLockIdOfCtrlTbl(idx, kLockIdList[idx]);
269
270         MutexSetFlagOfOwnerTbl(idx, FALSE);
271     }
272
273     return ret_api;
274 }
275
276 /**
277  * @brief
278  *   Terminate
279  */
280 RET_API MutexTerm(void) {  // LCOV_EXCL_START 8:dead code
281     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
282     RET_API ret_api = RET_NORMAL;
283
284     CloseSharedMemory(g_h_shm);
285
286     MutexDeleteMutex();
287
288     return ret_api;
289 }
290 // LCOV_EXCL_STOP
291
292 /**
293  * @brief
294  *   Create Mutex
295  *
296  *   Create a named mutex that can be locked between processes.
297  *
298  * @param[in]    lp_mutex_attributes    Security descriptor(Unused)
299  * @param[in]    b_initial_owner        Initial owner flag(Unused)
300  * @param[in]    lp_name                Mutex name
301  *
302  * @return Except NULL Handle of the created mutex (Management information pointer)<br>
303  *         NULL       ABEND
304  */
305 HANDLE _pb_CreateMutex(LPSECURITY_ATTRIBUTES lp_mutex_attributes,   // NOLINT(readability/nolint) WPF_SYSAPI.h API
306     BOOL b_initial_owner, LPCTSTR lp_name) {
307     int32    idx;            /* For accessing Mutex control table */
308     HANDLE   h_mutex = NULL; /* Mutex handle      */
309     void    *p_addr;
310     int32    errno;
311     LOCK_ID  lock_id;
312     size_t   len;
313
314     /* Null check */
315     if (lp_name == NULL) {
316         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Argument ERROR!!");
317     } else {
318         len = _tcslen(lp_name);
319         if (len >= NAME_MAX) {
320             FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Argument ERROR!! " \
321                 "Length of mutex name is too long(>=%d). [len:%zu]", NAME_MAX, len);
322         } else {
323             MutexLockMutex(); /* Get Mutex for accessing Mutex control info */
324
325             /* Retrieve whether the specified mutex name exists in the Mutex control table */
326             idx = MutexSearchNameOfCtrlTbl(lp_name);
327
328             /* Already assigned */
329             if (idx != MAX_CTRL_MUTEX_NUM) {
330                 /* Get mutex handle from Mutex control table */
331                 h_mutex = MutexGetMutexHandleOfCtrlTbl(idx);
332
333                 /* First-time creation within a process */
334                 if (h_mutex == NULL) {
335                     /* Get Lock ID */
336                     lock_id = MutexGetLockIdOfCtrlTbl(idx);
337
338                     /* Lock information mapping (CLS) */
339                     p_addr = CL_LockMap(lock_id);  // LCOV_EXCL_BR_LINE 4: nsfw error
340                     /* In case of an error */
341                     if (p_addr == MAP_FAILED) {  // LCOV_EXCL_BR_LINE 4: nsfw error
342                         /* Output an error log and return an error value */
343                         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, \
344                             "CL_LockMap ERROR!! [p_addr:%p, errno:%d]", p_addr, errno);
345                     } else {
346                         /* Successful allocation */
347                         /* Set Mutex handle in the mutex management table */
348                         MutexSetMutexHandleOfCtrlTbl(idx, (HANDLE)p_addr);
349
350                         h_mutex = (HANDLE)p_addr;
351                     }
352                 }
353
354                 if (h_mutex != NULL) {  // LCOV_EXCL_BR_LINE 200: h_mutex can not be NULL
355                     /* Increment Mutex reference counter */
356                     MutexIncRefCntOfCtrlTbl(idx);
357
358                     FRAMEWORKUNIFIEDLOG(ZONE_20, __FUNCTION__, 
359                                    "### MUTEX TABLE INFORMATION # (+) idx:%d HANDLE:%p, " \
360                                    "ref_cnt:%d, LOCK_ID:%d, NAME:%s, ref_cnt:%d, tid:%d, fail:%d, forbid:%d",
361                                    idx,
362                                    g_mutex_ctrl_tbl.handle[idx],
363                                    g_mutex_ctrl_tbl.ref_cnt[idx],
364                                    g_mutex_ctrl_tbl.info->detail[idx].lock_id,
365                                    g_mutex_ctrl_tbl.info->detail[idx].name,
366                                    g_mutex_ctrl_tbl.info->detail[idx].ref_cnt,
367                                    g_mutex_ctrl_tbl.info->detail[idx].thread_id,
368                                    g_mutex_ctrl_tbl.info->detail[idx].is_rel_fail,
369                                    g_mutex_ctrl_tbl.info->detail[idx].is_forbid_access);
370                 }
371             } else {
372                 /* For a new assignment */
373                 /* Get the free space in the mutex management table. */
374                 idx = MutexSearchEmptyOfCtrlTbl();
375
376                 /* Get Lock ID */
377                 lock_id = MutexGetLockIdOfCtrlTbl(idx);
378
379                 /* Lock information mapping (CLS) */
380                 p_addr = CL_LockMap(lock_id);
381
382                 /* In case of an error */
383                 if (p_addr == MAP_FAILED) {  // LCOV_EXCL_BR_LINE 4: nsfw error
384                     /* Output an error log and return an error value. */
385                     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "CL_LockMap ERROR!! " \
386                         "[p_addr:%p, errno:%d] In _pb_CreateMutex", p_addr, errno);
387                 } else {
388                     /* Successful allocation */
389                     /* Set Mutex Hanlde in the mutex management table */
390                     MutexSetMutexHandleOfCtrlTbl(idx, (HANDLE)p_addr);
391
392                     /* Set the mutex name in the mutex management table */
393                     MutexSetMutexNameOfCtrlTbl(idx, lp_name);
394
395                     /* Increment Mutex reference counter */
396                     MutexIncRefCntOfCtrlTbl(idx);
397
398                     /* Increment mutex using counter */
399                     MutexIncUseCntOfCtrlInfo();
400
401                     h_mutex = (HANDLE)p_addr;
402
403                     FRAMEWORKUNIFIEDLOG(ZONE_20, __FUNCTION__, 
404                                    "### MUTEX TABLE INFORMATION # (++) idx:%d HANDLE:%p, ref_cnt:%d, " \
405                                    "LOCK_ID:%d, NAME:%s, ref_cnt:%d, tid:%d, fail:%d, forbid:%d",
406                                    idx,
407                                    g_mutex_ctrl_tbl.handle[idx],
408                                    g_mutex_ctrl_tbl.ref_cnt[idx],
409                                    g_mutex_ctrl_tbl.info->detail[idx].lock_id,
410                                    g_mutex_ctrl_tbl.info->detail[idx].name,
411                                    g_mutex_ctrl_tbl.info->detail[idx].ref_cnt,
412                                    g_mutex_ctrl_tbl.info->detail[idx].thread_id,
413                                    g_mutex_ctrl_tbl.info->detail[idx].is_rel_fail,
414                                    g_mutex_ctrl_tbl.info->detail[idx].is_forbid_access);
415                 }
416             }
417
418             if (h_mutex != NULL) {  // LCOV_EXCL_BR_LINE 200: can not be NULL
419                 /* Update Mutex ownership */
420                 MutexSetFlagOfOwnerTbl(idx, TRUE);
421             }
422
423             /* Release the Mutex for accessing Mutex control info */
424             MutexUnlockMutex();
425         }
426     }
427
428     return h_mutex;
429 }
430
431 /**
432  * @brief
433  *   Delete the mutex
434  *
435  *   Decrement the Mutex reference counter and delete it when it reaches zero.
436  *
437  * @param[in]    h_mutex    Mutex handle (CreateMutex return value)
438  *
439  * @return WAIT_OBJECT_0    Normal completion<br>
440  *              WAIT_FAILED        ABEND
441  */
442 DWORD PbDeleteMutex(HANDLE h_mutex) {
443     DWORD lret = WAIT_OBJECT_0;
444     u_int32 idx;
445     int32    ref_cnt;
446     int32    ret;
447     BOOL    is_owner;
448     uint32_t tid;
449
450     /* Null check */
451     if (h_mutex == NULL) {  // LCOV_EXCL_BR_LINE 6: h_mutex cannot be NULL
452         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,  "Argment ERROR");
453         AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
454         lret = WAIT_FAILED; /* ABEND */  // LCOV_EXCL_LINE 6: h_mutex cannot be NULL
455     } else {
456         MutexLockMutex(); /* Get Mutex for accessing Mutex control info */
457
458         /* Retrieve the index of the management table containing the Mutex handles */
459         idx = MutexGetIdxOfCtrlTbl(h_mutex);
460         /* When the specified Mutex handle is not registered */
461         if (idx == MAX_CTRL_MUTEX_NUM) {  // LCOV_EXCL_BR_LINE 200: idx can no be MAX_CTRL_MUTEX_NUM
462             FRAMEWORKUNIFIEDLOG(ZONE_20, __FUNCTION__, "Argment ERROR [h_mutex:%p]", h_mutex);
463             AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
464             lret = WAIT_FAILED; /* ABEND */    // LCOV_EXCL_LINE 200: idx can no be MAX_CTRL_MUTEX_NUM
465         } else {
466             /* If it is registered */
467             /* Determinate its ownership */
468             is_owner = MutexGetFlagOfOwnerTbl(idx);
469             if (is_owner == TRUE) {
470                 /* Get Mutex reference counter */
471                 ref_cnt = MutexGetRefCntOfCtrlTbl(idx);
472
473                 /* No clients are using the specified Mutex. */
474                 if ((ref_cnt - 1) <= 0) {
475                     /* Lock information unmapping (CLS) */
476                     ret = CL_LockUnmap(reinterpret_cast<void*>(h_mutex));  // LCOV_EXCL_BR_LINE 4: nsfw error
477                     /* When an error occurs */
478                     if (ret == -1) {  // LCOV_EXCL_BR_LINE 4: nsfw error
479                         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "CL_LockUnmap ERROR");
480                         AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
481                         lret = WAIT_FAILED; /* ABEND */  // LCOV_EXCL_LINE 4: nsfw error
482                     } else {
483                         /* If successful */
484                         /* Remove Mutex hanlde from Mutex control table */
485                         MutexSetMutexHandleOfCtrlTbl(idx, NULL);
486
487                         /* Delete the mutex name from the Mutex control table */
488                         MutexSetMutexNameOfCtrlTbl(idx, "");
489
490                         /* Update Mutex ownership */
491                         MutexSetFlagOfOwnerTbl(idx, FALSE);
492
493                         /* Decrement Mutex reference counter */
494                         MutexDecRefCntOfCtrlTbl(idx);
495
496                         /* Decrement mutex using counter */
497                         MutexDecUseCntOfCtrlInfo();
498
499                         tid = MutexGetTidOfCtrlTbl(idx);
500                         if (tid != 0) {  // LCOV_EXCL_BR_LINE 200: tid can not be 0
501                             AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
502                             MutexSetIsForbidAccessOfCtrlTbl(idx, TRUE);  // LCOV_EXCL_LINE 200: tid can not be 0
503                         }
504
505                         /* Initialize debug information */
506                         MutexSetTidOfCtrlTbl(idx, 0);
507                         MutexSetIsRelFailOfCtrlTbl(idx, FALSE);
508
509                         FRAMEWORKUNIFIEDLOG(ZONE_20, __FUNCTION__, 
510                                        "### MUTEX TABLE INFORMATION # (--) idx:%d HANDLE:%p, ref_cnt(local):%d, " \
511                                        "LOCK_ID:%d, NAME:%s, ref_cnt:%d, tid:%d, fail:%d, forbid:%d",
512                                        idx,
513                                        g_mutex_ctrl_tbl.handle[idx],
514                                        g_mutex_ctrl_tbl.ref_cnt[idx],
515                                        g_mutex_ctrl_tbl.info->detail[idx].lock_id,
516                                        g_mutex_ctrl_tbl.info->detail[idx].name,
517                                        g_mutex_ctrl_tbl.info->detail[idx].ref_cnt,
518                                        g_mutex_ctrl_tbl.info->detail[idx].thread_id,
519                                        g_mutex_ctrl_tbl.info->detail[idx].is_rel_fail,
520                                        g_mutex_ctrl_tbl.info->detail[idx].is_forbid_access);
521                     }
522                 } else {
523                     /* If exists */
524                     /* Determine if any clients are referencing in the process */
525                     ref_cnt = MutexGetRefCntLocalOfCtrlTbl(idx);
526                     if ((ref_cnt - 1) <= 0) {  // LCOV_EXCL_BR_LINE 200: ref_cnt can not bigger than 1
527                         // LCOV_EXCL_START 200: ref_cnt can not bigger than 1
528                         //AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
529                         /* Lock information unmapping (CLS) */
530                         ret = CL_LockUnmap(reinterpret_cast<void*>(h_mutex));
531                         /* When an error occurs */
532                         if (ret == -1) {
533                             FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "CL_LockUnmap ERROR");
534                             lret = WAIT_FAILED; /* ABEND */
535                         } else {
536                             /* If successful */
537                             /* Remove Mutex Hanlde from the Mutex control table */
538                             MutexSetMutexHandleOfCtrlTbl(idx, NULL);
539
540                             /* Update Mutex ownership */
541                             MutexSetFlagOfOwnerTbl(idx, FALSE);
542                         }
543                         // LCOV_EXCL_STOP
544                     }
545
546                     if (lret == WAIT_OBJECT_0) {
547                         /* Decrement Mutex refernce counter */
548                         MutexDecRefCntOfCtrlTbl(idx);
549
550                         FRAMEWORKUNIFIEDLOG(ZONE_20, __FUNCTION__, 
551                                        "### MUTEX TABLE INFORMATION # (-) idx:%d HANDLE:%p, ref_cnt(local):%d, " \
552                                        "LOCK_ID:%d, NAME:%s, ref_cnt:%d, tid:%d, fail:%d, forbid:%d",
553                                        idx,
554                                        g_mutex_ctrl_tbl.handle[idx],
555                                        g_mutex_ctrl_tbl.ref_cnt[idx],
556                                        g_mutex_ctrl_tbl.info->detail[idx].lock_id,
557                                        g_mutex_ctrl_tbl.info->detail[idx].name,
558                                        g_mutex_ctrl_tbl.info->detail[idx].ref_cnt,
559                                        g_mutex_ctrl_tbl.info->detail[idx].thread_id,
560                                        g_mutex_ctrl_tbl.info->detail[idx].is_rel_fail,
561                                        g_mutex_ctrl_tbl.info->detail[idx].is_forbid_access);
562                     }
563                 }
564
565             } else {  // LCOV_EXCL_BR_LINE 200: is_owner can not be FALSE
566                 /* If it is not the owner */
567                 AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
568                 lret = WAIT_FAILED;  // LCOV_EXCL_LINE 200: is_owner can not be FALSE
569             }
570         }
571
572         MutexUnlockMutex(); /* Release of Mutex for accessing Mutex control info */
573     }
574
575     return lret;
576 }
577
578 /**
579  * @brief
580  *   Mutex Lock
581  *
582  *   Take ownership of the mutex and start locking.
583  *
584  * @param[in]    h_mutex    Mutex handle(CreateMutex return value)
585  * @param[in]    timeout    Timeout (Millisecond)
586  *
587  * @return WAIT_OBJECT_0    Succeeded to get ownership<br>
588  *              WAIT_TIMEOUT    Failed to get ownership (Timeout)<br>
589  *              WAIT_FAILED        Failed to get ownership (Error)
590  */
591 DWORD PbMutexLock(HANDLE h_mutex, DWORD timeout) {
592     DWORD    lret = WAIT_OBJECT_0;
593     int32    ret;
594     DWORD    time_out_cnt = 0;
595     u_int32    idx;
596     BOOL    is_owner;
597     uint32_t tid;
598
599     /* Null check */
600     if (h_mutex == NULL) {  // LCOV_EXCL_BR_LINE 6: h_mutex cannot be NULL
601         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Argment ERROR");
602         AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
603         lret = WAIT_FAILED; /* ABEND */  // LCOV_EXCL_LINE 6: h_mutex cannot be NULL
604     } else {
605         /* Retrieve the index of the management table containing the Mutex handles */
606         idx = MutexGetIdxOfCtrlTbl(h_mutex);
607         /* When the specified Mutex handle is not registered */
608         if (idx == MAX_CTRL_MUTEX_NUM) {  // LCOV_EXCL_BR_LINE 6: idx cannot be MAX_CTRL_MUTEX_NUM
609             FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Argment ERROR [h_mutex:%p, idx:%d]", \
610                     h_mutex, idx);
611             AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
612             lret = WAIT_FAILED; /* ABEND */  // LCOV_EXCL_LINE 6: idx cannot be MAX_CTRL_MUTEX_NUM
613         } else {
614             /* If it is registered */
615             /* Determinate ownership */
616             is_owner = MutexGetFlagOfOwnerTbl(idx);
617             if (is_owner == TRUE) {
618                 tid = PbGetTid();
619
620                 /* No timeout specified */
621                 if (timeout == INFINITE) {  // LCOV_EXCL_BR_LINE 6: timeout will not be other case
622                     /* Get Lock (CLS) */
623                     ret = CL_LockGet(reinterpret_cast<void*>(h_mutex));  // LCOV_EXCL_BR_LINE 4: nsfw error
624                     if (ret != 0) /* In case of ABEND */ {  // LCOV_EXCL_BR_LINE 4: nsfw error
625                         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, \
626                             "CL_LockGet ERROR [h_mutex:%p, ret:%d]", h_mutex, ret);
627                         AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
628                         lret = WAIT_FAILED; /* ABEND */  // LCOV_EXCL_LINE 4: nsfw error
629                     } else {
630                         /* Set thread ID during lock */
631                         MutexSetTidOfCtrlTbl(idx, tid);
632                     }
633                 } else {
634                     // LCOV_EXCL_START 6: timeout will not be other case
635                     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
636                     /* Timeout specified */
637                     while (1) {
638                         /* Get Lock (CLS) */
639                         ret = CL_LockNowait(reinterpret_cast<void*>(h_mutex));
640                         if (ret != 0) /* In case of ABEND */ {
641                             /* Invalid parameter */
642                             if (ret != EBUSY) {
643                                 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, \
644                                     "CL_LockGet ERROR [h_mutex:%p, ret:%d]", h_mutex, ret);
645                                 lret = WAIT_FAILED; /* ABEND */
646                                 break;
647                             }
648
649                             time_out_cnt++;
650                             if (time_out_cnt <= timeout) {
651                                 usleep(1000);
652                             } else {
653                                 /* Timeout error */
654                                 lret = WAIT_TIMEOUT;
655                                 break;
656                             }
657                         } else {
658                             /* Successful acquisition */
659                             /* Set thread ID during lock */
660                             MutexSetTidOfCtrlTbl(idx, tid);
661
662                             break;
663                         }
664                     }
665                     // LCOV_EXCL_STOP
666                 }
667             } else {  // LCOV_EXCL_BR_LINE 200: is_owner can not be FALSE
668                 /* If it is not the owner */
669                 AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
670                 lret = WAIT_FAILED;  // LCOV_EXCL_LINE 200: is_owner can not be FALSE
671             }
672         }
673     }
674
675     return lret;
676 }
677
678 /**
679  * @brief
680  *   Mutex Unlock
681  *
682  *   Release the mutex ownership and terminate the lock.
683  *
684  * @param[in]    h_mutex    Mutex handle(CreateMutex return value)
685  *
686  * @return TURE    Normal<br>
687  *              FALSE    Error
688  */
689 BOOL PbMutexUnlock(HANDLE h_mutex) {
690     BOOL     bret = FALSE;
691     int32    ret;
692     u_int32  idx;
693     BOOL    is_owner;
694     uint32_t tid;
695
696     /* Null check */
697     if (h_mutex == NULL) {  // LCOV_EXCL_BR_LINE 6: h_mutex can not be NULL
698         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Argment ERROR");
699     } else {
700         /* Retrieve the index of the management table containing the Mutex handle */
701         idx = MutexGetIdxOfCtrlTbl(h_mutex);
702         /* When the specified Mutex handle is not registered */
703         if (idx == MAX_CTRL_MUTEX_NUM) {  // LCOV_EXCL_BR_LINE 200: idx can not be MAX_CTRL_MUTEX_NUM
704             FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, \
705                 "MutexGetIdxOfCtrlTbl ERROR [h_mutex:%p, idx:%d]", h_mutex, idx);
706         } else {
707             /* If it is registered */
708             /* Determinate ownership */
709             is_owner = MutexGetFlagOfOwnerTbl(idx);
710             if (is_owner == TRUE) {  // LCOV_EXCL_BR_LINE 4: nsfw error
711                 /* Release thread ID setting during lock */
712                 MutexSetTidOfCtrlTbl(idx, 0);
713
714                 /* Release the Lock */
715                 ret = CL_LockRelease(reinterpret_cast<void*>(h_mutex));
716                 if (ret == 0) /* If successful */ {  // LCOV_EXCL_BR_LINE 4: nsfw error
717                     bret = TRUE;
718
719                 } else {  // LCOV_EXCL_BR_LINE 4: nsfw error
720                     // LCOV_EXCL_START 4: nsfw error
721                     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
722                     /* Failed */
723                     tid = PbGetTid();
724
725                     /* Retry to set the thread ID during lock */
726                     MutexSetTidOfCtrlTbl(idx, tid);
727
728                     /* Add the release failure information */
729                     MutexSetIsRelFailOfCtrlTbl(idx, TRUE);
730
731                     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, \
732                         "CL_LockRelease ERROR [h_mutex:%p, ret:%d]", h_mutex, ret);
733                     // LCOV_EXCL_STOP
734                 }
735             }
736         }
737     }
738
739     return bret;
740 }
741
742 /*---------------------------------------------------------------------------------*
743  * Local Function                                                                  *
744  *---------------------------------------------------------------------------------*/
745 /**
746  * @brief
747  *   Set the Lock ID (Mutex control table)
748  *
749  *   If an invalid value is specified for an argument, the system assumes that it is a design problem and calls _pb_Exit().
750  *
751  * @param[in]    idx    Control table accessor
752  * @param[in]    l_id    Lock ID
753  */
754 static void MutexSetLockIdOfCtrlTbl(u_int32 idx, LOCK_ID l_id) {
755     /* check index */
756     if (idx >= MAX_CTRL_MUTEX_NUM) {  // LCOV_EXCL_BR_LINE 6: idx cannot greater
757         /* forbidden */
758         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Argment ERROR [idx:%d, l_id:%d]", idx, l_id);
759         AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
760         _pb_Exit();  // LCOV_EXCL_LINE 6: idx cannot greater
761         /* don't arrive here. */
762     } else {
763         g_mutex_ctrl_tbl.info->detail[idx].lock_id = l_id;
764     }
765
766     return;
767 }
768
769 /**
770  * @brief
771  *   Get the Lock ID (Mutex control table)
772  *
773  *   If an invalid value is specified for an argument, the system assumes that it is a design problem and calls _pb_Exit().
774  *
775  * @param[in]    idx    Control table accessor
776  */
777 static LOCK_ID MutexGetLockIdOfCtrlTbl(u_int32 idx) {
778     /* check index */
779     if (idx >= MAX_CTRL_MUTEX_NUM) {  // LCOV_EXCL_BR_LINE 6: idx cannot greater
780         /* forbidden */
781         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Argment ERROR [idx:%d]", idx);
782         AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
783         _pb_Exit();  // LCOV_EXCL_LINE 6: idx cannot greater
784         /* don't arrive here. */
785     }
786
787     return g_mutex_ctrl_tbl.info->detail[idx].lock_id;
788 }
789
790 /**
791  * @brief
792  *   Set the Mutex name (Mutex control table)
793  *
794  *   If an invalid value is specified for an argument, the system assumes that it is a design problem and calls _pb_Exit().
795  *
796  * @param[in]    idx        Control table accessor
797  * @param[in]    name    Mutex name
798  */
799 static void MutexSetMutexNameOfCtrlTbl(u_int32 idx, LPCTSTR name) {
800     /* check index */
801     if (idx >= MAX_CTRL_MUTEX_NUM) {  // LCOV_EXCL_BR_LINE 6: idx cannot greater
802         /* forbidden */
803         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Argment ERROR [idx:%d, name:%s]", idx, name);
804         AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
805         _pb_Exit();  // LCOV_EXCL_LINE 6: idx cannot greater
806         /* don't arrive here. */
807     } else {
808         _tcscpy(g_mutex_ctrl_tbl.info->detail[idx].name, name);
809     }
810
811     return;
812 }
813
814 /**
815  * @brief
816  *   Set the Mutex handle (Mutex control table)
817  *
818  *   If an invalid value is specified for an argument, the system assumes that it is a design problem and calls _pb_Exit().
819  *
820  * @param[in]    idx        Control table accessor
821  * @param[in]    handle    Mutex handle
822  */
823 static void MutexSetMutexHandleOfCtrlTbl(u_int32 idx, HANDLE handle) {
824     /* check index */
825     if (idx >= MAX_CTRL_MUTEX_NUM) {  // LCOV_EXCL_BR_LINE 6: idx cannot greater
826         /* forbidden */
827         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Argment ERROR [idx:%d, handle:%p]", idx, handle);
828         AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
829         _pb_Exit();  // LCOV_EXCL_LINE 6: idx cannot greater
830         /* don't arrive here. */
831     } else {
832         g_mutex_ctrl_tbl.handle[idx] = handle;
833     }
834
835     return;
836 }
837
838 /**
839  * @brief
840  *   Get the Mutex handle (Mutex control table)
841  *
842  *   If an invalid value is specified for an argument, the system assumes that it is a design problem and calls _pb_Exit().
843  *
844  * @param[in]    idx    Control table accessor
845  *
846  * @return Mutex handle
847  */
848 static HANDLE MutexGetMutexHandleOfCtrlTbl(u_int32 idx) {
849     HANDLE handle = NULL;
850
851     /* check index */
852     if (idx >= MAX_CTRL_MUTEX_NUM) {  // LCOV_EXCL_BR_LINE 6: idx cannot greater
853         /* forbidden */
854         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Argment ERROR [idx:%d]", idx);
855         AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
856         _pb_Exit();  // LCOV_EXCL_LINE 6: idx cannot greater
857         /* don't arrive here. */
858     } else {
859         handle = g_mutex_ctrl_tbl.handle[idx];
860     }
861
862     return handle;
863 }
864
865 /**
866  * @brief
867  *   Set the thread ID during lock acquisition (Mutex control table)
868  *
869  * @param[in]    idx    Control table accessor
870  * @param[in]    tid    Thread ID
871  * @note   If an invalid value is specified for an argument, the system assumes that it is a design problem and calls _pb_Exit().
872  */
873 static void MutexSetTidOfCtrlTbl(uint32_t idx, uint32_t tid) {
874     /* check index */
875     if (idx >= MAX_CTRL_MUTEX_NUM) {  // LCOV_EXCL_BR_LINE 6: idx cannot greater
876         /* forbidden */
877         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Argument ERROR [idx:%d]", idx);
878         AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
879         _pb_Exit();  // LCOV_EXCL_LINE 6: idx cannot greater
880         /* don't arrive here. */
881     } else {
882         g_mutex_ctrl_tbl.info->detail[idx].thread_id = tid;
883     }
884
885     return;
886 }
887
888
889 /**
890  * @brief
891  *   Get the thread ID during lock acquisition (Mutex control table)
892  *
893  * @param[in]    idx    Control table accessor
894  *
895  * @return Thread ID
896  *
897  * @note   If an invalid value is specified for an argument, the system assumes that it is a design problem and calls _pb_Exit().
898  */
899 static uint32_t MutexGetTidOfCtrlTbl(uint32_t idx) {
900     uint32_t tid = 0;
901
902     /* check index */
903     if (idx >= MAX_CTRL_MUTEX_NUM) {  // LCOV_EXCL_BR_LINE 6: idx cannot greater
904         /* forbidden */
905         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Argument ERROR [idx:%d]", idx);
906         AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
907         _pb_Exit();  // LCOV_EXCL_LINE 6: idx cannot greater
908         /* don't arrive here. */
909     } else {
910         tid = g_mutex_ctrl_tbl.info->detail[idx].thread_id;
911     }
912
913     return tid;
914 }
915
916
917 /**
918  * @brief
919  *   Set the release failure information (Mutex control table)
920  *
921  * @param[in]    idx     Control table accessor
922  * @param[in]   flag   Failed to release:TURE
923  * @note   If an invalid value is specified for an argument, the system assumes that it is a design problem and calls _pb_Exit().
924  */
925 static void MutexSetIsRelFailOfCtrlTbl(uint32_t idx, BOOL flag) {
926     /* check index */
927     if (idx >= MAX_CTRL_MUTEX_NUM) {  // LCOV_EXCL_BR_LINE 6: idx cannot greater
928         /* forbidden */
929         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Argument ERROR [idx:%d]", idx);
930         AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
931         _pb_Exit();  // LCOV_EXCL_LINE 6: idx cannot greater
932         /* don't arrive here. */
933     } else {
934         g_mutex_ctrl_tbl.info->detail[idx].is_rel_fail = flag;
935     }
936
937     return;
938 }
939
940 /**
941  * @brief
942  *   Set deletion-occurrence information during Lock acquisition (Mutex control table)
943  *
944  * @param[in]    idx     Control table accessor
945  * @param[in]   flag  When deletion occurs while Lock is being acquired:TRUE
946  * @note   If an invalid value is specified for an argument, the system assumes that it is a design problem and calls _pb_Exit().
947  */
948 static void MutexSetIsForbidAccessOfCtrlTbl(uint32_t idx, BOOL flag) {  // LCOV_EXCL_START 8:dead code
949     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
950     /* check index */
951     if (idx >= MAX_CTRL_MUTEX_NUM) {  // LCOV_EXCL_BR_LINE 6: idx cannot greater
952         /* forbidden */
953         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Argument ERROR [idx:%d]", idx);
954         AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
955         _pb_Exit();  // LCOV_EXCL_LINE 6: idx cannot greater
956         /* don't arrive here. */
957     } else {
958         g_mutex_ctrl_tbl.info->detail[idx].is_forbid_access = flag;
959     }
960
961     return;
962 }
963 // LCOV_EXCL_STOP
964
965 /**
966  * @brief
967  *   Get index for access (Mutex control table)
968  *
969  *   Return the index for accessing the area of the Mutex control table in which <br>
970  *   the specified mutex handle is registered. If the specified handle is not <br>
971  *   registered, the maximum number of mutex management (MAX_MUTEX_CTRL_NUM) is returned.
972  *
973  * @param[in]    h_mutex Mutex handle
974  *
975  * @return Index for access (Specified handle is already registered)<br>
976  *              Maximum mutex management value (Specified handle is not registered)
977  */
978 static u_int32 MutexGetIdxOfCtrlTbl(HANDLE h_mutex) {
979     u_int32 idx;
980     HANDLE handle;
981
982     for (idx = 0; idx < MAX_CTRL_MUTEX_NUM; idx++) {
983         /* Get handle from Mutex control table */
984         handle = MutexGetMutexHandleOfCtrlTbl(idx);
985
986         if (handle == h_mutex) {
987             break;
988         }
989     }
990
991     return idx;
992 }
993
994 /**
995  * @brief
996  *   Increment the Mutex reference counter (Mutex control table)
997  *
998  *   If an invalid value is specified for an argument, the system assumes that it is a design problem and calls _pb_Exit().
999  *
1000  * @param[in]    idx Control table accessor
1001  */
1002 static void MutexIncRefCntOfCtrlTbl(u_int32 idx) {
1003     /* check index */
1004     if (idx >= MAX_CTRL_MUTEX_NUM) {  // LCOV_EXCL_BR_LINE 6: idx cannot greater
1005         /* forbidden */
1006         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Argment ERROR [idx:%d]", idx);
1007         AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1008         _pb_Exit();  // LCOV_EXCL_LINE 6: idx cannot greater
1009         /* don't arrive here. */
1010     }
1011
1012     g_mutex_ctrl_tbl.info->detail[idx].ref_cnt++;
1013     g_mutex_ctrl_tbl.ref_cnt[idx]++;
1014
1015     return;
1016 }
1017
1018 /**
1019  * @brief
1020  *   Decrement the Mutex reference counter (Mutex control table)
1021  *
1022  *   If an invalid value is specified for an argument, the system assumes that it is a design problem and calls _pb_Exit().
1023  *
1024  * @param[in]    idx Control table accessor
1025  */
1026 static void MutexDecRefCntOfCtrlTbl(u_int32 idx) {
1027     /* check index */
1028     if (idx >= MAX_CTRL_MUTEX_NUM) {  // LCOV_EXCL_BR_LINE 6: idx cannot greater
1029         /* forbidden */
1030         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Argment ERROR [idx:%d]", idx);
1031         AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1032         _pb_Exit();  // LCOV_EXCL_LINE 6: idx cannot greater
1033         /* don't arrive here. */
1034     }
1035
1036     g_mutex_ctrl_tbl.info->detail[idx].ref_cnt--;
1037     g_mutex_ctrl_tbl.ref_cnt[idx]--;
1038
1039     return;
1040 }
1041
1042 /**
1043  * @brief
1044  *   Get the Mutex Reference Counter (Mutex control table)
1045  *
1046  *   If an invalid value is specified for an argument, the system assumes that it is a design problem and calls _pb_Exit().
1047  *
1048  * @param[in]    idx Control table accessor
1049  *
1050  * @return Reference counter value
1051  */
1052 static int32 MutexGetRefCntOfCtrlTbl(u_int32 idx) {
1053     int32 ret = 0;
1054
1055     /* check index */
1056     if (idx >= MAX_CTRL_MUTEX_NUM) {  // LCOV_EXCL_BR_LINE 6: idx cannot greater
1057         /* forbidden */
1058         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Argment ERROR [idx:%d]", idx);
1059         AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1060         _pb_Exit();  // LCOV_EXCL_LINE 6: idx cannot greater
1061         /* don't arrive here. */
1062     } else {
1063         ret = g_mutex_ctrl_tbl.info->detail[idx].ref_cnt;
1064     }
1065
1066     return ret;
1067 }
1068
1069 /**
1070  * @brief
1071  *   Get mutex reference counter (in-process)(Mutex control table)
1072  *
1073  *   If an invalid value is specified for an argument, the system assumes that it is a design problem and calls _pb_Exit().
1074  *
1075  * @param[in]    idx Control table accessor
1076  *
1077  * @return Reference counter value
1078  */
1079 static int32 MutexGetRefCntLocalOfCtrlTbl(u_int32 idx) {
1080     /* check index */
1081     if (idx >= MAX_CTRL_MUTEX_NUM) {  // LCOV_EXCL_BR_LINE 6: idx cannot greater
1082         /* forbidden */
1083         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Argment ERROR [idx:%d]", idx);
1084         AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1085         _pb_Exit();  // LCOV_EXCL_LINE 6: idx cannot greater
1086         /* don't arrive here. */
1087     }
1088
1089     return g_mutex_ctrl_tbl.ref_cnt[idx];
1090 }
1091
1092 /**
1093  * @brief
1094  *   Search mutex name (Mutex control table)
1095  *
1096  *   Retrieve whether the specified mutex is already registered in the Mutex control table.<br>
1097  *   If it is registered, the access index is returned.If it is not registered,<br>
1098  *   return the maximum mutex management value(MAX_CTRL_MUTEX_NUM).
1099  *
1100  * @param[in]    name Mutex name
1101  *
1102  * @return Index for access(If it is registered)<br>
1103  *              Maximum mutex management value (Not registered)
1104  */
1105 static u_int32 MutexSearchNameOfCtrlTbl(LPCTSTR name) {
1106     int32    ret;
1107     u_int32 idx;
1108
1109     for (idx = 0; idx < MAX_CTRL_MUTEX_NUM; idx++) {
1110         ret = _tcscmp(g_mutex_ctrl_tbl.info->detail[idx].name, name);
1111
1112         /* If there is a match */
1113         if (ret == 0) {
1114             break;
1115         }
1116     }
1117
1118     return idx;
1119 }
1120
1121 /**
1122  * @brief
1123  *   Search unused area (Mutex control table)
1124  *
1125  *   Return the lowest-numbered index for accessing unused space in the Mutex control table.<br>
1126  *   Return the maximum value((MAX_CTRL_MUTEX_NUM)) of message queue management <br>
1127  *   when no unused area exists. 
1128  *
1129  * @return Index for access (Unused area exists)<br>
1130  *              Maximum mutex management value (No unused area)
1131  */
1132 static u_int32 MutexSearchEmptyOfCtrlTbl(void) {
1133     u_int32    idx;
1134
1135     idx = MutexSearchNameOfCtrlTbl("");
1136
1137     return idx;
1138 }
1139
1140 /**
1141  * @brief
1142  *   Set the Mutex ownership flag (Ownership-information table)
1143  *
1144  * @param[in]    idx    Control table accessor
1145  * @param[in]    flag ownership-information
1146  */
1147 static void MutexSetFlagOfOwnerTbl(u_int32 idx, BOOL flag) {
1148     /* check index */
1149     if (idx >= MAX_CTRL_MUTEX_NUM) {  // LCOV_EXCL_BR_LINE 6: idx cannot greater
1150         /* forbidden */
1151         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Argment ERROR [idx:%d, flag:%d]", idx, flag);
1152         AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1153         _pb_Exit();  // LCOV_EXCL_LINE 6: idx cannot greater
1154         /* don't arrive here. */
1155     } else {
1156         g_is_mutex_owner_tbl[idx] = flag;
1157     }
1158
1159     return;
1160 }
1161
1162 /**
1163  * @brief
1164  *   Get the Mutex ownership flag (Ownership-information table)
1165  *
1166  * @param[in]    idx    Control table accessor
1167  *
1168  * @return    TRUE    Owned
1169  *            FALSE    Not owned
1170  */
1171 static BOOL MutexGetFlagOfOwnerTbl(u_int32 idx) {
1172     BOOL    bret = FALSE;
1173
1174     /* check index */
1175     if (idx >= MAX_CTRL_MUTEX_NUM) {  // LCOV_EXCL_BR_LINE 6: idx cannot greater
1176         /* forbidden */
1177         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Argment ERROR [idx:%d]", idx);
1178         AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1179         _pb_Exit();  // LCOV_EXCL_LINE 6: idx cannot greater
1180         /* don't arrive here. */
1181     } else {
1182         bret = g_is_mutex_owner_tbl[idx];
1183     }
1184
1185     return bret;
1186 }
1187
1188 /**
1189  * @brief
1190  *   Create Mutex for accessing the Mutex control table
1191  */
1192 static void MutexCreateMutex(void) {
1193     g_h_mtx = (HANDLE)CL_LockMap(MUTEX_LOCK_ID_0);  // LCOV_EXCL_BR_LINE 4: nsfw error
1194     if (g_h_mtx == NULL) {    // LCOV_EXCL_BR_LINE 4: nsfw error
1195         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "CL_LockMap ERROR [g_h_mtx:%p]", g_h_mtx);
1196         AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1197         _pb_Exit();  // LCOV_EXCL_LINE 4: nsfw error
1198         /* don't arrive here. */
1199     }
1200
1201     return;
1202 }
1203
1204 /**
1205  * @brief
1206  *   Delete Mutex for accessing Mutex control table
1207  */
1208 static void MutexDeleteMutex(void) {  // LCOV_EXCL_START 8:dead code
1209     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1210     int32 ret;
1211
1212     ret = CL_LockUnmap(g_h_mtx);
1213     if (ret != 0) {
1214         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, \
1215             "CL_LockUnmap ERROR [g_h_mtx:%p, ret:%d]", g_h_mtx, ret);
1216         _pb_Exit();
1217         /* don't arrive here. */
1218     }
1219
1220     return;
1221 }
1222 // LCOV_EXCL_STOP
1223
1224 /**
1225  * @brief
1226  *   Get Mutex for accessing Mutex control table
1227  */
1228 static void MutexLockMutex(void) {
1229     int32 ret;
1230
1231     ret = CL_LockGet(g_h_mtx);  // LCOV_EXCL_BR_LINE 4: nsfw error
1232     if (ret != 0) {  // LCOV_EXCL_BR_LINE 4: nsfw error
1233         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, \
1234             "CL_LockGet ERROR [g_h_mtx:%p, ret:%d]", g_h_mtx, ret);
1235         AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1236         _pb_Exit();  // LCOV_EXCL_LINE 4: nsfw error
1237         /* don't arrive here. */
1238     }
1239
1240     return;
1241 }
1242
1243 /**
1244  * @brief
1245  *   Release Mutex for accessing Mutex control table
1246  */
1247 static void MutexUnlockMutex(void) {
1248     int32 ret;
1249
1250     ret = CL_LockRelease(g_h_mtx);  // LCOV_EXCL_BR_LINE 4: nsfw error
1251     if (ret != 0) {  // LCOV_EXCL_BR_LINE 4: nsfw error
1252         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, \
1253             "CL_LockRelease ERROR [g_h_mtx:%p, ret:%d]", g_h_mtx, ret);
1254         AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1255         _pb_Exit();  // LCOV_EXCL_LINE 4: nsfw error
1256         /* don't arrive here. */
1257     }
1258
1259     return;
1260 }
1261
1262 /**
1263  * @brief
1264  *   Get dump information
1265  *
1266  * @param[out]    p_buf      Dump info
1267  * @param[in/out] p_len      Buffer size
1268  */
1269 void _pb_GetDebugMutexMngTbl(void* p_buf, uint8_t* p_len) {
1270     static uint8_t buf[DEBUG_DUMP_MAX_SIZE];
1271     static uint8_t buf_tmp[512];
1272     static uint8_t buf_info[DEBUG_DUMP_MAX_SIZE];
1273     uint32_t        i;
1274     uint8_t         cnt = 0;
1275
1276     if ((p_buf != NULL) && (p_len != NULL)) {
1277         memset(&buf[0], 0x00, sizeof(buf));
1278         memset(&buf_info[0], 0x00, sizeof(buf_info));
1279         for (i = 0; i < MAX_CTRL_MUTEX_NUM; i++) {
1280             memset(&buf_tmp[0], 0x00, sizeof(buf_tmp));
1281             snprintf(reinterpret_cast<char *>(&buf_tmp[0]), sizeof(buf_tmp),
1282                     "\n  [%02d]id:%10d, nm:%40s, ref_cnt:%10d, tid:%03d, fail:%01d, " \
1283                     "forbid:%01d, handle:%10p, ref_cnt(local):%10d",
1284                     i,
1285                     g_mutex_ctrl_tbl.info->detail[i].lock_id,
1286                     g_mutex_ctrl_tbl.info->detail[i].name,
1287                     g_mutex_ctrl_tbl.info->detail[i].ref_cnt,
1288                     g_mutex_ctrl_tbl.info->detail[i].thread_id,
1289                     g_mutex_ctrl_tbl.info->detail[i].is_rel_fail,
1290                     g_mutex_ctrl_tbl.info->detail[i].is_forbid_access,
1291                     g_mutex_ctrl_tbl.handle[i],
1292                     g_mutex_ctrl_tbl.ref_cnt[i]);
1293             strncat(reinterpret_cast<char *>(&buf_info[0]), reinterpret_cast<char *>(&buf_tmp[0]), \
1294                 strlen(reinterpret_cast<char *>(&buf_tmp[0])));
1295             if (((i + 1) % 10) == 0) {
1296                 cnt++;
1297                 memset(&buf[0], 0x00, sizeof(buf));
1298                 snprintf(reinterpret_cast<char *>(&buf[0]), sizeof(buf),
1299                         "Mutex-%d%s",
1300                         cnt,
1301                         &buf_info[0]);
1302                 memcpy(p_buf, &buf[0], sizeof(buf));
1303                 p_buf = reinterpret_cast<void *>((reinterpret_cast<uint8_t *>(p_buf)) + sizeof(buf));
1304                 memset(&buf_info[0], 0x00, sizeof(buf_info));
1305                 if (cnt >= *p_len) {
1306                     break;
1307                 }
1308             }
1309         }
1310         if (cnt < *p_len) {
1311             if (buf_info[0] != 0x00) {
1312                 cnt++;
1313                 memset(&buf[0], 0x00, sizeof(buf));
1314                 snprintf(reinterpret_cast<char *>(&buf[0]), sizeof(buf),
1315                         "Mutex-%d%s",
1316                         cnt,
1317                         &buf_info[0]);
1318                 memcpy(p_buf, &buf[0], sizeof(buf));
1319             }
1320             *p_len = cnt;
1321         }
1322     }
1323 }
1324
1325 /**
1326  * @brief
1327  *   Increment the Mutex using counter
1328  *
1329  * @param[in] none
1330  */
1331 static void MutexIncUseCntOfCtrlInfo(void) {
1332     g_mutex_ctrl_tbl.info->use_cnt++;
1333     return;
1334 }
1335
1336 /**
1337  * @brief
1338  *   Decrement Mutex using counter
1339  *
1340  * @param[in] none
1341  */
1342 static void MutexDecUseCntOfCtrlInfo(void) {
1343     g_mutex_ctrl_tbl.info->use_cnt--;
1344     return;
1345 }
1346
1347 /**
1348  * @brief
1349  *   Increment Mutex reserved counter
1350  *
1351  * @param[in] none
1352  */
1353 static void MutexIncRsvCntOfCtrlInfo(void) {
1354     g_mutex_ctrl_tbl.info->rsv_cnt++;
1355     return;
1356 }
1357
1358 /**
1359  * @brief
1360  *   Decrement Mutex reserved counter
1361  *
1362  * @param[in] none
1363  */
1364 static void MutexDecRsvCntOfCtrlInfo(void) {
1365     g_mutex_ctrl_tbl.info->rsv_cnt--;
1366     return;
1367 }
1368
1369 /**
1370  * @brief
1371  *   Determine resources ready (Mutex control data)
1372  *
1373  * @param[in]  none
1374  *
1375  * @return BOOL
1376  * @retval TRUE  : Normal
1377  * @retval FALSE : Error (Resource shortage)
1378  */
1379 BOOL _pb_GetMutexResource(void) {
1380     BOOL ret = TRUE;
1381     uint32_t cnt;
1382
1383     MutexLockMutex();
1384
1385     /* Increment Mutex reserved counter */
1386     MutexIncRsvCntOfCtrlInfo();
1387
1388     cnt = g_mutex_ctrl_tbl.info->use_cnt + g_mutex_ctrl_tbl.info->rsv_cnt;
1389     if (cnt >= FULL_CTRL_MUTEX_NUM) {
1390         ret = FALSE;
1391         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, \
1392             "Lack of resources [FATAL][use_cnt:%d rsv_cnt:%d]", g_mutex_ctrl_tbl.info->use_cnt, \
1393             g_mutex_ctrl_tbl.info->rsv_cnt);
1394     } else if (cnt >= WARN_CTRL_MUTEX_NUM) {
1395         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, \
1396             "Lack of resources [WARN][use_cnt:%d rsv_cnt:%d]", g_mutex_ctrl_tbl.info->use_cnt, \
1397             g_mutex_ctrl_tbl.info->rsv_cnt);
1398     }
1399
1400     MutexUnlockMutex();
1401
1402     return ret;
1403 }
1404
1405 /**
1406  * @brief
1407  *   Release resources (Mutex control data)
1408  *
1409  * @param[in]  none
1410  *
1411  * @return none
1412  */
1413 void _pb_ReleaseMutexResource(void) {
1414     MutexLockMutex();
1415
1416     /* Decrement Mutex reserved counter */
1417     MutexDecRsvCntOfCtrlInfo();
1418
1419     MutexUnlockMutex();
1420
1421     return;
1422 }
1423 #endif /* PT_PB_MUTEX_STUB__CWORD71_  */