Arun Sharma | acac43e | 2011-07-26 16:09:08 -0700 | [diff] [blame] | 1 | /* Atomic operations usable in machine independent code */ |
Eric Dumazet | 3f9d35b | 2010-11-11 14:05:08 -0800 | [diff] [blame] | 2 | #ifndef _LINUX_ATOMIC_H |
| 3 | #define _LINUX_ATOMIC_H |
| 4 | #include <asm/atomic.h> |
| 5 | |
| 6 | /** |
Arun Sharma | f24219b | 2011-07-26 16:09:07 -0700 | [diff] [blame] | 7 | * atomic_add_unless - add unless the number is already a given value |
| 8 | * @v: pointer of type atomic_t |
| 9 | * @a: the amount to add to v... |
| 10 | * @u: ...unless v is equal to u. |
| 11 | * |
| 12 | * Atomically adds @a to @v, so long as @v was not already @u. |
| 13 | * Returns non-zero if @v was not @u, and zero otherwise. |
| 14 | */ |
| 15 | static inline int atomic_add_unless(atomic_t *v, int a, int u) |
| 16 | { |
| 17 | return __atomic_add_unless(v, a, u) != u; |
| 18 | } |
| 19 | |
| 20 | /** |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 21 | * atomic_inc_not_zero - increment unless the number is zero |
| 22 | * @v: pointer of type atomic_t |
| 23 | * |
| 24 | * Atomically increments @v by 1, so long as @v is non-zero. |
| 25 | * Returns non-zero if @v was non-zero, and zero otherwise. |
| 26 | */ |
Anton Blanchard | b1ada60 | 2012-02-29 21:09:53 +0000 | [diff] [blame^] | 27 | #ifndef atomic_inc_not_zero |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 28 | #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0) |
Anton Blanchard | b1ada60 | 2012-02-29 21:09:53 +0000 | [diff] [blame^] | 29 | #endif |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 30 | |
| 31 | /** |
Eric Dumazet | 3f9d35b | 2010-11-11 14:05:08 -0800 | [diff] [blame] | 32 | * atomic_inc_not_zero_hint - increment if not null |
| 33 | * @v: pointer of type atomic_t |
| 34 | * @hint: probable value of the atomic before the increment |
| 35 | * |
| 36 | * This version of atomic_inc_not_zero() gives a hint of probable |
| 37 | * value of the atomic. This helps processor to not read the memory |
| 38 | * before doing the atomic read/modify/write cycle, lowering |
| 39 | * number of bus transactions on some arches. |
| 40 | * |
| 41 | * Returns: 0 if increment was not done, 1 otherwise. |
| 42 | */ |
| 43 | #ifndef atomic_inc_not_zero_hint |
| 44 | static inline int atomic_inc_not_zero_hint(atomic_t *v, int hint) |
| 45 | { |
| 46 | int val, c = hint; |
| 47 | |
| 48 | /* sanity test, should be removed by compiler if hint is a constant */ |
| 49 | if (!hint) |
| 50 | return atomic_inc_not_zero(v); |
| 51 | |
| 52 | do { |
| 53 | val = atomic_cmpxchg(v, c, c + 1); |
| 54 | if (val == c) |
| 55 | return 1; |
| 56 | c = val; |
| 57 | } while (c); |
| 58 | |
| 59 | return 0; |
| 60 | } |
| 61 | #endif |
| 62 | |
Al Viro | 07b8ce1 | 2011-06-20 10:52:57 -0400 | [diff] [blame] | 63 | #ifndef atomic_inc_unless_negative |
| 64 | static inline int atomic_inc_unless_negative(atomic_t *p) |
| 65 | { |
| 66 | int v, v1; |
| 67 | for (v = 0; v >= 0; v = v1) { |
| 68 | v1 = atomic_cmpxchg(p, v, v + 1); |
| 69 | if (likely(v1 == v)) |
| 70 | return 1; |
| 71 | } |
| 72 | return 0; |
| 73 | } |
| 74 | #endif |
| 75 | |
| 76 | #ifndef atomic_dec_unless_positive |
| 77 | static inline int atomic_dec_unless_positive(atomic_t *p) |
| 78 | { |
| 79 | int v, v1; |
| 80 | for (v = 0; v <= 0; v = v1) { |
| 81 | v1 = atomic_cmpxchg(p, v, v - 1); |
| 82 | if (likely(v1 == v)) |
| 83 | return 1; |
| 84 | } |
| 85 | return 0; |
| 86 | } |
| 87 | #endif |
| 88 | |
Paul E. McKenney | 55c2945 | 2011-05-11 05:33:33 -0700 | [diff] [blame] | 89 | #ifndef CONFIG_ARCH_HAS_ATOMIC_OR |
| 90 | static inline void atomic_or(int i, atomic_t *v) |
| 91 | { |
| 92 | int old; |
| 93 | int new; |
| 94 | |
| 95 | do { |
| 96 | old = atomic_read(v); |
| 97 | new = old | i; |
| 98 | } while (atomic_cmpxchg(v, old, new) != old); |
| 99 | } |
| 100 | #endif /* #ifndef CONFIG_ARCH_HAS_ATOMIC_OR */ |
| 101 | |
Arun Sharma | 7847777 | 2011-07-26 16:09:08 -0700 | [diff] [blame] | 102 | #include <asm-generic/atomic-long.h> |
| 103 | #ifdef CONFIG_GENERIC_ATOMIC64 |
| 104 | #include <asm-generic/atomic64.h> |
| 105 | #endif |
Eric Dumazet | 3f9d35b | 2010-11-11 14:05:08 -0800 | [diff] [blame] | 106 | #endif /* _LINUX_ATOMIC_H */ |