1 /* 2 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. 3 * 4 * HDF is dual licensed: you can use it either under the terms of 5 * the GPL, or the BSD license, at your option. 6 * See the LICENSE file in the root of this repository for complete details. 7 */ 8 9 /** 10 * @addtogroup OSAL 11 * @{ 12 * 13 * @brief Defines the structures and interfaces for the Operating System Abstraction Layer (OSAL) module. 14 * 15 * The OSAL module OpenHarmony OS interface differences and provides unified OS interfaces externally, 16 * including the memory management, thread, mutex, spinlock, semaphore, timer, file, interrupt, time, 17 * atomic, firmware, and I/O operation modules. 18 * 19 * @since 1.0 20 * @version 1.0 21 */ 22 23 /** 24 * @file osal_atomic.h 25 * 26 * @brief Declares atomic and bit operation interfaces. 27 * 28 * This file provides interfaces such as reading and setting an atomic, 29 * incrementing and decrementing an atomic counter by 1. 30 * This file also provides interfaces such as checking the bit status of a variable, 31 * and setting and clearing the bit value of a variable. 32 * 33 * @since 1.0 34 * @version 1.0 35 */ 36 37 #ifndef OSAL_ATOMIC_H 38 #define OSAL_ATOMIC_H 39 40 #include "osal_atomic_def.h" 41 42 #ifdef __cplusplus 43 extern "C" { 44 #endif /* __cplusplus */ 45 46 /** 47 * @brief Describes an atomic. 48 * 49 * @since 1.0 50 * @version 1.0 51 */ 52 typedef struct { 53 volatile int32_t counter;/**< Counter (an atomic variable). 54 * An operation on the atomic is to operate this variable. 55 */ 56 } OsalAtomic; 57 58 /** 59 * @brief Reads the counter of an atomic. 60 * 61 * @param v Indicates the pointer to the atomic {@link OsalAtomic}. 62 * 63 * @return Returns the counter. 64 * 65 * @since 1.0 66 * @version 1.0 67 */ 68 #define OsalAtomicRead(v) OsalAtomicReadWrapper(v) 69 70 /** 71 * @brief Sets the counter for an atomic. 72 * 73 * @param v Indicates the pointer to the atomic {@link OsalAtomic}. 74 * @param counter Indicates the counter to set. 75 * 76 * @since 1.0 77 * @version 1.0 78 */ 79 #define OsalAtomicSet(v, counter) OsalAtomicSetWrapper(v, counter) 80 81 /** 82 * @brief Increments the counter of an atomic by 1. 83 * 84 * @param v Indicates the pointer to the atomic {@link OsalAtomic}. 85 * 86 * @since 1.0 87 * @version 1.0 88 */ 89 #define OsalAtomicInc(v) OsalAtomicIncWrapper(v) 90 91 /** 92 * @brief Increments the counter of an atomic by 1 and returns the new counter. 93 * 94 * @param v Indicates the pointer to the atomic {@link OsalAtomic}. 95 * 96 * @return Returns the new counter. 97 * 98 * @since 1.0 99 * @version 1.0 100 */ 101 #define OsalAtomicIncReturn(v) OsalAtomicIncRetWrapper(v) 102 103 /** 104 * @brief Decrements the counter of an atomic by 1. 105 * 106 * @param v Indicates the pointer to the atomic {@link OsalAtomic}. 107 * 108 * @since 1.0 109 * @version 1.0 110 */ 111 #define OsalAtomicDec(v) OsalAtomicDecWrapper(v) 112 113 /** 114 * @brief Decrements the counter of an atomic by 1 and returns the new counter. 115 * 116 * @param v Indicates the pointer to the atomic {@link OsalAtomic}. 117 * 118 * @return Returns the new counter. 119 * 120 * @since 1.0 121 * @version 1.0 122 */ 123 #define OsalAtomicDecReturn(v) OsalAtomicDecRetWrapper(v) 124 125 /** 126 * @brief Tests the value of a specified bit of a variable. 127 * 128 * @param nr Indicates the bit of the variable. The value ranges from <b>0</b> to <b>31</b>. 129 * @param addr Indicates the pointer to the variable. 130 * 131 * @return Returns the bit value. 132 * 133 * @since 1.0 134 * @version 1.0 135 */ 136 #define OsalTestBit(nr, addr) OsalTestBitWrapper(nr, addr) 137 138 /** 139 * @brief Sets the value of a specified bit of the variable and returns the bit value before the setting. 140 * 141 * @param nr Indicates the bit of the variable. The value ranges from <b>0</b> to <b>31</b>. 142 * @param addr Indicates the pointer to the variable. 143 * 144 * @return Returns the bit value before the setting. 145 * 146 * @since 1.0 147 * @version 1.0 148 */ 149 #define OsalTestSetBit(nr, addr) OsalTestSetBitWrapper(nr, addr) 150 151 /** 152 * @brief Clears the value of a specified bit of the variable and returns the bit value before clearing. 153 * 154 * @param nr Indicates the bit of the variable. The value ranges from <b>0</b> to <b>31</b>. 155 * @param addr Indicates the pointer to the variable. 156 * 157 * @return Returns the bit value before the bit is cleared. 158 * 159 * @since 1.0 160 * @version 1.0 161 */ 162 #define OsalTestClearBit(nr, addr) OsalTestClearBitWrapper(nr, addr) 163 164 /** 165 * @brief Clears the value of a specified bit of the variable. 166 * 167 * @param nr Indicates the bit of the variable. The value ranges from <b>0</b> to <b>31</b>. 168 * @param addr Indicates the pointer to the variable. 169 * 170 * @since 1.0 171 * @version 1.0 172 */ 173 #define OsalClearBit(nr, addr) OsalClearBitWrapper(nr, addr) 174 175 #ifdef __cplusplus 176 } 177 #endif /* __cplusplus */ 178 179 #endif /* OSAL_ATOMIC_H */ 180 /** @} */ 181