1 /*
2  * Copyright (c) 2001-2019, Arm Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 
8 
9 /************* Include Files ****************/
10 #include <time.h>
11 #include "cc_pal_types.h"
12 #include "cc_pal_mutex.h"
13 #include "cc_pal_log.h"
14 #include <stdio.h>
15 
16 /************************ Defines ******************************/
17 
18 /************************ Enums ******************************/
19 
20 /************************ Typedefs ******************************/
21 
22 /************************ Global Data ******************************/
23 
24 /************************ Private Functions ******************************/
25 
26 /************************ Public Functions ******************************/
27 
28 /**
29  * @brief This function purpose is to create a mutex.
30  *
31  *
32  * @param[out] pMutexId - Pointer to created mutex handle
33  *
34  * @return returns 0 on success, otherwise indicates failure
35  */
CC_PalMutexCreate(CC_PalMutex * pMutexId)36 CCError_t CC_PalMutexCreate(CC_PalMutex *pMutexId)
37 {
38     int  rc = CC_SUCCESS;
39 
40     rc = pthread_mutex_init(pMutexId, NULL);
41     if (rc != 0) {
42         printf /* CC_PAL_LOG_ERR */("pthread_mutex_init failed 0x%x", rc);
43         return CC_FAIL;
44     }
45     return CC_SUCCESS;
46 }
47 
48 
49 /**
50  * @brief This function purpose is to destroy a mutex
51  *
52  *
53  * @param[in] pMutexId - pointer to Mutex handle
54  *
55  * @return returns 0 on success, otherwise indicates failure
56  */
CC_PalMutexDestroy(CC_PalMutex * pMutexId)57 CCError_t CC_PalMutexDestroy(CC_PalMutex *pMutexId)
58 {
59     int  rc = CC_SUCCESS;
60 
61     rc = pthread_mutex_destroy(pMutexId);
62     if (rc != 0) {
63         printf /* CC_PAL_LOG_ERR */("pthread_mutex_destroy failed 0x%x", rc);
64         return CC_FAIL;
65     }
66     return CC_SUCCESS;
67 }
68 
69 
70 /**
71  * @brief This function purpose is to Wait for Mutex with aTimeOut. aTimeOut is
72  *        specified in milliseconds. (CC_INFINITE is blocking)
73  *
74  *
75  * @param[in] pMutexId - pointer to Mutex handle
76  * @param[in] timeOut - timeout in mSec, or CC_INFINITE
77  *
78  * @return returns 0 on success, otherwise indicates failure
79  */
CC_PalMutexLock(CC_PalMutex * pMutexId,uint32_t timeOut)80 CCError_t CC_PalMutexLock (CC_PalMutex *pMutexId, uint32_t timeOut)
81 {
82     int  rc = CC_SUCCESS;
83 
84     timeOut = timeOut; // to remove compilation warnings
85 
86     rc = pthread_mutex_lock(pMutexId);
87     if (rc != 0) {
88         printf /* CC_PAL_LOG_ERR */("pthread_mutex_lock failed 0x%x", rc);
89         return CC_FAIL;
90     }
91     return CC_SUCCESS;
92 }
93 
94 
95 
96 /**
97  * @brief This function purpose is to release the mutex.
98  *
99  *
100  * @param[in] pMutexId - pointer to Mutex handle
101  *
102  * @return returns 0 on success, otherwise indicates failure
103  */
CC_PalMutexUnlock(CC_PalMutex * pMutexId)104 CCError_t CC_PalMutexUnlock (CC_PalMutex *pMutexId)
105 {
106     int  rc = CC_SUCCESS;
107 
108     rc = pthread_mutex_unlock(pMutexId);
109     if (rc != 0) {
110         printf /* CC_PAL_LOG_ERR */("pthread_mutex_unlock failed 0x%x", rc);
111         return CC_FAIL;
112     }
113     return CC_SUCCESS;
114 }
115