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