blob: 6e85975b9ed239cbf953ffd9ec70bd0ad34414fe [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef _ARCH_I386_LOCAL_H
2#define _ARCH_I386_LOCAL_H
3
4#include <linux/percpu.h>
Mathieu Desnoyersa0752272007-05-08 00:34:44 -07005#include <asm/system.h>
6#include <asm/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007
8typedef struct
9{
Mathieu Desnoyersa0752272007-05-08 00:34:44 -070010 atomic_long_t a;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011} local_t;
12
Mathieu Desnoyersa0752272007-05-08 00:34:44 -070013#define LOCAL_INIT(i) { ATOMIC_LONG_INIT(i) }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
Mathieu Desnoyersa0752272007-05-08 00:34:44 -070015#define local_read(l) atomic_long_read(&(l)->a)
16#define local_set(l,i) atomic_long_set(&(l)->a, (i))
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Mathieu Desnoyersa0752272007-05-08 00:34:44 -070018static __inline__ void local_inc(local_t *l)
Linus Torvalds1da177e2005-04-16 15:20:36 -070019{
20 __asm__ __volatile__(
21 "incl %0"
Mathieu Desnoyersa0752272007-05-08 00:34:44 -070022 :"+m" (l->a.counter));
Linus Torvalds1da177e2005-04-16 15:20:36 -070023}
24
Mathieu Desnoyersa0752272007-05-08 00:34:44 -070025static __inline__ void local_dec(local_t *l)
Linus Torvalds1da177e2005-04-16 15:20:36 -070026{
27 __asm__ __volatile__(
28 "decl %0"
Mathieu Desnoyersa0752272007-05-08 00:34:44 -070029 :"+m" (l->a.counter));
Linus Torvalds1da177e2005-04-16 15:20:36 -070030}
31
Mathieu Desnoyersa0752272007-05-08 00:34:44 -070032static __inline__ void local_add(long i, local_t *l)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033{
34 __asm__ __volatile__(
35 "addl %1,%0"
Mathieu Desnoyersa0752272007-05-08 00:34:44 -070036 :"+m" (l->a.counter)
Linus Torvaldsb862f3b2006-07-08 15:24:18 -070037 :"ir" (i));
Linus Torvalds1da177e2005-04-16 15:20:36 -070038}
39
Mathieu Desnoyersa0752272007-05-08 00:34:44 -070040static __inline__ void local_sub(long i, local_t *l)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
42 __asm__ __volatile__(
43 "subl %1,%0"
Mathieu Desnoyersa0752272007-05-08 00:34:44 -070044 :"+m" (l->a.counter)
Linus Torvaldsb862f3b2006-07-08 15:24:18 -070045 :"ir" (i));
Linus Torvalds1da177e2005-04-16 15:20:36 -070046}
47
Mathieu Desnoyersa0752272007-05-08 00:34:44 -070048/**
49 * local_sub_and_test - subtract value from variable and test result
50 * @i: integer value to subtract
51 * @l: pointer of type local_t
52 *
53 * Atomically subtracts @i from @l and returns
54 * true if the result is zero, or false for all
55 * other cases.
56 */
57static __inline__ int local_sub_and_test(long i, local_t *l)
58{
59 unsigned char c;
60
61 __asm__ __volatile__(
62 "subl %2,%0; sete %1"
63 :"+m" (l->a.counter), "=qm" (c)
64 :"ir" (i) : "memory");
65 return c;
66}
67
68/**
69 * local_dec_and_test - decrement and test
70 * @l: pointer of type local_t
71 *
72 * Atomically decrements @l by 1 and
73 * returns true if the result is 0, or false for all other
74 * cases.
75 */
76static __inline__ int local_dec_and_test(local_t *l)
77{
78 unsigned char c;
79
80 __asm__ __volatile__(
81 "decl %0; sete %1"
82 :"+m" (l->a.counter), "=qm" (c)
83 : : "memory");
84 return c != 0;
85}
86
87/**
88 * local_inc_and_test - increment and test
89 * @l: pointer of type local_t
90 *
91 * Atomically increments @l by 1
92 * and returns true if the result is zero, or false for all
93 * other cases.
94 */
95static __inline__ int local_inc_and_test(local_t *l)
96{
97 unsigned char c;
98
99 __asm__ __volatile__(
100 "incl %0; sete %1"
101 :"+m" (l->a.counter), "=qm" (c)
102 : : "memory");
103 return c != 0;
104}
105
106/**
107 * local_add_negative - add and test if negative
108 * @l: pointer of type local_t
109 * @i: integer value to add
110 *
111 * Atomically adds @i to @l and returns true
112 * if the result is negative, or false when
113 * result is greater than or equal to zero.
114 */
115static __inline__ int local_add_negative(long i, local_t *l)
116{
117 unsigned char c;
118
119 __asm__ __volatile__(
120 "addl %2,%0; sets %1"
121 :"+m" (l->a.counter), "=qm" (c)
122 :"ir" (i) : "memory");
123 return c;
124}
125
126/**
127 * local_add_return - add and return
128 * @l: pointer of type local_t
129 * @i: integer value to add
130 *
131 * Atomically adds @i to @l and returns @i + @l
132 */
133static __inline__ long local_add_return(long i, local_t *l)
134{
135 long __i;
136#ifdef CONFIG_M386
137 unsigned long flags;
Thomas Gleixner03491c92007-05-23 13:58:19 -0700138 if(unlikely(boot_cpu_data.x86 <= 3))
Mathieu Desnoyersa0752272007-05-08 00:34:44 -0700139 goto no_xadd;
140#endif
141 /* Modern 486+ processor */
142 __i = i;
143 __asm__ __volatile__(
144 "xaddl %0, %1;"
145 :"+r" (i), "+m" (l->a.counter)
146 : : "memory");
147 return i + __i;
148
149#ifdef CONFIG_M386
150no_xadd: /* Legacy 386 processor */
151 local_irq_save(flags);
152 __i = local_read(l);
153 local_set(l, i + __i);
154 local_irq_restore(flags);
155 return i + __i;
156#endif
157}
158
159static __inline__ long local_sub_return(long i, local_t *l)
160{
161 return local_add_return(-i,l);
162}
163
164#define local_inc_return(l) (local_add_return(1,l))
165#define local_dec_return(l) (local_sub_return(1,l))
166
167#define local_cmpxchg(l, o, n) \
168 (cmpxchg_local(&((l)->a.counter), (o), (n)))
169/* Always has a lock prefix */
170#define local_xchg(l, n) (xchg(&((l)->a.counter), (n)))
171
172/**
173 * local_add_unless - add unless the number is a given value
174 * @l: pointer of type local_t
175 * @a: the amount to add to l...
176 * @u: ...unless l is equal to u.
177 *
178 * Atomically adds @a to @l, so long as it was not @u.
179 * Returns non-zero if @l was not @u, and zero otherwise.
180 */
181#define local_add_unless(l, a, u) \
182({ \
183 long c, old; \
184 c = local_read(l); \
185 for (;;) { \
186 if (unlikely(c == (u))) \
187 break; \
188 old = local_cmpxchg((l), c, c + (a)); \
189 if (likely(old == c)) \
190 break; \
191 c = old; \
192 } \
193 c != (u); \
194})
195#define local_inc_not_zero(l) local_add_unless((l), 1, 0)
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197/* On x86, these are no better than the atomic variants. */
198#define __local_inc(l) local_inc(l)
199#define __local_dec(l) local_dec(l)
200#define __local_add(i,l) local_add((i),(l))
201#define __local_sub(i,l) local_sub((i),(l))
202
203/* Use these for per-cpu local_t variables: on some archs they are
204 * much more efficient than these naive implementations. Note they take
205 * a variable, not an address.
206 */
Andi Kleenda531122006-06-26 13:59:20 +0200207
208/* Need to disable preemption for the cpu local counters otherwise we could
209 still access a variable of a previous CPU in a non atomic way. */
Mathieu Desnoyersa0752272007-05-08 00:34:44 -0700210#define cpu_local_wrap_v(l) \
Andi Kleenda531122006-06-26 13:59:20 +0200211 ({ local_t res__; \
212 preempt_disable(); \
Mathieu Desnoyersa0752272007-05-08 00:34:44 -0700213 res__ = (l); \
Andi Kleenda531122006-06-26 13:59:20 +0200214 preempt_enable(); \
215 res__; })
Mathieu Desnoyersa0752272007-05-08 00:34:44 -0700216#define cpu_local_wrap(l) \
Andi Kleenda531122006-06-26 13:59:20 +0200217 ({ preempt_disable(); \
Mathieu Desnoyersa0752272007-05-08 00:34:44 -0700218 l; \
Andi Kleenda531122006-06-26 13:59:20 +0200219 preempt_enable(); }) \
220
Mathieu Desnoyersa0752272007-05-08 00:34:44 -0700221#define cpu_local_read(l) cpu_local_wrap_v(local_read(&__get_cpu_var(l)))
222#define cpu_local_set(l, i) cpu_local_wrap(local_set(&__get_cpu_var(l), (i)))
223#define cpu_local_inc(l) cpu_local_wrap(local_inc(&__get_cpu_var(l)))
224#define cpu_local_dec(l) cpu_local_wrap(local_dec(&__get_cpu_var(l)))
225#define cpu_local_add(i, l) cpu_local_wrap(local_add((i), &__get_cpu_var(l)))
226#define cpu_local_sub(i, l) cpu_local_wrap(local_sub((i), &__get_cpu_var(l)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Mathieu Desnoyersa0752272007-05-08 00:34:44 -0700228#define __cpu_local_inc(l) cpu_local_inc(l)
229#define __cpu_local_dec(l) cpu_local_dec(l)
230#define __cpu_local_add(i, l) cpu_local_add((i), (l))
231#define __cpu_local_sub(i, l) cpu_local_sub((i), (l))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233#endif /* _ARCH_I386_LOCAL_H */