blob: 66fed6364122b2f47b78b1cd5fef0fd2f1f53b33 [file] [log] [blame]
Eric Dumazet3f9d35b2010-11-11 14:05:08 -08001#ifndef _LINUX_ATOMIC_H
2#define _LINUX_ATOMIC_H
3#include <asm/atomic.h>
4
5/**
Arun Sharma60063492011-07-26 16:09:06 -07006 * 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 Dumazet3f9d35b2010-11-11 14:05:08 -080015 * 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
27static 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 Viro07b8ce12011-06-20 10:52:57 -040046#ifndef atomic_inc_unless_negative
47static 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
60static 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. McKenney55c29452011-05-11 05:33:33 -070072#ifndef CONFIG_ARCH_HAS_ATOMIC_OR
73static 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 Dumazet3f9d35b2010-11-11 14:05:08 -080085#endif /* _LINUX_ATOMIC_H */