OLD | NEW |
(Empty) | |
| 1 #ifndef _INTERNAL_ATOMIC_H |
| 2 #define _INTERNAL_ATOMIC_H |
| 3 |
| 4 #include <stdint.h> |
| 5 |
| 6 static inline int a_ctz_l(unsigned long x) |
| 7 { |
| 8 static const char debruijn32[32] = { |
| 9 0, 1, 23, 2, 29, 24, 19, 3, 30, 27, 25, 11, 20, 8, 4, 13, |
| 10 31, 22, 28, 18, 26, 10, 7, 12, 21, 17, 9, 6, 16, 5, 15, 14 |
| 11 }; |
| 12 return debruijn32[(x&-x)*0x076be629 >> 27]; |
| 13 } |
| 14 |
| 15 static inline int a_ctz_64(uint64_t x) |
| 16 { |
| 17 uint32_t y = x; |
| 18 if (!y) { |
| 19 y = x>>32; |
| 20 return 32 + a_ctz_l(y); |
| 21 } |
| 22 return a_ctz_l(y); |
| 23 } |
| 24 |
| 25 #if __ARM_ARCH_7A__ || __ARM_ARCH_7R__ || __ARM_ARCH >= 7 |
| 26 |
| 27 static inline void a_barrier() |
| 28 { |
| 29 __asm__ __volatile__("dmb ish"); |
| 30 } |
| 31 |
| 32 static inline int a_cas(volatile int *p, int t, int s) |
| 33 { |
| 34 int old; |
| 35 __asm__ __volatile__( |
| 36 " dmb ish\n" |
| 37 "1: ldrex %0,%3\n" |
| 38 " cmp %0,%1\n" |
| 39 " bne 1f\n" |
| 40 " strex %0,%2,%3\n" |
| 41 " cmp %0, #0\n" |
| 42 " bne 1b\n" |
| 43 " mov %0, %1\n" |
| 44 "1: dmb ish\n" |
| 45 : "=&r"(old) |
| 46 : "r"(t), "r"(s), "Q"(*p) |
| 47 : "memory", "cc" ); |
| 48 return old; |
| 49 } |
| 50 |
| 51 static inline int a_swap(volatile int *x, int v) |
| 52 { |
| 53 int old, tmp; |
| 54 __asm__ __volatile__( |
| 55 " dmb ish\n" |
| 56 "1: ldrex %0,%3\n" |
| 57 " strex %1,%2,%3\n" |
| 58 " cmp %1, #0\n" |
| 59 " bne 1b\n" |
| 60 " dmb ish\n" |
| 61 : "=&r"(old), "=&r"(tmp) |
| 62 : "r"(v), "Q"(*x) |
| 63 : "memory", "cc" ); |
| 64 return old; |
| 65 } |
| 66 |
| 67 static inline int a_fetch_add(volatile int *x, int v) |
| 68 { |
| 69 int old, tmp; |
| 70 __asm__ __volatile__( |
| 71 " dmb ish\n" |
| 72 "1: ldrex %0,%3\n" |
| 73 " add %0,%0,%2\n" |
| 74 " strex %1,%0,%3\n" |
| 75 " cmp %1, #0\n" |
| 76 " bne 1b\n" |
| 77 " dmb ish\n" |
| 78 : "=&r"(old), "=&r"(tmp) |
| 79 : "r"(v), "Q"(*x) |
| 80 : "memory", "cc" ); |
| 81 return old-v; |
| 82 } |
| 83 |
| 84 static inline void a_inc(volatile int *x) |
| 85 { |
| 86 int tmp, tmp2; |
| 87 __asm__ __volatile__( |
| 88 " dmb ish\n" |
| 89 "1: ldrex %0,%2\n" |
| 90 " add %0,%0,#1\n" |
| 91 " strex %1,%0,%2\n" |
| 92 " cmp %1, #0\n" |
| 93 " bne 1b\n" |
| 94 " dmb ish\n" |
| 95 : "=&r"(tmp), "=&r"(tmp2) |
| 96 : "Q"(*x) |
| 97 : "memory", "cc" ); |
| 98 } |
| 99 |
| 100 static inline void a_dec(volatile int *x) |
| 101 { |
| 102 int tmp, tmp2; |
| 103 __asm__ __volatile__( |
| 104 " dmb ish\n" |
| 105 "1: ldrex %0,%2\n" |
| 106 " sub %0,%0,#1\n" |
| 107 " strex %1,%0,%2\n" |
| 108 " cmp %1, #0\n" |
| 109 " bne 1b\n" |
| 110 " dmb ish\n" |
| 111 : "=&r"(tmp), "=&r"(tmp2) |
| 112 : "Q"(*x) |
| 113 : "memory", "cc" ); |
| 114 } |
| 115 |
| 116 static inline void a_and(volatile int *x, int v) |
| 117 { |
| 118 int tmp, tmp2; |
| 119 __asm__ __volatile__( |
| 120 " dmb ish\n" |
| 121 "1: ldrex %0,%3\n" |
| 122 " and %0,%0,%2\n" |
| 123 " strex %1,%0,%3\n" |
| 124 " cmp %1, #0\n" |
| 125 " bne 1b\n" |
| 126 " dmb ish\n" |
| 127 : "=&r"(tmp), "=&r"(tmp2) |
| 128 : "r"(v), "Q"(*x) |
| 129 : "memory", "cc" ); |
| 130 } |
| 131 |
| 132 static inline void a_or(volatile int *x, int v) |
| 133 { |
| 134 int tmp, tmp2; |
| 135 __asm__ __volatile__( |
| 136 " dmb ish\n" |
| 137 "1: ldrex %0,%3\n" |
| 138 " orr %0,%0,%2\n" |
| 139 " strex %1,%0,%3\n" |
| 140 " cmp %1, #0\n" |
| 141 " bne 1b\n" |
| 142 " dmb ish\n" |
| 143 : "=&r"(tmp), "=&r"(tmp2) |
| 144 : "r"(v), "Q"(*x) |
| 145 : "memory", "cc" ); |
| 146 } |
| 147 |
| 148 static inline void a_store(volatile int *p, int x) |
| 149 { |
| 150 __asm__ __volatile__( |
| 151 " dmb ish\n" |
| 152 " str %1,%0\n" |
| 153 " dmb ish\n" |
| 154 : "=m"(*p) |
| 155 : "r"(x) |
| 156 : "memory", "cc" ); |
| 157 } |
| 158 |
| 159 #else |
| 160 |
| 161 int __a_cas(int, int, volatile int *) __attribute__((__visibility__("hidden"))); |
| 162 #define __k_cas __a_cas |
| 163 |
| 164 static inline void a_barrier() |
| 165 { |
| 166 __asm__ __volatile__("bl __a_barrier" |
| 167 : : : "memory", "cc", "ip", "lr" ); |
| 168 } |
| 169 |
| 170 static inline int a_cas(volatile int *p, int t, int s) |
| 171 { |
| 172 int old; |
| 173 for (;;) { |
| 174 if (!__k_cas(t, s, p)) |
| 175 return t; |
| 176 if ((old=*p) != t) |
| 177 return old; |
| 178 } |
| 179 } |
| 180 |
| 181 static inline int a_swap(volatile int *x, int v) |
| 182 { |
| 183 int old; |
| 184 do old = *x; |
| 185 while (__k_cas(old, v, x)); |
| 186 return old; |
| 187 } |
| 188 |
| 189 static inline int a_fetch_add(volatile int *x, int v) |
| 190 { |
| 191 int old; |
| 192 do old = *x; |
| 193 while (__k_cas(old, old+v, x)); |
| 194 return old; |
| 195 } |
| 196 |
| 197 static inline void a_inc(volatile int *x) |
| 198 { |
| 199 a_fetch_add(x, 1); |
| 200 } |
| 201 |
| 202 static inline void a_dec(volatile int *x) |
| 203 { |
| 204 a_fetch_add(x, -1); |
| 205 } |
| 206 |
| 207 static inline void a_store(volatile int *p, int x) |
| 208 { |
| 209 a_barrier(); |
| 210 *p = x; |
| 211 a_barrier(); |
| 212 } |
| 213 |
| 214 static inline void a_and(volatile int *p, int v) |
| 215 { |
| 216 int old; |
| 217 do old = *p; |
| 218 while (__k_cas(old, old&v, p)); |
| 219 } |
| 220 |
| 221 static inline void a_or(volatile int *p, int v) |
| 222 { |
| 223 int old; |
| 224 do old = *p; |
| 225 while (__k_cas(old, old|v, p)); |
| 226 } |
| 227 |
| 228 #endif |
| 229 |
| 230 static inline void *a_cas_p(volatile void *p, void *t, void *s) |
| 231 { |
| 232 return (void *)a_cas(p, (int)t, (int)s); |
| 233 } |
| 234 |
| 235 #define a_spin a_barrier |
| 236 |
| 237 static inline void a_crash() |
| 238 { |
| 239 *(volatile char *)0=0; |
| 240 } |
| 241 |
| 242 static inline void a_or_l(volatile void *p, long v) |
| 243 { |
| 244 a_or(p, v); |
| 245 } |
| 246 |
| 247 static inline void a_and_64(volatile uint64_t *p, uint64_t v) |
| 248 { |
| 249 union { uint64_t v; uint32_t r[2]; } u = { v }; |
| 250 a_and((int *)p, u.r[0]); |
| 251 a_and((int *)p+1, u.r[1]); |
| 252 } |
| 253 |
| 254 static inline void a_or_64(volatile uint64_t *p, uint64_t v) |
| 255 { |
| 256 union { uint64_t v; uint32_t r[2]; } u = { v }; |
| 257 a_or((int *)p, u.r[0]); |
| 258 a_or((int *)p+1, u.r[1]); |
| 259 } |
| 260 |
| 261 #endif |
OLD | NEW |