]>
git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - arch/x86/include/asm/atomic.h
1 #ifndef _ASM_X86_ATOMIC_H
2 #define _ASM_X86_ATOMIC_H
4 #include <linux/compiler.h>
5 #include <linux/types.h>
6 #include <asm/alternative.h>
7 #include <asm/cmpxchg.h>
9 #include <asm/barrier.h>
12 * Atomic operations that C can't guarantee us. Useful for
13 * resource counting etc..
16 #define ATOMIC_INIT(i) { (i) }
19 * atomic_read - read atomic variable
20 * @v: pointer of type atomic_t
22 * Atomically reads the value of @v.
24 static __always_inline
int atomic_read(const atomic_t
*v
)
26 return READ_ONCE((v
)->counter
);
30 * atomic_set - set atomic variable
31 * @v: pointer of type atomic_t
34 * Atomically sets the value of @v to @i.
36 static __always_inline
void atomic_set(atomic_t
*v
, int i
)
38 WRITE_ONCE(v
->counter
, i
);
42 * atomic_add - add integer to atomic variable
43 * @i: integer value to add
44 * @v: pointer of type atomic_t
46 * Atomically adds @i to @v.
48 static __always_inline
void atomic_add(int i
, atomic_t
*v
)
50 asm volatile(LOCK_PREFIX
"addl %1,%0"
56 * atomic_sub - subtract integer from atomic variable
57 * @i: integer value to subtract
58 * @v: pointer of type atomic_t
60 * Atomically subtracts @i from @v.
62 static __always_inline
void atomic_sub(int i
, atomic_t
*v
)
64 asm volatile(LOCK_PREFIX
"subl %1,%0"
70 * atomic_sub_and_test - subtract value from variable and test result
71 * @i: integer value to subtract
72 * @v: pointer of type atomic_t
74 * Atomically subtracts @i from @v and returns
75 * true if the result is zero, or false for all
78 static __always_inline
bool atomic_sub_and_test(int i
, atomic_t
*v
)
80 GEN_BINARY_RMWcc(LOCK_PREFIX
"subl", v
->counter
, "er", i
, "%0", e
);
84 * atomic_inc - increment atomic variable
85 * @v: pointer of type atomic_t
87 * Atomically increments @v by 1.
89 static __always_inline
void atomic_inc(atomic_t
*v
)
91 asm volatile(LOCK_PREFIX
"incl %0"
96 * atomic_dec - decrement atomic variable
97 * @v: pointer of type atomic_t
99 * Atomically decrements @v by 1.
101 static __always_inline
void atomic_dec(atomic_t
*v
)
103 asm volatile(LOCK_PREFIX
"decl %0"
104 : "+m" (v
->counter
));
108 * atomic_dec_and_test - decrement and test
109 * @v: pointer of type atomic_t
111 * Atomically decrements @v by 1 and
112 * returns true if the result is 0, or false for all other
115 static __always_inline
bool atomic_dec_and_test(atomic_t
*v
)
117 GEN_UNARY_RMWcc(LOCK_PREFIX
"decl", v
->counter
, "%0", e
);
121 * atomic_inc_and_test - increment and test
122 * @v: pointer of type atomic_t
124 * Atomically increments @v by 1
125 * and returns true if the result is zero, or false for all
128 static __always_inline
bool atomic_inc_and_test(atomic_t
*v
)
130 GEN_UNARY_RMWcc(LOCK_PREFIX
"incl", v
->counter
, "%0", e
);
134 * atomic_add_negative - add and test if negative
135 * @i: integer value to add
136 * @v: pointer of type atomic_t
138 * Atomically adds @i to @v and returns true
139 * if the result is negative, or false when
140 * result is greater than or equal to zero.
142 static __always_inline
bool atomic_add_negative(int i
, atomic_t
*v
)
144 GEN_BINARY_RMWcc(LOCK_PREFIX
"addl", v
->counter
, "er", i
, "%0", s
);
148 * atomic_add_return - add integer and return
149 * @i: integer value to add
150 * @v: pointer of type atomic_t
152 * Atomically adds @i to @v and returns @i + @v
154 static __always_inline
int atomic_add_return(int i
, atomic_t
*v
)
156 return i
+ xadd(&v
->counter
, i
);
160 * atomic_sub_return - subtract integer and return
161 * @v: pointer of type atomic_t
162 * @i: integer value to subtract
164 * Atomically subtracts @i from @v and returns @v - @i
166 static __always_inline
int atomic_sub_return(int i
, atomic_t
*v
)
168 return atomic_add_return(-i
, v
);
171 #define atomic_inc_return(v) (atomic_add_return(1, v))
172 #define atomic_dec_return(v) (atomic_sub_return(1, v))
174 static __always_inline
int atomic_fetch_add(int i
, atomic_t
*v
)
176 return xadd(&v
->counter
, i
);
179 static __always_inline
int atomic_fetch_sub(int i
, atomic_t
*v
)
181 return xadd(&v
->counter
, -i
);
184 static __always_inline
int atomic_cmpxchg(atomic_t
*v
, int old
, int new)
186 return cmpxchg(&v
->counter
, old
, new);
189 #define atomic_try_cmpxchg atomic_try_cmpxchg
190 static __always_inline
bool atomic_try_cmpxchg(atomic_t
*v
, int *old
, int new)
192 return try_cmpxchg(&v
->counter
, old
, new);
195 static inline int atomic_xchg(atomic_t
*v
, int new)
197 return xchg(&v
->counter
, new);
200 #define ATOMIC_OP(op) \
201 static inline void atomic_##op(int i, atomic_t *v) \
203 asm volatile(LOCK_PREFIX #op"l %1,%0" \
204 : "+m" (v->counter) \
209 #define ATOMIC_FETCH_OP(op, c_op) \
210 static inline int atomic_fetch_##op(int i, atomic_t *v) \
212 int val = atomic_read(v); \
214 } while (!atomic_try_cmpxchg(v, &val, val c_op i)); \
218 #define ATOMIC_OPS(op, c_op) \
220 ATOMIC_FETCH_OP(op, c_op)
227 #undef ATOMIC_FETCH_OP
231 * __atomic_add_unless - add unless the number is already a given value
232 * @v: pointer of type atomic_t
233 * @a: the amount to add to v...
234 * @u: ...unless v is equal to u.
236 * Atomically adds @a to @v, so long as @v was not already @u.
237 * Returns the old value of @v.
239 static __always_inline
int __atomic_add_unless(atomic_t
*v
, int a
, int u
)
241 int c
= atomic_read(v
);
243 if (unlikely(c
== u
))
245 } while (!atomic_try_cmpxchg(v
, &c
, c
+ a
));
250 # include <asm/atomic64_32.h>
252 # include <asm/atomic64_64.h>
255 #endif /* _ASM_X86_ATOMIC_H */