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