blob: d5e167a6a398366ad0dcb46aaacf13cf8e2b4371 [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 Sharmaf24219b2011-07-26 16:09:07 -07006 * 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 */
14static 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 Sharma60063492011-07-26 16:09:06 -070020 * 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 Dumazet3f9d35b2010-11-11 14:05:08 -080029 * 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
41static 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 Viro07b8ce12011-06-20 10:52:57 -040060#ifndef atomic_inc_unless_negative
61static 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
74static 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. McKenney55c29452011-05-11 05:33:33 -070086#ifndef CONFIG_ARCH_HAS_ATOMIC_OR
87static 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 Dumazet3f9d35b2010-11-11 14:05:08 -080099#endif /* _LINUX_ATOMIC_H */