| 1 | /* |
| 2 | * Copyright (C) 2018 - 2022, Stephan Mueller <[email protected]> |
| 3 | * |
| 4 | * License: see COPYING file in root directory |
| 5 | * |
| 6 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 7 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 8 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF |
| 9 | * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE |
| 10 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 11 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
| 12 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 13 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 14 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 15 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE |
| 16 | * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH |
| 17 | * DAMAGE. |
| 18 | */ |
| 19 | |
| 20 | #ifndef _ATOMIC_H |
| 21 | #define _ATOMIC_Hcxx . . $ `lib/atomic.h:21:9`! |
| 22 | |
| 23 | /* |
| 24 | * Atomic operations only work on: |
| 25 | * GCC >= 4.1 |
| 26 | * Clang / LLVM |
| 27 | */ |
| 28 | |
| 29 | /** |
| 30 | * Atomic type and operations equivalent to the Linux kernel. |
| 31 | */ |
| 32 | typedef structcxx . . $ $anonymous_type_62897d179ee1dabb_0# { |
| 33 | volatile int countercxx . . $ $anonymous_type_62897d179ee1dabb_0#counter.; |
| 34 | } atomic_tcxx . . $ atomic_t#; |
| 35 | |
| 36 | /** |
| 37 | * Memory barrier |
| 38 | */ |
| 39 | static inline void mbcxx . . $ mb(49f6e7a06ebc5aa8).(void) |
| 40 | { |
| 41 | __sync_synchronizecxx . . $ __sync_synchronize(49f6e7a06ebc5aa8).(); |
| 42 | } |
| 43 | |
| 44 | #define ATOMIC_INITcxx . . $ `lib/atomic.h:44:9`!(i) { (i) } |
| 45 | |
| 46 | /** |
| 47 | * Read atomic variable |
| 48 | * @param v atomic variable |
| 49 | * @return variable content |
| 50 | */ |
| 51 | static inline int atomic_readcxx . . $ atomic_read(b31a9db3ff35b06b).(const atomic_tcxx . . $ atomic_t# *v) |
| 52 | { |
| 53 | int i; |
| 54 | |
| 55 | mbcxx . . $ mb(49f6e7a06ebc5aa8).(); |
| 56 | i = ((v)->countercxx . . $ $anonymous_type_62897d179ee1dabb_0#counter.); |
| 57 | mbcxx . . $ mb(49f6e7a06ebc5aa8).(); |
| 58 | |
| 59 | return i; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Set atomic variable |
| 64 | * @param i value to be set |
| 65 | * @param v atomic variable |
| 66 | */ |
| 67 | static inline void atomic_setcxx . . $ atomic_set(d9e8d0a15ff6fbc8).(int i, atomic_tcxx . . $ atomic_t# *v) |
| 68 | { |
| 69 | mbcxx . . $ mb(49f6e7a06ebc5aa8).(); |
| 70 | ((v)->countercxx . . $ $anonymous_type_62897d179ee1dabb_0#counter.) = i; |
| 71 | mbcxx . . $ mb(49f6e7a06ebc5aa8).(); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Atomic add operation |
| 76 | * @param i integer value to add |
| 77 | * @param v atomic variable |
| 78 | * @return variable content after operation |
| 79 | */ |
| 80 | static inline int atomic_addcxx . . $ atomic_add(b7189d281c2d1882).(int i, atomic_tcxx . . $ atomic_t# *v) |
| 81 | { |
| 82 | return __sync_add_and_fetchcxx . . $ __sync_add_and_fetch_4(a634857e7d64165d).(&v->countercxx . . $ $anonymous_type_62897d179ee1dabb_0#counter., i); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Atomic add value from variable and test for zero |
| 87 | * @param i integer value to add |
| 88 | * @param v atomic variable |
| 89 | * @return true if the result is zero, or false for all other cases. |
| 90 | */ |
| 91 | static inline int atomic_add_and_testcxx . . $ atomic_add_and_test(b7189d281c2d1882).(int i, atomic_tcxx . . $ atomic_t# *v) |
| 92 | { |
| 93 | return !(__sync_add_and_fetchcxx . . $ __sync_add_and_fetch_4(a634857e7d64165d).(&v->countercxx . . $ $anonymous_type_62897d179ee1dabb_0#counter., i)); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Atomic increment by 1 |
| 98 | * @param v atomic variable |
| 99 | * @return variable content after operation |
| 100 | */ |
| 101 | static inline int atomic_inccxx . . $ atomic_inc(3e4ce074a99d0a0f).(atomic_tcxx . . $ atomic_t# *v) |
| 102 | { |
| 103 | return atomic_addcxx . . $ atomic_add(b7189d281c2d1882).(1, v); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Atomic increment and test for zero |
| 108 | * @param v pointer of type atomic_t |
| 109 | * @return true if the result is zero, or false for all other cases. |
| 110 | */ |
| 111 | static inline int atomic_inc_and_testcxx . . $ atomic_inc_and_test(3e4ce074a99d0a0f).(atomic_tcxx . . $ atomic_t# *v) |
| 112 | { |
| 113 | return atomic_add_and_testcxx . . $ atomic_add_and_test(b7189d281c2d1882).(1, v); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Atomic subtract operation |
| 118 | * @param i integer value to subtract |
| 119 | * @param v atomic variable |
| 120 | * @return variable content after operation |
| 121 | */ |
| 122 | static inline int atomic_subcxx . . $ atomic_sub(b7189d281c2d1882).(int i, atomic_tcxx . . $ atomic_t# *v) |
| 123 | { |
| 124 | return __sync_sub_and_fetchcxx . . $ __sync_sub_and_fetch_4(a634857e7d64165d).(&v->countercxx . . $ $anonymous_type_62897d179ee1dabb_0#counter., i); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Atomic subtract value from variable and test for zero |
| 129 | * @param i integer value to subtract |
| 130 | * @param v atomic variable |
| 131 | * @return true if the result is zero, or false for all other cases. |
| 132 | */ |
| 133 | static inline int atomic_sub_and_testcxx . . $ atomic_sub_and_test(b7189d281c2d1882).(int i, atomic_tcxx . . $ atomic_t# *v) |
| 134 | { |
| 135 | return !(__sync_sub_and_fetchcxx . . $ __sync_sub_and_fetch_4(a634857e7d64165d).(&v->countercxx . . $ $anonymous_type_62897d179ee1dabb_0#counter., i)); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Atomic decrement by 1 |
| 140 | * @param v: atomic variable |
| 141 | * @return variable content after operation |
| 142 | */ |
| 143 | static inline int atomic_deccxx . . $ atomic_dec(3e4ce074a99d0a0f).(atomic_tcxx . . $ atomic_t# *v) |
| 144 | { |
| 145 | return atomic_subcxx . . $ atomic_sub(b7189d281c2d1882).(1, v); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Atomic decrement by 1 and test for zero |
| 150 | * @param v atomic variable |
| 151 | * @return true if the result is zero, or false for all other cases. |
| 152 | */ |
| 153 | static inline int atomic_dec_and_testcxx . . $ atomic_dec_and_test(3e4ce074a99d0a0f).(atomic_tcxx . . $ atomic_t# *v) |
| 154 | { |
| 155 | return atomic_sub_and_testcxx . . $ atomic_sub_and_test(b7189d281c2d1882).(1, v); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Atomic or operation |
| 160 | * @param i integer value to or |
| 161 | * @param v atomic variable |
| 162 | * @return variable content after operation |
| 163 | */ |
| 164 | static inline int atomic_orcxx . . $ atomic_or(b7189d281c2d1882).(int i, atomic_tcxx . . $ atomic_t# *v) |
| 165 | { |
| 166 | return __sync_or_and_fetchcxx . . $ __sync_or_and_fetch_4(a634857e7d64165d).(&v->countercxx . . $ $anonymous_type_62897d179ee1dabb_0#counter., i); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Atomic xor operation |
| 171 | * @param i integer value to xor |
| 172 | * @param v atomic variable |
| 173 | * @return variable content after operation |
| 174 | */ |
| 175 | static inline int atomic_xorcxx . . $ atomic_xor(b7189d281c2d1882).(int i, atomic_tcxx . . $ atomic_t# *v) |
| 176 | { |
| 177 | return __sync_xor_and_fetchcxx . . $ __sync_xor_and_fetch_4(a634857e7d64165d).(&v->countercxx . . $ $anonymous_type_62897d179ee1dabb_0#counter., i); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Atomic and operation |
| 182 | * @param i integer value to and |
| 183 | * @param v atomic variable |
| 184 | * @return variable content after operation |
| 185 | */ |
| 186 | static inline int atomic_andcxx . . $ atomic_and(b7189d281c2d1882).(int i, atomic_tcxx . . $ atomic_t# *v) |
| 187 | { |
| 188 | return __sync_and_and_fetchcxx . . $ __sync_and_and_fetch_4(a634857e7d64165d).(&v->countercxx . . $ $anonymous_type_62897d179ee1dabb_0#counter., i); |
| 189 | } |
| 190 | |
| 191 | #ifdef __clang__cxx . . $ `<built-in>:3:9`! |
| 192 | #pragma clang diagnostic push |
| 193 | #pragma clang diagnostic ignored "-Wsync-fetch-and-nand-semantics-changed" |
| 194 | #endif |
| 195 | /** |
| 196 | * Atomic nand operation |
| 197 | * @param i integer value to nand |
| 198 | * @param v atomic variable |
| 199 | * @return variable content after operation |
| 200 | */ |
| 201 | static inline int atomic_nandcxx . . $ atomic_nand(b7189d281c2d1882).(int i, atomic_tcxx . . $ atomic_t# *v) |
| 202 | { |
| 203 | return __sync_nand_and_fetchcxx . . $ __sync_nand_and_fetch_4(a634857e7d64165d).(&v->countercxx . . $ $anonymous_type_62897d179ee1dabb_0#counter., i); |
| 204 | } |
| 205 | #ifdef __clang__cxx . . $ `<built-in>:3:9`! |
| 206 | #pragma clang diagnostic pop |
| 207 | #endif |
| 208 | |
| 209 | /** |
| 210 | * Atomic compare and exchange operation (if current value of atomic |
| 211 | * variable is equal to the old value, set the new value) |
| 212 | * @param v atomic variable |
| 213 | * @param old integer value to compare with |
| 214 | * @param new integer value to set atomic variable to |
| 215 | * @return true if comparison is successful and new was written |
| 216 | */ |
| 217 | static inline int atomic_cmpxchgcxx . . $ atomic_cmpxchg(8536cada7423cff).(atomic_tcxx . . $ atomic_t# *v, int old, int new) |
| 218 | { |
| 219 | return __sync_bool_compare_and_swapcxx . . $ __sync_bool_compare_and_swap_4(2bb2bf1fe70f5e29).(&v->countercxx . . $ $anonymous_type_62897d179ee1dabb_0#counter., old, new); |
| 220 | } |
| 221 | |
| 222 | #endif |