1 #ifndef _ASM_X86_ATOMIC_64_H
2 #define _ASM_X86_ATOMIC_64_H
4 #include <linux/types.h>
5 #include <asm/alternative.h>
6 #include <asm/cmpxchg.h>
9 * Atomic operations that C can't guarantee us. Useful for
10 * resource counting etc..
13 #define ATOMIC_INIT(i) { (i) }
16 * atomic_read - read atomic variable
17 * @v: pointer of type atomic_t
19 * Atomically reads the value of @v.
21 static inline int atomic_read(const atomic_t *v)
27 * atomic_set - set atomic variable
28 * @v: pointer of type atomic_t
31 * Atomically sets the value of @v to @i.
33 static inline void atomic_set(atomic_t *v, int i)
39 * atomic_add - add integer to atomic variable
40 * @i: integer value to add
41 * @v: pointer of type atomic_t
43 * Atomically adds @i to @v.
45 static inline void atomic_add(int i, atomic_t *v)
47 asm volatile(LOCK_PREFIX "addl %1,%0"
49 : "ir" (i), "m" (v->counter));
53 * atomic_sub - subtract the atomic variable
54 * @i: integer value to subtract
55 * @v: pointer of type atomic_t
57 * Atomically subtracts @i from @v.
59 static inline void atomic_sub(int i, atomic_t *v)
61 asm volatile(LOCK_PREFIX "subl %1,%0"
63 : "ir" (i), "m" (v->counter));
67 * atomic_sub_and_test - subtract value from variable and test result
68 * @i: integer value to subtract
69 * @v: pointer of type atomic_t
71 * Atomically subtracts @i from @v and returns
72 * true if the result is zero, or false for all
75 static inline int atomic_sub_and_test(int i, atomic_t *v)
79 asm volatile(LOCK_PREFIX "subl %2,%0; sete %1"
80 : "=m" (v->counter), "=qm" (c)
81 : "ir" (i), "m" (v->counter) : "memory");
86 * atomic_inc - increment atomic variable
87 * @v: pointer of type atomic_t
89 * Atomically increments @v by 1.
91 static inline void atomic_inc(atomic_t *v)
93 asm volatile(LOCK_PREFIX "incl %0"
99 * atomic_dec - decrement atomic variable
100 * @v: pointer of type atomic_t
102 * Atomically decrements @v by 1.
104 static inline void atomic_dec(atomic_t *v)
106 asm volatile(LOCK_PREFIX "decl %0"
112 * atomic_dec_and_test - decrement and test
113 * @v: pointer of type atomic_t
115 * Atomically decrements @v by 1 and
116 * returns true if the result is 0, or false for all other
119 static inline int atomic_dec_and_test(atomic_t *v)
123 asm volatile(LOCK_PREFIX "decl %0; sete %1"
124 : "=m" (v->counter), "=qm" (c)
125 : "m" (v->counter) : "memory");
130 * atomic_inc_and_test - increment and test
131 * @v: pointer of type atomic_t
133 * Atomically increments @v by 1
134 * and returns true if the result is zero, or false for all
137 static inline int atomic_inc_and_test(atomic_t *v)
141 asm volatile(LOCK_PREFIX "incl %0; sete %1"
142 : "=m" (v->counter), "=qm" (c)
143 : "m" (v->counter) : "memory");
148 * atomic_add_negative - add and test if negative
149 * @i: integer value to add
150 * @v: pointer of type atomic_t
152 * Atomically adds @i to @v and returns true
153 * if the result is negative, or false when
154 * result is greater than or equal to zero.
156 static inline int atomic_add_negative(int i, atomic_t *v)
160 asm volatile(LOCK_PREFIX "addl %2,%0; sets %1"
161 : "=m" (v->counter), "=qm" (c)
162 : "ir" (i), "m" (v->counter) : "memory");
167 * atomic_add_return - add and return
168 * @i: integer value to add
169 * @v: pointer of type atomic_t
171 * Atomically adds @i to @v and returns @i + @v
173 static inline int atomic_add_return(int i, atomic_t *v)
176 asm volatile(LOCK_PREFIX "xaddl %0, %1"
177 : "+r" (i), "+m" (v->counter)
182 static inline int atomic_sub_return(int i, atomic_t *v)
184 return atomic_add_return(-i, v);
187 #define atomic_inc_return(v) (atomic_add_return(1, v))
188 #define atomic_dec_return(v) (atomic_sub_return(1, v))
190 /* The 64-bit atomic type */
192 #define ATOMIC64_INIT(i) { (i) }
195 * atomic64_read - read atomic64 variable
196 * @v: pointer of type atomic64_t
198 * Atomically reads the value of @v.
199 * Doesn't imply a read memory barrier.
201 static inline long atomic64_read(const atomic64_t *v)
207 * atomic64_set - set atomic64 variable
208 * @v: pointer to type atomic64_t
211 * Atomically sets the value of @v to @i.
213 static inline void atomic64_set(atomic64_t *v, long i)
219 * atomic64_add - add integer to atomic64 variable
220 * @i: integer value to add
221 * @v: pointer to type atomic64_t
223 * Atomically adds @i to @v.
225 static inline void atomic64_add(long i, atomic64_t *v)
227 asm volatile(LOCK_PREFIX "addq %1,%0"
229 : "er" (i), "m" (v->counter));
233 * atomic64_sub - subtract the atomic64 variable
234 * @i: integer value to subtract
235 * @v: pointer to type atomic64_t
237 * Atomically subtracts @i from @v.
239 static inline void atomic64_sub(long i, atomic64_t *v)
241 asm volatile(LOCK_PREFIX "subq %1,%0"
243 : "er" (i), "m" (v->counter));
247 * atomic64_sub_and_test - subtract value from variable and test result
248 * @i: integer value to subtract
249 * @v: pointer to type atomic64_t
251 * Atomically subtracts @i from @v and returns
252 * true if the result is zero, or false for all
255 static inline int atomic64_sub_and_test(long i, atomic64_t *v)
259 asm volatile(LOCK_PREFIX "subq %2,%0; sete %1"
260 : "=m" (v->counter), "=qm" (c)
261 : "er" (i), "m" (v->counter) : "memory");
266 * atomic64_inc - increment atomic64 variable
267 * @v: pointer to type atomic64_t
269 * Atomically increments @v by 1.
271 static inline void atomic64_inc(atomic64_t *v)
273 asm volatile(LOCK_PREFIX "incq %0"
279 * atomic64_dec - decrement atomic64 variable
280 * @v: pointer to type atomic64_t
282 * Atomically decrements @v by 1.
284 static inline void atomic64_dec(atomic64_t *v)
286 asm volatile(LOCK_PREFIX "decq %0"
292 * atomic64_dec_and_test - decrement and test
293 * @v: pointer to type atomic64_t
295 * Atomically decrements @v by 1 and
296 * returns true if the result is 0, or false for all other
299 static inline int atomic64_dec_and_test(atomic64_t *v)
303 asm volatile(LOCK_PREFIX "decq %0; sete %1"
304 : "=m" (v->counter), "=qm" (c)
305 : "m" (v->counter) : "memory");
310 * atomic64_inc_and_test - increment and test
311 * @v: pointer to type atomic64_t
313 * Atomically increments @v by 1
314 * and returns true if the result is zero, or false for all
317 static inline int atomic64_inc_and_test(atomic64_t *v)
321 asm volatile(LOCK_PREFIX "incq %0; sete %1"
322 : "=m" (v->counter), "=qm" (c)
323 : "m" (v->counter) : "memory");
328 * atomic64_add_negative - add and test if negative
329 * @i: integer value to add
330 * @v: pointer to type atomic64_t
332 * Atomically adds @i to @v and returns true
333 * if the result is negative, or false when
334 * result is greater than or equal to zero.
336 static inline int atomic64_add_negative(long i, atomic64_t *v)
340 asm volatile(LOCK_PREFIX "addq %2,%0; sets %1"
341 : "=m" (v->counter), "=qm" (c)
342 : "er" (i), "m" (v->counter) : "memory");
347 * atomic64_add_return - add and return
348 * @i: integer value to add
349 * @v: pointer to type atomic64_t
351 * Atomically adds @i to @v and returns @i + @v
353 static inline long atomic64_add_return(long i, atomic64_t *v)
356 asm volatile(LOCK_PREFIX "xaddq %0, %1;"
357 : "+r" (i), "+m" (v->counter)
362 static inline long atomic64_sub_return(long i, atomic64_t *v)
364 return atomic64_add_return(-i, v);
367 #define atomic64_inc_return(v) (atomic64_add_return(1, (v)))
368 #define atomic64_dec_return(v) (atomic64_sub_return(1, (v)))
370 static inline long atomic64_cmpxchg(atomic64_t *v, long old, long new)
372 return cmpxchg(&v->counter, old, new);
375 static inline long atomic64_xchg(atomic64_t *v, long new)
377 return xchg(&v->counter, new);
380 static inline long atomic_cmpxchg(atomic_t *v, int old, int new)
382 return cmpxchg(&v->counter, old, new);
385 static inline long atomic_xchg(atomic_t *v, int new)
387 return xchg(&v->counter, new);
391 * atomic_add_unless - add unless the number is a given value
392 * @v: pointer of type atomic_t
393 * @a: the amount to add to v...
394 * @u: ...unless v is equal to u.
396 * Atomically adds @a to @v, so long as it was not @u.
397 * Returns non-zero if @v was not @u, and zero otherwise.
399 static inline int atomic_add_unless(atomic_t *v, int a, int u)
404 if (unlikely(c == (u)))
406 old = atomic_cmpxchg((v), c, c + (a));
407 if (likely(old == c))
414 #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
417 * atomic64_add_unless - add unless the number is a given value
418 * @v: pointer of type atomic64_t
419 * @a: the amount to add to v...
420 * @u: ...unless v is equal to u.
422 * Atomically adds @a to @v, so long as it was not @u.
423 * Returns non-zero if @v was not @u, and zero otherwise.
425 static inline int atomic64_add_unless(atomic64_t *v, long a, long u)
428 c = atomic64_read(v);
430 if (unlikely(c == (u)))
432 old = atomic64_cmpxchg((v), c, c + (a));
433 if (likely(old == c))
441 * atomic_inc_short - increment of a short integer
442 * @v: pointer to type int
444 * Atomically adds 1 to @v
445 * Returns the new value of @u
447 static inline short int atomic_inc_short(short int *v)
449 asm(LOCK_PREFIX "addw $1, %0" : "+m" (*v));
454 * atomic_or_long - OR of two long integers
455 * @v1: pointer to type unsigned long
456 * @v2: pointer to type unsigned long
458 * Atomically ORs @v1 and @v2
459 * Returns the result of the OR
461 static inline void atomic_or_long(unsigned long *v1, unsigned long v2)
463 asm(LOCK_PREFIX "orq %1, %0" : "+m" (*v1) : "r" (v2));
466 #define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1, 0)
468 /* These are x86-specific, used by some header files */
469 #define atomic_clear_mask(mask, addr) \
470 asm volatile(LOCK_PREFIX "andl %0,%1" \
471 : : "r" (~(mask)), "m" (*(addr)) : "memory")
473 #define atomic_set_mask(mask, addr) \
474 asm volatile(LOCK_PREFIX "orl %0,%1" \
475 : : "r" ((unsigned)(mask)), "m" (*(addr)) \
478 /* Atomic operations are already serializing on x86 */
479 #define smp_mb__before_atomic_dec() barrier()
480 #define smp_mb__after_atomic_dec() barrier()
481 #define smp_mb__before_atomic_inc() barrier()
482 #define smp_mb__after_atomic_inc() barrier()
484 #include <asm-generic/atomic-long.h>
485 #endif /* _ASM_X86_ATOMIC_64_H */