1 #ifndef _ASM_X86_ATOMIC_32_H
2 #define _ASM_X86_ATOMIC_32_H
4 #include <linux/compiler.h>
5 #include <asm/processor.h>
6 #include <asm/cmpxchg.h>
9 * Atomic operations that C can't guarantee us. Useful for
10 * resource counting etc..
14 * Make sure gcc doesn't try to be clever and move things around
15 * on us. We need to use _exactly_ the address the user gave us,
16 * not some alias that contains the same information.
22 #define ATOMIC_INIT(i) { (i) }
25 * atomic_read - read atomic variable
26 * @v: pointer of type atomic_t
28 * Atomically reads the value of @v.
30 #define atomic_read(v) ((v)->counter)
33 * atomic_set - set atomic variable
34 * @v: pointer of type atomic_t
37 * Atomically sets the value of @v to @i.
39 #define atomic_set(v, i) (((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 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 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 inline int atomic_sub_and_test(int i, atomic_t *v)
82 asm volatile(LOCK_PREFIX "subl %2,%0; sete %1"
83 : "+m" (v->counter), "=qm" (c)
84 : "ir" (i) : "memory");
89 * atomic_inc - increment atomic variable
90 * @v: pointer of type atomic_t
92 * Atomically increments @v by 1.
94 static inline void atomic_inc(atomic_t *v)
96 asm volatile(LOCK_PREFIX "incl %0"
101 * atomic_dec - decrement atomic variable
102 * @v: pointer of type atomic_t
104 * Atomically decrements @v by 1.
106 static inline void atomic_dec(atomic_t *v)
108 asm volatile(LOCK_PREFIX "decl %0"
109 : "+m" (v->counter));
113 * atomic_dec_and_test - decrement and test
114 * @v: pointer of type atomic_t
116 * Atomically decrements @v by 1 and
117 * returns true if the result is 0, or false for all other
120 static inline int atomic_dec_and_test(atomic_t *v)
124 asm volatile(LOCK_PREFIX "decl %0; sete %1"
125 : "+m" (v->counter), "=qm" (c)
131 * atomic_inc_and_test - increment and test
132 * @v: pointer of type atomic_t
134 * Atomically increments @v by 1
135 * and returns true if the result is zero, or false for all
138 static inline int atomic_inc_and_test(atomic_t *v)
142 asm volatile(LOCK_PREFIX "incl %0; sete %1"
143 : "+m" (v->counter), "=qm" (c)
149 * atomic_add_negative - add and test if negative
150 * @v: pointer of type atomic_t
151 * @i: integer value to add
153 * Atomically adds @i to @v and returns true
154 * if the result is negative, or false when
155 * result is greater than or equal to zero.
157 static inline int atomic_add_negative(int i, atomic_t *v)
161 asm volatile(LOCK_PREFIX "addl %2,%0; sets %1"
162 : "+m" (v->counter), "=qm" (c)
163 : "ir" (i) : "memory");
168 * atomic_add_return - add integer and return
169 * @v: pointer of type atomic_t
170 * @i: integer value to add
172 * Atomically adds @i to @v and returns @i + @v
174 static inline int atomic_add_return(int i, atomic_t *v)
179 if (unlikely(boot_cpu_data.x86 <= 3))
182 /* Modern 486+ processor */
184 asm volatile(LOCK_PREFIX "xaddl %0, %1"
185 : "+r" (i), "+m" (v->counter)
190 no_xadd: /* Legacy 386 processor */
191 local_irq_save(flags);
192 __i = atomic_read(v);
193 atomic_set(v, i + __i);
194 local_irq_restore(flags);
200 * atomic_sub_return - subtract integer and return
201 * @v: pointer of type atomic_t
202 * @i: integer value to subtract
204 * Atomically subtracts @i from @v and returns @v - @i
206 static inline int atomic_sub_return(int i, atomic_t *v)
208 return atomic_add_return(-i, v);
211 #define atomic_cmpxchg(v, old, new) (cmpxchg(&((v)->counter), (old), (new)))
212 #define atomic_xchg(v, new) (xchg(&((v)->counter), (new)))
215 * atomic_add_unless - add unless the number is already a given value
216 * @v: pointer of type atomic_t
217 * @a: the amount to add to v...
218 * @u: ...unless v is equal to u.
220 * Atomically adds @a to @v, so long as @v was not already @u.
221 * Returns non-zero if @v was not @u, and zero otherwise.
223 static inline int atomic_add_unless(atomic_t *v, int a, int u)
228 if (unlikely(c == (u)))
230 old = atomic_cmpxchg((v), c, c + (a));
231 if (likely(old == c))
238 #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
240 #define atomic_inc_return(v) (atomic_add_return(1, v))
241 #define atomic_dec_return(v) (atomic_sub_return(1, v))
243 /* These are x86-specific, used by some header files */
244 #define atomic_clear_mask(mask, addr) \
245 asm volatile(LOCK_PREFIX "andl %0,%1" \
246 : : "r" (~(mask)), "m" (*(addr)) : "memory")
248 #define atomic_set_mask(mask, addr) \
249 asm volatile(LOCK_PREFIX "orl %0,%1" \
250 : : "r" (mask), "m" (*(addr)) : "memory")
252 /* Atomic operations are already serializing on x86 */
253 #define smp_mb__before_atomic_dec() barrier()
254 #define smp_mb__after_atomic_dec() barrier()
255 #define smp_mb__before_atomic_inc() barrier()
256 #define smp_mb__after_atomic_inc() barrier()
258 /* An 64bit atomic type */
261 unsigned long long counter;
264 #define ATOMIC64_INIT(val) { (val) }
267 * atomic64_read - read atomic64 variable
268 * @v: pointer of type atomic64_t
270 * Atomically reads the value of @v.
271 * Doesn't imply a read memory barrier.
273 #define __atomic64_read(ptr) ((ptr)->counter)
275 static inline unsigned long long
276 cmpxchg8b(unsigned long long *ptr, unsigned long long old, unsigned long long new)
280 LOCK_PREFIX "cmpxchg8b (%[ptr])\n"
294 static inline unsigned long long
295 atomic64_cmpxchg(atomic64_t *ptr, unsigned long long old_val,
296 unsigned long long new_val)
298 return cmpxchg8b(&ptr->counter, old_val, new_val);
302 * atomic64_set - set atomic64 variable
303 * @ptr: pointer to type atomic64_t
304 * @new_val: value to assign
306 * Atomically sets the value of @ptr to @new_val.
308 static inline void atomic64_set(atomic64_t *ptr, unsigned long long new_val)
310 unsigned long long old_val;
313 old_val = atomic_read(ptr);
314 } while (atomic64_cmpxchg(ptr, old_val, new_val) != old_val);
318 * atomic64_read - read atomic64 variable
319 * @ptr: pointer to type atomic64_t
321 * Atomically reads the value of @ptr and returns it.
323 static inline unsigned long long atomic64_read(atomic64_t *ptr)
325 unsigned long long curr_val;
328 curr_val = __atomic64_read(ptr);
329 } while (atomic64_cmpxchg(ptr, curr_val, curr_val) != curr_val);
335 * atomic64_add_return - add and return
336 * @delta: integer value to add
337 * @ptr: pointer to type atomic64_t
339 * Atomically adds @delta to @ptr and returns @delta + *@ptr
341 static inline unsigned long long
342 atomic64_add_return(unsigned long long delta, atomic64_t *ptr)
344 unsigned long long old_val, new_val;
347 old_val = atomic_read(ptr);
348 new_val = old_val + delta;
350 } while (atomic64_cmpxchg(ptr, old_val, new_val) != old_val);
355 static inline long atomic64_sub_return(unsigned long long delta, atomic64_t *ptr)
357 return atomic64_add_return(-delta, ptr);
360 static inline long atomic64_inc_return(atomic64_t *ptr)
362 return atomic64_add_return(1, ptr);
365 static inline long atomic64_dec_return(atomic64_t *ptr)
367 return atomic64_sub_return(1, ptr);
371 * atomic64_add - add integer to atomic64 variable
372 * @delta: integer value to add
373 * @ptr: pointer to type atomic64_t
375 * Atomically adds @delta to @ptr.
377 static inline void atomic64_add(unsigned long long delta, atomic64_t *ptr)
379 atomic64_add_return(delta, ptr);
383 * atomic64_sub - subtract the atomic64 variable
384 * @delta: integer value to subtract
385 * @ptr: pointer to type atomic64_t
387 * Atomically subtracts @delta from @ptr.
389 static inline void atomic64_sub(unsigned long long delta, atomic64_t *ptr)
391 atomic64_add(-delta, ptr);
395 * atomic64_sub_and_test - subtract value from variable and test result
396 * @delta: integer value to subtract
397 * @ptr: pointer to type atomic64_t
399 * Atomically subtracts @delta from @ptr and returns
400 * true if the result is zero, or false for all
404 atomic64_sub_and_test(unsigned long long delta, atomic64_t *ptr)
406 unsigned long long old_val = atomic64_sub_return(delta, ptr);
412 * atomic64_inc - increment atomic64 variable
413 * @ptr: pointer to type atomic64_t
415 * Atomically increments @ptr by 1.
417 static inline void atomic64_inc(atomic64_t *ptr)
419 atomic64_add(1, ptr);
423 * atomic64_dec - decrement atomic64 variable
424 * @ptr: pointer to type atomic64_t
426 * Atomically decrements @ptr by 1.
428 static inline void atomic64_dec(atomic64_t *ptr)
430 atomic64_sub(1, ptr);
434 * atomic64_dec_and_test - decrement and test
435 * @ptr: pointer to type atomic64_t
437 * Atomically decrements @ptr by 1 and
438 * returns true if the result is 0, or false for all other
441 static inline int atomic64_dec_and_test(atomic64_t *ptr)
443 return atomic64_sub_and_test(1, ptr);
447 * atomic64_inc_and_test - increment and test
448 * @ptr: pointer to type atomic64_t
450 * Atomically increments @ptr by 1
451 * and returns true if the result is zero, or false for all
454 static inline int atomic64_inc_and_test(atomic64_t *ptr)
456 return atomic64_sub_and_test(-1, ptr);
460 * atomic64_add_negative - add and test if negative
461 * @delta: integer value to add
462 * @ptr: pointer to type atomic64_t
464 * Atomically adds @delta to @ptr and returns true
465 * if the result is negative, or false when
466 * result is greater than or equal to zero.
469 atomic64_add_negative(unsigned long long delta, atomic64_t *ptr)
471 long long old_val = atomic64_add_return(delta, ptr);
476 #include <asm-generic/atomic.h>
477 #endif /* _ASM_X86_ATOMIC_32_H */