4 #include <linux/percpu.h>
6 #include <asm/system.h>
7 #include <asm/atomic.h>
15 #define LOCAL_INIT(i) { ATOMIC_LONG_INIT(i) }
17 #define local_read(l) atomic_long_read(&(l)->a)
18 #define local_set(l,i) atomic_long_set(&(l)->a, (i))
20 static inline void local_inc(local_t *l)
24 :"+m" (l->a.counter));
27 static inline void local_dec(local_t *l)
31 :"+m" (l->a.counter));
34 static inline void local_add(long i, local_t *l)
42 static inline void local_sub(long i, local_t *l)
51 * local_sub_and_test - subtract value from variable and test result
52 * @i: integer value to subtract
53 * @l: pointer to type local_t
55 * Atomically subtracts @i from @l and returns
56 * true if the result is zero, or false for all
59 static inline int local_sub_and_test(long i, local_t *l)
64 _ASM_SUB "%2,%0; sete %1"
65 :"+m" (l->a.counter), "=qm" (c)
66 :"ir" (i) : "memory");
71 * local_dec_and_test - decrement and test
72 * @l: pointer to type local_t
74 * Atomically decrements @l by 1 and
75 * returns true if the result is 0, or false for all other
78 static inline int local_dec_and_test(local_t *l)
83 _ASM_DEC "%0; sete %1"
84 :"+m" (l->a.counter), "=qm" (c)
90 * local_inc_and_test - increment and test
91 * @l: pointer to type local_t
93 * Atomically increments @l by 1
94 * and returns true if the result is zero, or false for all
97 static inline int local_inc_and_test(local_t *l)
101 __asm__ __volatile__(
102 _ASM_INC "%0; sete %1"
103 :"+m" (l->a.counter), "=qm" (c)
109 * local_add_negative - add and test if negative
110 * @i: integer value to add
111 * @l: pointer to type local_t
113 * Atomically adds @i to @l and returns true
114 * if the result is negative, or false when
115 * result is greater than or equal to zero.
117 static inline int local_add_negative(long i, local_t *l)
121 __asm__ __volatile__(
122 _ASM_ADD "%2,%0; sets %1"
123 :"+m" (l->a.counter), "=qm" (c)
124 :"ir" (i) : "memory");
129 * local_add_return - add and return
130 * @i: integer value to add
131 * @l: pointer to type local_t
133 * Atomically adds @i to @l and returns @i + @l
135 static inline long local_add_return(long i, local_t *l)
140 if(unlikely(boot_cpu_data.x86 <= 3))
143 /* Modern 486+ processor */
145 __asm__ __volatile__(
147 :"+r" (i), "+m" (l->a.counter)
152 no_xadd: /* Legacy 386 processor */
153 local_irq_save(flags);
155 local_set(l, i + __i);
156 local_irq_restore(flags);
161 static inline long local_sub_return(long i, local_t *l)
163 return local_add_return(-i,l);
166 #define local_inc_return(l) (local_add_return(1,l))
167 #define local_dec_return(l) (local_sub_return(1,l))
169 #define local_cmpxchg(l, o, n) \
170 (cmpxchg_local(&((l)->a.counter), (o), (n)))
171 /* Always has a lock prefix */
172 #define local_xchg(l, n) (xchg(&((l)->a.counter), (n)))
175 * local_add_unless - add unless the number is a given value
176 * @l: pointer of type local_t
177 * @a: the amount to add to l...
178 * @u: ...unless l is equal to u.
180 * Atomically adds @a to @l, so long as it was not @u.
181 * Returns non-zero if @l was not @u, and zero otherwise.
183 #define local_add_unless(l, a, u) \
188 if (unlikely(c == (u))) \
190 old = local_cmpxchg((l), c, c + (a)); \
191 if (likely(old == c)) \
197 #define local_inc_not_zero(l) local_add_unless((l), 1, 0)
199 /* On x86_32, these are no better than the atomic variants.
200 * On x86-64 these are better than the atomic variants on SMP kernels
201 * because they dont use a lock prefix.
203 #define __local_inc(l) local_inc(l)
204 #define __local_dec(l) local_dec(l)
205 #define __local_add(i,l) local_add((i),(l))
206 #define __local_sub(i,l) local_sub((i),(l))
208 /* Use these for per-cpu local_t variables: on some archs they are
209 * much more efficient than these naive implementations. Note they take
210 * a variable, not an address.
212 * X86_64: This could be done better if we moved the per cpu data directly
216 /* Need to disable preemption for the cpu local counters otherwise we could
217 still access a variable of a previous CPU in a non atomic way. */
218 #define cpu_local_wrap_v(l) \
224 #define cpu_local_wrap(l) \
225 ({ preempt_disable(); \
227 preempt_enable(); }) \
229 #define cpu_local_read(l) cpu_local_wrap_v(local_read(&__get_cpu_var(l)))
230 #define cpu_local_set(l, i) cpu_local_wrap(local_set(&__get_cpu_var(l), (i)))
231 #define cpu_local_inc(l) cpu_local_wrap(local_inc(&__get_cpu_var(l)))
232 #define cpu_local_dec(l) cpu_local_wrap(local_dec(&__get_cpu_var(l)))
233 #define cpu_local_add(i, l) cpu_local_wrap(local_add((i), &__get_cpu_var(l)))
234 #define cpu_local_sub(i, l) cpu_local_wrap(local_sub((i), &__get_cpu_var(l)))
236 #define __cpu_local_inc(l) cpu_local_inc(l)
237 #define __cpu_local_dec(l) cpu_local_dec(l)
238 #define __cpu_local_add(i, l) cpu_local_add((i), (l))
239 #define __cpu_local_sub(i, l) cpu_local_sub((i), (l))
241 #endif /* _ARCH_LOCAL_H */