• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *   Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  * Description: mutex
15  *
16  * Create: 2021-12-16
17  */
18 
19 #include <los_mux.h>
20 #include <los_mux_pri.h>
21 #include "soc_osal.h"
22 #include "osal_errno.h"
23 #include "osal_inner.h"
24 
osal_mutex_init(osal_mutex * mutex)25 int osal_mutex_init(osal_mutex *mutex)
26 {
27     if (mutex == NULL) {
28         osal_log("parameter invalid!\n");
29         return OSAL_FAILURE;
30     }
31     if (GET_MUX_INDEX((unsigned int)(UINTPTR)mutex->mutex) >= (unsigned int)KERNEL_MUX_LIMIT) {
32         osal_log("mutex id invalid!\n");
33         return OSAL_FAILURE;
34     }
35     LosMuxCB *mutex_handle = GET_MUX((unsigned int)(UINTPTR)mutex->mutex);
36     if (mutex_handle->muxStat == LOS_USED && ((unsigned int)(UINTPTR)mutex->mutex != 0)) {
37         osal_log("The mutex has been initialized. mutex id:%d.\n", (unsigned int)(UINTPTR)mutex->mutex);
38         return OSAL_FAILURE;
39     }
40 
41     unsigned int ret = LOS_MuxCreate((unsigned int *)&(mutex->mutex));
42     if (ret != LOS_OK) {
43         osal_log("LOS_MuxCreate failed! ret = %#x address:%#x.\n", ret, (uint32_t)__builtin_return_address(0));
44         return OSAL_FAILURE;
45     }
46     return OSAL_SUCCESS;
47 }
48 
osal_mutex_destroy(osal_mutex * mutex)49 void osal_mutex_destroy(osal_mutex *mutex)
50 {
51     if (mutex == NULL) {
52         osal_log("parameter invalid!\n");
53         return;
54     }
55 
56     unsigned int ret = LOS_MuxDelete((unsigned int)(UINTPTR)mutex->mutex);
57     if (ret != LOS_OK) {
58         osal_log("LOS_MuxDelete failed! ret = %#x address:%#x.\n", ret, (uint32_t)__builtin_return_address(0));
59     }
60     mutex->mutex = NULL;
61 }
62 
osal_mutex_lock(osal_mutex * mutex)63 int osal_mutex_lock(osal_mutex *mutex)
64 {
65     if (mutex == NULL) {
66         osal_log("parameter invalid!\n");
67         return OSAL_FAILURE;
68     }
69     unsigned int ret = LOS_MuxPend((unsigned int)(UINTPTR)mutex->mutex, LOS_WAIT_FOREVER);
70     if (ret != LOS_OK) {
71         osal_log("LOS_MuxPend failed! ret = %#x address:%#x.\n", ret, (uint32_t)__builtin_return_address(0));
72         return OSAL_FAILURE;
73     }
74 
75     return OSAL_SUCCESS;
76 }
77 
osal_mutex_lock_timeout(osal_mutex * mutex,unsigned int timeout)78 int osal_mutex_lock_timeout(osal_mutex *mutex, unsigned int timeout)
79 {
80     if (mutex == NULL) {
81         osal_log("parameter invalid!\n");
82         return OSAL_FAILURE;
83     }
84 
85     unsigned int tick = (timeout == OSAL_MUTEX_WAIT_FOREVER) ? LOS_WAIT_FOREVER : osal_msecs_to_jiffies(timeout);
86 
87     unsigned int ret = LOS_MuxPend((unsigned int)(UINTPTR)mutex->mutex, tick);
88     if (ret != LOS_OK) {
89         osal_log("LOS_MuxPend failed! ret = %#x address:%#x.\n", ret, (uint32_t)__builtin_return_address(0));
90         return OSAL_FAILURE;
91     }
92 
93     return OSAL_SUCCESS;
94 }
95 
osal_mutex_lock_interruptible(osal_mutex * mutex)96 int osal_mutex_lock_interruptible(osal_mutex *mutex)
97 {
98     if (mutex == NULL) {
99         osal_log("parameter invalid!\n");
100         return OSAL_FAILURE;
101     }
102 
103     unsigned int ret = LOS_MuxPend((unsigned int)(UINTPTR)mutex->mutex, LOS_WAIT_FOREVER);
104     if (ret == LOS_ERRNO_MUX_PEND_INTERR) {
105         osal_log("The mutex is being locked during an interrupt!\n");
106         return OSAL_EINTR;
107     } else if (ret != LOS_OK) {
108         osal_log("LOS_MuxPend failed! ret = %#x address:%#x.\n", ret, (uint32_t)__builtin_return_address(0));
109         return OSAL_FAILURE;
110     }
111 
112     return OSAL_SUCCESS;
113 }
114 
osal_mutex_trylock(osal_mutex * mutex)115 int osal_mutex_trylock(osal_mutex *mutex)
116 {
117     if (mutex == NULL) {
118         osal_log("parameter invalid!\n");
119         return FALSE;
120     }
121 
122     unsigned int ret = LOS_MuxPend((unsigned int)(UINTPTR)mutex->mutex, LOS_NO_WAIT);
123     if (ret != LOS_OK) {
124         return FALSE;
125     }
126 
127     return TRUE;
128 }
129 
osal_mutex_unlock(osal_mutex * mutex)130 void osal_mutex_unlock(osal_mutex *mutex)
131 {
132     if (mutex == NULL) {
133         osal_log("parameter invalid!\n");
134         return;
135     }
136 
137     unsigned int ret = LOS_MuxPost((unsigned int)(UINTPTR)mutex->mutex);
138     if (ret != LOS_OK) {
139         osal_log("LOS_MuxPost failed! ret = %#x address:%#x..\n", ret, (uint32_t)__builtin_return_address(0));
140     }
141 }
142 
osal_mutex_is_locked(osal_mutex * mutex)143 int osal_mutex_is_locked(osal_mutex *mutex)
144 {
145     osal_unused(mutex);
146     return -1;
147 }
148