1 /*
2 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without specific prior written
16 * permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "osal_mutex.h"
32 #include "los_mux.h"
33 #include "los_sys.h"
34 #include "hdf_log.h"
35 #include "osal_mem.h"
36
37 #define HDF_LOG_TAG osal_mutex
38
OsalMutexInit(struct OsalMutex * mutex)39 int32_t OsalMutexInit(struct OsalMutex *mutex)
40 {
41 uint32_t ret;
42 LosMux *mux = NULL;
43
44 if (mutex == NULL) {
45 HDF_LOGE("%s invalid param", __func__);
46 return HDF_ERR_INVALID_PARAM;
47 }
48
49 mux = (LosMux *)OsalMemCalloc(sizeof(LosMux));
50 if (mux == NULL) {
51 HDF_LOGE("%s malloc fail", __func__);
52 mutex->realMutex = NULL;
53 return HDF_ERR_MALLOC_FAIL;
54 }
55 ret = LOS_MuxInit(mux, NULL);
56 if (ret == LOS_OK) {
57 mutex->realMutex = (void *)mux;
58 } else {
59 mutex->realMutex = NULL;
60 OsalMemFree(mux);
61 HDF_LOGE("%s create fail %d %d", __func__, ret, __LINE__);
62 return HDF_FAILURE;
63 }
64
65 return HDF_SUCCESS;
66 }
67
OsalMutexDestroy(struct OsalMutex * mutex)68 int32_t OsalMutexDestroy(struct OsalMutex *mutex)
69 {
70 uint32_t ret;
71
72 if (mutex == NULL || mutex->realMutex == NULL) {
73 HDF_LOGE("%s invalid param", __func__);
74 return HDF_ERR_INVALID_PARAM;
75 }
76
77 ret = LOS_MuxDestroy((LosMux *)mutex->realMutex);
78 if (ret != LOS_OK) {
79 HDF_LOGE("%s fail %d %d", __func__, ret, __LINE__);
80 return HDF_FAILURE;
81 }
82
83 OsalMemFree(mutex->realMutex);
84 mutex->realMutex = NULL;
85 return HDF_SUCCESS;
86 }
87
OsalMutexLock(struct OsalMutex * mutex)88 int32_t OsalMutexLock(struct OsalMutex *mutex)
89 {
90 uint32_t ret;
91
92 if (mutex == NULL || mutex->realMutex == NULL) {
93 HDF_LOGE("%s invalid param", __func__);
94 return HDF_ERR_INVALID_PARAM;
95 }
96
97 ret = LOS_MuxLock((LosMux *)mutex->realMutex, LOS_MS2Tick(HDF_WAIT_FOREVER));
98 if (ret != LOS_OK) {
99 HDF_LOGE("%s fail %d %d", __func__, ret, __LINE__);
100 return HDF_FAILURE;
101 }
102
103 return HDF_SUCCESS;
104 }
105
OsalMutexTimedLock(struct OsalMutex * mutex,uint32_t ms)106 int32_t OsalMutexTimedLock(struct OsalMutex *mutex, uint32_t ms)
107 {
108 uint32_t ret;
109
110 if (mutex == NULL || mutex->realMutex == NULL) {
111 HDF_LOGE("%s invalid param", __func__);
112 return HDF_ERR_INVALID_PARAM;
113 }
114
115 ret = LOS_MuxLock((LosMux *)mutex->realMutex, LOS_MS2Tick(ms));
116 if (ret != LOS_OK) {
117 if (ret == LOS_ETIMEDOUT) {
118 return HDF_ERR_TIMEOUT;
119 }
120 HDF_LOGE("%s fail %d %d", __func__, ret, __LINE__);
121 return HDF_FAILURE;
122 }
123
124 return HDF_SUCCESS;
125 }
126
OsalMutexUnlock(struct OsalMutex * mutex)127 int32_t OsalMutexUnlock(struct OsalMutex *mutex)
128 {
129 uint32_t ret;
130
131 if (mutex == NULL || mutex->realMutex == NULL) {
132 HDF_LOGE("%s invalid param", __func__);
133 return HDF_ERR_INVALID_PARAM;
134 }
135
136 ret = LOS_MuxUnlock((LosMux *)mutex->realMutex);
137 if (ret != LOS_OK) {
138 HDF_LOGE("%s fail %d %d", __func__, ret, __LINE__);
139 return HDF_FAILURE;
140 }
141
142 return HDF_SUCCESS;
143 }
144
145