1 #ifndef __ARCH_I386_ATOMIC__
2 #define __ARCH_I386_ATOMIC__
4 #include <linux/config.h>
5 #include <linux/compiler.h>
6 #include <asm/processor.h>
9 * Atomic operations that C can't guarantee us. Useful for
10 * resource counting etc..
14 #define LOCK "lock ; "
20 * Make sure gcc doesn't try to be clever and move things around
21 * on us. We need to use _exactly_ the address the user gave us,
22 * not some alias that contains the same information.
24 typedef struct { volatile int counter; } atomic_t;
26 #define ATOMIC_INIT(i) { (i) }
29 * atomic_read - read atomic variable
30 * @v: pointer of type atomic_t
32 * Atomically reads the value of @v.
34 #define atomic_read(v) ((v)->counter)
37 * atomic_set - set atomic variable
38 * @v: pointer of type atomic_t
41 * Atomically sets the value of @v to @i.
43 #define atomic_set(v,i) (((v)->counter) = (i))
46 * atomic_add - add integer to atomic variable
47 * @i: integer value to add
48 * @v: pointer of type atomic_t
50 * Atomically adds @i to @v.
52 static __inline__ void atomic_add(int i, atomic_t *v)
57 :"ir" (i), "m" (v->counter));
61 * atomic_sub - subtract the atomic variable
62 * @i: integer value to subtract
63 * @v: pointer of type atomic_t
65 * Atomically subtracts @i from @v.
67 static __inline__ void atomic_sub(int i, atomic_t *v)
72 :"ir" (i), "m" (v->counter));
76 * atomic_sub_and_test - subtract value from variable and test result
77 * @i: integer value to subtract
78 * @v: pointer of type atomic_t
80 * Atomically subtracts @i from @v and returns
81 * true if the result is zero, or false for all
84 static __inline__ int atomic_sub_and_test(int i, atomic_t *v)
89 LOCK "subl %2,%0; sete %1"
90 :"=m" (v->counter), "=qm" (c)
91 :"ir" (i), "m" (v->counter) : "memory");
96 * atomic_inc - increment atomic variable
97 * @v: pointer of type atomic_t
99 * Atomically increments @v by 1.
101 static __inline__ void atomic_inc(atomic_t *v)
103 __asm__ __volatile__(
110 * atomic_dec - decrement atomic variable
111 * @v: pointer of type atomic_t
113 * Atomically decrements @v by 1.
115 static __inline__ void atomic_dec(atomic_t *v)
117 __asm__ __volatile__(
124 * atomic_dec_and_test - decrement and test
125 * @v: pointer of type atomic_t
127 * Atomically decrements @v by 1 and
128 * returns true if the result is 0, or false for all other
131 static __inline__ int atomic_dec_and_test(atomic_t *v)
135 __asm__ __volatile__(
136 LOCK "decl %0; sete %1"
137 :"=m" (v->counter), "=qm" (c)
138 :"m" (v->counter) : "memory");
143 * atomic_inc_and_test - increment and test
144 * @v: pointer of type atomic_t
146 * Atomically increments @v by 1
147 * and returns true if the result is zero, or false for all
150 static __inline__ int atomic_inc_and_test(atomic_t *v)
154 __asm__ __volatile__(
155 LOCK "incl %0; sete %1"
156 :"=m" (v->counter), "=qm" (c)
157 :"m" (v->counter) : "memory");
162 * atomic_add_negative - add and test if negative
163 * @v: pointer of type atomic_t
164 * @i: integer value to add
166 * Atomically adds @i to @v and returns true
167 * if the result is negative, or false when
168 * result is greater than or equal to zero.
170 static __inline__ int atomic_add_negative(int i, atomic_t *v)
174 __asm__ __volatile__(
175 LOCK "addl %2,%0; sets %1"
176 :"=m" (v->counter), "=qm" (c)
177 :"ir" (i), "m" (v->counter) : "memory");
182 * atomic_add_return - add and return
183 * @v: pointer of type atomic_t
184 * @i: integer value to add
186 * Atomically adds @i to @v and returns @i + @v
188 static __inline__ int atomic_add_return(int i, atomic_t *v)
192 if(unlikely(boot_cpu_data.x86==3))
195 /* Modern 486+ processor */
197 __asm__ __volatile__(
200 :"m"(v->counter), "0"(i));
204 no_xadd: /* Legacy 386 processor */
206 __i = atomic_read(v);
207 atomic_set(v, i + __i);
213 static __inline__ int atomic_sub_return(int i, atomic_t *v)
215 return atomic_add_return(-i,v);
218 #define atomic_cmpxchg(v, old, new) ((int)cmpxchg(&((v)->counter), old, new))
221 * atomic_add_unless - add unless the number is a given value
222 * @v: pointer of type atomic_t
223 * @a: the amount to add to v...
224 * @u: ...unless v is equal to u.
226 * Atomically adds @a to @v, so long as it was not @u.
227 * Returns non-zero if @v was not @u, and zero otherwise.
229 #define atomic_add_unless(v, a, u) \
232 c = atomic_read(v); \
233 while (c != (u) && (old = atomic_cmpxchg((v), c, c + (a))) != c) \
237 #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
239 #define atomic_inc_return(v) (atomic_add_return(1,v))
240 #define atomic_dec_return(v) (atomic_sub_return(1,v))
242 /* These are x86-specific, used by some header files */
243 #define atomic_clear_mask(mask, addr) \
244 __asm__ __volatile__(LOCK "andl %0,%1" \
245 : : "r" (~(mask)),"m" (*addr) : "memory")
247 #define atomic_set_mask(mask, addr) \
248 __asm__ __volatile__(LOCK "orl %0,%1" \
249 : : "r" (mask),"m" (*(addr)) : "memory")
251 /* Atomic operations are already serializing on x86 */
252 #define smp_mb__before_atomic_dec() barrier()
253 #define smp_mb__after_atomic_dec() barrier()
254 #define smp_mb__before_atomic_inc() barrier()
255 #define smp_mb__after_atomic_inc() barrier()
257 #include <asm-generic/atomic.h>