diff options
Diffstat (limited to 'include/linux/atomic.h')
| -rw-r--r-- | include/linux/atomic.h | 361 |
1 files changed, 348 insertions, 13 deletions
diff --git a/include/linux/atomic.h b/include/linux/atomic.h index 5b08a8540ecf..00a5763e850e 100644 --- a/include/linux/atomic.h +++ b/include/linux/atomic.h | |||
| @@ -2,6 +2,329 @@ | |||
| 2 | #ifndef _LINUX_ATOMIC_H | 2 | #ifndef _LINUX_ATOMIC_H |
| 3 | #define _LINUX_ATOMIC_H | 3 | #define _LINUX_ATOMIC_H |
| 4 | #include <asm/atomic.h> | 4 | #include <asm/atomic.h> |
| 5 | #include <asm/barrier.h> | ||
| 6 | |||
| 7 | /* | ||
| 8 | * Relaxed variants of xchg, cmpxchg and some atomic operations. | ||
| 9 | * | ||
| 10 | * We support four variants: | ||
| 11 | * | ||
| 12 | * - Fully ordered: The default implementation, no suffix required. | ||
| 13 | * - Acquire: Provides ACQUIRE semantics, _acquire suffix. | ||
| 14 | * - Release: Provides RELEASE semantics, _release suffix. | ||
| 15 | * - Relaxed: No ordering guarantees, _relaxed suffix. | ||
| 16 | * | ||
| 17 | * For compound atomics performing both a load and a store, ACQUIRE | ||
| 18 | * semantics apply only to the load and RELEASE semantics only to the | ||
| 19 | * store portion of the operation. Note that a failed cmpxchg_acquire | ||
| 20 | * does -not- imply any memory ordering constraints. | ||
| 21 | * | ||
| 22 | * See Documentation/memory-barriers.txt for ACQUIRE/RELEASE definitions. | ||
| 23 | */ | ||
| 24 | |||
| 25 | #ifndef atomic_read_acquire | ||
| 26 | #define atomic_read_acquire(v) smp_load_acquire(&(v)->counter) | ||
| 27 | #endif | ||
| 28 | |||
| 29 | #ifndef atomic_set_release | ||
| 30 | #define atomic_set_release(v, i) smp_store_release(&(v)->counter, (i)) | ||
| 31 | #endif | ||
| 32 | |||
| 33 | /* | ||
| 34 | * The idea here is to build acquire/release variants by adding explicit | ||
| 35 | * barriers on top of the relaxed variant. In the case where the relaxed | ||
| 36 | * variant is already fully ordered, no additional barriers are needed. | ||
| 37 | */ | ||
| 38 | #define __atomic_op_acquire(op, args...) \ | ||
| 39 | ({ \ | ||
| 40 | typeof(op##_relaxed(args)) __ret = op##_relaxed(args); \ | ||
| 41 | smp_mb__after_atomic(); \ | ||
| 42 | __ret; \ | ||
| 43 | }) | ||
| 44 | |||
| 45 | #define __atomic_op_release(op, args...) \ | ||
| 46 | ({ \ | ||
| 47 | smp_mb__before_atomic(); \ | ||
| 48 | op##_relaxed(args); \ | ||
| 49 | }) | ||
| 50 | |||
| 51 | #define __atomic_op_fence(op, args...) \ | ||
| 52 | ({ \ | ||
| 53 | typeof(op##_relaxed(args)) __ret; \ | ||
| 54 | smp_mb__before_atomic(); \ | ||
| 55 | __ret = op##_relaxed(args); \ | ||
| 56 | smp_mb__after_atomic(); \ | ||
| 57 | __ret; \ | ||
| 58 | }) | ||
| 59 | |||
| 60 | /* atomic_add_return_relaxed */ | ||
| 61 | #ifndef atomic_add_return_relaxed | ||
| 62 | #define atomic_add_return_relaxed atomic_add_return | ||
| 63 | #define atomic_add_return_acquire atomic_add_return | ||
| 64 | #define atomic_add_return_release atomic_add_return | ||
| 65 | |||
| 66 | #else /* atomic_add_return_relaxed */ | ||
| 67 | |||
| 68 | #ifndef atomic_add_return_acquire | ||
| 69 | #define atomic_add_return_acquire(...) \ | ||
| 70 | __atomic_op_acquire(atomic_add_return, __VA_ARGS__) | ||
| 71 | #endif | ||
| 72 | |||
| 73 | #ifndef atomic_add_return_release | ||
| 74 | #define atomic_add_return_release(...) \ | ||
| 75 | __atomic_op_release(atomic_add_return, __VA_ARGS__) | ||
| 76 | #endif | ||
| 77 | |||
| 78 | #ifndef atomic_add_return | ||
| 79 | #define atomic_add_return(...) \ | ||
| 80 | __atomic_op_fence(atomic_add_return, __VA_ARGS__) | ||
| 81 | #endif | ||
| 82 | #endif /* atomic_add_return_relaxed */ | ||
| 83 | |||
| 84 | /* atomic_sub_return_relaxed */ | ||
| 85 | #ifndef atomic_sub_return_relaxed | ||
| 86 | #define atomic_sub_return_relaxed atomic_sub_return | ||
| 87 | #define atomic_sub_return_acquire atomic_sub_return | ||
| 88 | #define atomic_sub_return_release atomic_sub_return | ||
| 89 | |||
| 90 | #else /* atomic_sub_return_relaxed */ | ||
| 91 | |||
| 92 | #ifndef atomic_sub_return_acquire | ||
| 93 | #define atomic_sub_return_acquire(...) \ | ||
| 94 | __atomic_op_acquire(atomic_sub_return, __VA_ARGS__) | ||
| 95 | #endif | ||
| 96 | |||
| 97 | #ifndef atomic_sub_return_release | ||
| 98 | #define atomic_sub_return_release(...) \ | ||
| 99 | __atomic_op_release(atomic_sub_return, __VA_ARGS__) | ||
| 100 | #endif | ||
| 101 | |||
| 102 | #ifndef atomic_sub_return | ||
| 103 | #define atomic_sub_return(...) \ | ||
| 104 | __atomic_op_fence(atomic_sub_return, __VA_ARGS__) | ||
| 105 | #endif | ||
| 106 | #endif /* atomic_sub_return_relaxed */ | ||
| 107 | |||
| 108 | /* atomic_xchg_relaxed */ | ||
| 109 | #ifndef atomic_xchg_relaxed | ||
| 110 | #define atomic_xchg_relaxed atomic_xchg | ||
| 111 | #define atomic_xchg_acquire atomic_xchg | ||
| 112 | #define atomic_xchg_release atomic_xchg | ||
| 113 | |||
| 114 | #else /* atomic_xchg_relaxed */ | ||
| 115 | |||
| 116 | #ifndef atomic_xchg_acquire | ||
| 117 | #define atomic_xchg_acquire(...) \ | ||
| 118 | __atomic_op_acquire(atomic_xchg, __VA_ARGS__) | ||
| 119 | #endif | ||
| 120 | |||
| 121 | #ifndef atomic_xchg_release | ||
| 122 | #define atomic_xchg_release(...) \ | ||
| 123 | __atomic_op_release(atomic_xchg, __VA_ARGS__) | ||
| 124 | #endif | ||
| 125 | |||
| 126 | #ifndef atomic_xchg | ||
| 127 | #define atomic_xchg(...) \ | ||
| 128 | __atomic_op_fence(atomic_xchg, __VA_ARGS__) | ||
| 129 | #endif | ||
| 130 | #endif /* atomic_xchg_relaxed */ | ||
| 131 | |||
| 132 | /* atomic_cmpxchg_relaxed */ | ||
| 133 | #ifndef atomic_cmpxchg_relaxed | ||
| 134 | #define atomic_cmpxchg_relaxed atomic_cmpxchg | ||
| 135 | #define atomic_cmpxchg_acquire atomic_cmpxchg | ||
| 136 | #define atomic_cmpxchg_release atomic_cmpxchg | ||
| 137 | |||
| 138 | #else /* atomic_cmpxchg_relaxed */ | ||
| 139 | |||
| 140 | #ifndef atomic_cmpxchg_acquire | ||
| 141 | #define atomic_cmpxchg_acquire(...) \ | ||
| 142 | __atomic_op_acquire(atomic_cmpxchg, __VA_ARGS__) | ||
| 143 | #endif | ||
| 144 | |||
| 145 | #ifndef atomic_cmpxchg_release | ||
| 146 | #define atomic_cmpxchg_release(...) \ | ||
| 147 | __atomic_op_release(atomic_cmpxchg, __VA_ARGS__) | ||
| 148 | #endif | ||
| 149 | |||
| 150 | #ifndef atomic_cmpxchg | ||
| 151 | #define atomic_cmpxchg(...) \ | ||
| 152 | __atomic_op_fence(atomic_cmpxchg, __VA_ARGS__) | ||
| 153 | #endif | ||
| 154 | #endif /* atomic_cmpxchg_relaxed */ | ||
| 155 | |||
| 156 | #ifndef atomic64_read_acquire | ||
| 157 | #define atomic64_read_acquire(v) smp_load_acquire(&(v)->counter) | ||
| 158 | #endif | ||
| 159 | |||
| 160 | #ifndef atomic64_set_release | ||
| 161 | #define atomic64_set_release(v, i) smp_store_release(&(v)->counter, (i)) | ||
| 162 | #endif | ||
| 163 | |||
| 164 | /* atomic64_add_return_relaxed */ | ||
| 165 | #ifndef atomic64_add_return_relaxed | ||
| 166 | #define atomic64_add_return_relaxed atomic64_add_return | ||
| 167 | #define atomic64_add_return_acquire atomic64_add_return | ||
| 168 | #define atomic64_add_return_release atomic64_add_return | ||
| 169 | |||
| 170 | #else /* atomic64_add_return_relaxed */ | ||
| 171 | |||
| 172 | #ifndef atomic64_add_return_acquire | ||
| 173 | #define atomic64_add_return_acquire(...) \ | ||
| 174 | __atomic_op_acquire(atomic64_add_return, __VA_ARGS__) | ||
| 175 | #endif | ||
| 176 | |||
| 177 | #ifndef atomic64_add_return_release | ||
| 178 | #define atomic64_add_return_release(...) \ | ||
| 179 | __atomic_op_release(atomic64_add_return, __VA_ARGS__) | ||
| 180 | #endif | ||
| 181 | |||
| 182 | #ifndef atomic64_add_return | ||
| 183 | #define atomic64_add_return(...) \ | ||
| 184 | __atomic_op_fence(atomic64_add_return, __VA_ARGS__) | ||
| 185 | #endif | ||
| 186 | #endif /* atomic64_add_return_relaxed */ | ||
| 187 | |||
| 188 | /* atomic64_sub_return_relaxed */ | ||
| 189 | #ifndef atomic64_sub_return_relaxed | ||
| 190 | #define atomic64_sub_return_relaxed atomic64_sub_return | ||
| 191 | #define atomic64_sub_return_acquire atomic64_sub_return | ||
| 192 | #define atomic64_sub_return_release atomic64_sub_return | ||
| 193 | |||
| 194 | #else /* atomic64_sub_return_relaxed */ | ||
| 195 | |||
| 196 | #ifndef atomic64_sub_return_acquire | ||
| 197 | #define atomic64_sub_return_acquire(...) \ | ||
| 198 | __atomic_op_acquire(atomic64_sub_return, __VA_ARGS__) | ||
| 199 | #endif | ||
| 200 | |||
| 201 | #ifndef atomic64_sub_return_release | ||
| 202 | #define atomic64_sub_return_release(...) \ | ||
| 203 | __atomic_op_release(atomic64_sub_return, __VA_ARGS__) | ||
| 204 | #endif | ||
| 205 | |||
| 206 | #ifndef atomic64_sub_return | ||
| 207 | #define atomic64_sub_return(...) \ | ||
| 208 | __atomic_op_fence(atomic64_sub_return, __VA_ARGS__) | ||
| 209 | #endif | ||
| 210 | #endif /* atomic64_sub_return_relaxed */ | ||
| 211 | |||
| 212 | /* atomic64_xchg_relaxed */ | ||
| 213 | #ifndef atomic64_xchg_relaxed | ||
| 214 | #define atomic64_xchg_relaxed atomic64_xchg | ||
| 215 | #define atomic64_xchg_acquire atomic64_xchg | ||
| 216 | #define atomic64_xchg_release atomic64_xchg | ||
| 217 | |||
| 218 | #else /* atomic64_xchg_relaxed */ | ||
| 219 | |||
| 220 | #ifndef atomic64_xchg_acquire | ||
| 221 | #define atomic64_xchg_acquire(...) \ | ||
| 222 | __atomic_op_acquire(atomic64_xchg, __VA_ARGS__) | ||
| 223 | #endif | ||
| 224 | |||
| 225 | #ifndef atomic64_xchg_release | ||
| 226 | #define atomic64_xchg_release(...) \ | ||
| 227 | __atomic_op_release(atomic64_xchg, __VA_ARGS__) | ||
| 228 | #endif | ||
| 229 | |||
| 230 | #ifndef atomic64_xchg | ||
| 231 | #define atomic64_xchg(...) \ | ||
| 232 | __atomic_op_fence(atomic64_xchg, __VA_ARGS__) | ||
| 233 | #endif | ||
| 234 | #endif /* atomic64_xchg_relaxed */ | ||
| 235 | |||
| 236 | /* atomic64_cmpxchg_relaxed */ | ||
| 237 | #ifndef atomic64_cmpxchg_relaxed | ||
| 238 | #define atomic64_cmpxchg_relaxed atomic64_cmpxchg | ||
| 239 | #define atomic64_cmpxchg_acquire atomic64_cmpxchg | ||
| 240 | #define atomic64_cmpxchg_release atomic64_cmpxchg | ||
| 241 | |||
| 242 | #else /* atomic64_cmpxchg_relaxed */ | ||
| 243 | |||
| 244 | #ifndef atomic64_cmpxchg_acquire | ||
| 245 | #define atomic64_cmpxchg_acquire(...) \ | ||
| 246 | __atomic_op_acquire(atomic64_cmpxchg, __VA_ARGS__) | ||
| 247 | #endif | ||
| 248 | |||
| 249 | #ifndef atomic64_cmpxchg_release | ||
| 250 | #define atomic64_cmpxchg_release(...) \ | ||
| 251 | __atomic_op_release(atomic64_cmpxchg, __VA_ARGS__) | ||
| 252 | #endif | ||
| 253 | |||
| 254 | #ifndef atomic64_cmpxchg | ||
| 255 | #define atomic64_cmpxchg(...) \ | ||
| 256 | __atomic_op_fence(atomic64_cmpxchg, __VA_ARGS__) | ||
| 257 | #endif | ||
| 258 | #endif /* atomic64_cmpxchg_relaxed */ | ||
| 259 | |||
| 260 | /* cmpxchg_relaxed */ | ||
| 261 | #ifndef cmpxchg_relaxed | ||
| 262 | #define cmpxchg_relaxed cmpxchg | ||
| 263 | #define cmpxchg_acquire cmpxchg | ||
| 264 | #define cmpxchg_release cmpxchg | ||
| 265 | |||
| 266 | #else /* cmpxchg_relaxed */ | ||
| 267 | |||
| 268 | #ifndef cmpxchg_acquire | ||
| 269 | #define cmpxchg_acquire(...) \ | ||
| 270 | __atomic_op_acquire(cmpxchg, __VA_ARGS__) | ||
| 271 | #endif | ||
| 272 | |||
| 273 | #ifndef cmpxchg_release | ||
| 274 | #define cmpxchg_release(...) \ | ||
| 275 | __atomic_op_release(cmpxchg, __VA_ARGS__) | ||
| 276 | #endif | ||
| 277 | |||
| 278 | #ifndef cmpxchg | ||
| 279 | #define cmpxchg(...) \ | ||
| 280 | __atomic_op_fence(cmpxchg, __VA_ARGS__) | ||
| 281 | #endif | ||
| 282 | #endif /* cmpxchg_relaxed */ | ||
| 283 | |||
| 284 | /* cmpxchg64_relaxed */ | ||
| 285 | #ifndef cmpxchg64_relaxed | ||
| 286 | #define cmpxchg64_relaxed cmpxchg64 | ||
| 287 | #define cmpxchg64_acquire cmpxchg64 | ||
| 288 | #define cmpxchg64_release cmpxchg64 | ||
| 289 | |||
| 290 | #else /* cmpxchg64_relaxed */ | ||
| 291 | |||
| 292 | #ifndef cmpxchg64_acquire | ||
| 293 | #define cmpxchg64_acquire(...) \ | ||
| 294 | __atomic_op_acquire(cmpxchg64, __VA_ARGS__) | ||
| 295 | #endif | ||
| 296 | |||
| 297 | #ifndef cmpxchg64_release | ||
| 298 | #define cmpxchg64_release(...) \ | ||
| 299 | __atomic_op_release(cmpxchg64, __VA_ARGS__) | ||
| 300 | #endif | ||
| 301 | |||
| 302 | #ifndef cmpxchg64 | ||
| 303 | #define cmpxchg64(...) \ | ||
| 304 | __atomic_op_fence(cmpxchg64, __VA_ARGS__) | ||
| 305 | #endif | ||
| 306 | #endif /* cmpxchg64_relaxed */ | ||
| 307 | |||
| 308 | /* xchg_relaxed */ | ||
| 309 | #ifndef xchg_relaxed | ||
| 310 | #define xchg_relaxed xchg | ||
| 311 | #define xchg_acquire xchg | ||
| 312 | #define xchg_release xchg | ||
| 313 | |||
| 314 | #else /* xchg_relaxed */ | ||
| 315 | |||
| 316 | #ifndef xchg_acquire | ||
| 317 | #define xchg_acquire(...) __atomic_op_acquire(xchg, __VA_ARGS__) | ||
| 318 | #endif | ||
| 319 | |||
| 320 | #ifndef xchg_release | ||
| 321 | #define xchg_release(...) __atomic_op_release(xchg, __VA_ARGS__) | ||
| 322 | #endif | ||
| 323 | |||
| 324 | #ifndef xchg | ||
| 325 | #define xchg(...) __atomic_op_fence(xchg, __VA_ARGS__) | ||
| 326 | #endif | ||
| 327 | #endif /* xchg_relaxed */ | ||
| 5 | 328 | ||
| 6 | /** | 329 | /** |
| 7 | * atomic_add_unless - add unless the number is already a given value | 330 | * atomic_add_unless - add unless the number is already a given value |
| @@ -28,6 +351,23 @@ static inline int atomic_add_unless(atomic_t *v, int a, int u) | |||
| 28 | #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0) | 351 | #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0) |
| 29 | #endif | 352 | #endif |
| 30 | 353 | ||
| 354 | #ifndef atomic_andnot | ||
| 355 | static inline void atomic_andnot(int i, atomic_t *v) | ||
| 356 | { | ||
| 357 | atomic_and(~i, v); | ||
| 358 | } | ||
| 359 | #endif | ||
| 360 | |||
| 361 | static inline __deprecated void atomic_clear_mask(unsigned int mask, atomic_t *v) | ||
| 362 | { | ||
| 363 | atomic_andnot(mask, v); | ||
| 364 | } | ||
| 365 | |||
| 366 | static inline __deprecated void atomic_set_mask(unsigned int mask, atomic_t *v) | ||
| 367 | { | ||
| 368 | atomic_or(mask, v); | ||
| 369 | } | ||
| 370 | |||
| 31 | /** | 371 | /** |
| 32 | * atomic_inc_not_zero_hint - increment if not null | 372 | * atomic_inc_not_zero_hint - increment if not null |
| 33 | * @v: pointer of type atomic_t | 373 | * @v: pointer of type atomic_t |
| @@ -111,21 +451,16 @@ static inline int atomic_dec_if_positive(atomic_t *v) | |||
| 111 | } | 451 | } |
| 112 | #endif | 452 | #endif |
| 113 | 453 | ||
| 114 | #ifndef CONFIG_ARCH_HAS_ATOMIC_OR | ||
| 115 | static inline void atomic_or(int i, atomic_t *v) | ||
| 116 | { | ||
| 117 | int old; | ||
| 118 | int new; | ||
| 119 | |||
| 120 | do { | ||
| 121 | old = atomic_read(v); | ||
| 122 | new = old | i; | ||
| 123 | } while (atomic_cmpxchg(v, old, new) != old); | ||
| 124 | } | ||
| 125 | #endif /* #ifndef CONFIG_ARCH_HAS_ATOMIC_OR */ | ||
| 126 | |||
| 127 | #include <asm-generic/atomic-long.h> | 454 | #include <asm-generic/atomic-long.h> |
| 128 | #ifdef CONFIG_GENERIC_ATOMIC64 | 455 | #ifdef CONFIG_GENERIC_ATOMIC64 |
| 129 | #include <asm-generic/atomic64.h> | 456 | #include <asm-generic/atomic64.h> |
| 130 | #endif | 457 | #endif |
| 458 | |||
| 459 | #ifndef atomic64_andnot | ||
| 460 | static inline void atomic64_andnot(long long i, atomic64_t *v) | ||
| 461 | { | ||
| 462 | atomic64_and(~i, v); | ||
| 463 | } | ||
| 464 | #endif | ||
| 465 | |||
| 131 | #endif /* _LINUX_ATOMIC_H */ | 466 | #endif /* _LINUX_ATOMIC_H */ |
