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 */ 15 /** 16 * @defgroup semaphore Semaphore 17 * @ingroup linux 18 */ 19 20 #ifndef _ASM_SEMAPHORE_H 21 #define _ASM_SEMAPHORE_H 22 23 #ifdef __cplusplus 24 extern "C" { 25 #endif /* __cplusplus */ 26 27 #define UNINIT_VALUE 0xFFFFFFFF 28 #define ERESTARTSYS 512 /* The return vlaue of down_interruptiable in Linux */ 29 30 struct semaphore { 31 unsigned int sem; 32 int count; 33 }; 34 35 typedef struct semaphore losMutexDef_t; 36 extern int _sema_lock(losMutexDef_t *sem); 37 extern int _sema_unlock(losMutexDef_t *sem); 38 extern int _sema_init(losMutexDef_t *sem, unsigned int value); 39 extern int _sema_destory(losMutexDef_t *sem); 40 extern int _sema_trylock(losMutexDef_t *sem); 41 42 /* The counting semaphore is initialized for use by the mutex lock. linux 43 * standard adaptation macro function, do not modify. */ 44 #define init_MUTEX(sem) (void)_sema_init((losMutexDef_t*)(sem), 1) 45 #define init_MUTEX_LOCKED(sem) (void)_sema_init((losMutexDef_t*)(sem), 0) 46 #define destory_MUTEX(sem) _sema_destory((losMutexDef_t *)(sem)) 47 #define DECLARE_MUTEX(x) struct semaphore x = { UNINIT_VALUE, 1 } 48 #define DECLARE_MUTEX_LOCKED(x) struct semaphore x = { UNINIT_VALUE, 0 } 49 50 /** 51 * @ingroup semaphore 52 * @brief Defines and initializes a semaphore. 53 * 54 * @par Description: 55 * This API is used to define a semaphore named #x and initialize it. 56 * 57 * @attention 58 * None. 59 * 60 * @param x Name of the semaphore. 61 * @retval #None 62 * @par Dependency: 63 * <ul> 64 * <li>semaphore.h: the header file that contains the API declaration.</li> 65 * </ul> 66 * @see sema_init. 67 */ 68 #define DEFINE_SEMAPHORE(x) DECLARE_MUTEX(x) 69 70 /** 71 * @ingroup semaphore 72 * @brief Init the semaphore. 73 * 74 * @par Description: 75 * This API is used to init the semaphore. 76 * 77 * @attention 78 * <ul> 79 * <li> Semaphore dynamic initialization interface, ONLY used as a binary semaphore. 80 * </li> 81 * </ul> 82 * 83 * @param sem [IN] the semaphore to be acquired. 84 * @param n [IN] initial value. 85 * @retval #None 86 * @par Dependency: 87 * <ul> 88 * <li>semaphore.h: the header file that contains the API declaration.</li> 89 * </ul> 90 * @see DEFINE_SEMAPHORE. 91 */ 92 #define sema_init(sem, n) (VOID)_sema_init((losMutexDef_t*)(sem), (n)) 93 94 /** 95 * @ingroup semaphore 96 * @brief acquire the semaphore. 97 * 98 * @par Description: 99 * This API is used to acquire the semaphore. If no more tasks are allowed to 100 * acquire the semaphore, calling this function will put the task to sleep until 101 * the semaphore is released. 102 * 103 * @attention 104 * <ul> 105 * <li>Blocking interface, disallowed in interrupt isr handler. </li> 106 * </ul> 107 * 108 * @param sem [IN] the semaphore to be acquired. 109 * 110 * @retval #None 111 * @par Dependency: 112 * <ul> 113 * <li>semaphore.h: the header file that contains the API declaration.</li> 114 * </ul> 115 * @see up. 116 */ 117 #define down(sem) (void)_sema_lock((losMutexDef_t*)(sem)) 118 119 /** 120 * @ingroup semaphore 121 * @brief acquire the semaphore. 122 * 123 * @par Description: 124 * This API is used to acquire the semaphore. If no more tasks are allowed to 125 * acquire the semaphore, calling this function will put the task to sleep until 126 * the semaphore is released. 127 * 128 * @attention 129 * <ul> 130 * <li> This interface is the same function as the down interface, 131 * inconsistent with standard linux behavior. </li> 132 * </ul> 133 * 134 * @param sem [IN] the semaphore to be acquired. 135 * 136 * @retval # 0 the semaphore has been acquired successfully. 137 * @retval # -1 it cannot be acquired. 138 * @par Dependency: 139 * <ul> 140 * <li>semaphore.h: the header file that contains the API declaration.</li> 141 * </ul> 142 * @see down. 143 */ 144 #define down_interruptible(sem) _sema_lock((losMutexDef_t*)(sem)) 145 146 /** 147 * @ingroup semaphore 148 * @brief try to acquire the semaphore, without waiting. 149 * 150 * @par Description: 151 * This API is used to try to acquire the semaphore, without waiting. 152 * 153 * @attention 154 * <ul> 155 * <li> The return value that cannot be obtained by the semaphore is 156 * inconsistent with the Linux standard interface definition(linux standard return 1). </li> 157 * </ul> 158 * 159 * @param sem [IN] the semaphore to be acquired. 160 * 161 * @retval # 0 the semaphore has been acquired successfully. 162 * @retval # 1 it cannot be acquired. 163 * @par Dependency: 164 * <ul> 165 * <li>semaphore.h: the header file that contains the API declaration.</li> 166 * </ul> 167 * @see down. 168 */ 169 extern int down_trylock(struct semaphore *sem); 170 171 /** 172 * @ingroup semaphore 173 * @brief release the semaphore. 174 * 175 * @par Description: 176 * This API is used to release the semaphore. 177 * 178 * @attention 179 * <ul> 180 * <li> None. </li> 181 * </ul> 182 * 183 * @param sem [IN] the semaphore to be acquired. 184 * 185 * @retval # None. 186 * @par Dependency: 187 * <ul> 188 * <li>semaphore.h: the header file that contains the API declaration.</li> 189 * </ul> 190 * @see down. 191 */ 192 #define up(sem) (void)_sema_unlock((losMutexDef_t*)(sem)) 193 194 /** 195 * @ingroup semaphore 196 * @brief destroy the semaphore at the address pointed to by sem. 197 * 198 * @par Description: 199 * This API is used to destroy the semaphore. 200 * 201 * @attention 202 * <ul> 203 * <li> None. </li> 204 * </ul> 205 * 206 * @param sem [IN] the semaphore to be acquired. 207 * 208 * @retval -1 The semaphore fails to be destoryed. 209 * @retval 0 The semaphore is successfully destoryed. 210 * @par Dependency: 211 * <ul> 212 * <li>semaphore.h: the header file that contains the API declaration.</li> 213 * </ul> 214 * @see None. 215 */ 216 #define sema_destory(sem) _sema_destory((losMutexDef_t *)(sem)) 217 218 #ifdef __cplusplus 219 } 220 #endif /* __cplusplus */ 221 222 #endif /* _ASM_SEMAPHORE_H */ 223