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: OS Abstract Layer. 15 */ 16 17 /** 18 * @defgroup osal_atomic osal_atomic 19 */ 20 #ifndef __OSAL_ATOMIC_H__ 21 #define __OSAL_ATOMIC_H__ 22 23 #ifdef __cplusplus 24 #if __cplusplus 25 extern "C" { 26 #endif 27 #endif 28 29 typedef struct { 30 volatile int counter; 31 } osal_atomic; 32 33 /** 34 * @ingroup osal_atomic 35 * @brief read atomic variable 36 * 37 * @par Description: 38 * This API is used to implement the atomic read and return the value read from the input parameter atomic. 39 * 40 * @par Support System: 41 * linux liteos freertos. 42 */ 43 int osal_atomic_read(osal_atomic *atomic); 44 45 /** 46 * @ingroup osal_atomic 47 * @brief set atomic variable 48 * 49 * @par Description: 50 * This API is used to implement the atomic setting operation. 51 * 52 * @par Support System: 53 * linux liteos freertos. 54 */ 55 void osal_atomic_set(osal_atomic *atomic, int i); 56 57 /** 58 * @ingroup osal_atomic 59 * @brief increment atomic variable and return 60 * 61 * @par Description: 62 * This API is used to implement the atomic self-addition and return the self-addition result. 63 * 64 * @par Support System: 65 * linux liteos freertos. 66 */ 67 int osal_atomic_inc_return(osal_atomic *atomic); 68 69 /** 70 * @ingroup osal_atomic 71 * @brief Adds the given value to the atomic variable and returns the increased result. 72 * 73 * @par Description: 74 * Adds the given value to the atomic variable and returns the increased result. 75 * 76 * @param atomic [in] The atomic to be change. 77 * @param count [in] Number of atomic variables to be add. 78 * 79 * @par Support System: 80 * freertos. 81 */ 82 int osal_atomic_add_return(osal_atomic *atomic, int count); 83 84 /** 85 * @ingroup osal_atomic 86 * @brief decrement atomic variable and return 87 * 88 * @par Description: 89 * This API is used to implement the atomic self-decrement and return the self-decrement result. 90 * 91 * @par Support System: 92 * linux liteos freertos. 93 */ 94 int osal_atomic_dec_return(osal_atomic *atomic); 95 96 /** 97 * @ingroup osal_atomic 98 * @brief increment atomic variable 99 * 100 * @par Description: 101 * This API is used to implement the atomic self-addition. 102 * 103 * @par Support System: 104 * linux liteos freertos. 105 */ 106 void osal_atomic_inc(osal_atomic *atomic); 107 108 /** 109 * @ingroup osal_atomic 110 * @brief Reduce a fixed number of atomic variables. 111 * 112 * @par Description: 113 * Reduce a fixed number of atomic variables 114 * 115 * @param atomic [in] The atomic to be change. 116 * @param count [in] Number of atomic variables to be reduced. 117 * 118 * @par Support System: 119 * freertos. 120 */ 121 void osal_atomic_sub(osal_atomic *atomic, unsigned int count); 122 123 /** 124 * @ingroup osal_atomic 125 * @brief decrement atomic variable 126 * 127 * @par Description: 128 * This API is used to implement the atomic self-decrement. 129 * 130 * @par Support System: 131 * linux liteos freertos. 132 */ 133 void osal_atomic_dec(osal_atomic *atomic); 134 135 /** 136 * @ingroup osal_atomic 137 * @brief Add a fixed number of atomic variables. 138 * 139 * @par Description: 140 * Add a fixed number of atomic variables 141 * 142 * @param atomic [in] The atomic to be change. 143 * @param count [in] Number of atomic variables to be add. 144 * 145 * @par Support System: 146 * freertos. 147 */ 148 void osal_atomic_add(osal_atomic *atomic, int count); 149 150 /** 151 * @ingroup osal_atomic 152 * @brief decrement atomic variable and test 153 * 154 * @par Description: 155 * This API is used to subtract 1 atomically from the variable atomic of the atomic type and 156 * checks whether the result is 0. If yes, true is returned. Otherwise, false is returned. 157 * 158 * @par Support System: 159 * linux. 160 */ 161 int osal_atomic_dec_and_test(osal_atomic *atomic); 162 163 /** 164 * @ingroup osal_atomic 165 * @brief increment atomic variable and test 166 * 167 * @par Description: 168 * This API is used to add 1 atomically from the variable atomic of the atomic type and checks whether the result is 0. 169 * If yes, true is returned. Otherwise, false is returned. 170 * 171 * @par Support System: 172 * linux. 173 */ 174 int osal_atomic_inc_and_test(osal_atomic *atomic); 175 176 /** 177 * @ingroup osal_atomic 178 * @brief increment unless the number is zero 179 * 180 * @par Description: 181 * Atomically increments atomic by 1, if atomic is non-zero. 182 * 183 * @return Returns true if the increment was done. 184 * 185 * @par Support System: 186 * linux. 187 */ 188 int osal_atomic_inc_not_zero(osal_atomic *atomic); 189 190 #ifdef __cplusplus 191 #if __cplusplus 192 } 193 #endif 194 #endif 195 #endif /* __OSAL_ATOMIC_H__ */ 196