• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "hdf_log.h"
33 #include "los_mux.h"
34 
35 #define HDF_LOG_TAG osal_mutex
36 
37 #define HDF_INVALID_MUX_ID UINT32_MAX
38 
OsalMutexInit(struct OsalMutex * mutex)39 int32_t OsalMutexInit(struct OsalMutex *mutex)
40 {
41     uint32_t ret;
42     uint32_t muxId = 0;
43 
44     if (mutex == NULL) {
45         HDF_LOGE("%s invalid param", __func__);
46         return HDF_ERR_INVALID_PARAM;
47     }
48 
49     ret = LOS_MuxCreate(&muxId);
50     if (ret == LOS_OK) {
51         mutex->realMutex = (void *)(uintptr_t)muxId;
52     } else {
53         mutex->realMutex = (void *)(uintptr_t)HDF_INVALID_MUX_ID;
54         HDF_LOGE("%s create fail %u %d", __func__, ret, __LINE__);
55         return HDF_FAILURE;
56     }
57 
58     return HDF_SUCCESS;
59 }
60 
OsalMutexDestroy(struct OsalMutex * mutex)61 int32_t OsalMutexDestroy(struct OsalMutex *mutex)
62 {
63     uint32_t ret;
64 
65     if (mutex == NULL || mutex->realMutex == (void *)(uintptr_t)HDF_INVALID_MUX_ID) {
66         HDF_LOGE("%s invalid param", __func__);
67         return HDF_ERR_INVALID_PARAM;
68     }
69 
70     ret = LOS_MuxDelete((uint32_t)(uintptr_t)mutex->realMutex);
71     if (ret != LOS_OK) {
72         HDF_LOGE("%s fail %u %d", __func__, ret, __LINE__);
73         return HDF_FAILURE;
74     }
75 
76     mutex->realMutex = (void *)(uintptr_t)HDF_INVALID_MUX_ID;
77     return HDF_SUCCESS;
78 }
79 
OsalMutexLock(struct OsalMutex * mutex)80 int32_t OsalMutexLock(struct OsalMutex *mutex)
81 {
82     uint32_t ret;
83 
84     if (mutex == NULL || mutex->realMutex == (void *)(uintptr_t)HDF_INVALID_MUX_ID) {
85         HDF_LOGE("%s invalid param", __func__);
86         return HDF_ERR_INVALID_PARAM;
87     }
88 
89     ret = LOS_MuxPend((uint32_t)(uintptr_t)mutex->realMutex, LOS_MS2Tick(HDF_WAIT_FOREVER));
90     if (ret != LOS_OK) {
91         HDF_LOGE("%s fail %u %d", __func__, ret, __LINE__);
92         return HDF_FAILURE;
93     }
94 
95     return HDF_SUCCESS;
96 }
97 
OsalMutexTimedLock(struct OsalMutex * mutex,uint32_t ms)98 int32_t OsalMutexTimedLock(struct OsalMutex *mutex, uint32_t ms)
99 {
100     uint32_t ret;
101 
102     if (mutex == NULL || mutex->realMutex == (void *)(uintptr_t)HDF_INVALID_MUX_ID) {
103         HDF_LOGE("%s invalid param", __func__);
104         return HDF_ERR_INVALID_PARAM;
105     }
106 
107     ret = LOS_MuxPend((uint32_t)(uintptr_t)mutex->realMutex, LOS_MS2Tick(ms));
108     if (ret != LOS_OK) {
109         if (ret == LOS_ERRNO_MUX_TIMEOUT) {
110             return HDF_ERR_TIMEOUT;
111         }
112         HDF_LOGE("%s fail %u %d", __func__, ret, __LINE__);
113         return HDF_FAILURE;
114     }
115 
116     return HDF_SUCCESS;
117 }
118 
OsalMutexUnlock(struct OsalMutex * mutex)119 int32_t OsalMutexUnlock(struct OsalMutex *mutex)
120 {
121     uint32_t ret;
122 
123     if (mutex == NULL || mutex->realMutex == (void *)(uintptr_t)HDF_INVALID_MUX_ID) {
124         HDF_LOGE("%s invalid param", __func__);
125         return HDF_ERR_INVALID_PARAM;
126     }
127 
128     ret = LOS_MuxPost((uint32_t)(uintptr_t)mutex->realMutex);
129     if (ret != LOS_OK) {
130         HDF_LOGE("%s fail %u %d", __func__, ret, __LINE__);
131         return HDF_FAILURE;
132     }
133 
134     return HDF_SUCCESS;
135 }
136