blob: 4ad6560847b1f5f7c20644b52e27505339880e79 [file] [log] [blame]
H. Peter Anvin1965aae2008-10-22 22:26:29 -07001#ifndef _ASM_X86_LOCAL_H
2#define _ASM_X86_LOCAL_H
Harvey Harrison5638f992008-01-30 13:31:26 +01003
4#include <linux/percpu.h>
5
Arun Sharma600634972011-07-26 16:09:06 -07006#include <linux/atomic.h>
Harvey Harrison5638f992008-01-30 13:31:26 +01007#include <asm/asm.h>
8
Harvey Harrison01c57fb2008-01-30 13:31:26 +01009typedef struct {
Harvey Harrison5638f992008-01-30 13:31:26 +010010 atomic_long_t a;
11} local_t;
12
13#define LOCAL_INIT(i) { ATOMIC_LONG_INIT(i) }
14
15#define local_read(l) atomic_long_read(&(l)->a)
Harvey Harrison01c57fb2008-01-30 13:31:26 +010016#define local_set(l, i) atomic_long_set(&(l)->a, (i))
Harvey Harrison5638f992008-01-30 13:31:26 +010017
18static inline void local_inc(local_t *l)
19{
Joe Perches69cde652008-03-23 01:02:39 -070020 asm volatile(_ASM_INC "%0"
21 : "+m" (l->a.counter));
Harvey Harrison5638f992008-01-30 13:31:26 +010022}
23
24static inline void local_dec(local_t *l)
25{
Joe Perches69cde652008-03-23 01:02:39 -070026 asm volatile(_ASM_DEC "%0"
27 : "+m" (l->a.counter));
Harvey Harrison5638f992008-01-30 13:31:26 +010028}
29
30static inline void local_add(long i, local_t *l)
31{
Joe Perches69cde652008-03-23 01:02:39 -070032 asm volatile(_ASM_ADD "%1,%0"
33 : "+m" (l->a.counter)
34 : "ir" (i));
Harvey Harrison5638f992008-01-30 13:31:26 +010035}
36
37static inline void local_sub(long i, local_t *l)
38{
Joe Perches69cde652008-03-23 01:02:39 -070039 asm volatile(_ASM_SUB "%1,%0"
40 : "+m" (l->a.counter)
41 : "ir" (i));
Harvey Harrison5638f992008-01-30 13:31:26 +010042}
43
44/**
45 * local_sub_and_test - subtract value from variable and test result
46 * @i: integer value to subtract
47 * @l: pointer to type local_t
48 *
49 * Atomically subtracts @i from @l and returns
50 * true if the result is zero, or false for all
51 * other cases.
52 */
53static inline int local_sub_and_test(long i, local_t *l)
54{
H. Peter Anvine0f6dec2013-12-04 14:31:28 -080055 GEN_BINARY_RMWcc(_ASM_SUB, l->a.counter, "er", i, "%0", "e");
Harvey Harrison5638f992008-01-30 13:31:26 +010056}
57
58/**
59 * local_dec_and_test - decrement and test
60 * @l: pointer to type local_t
61 *
62 * Atomically decrements @l by 1 and
63 * returns true if the result is 0, or false for all other
64 * cases.
65 */
66static inline int local_dec_and_test(local_t *l)
67{
Peter Zijlstra0c44c2d2013-09-11 15:19:24 +020068 GEN_UNARY_RMWcc(_ASM_DEC, l->a.counter, "%0", "e");
Harvey Harrison5638f992008-01-30 13:31:26 +010069}
70
71/**
72 * local_inc_and_test - increment and test
73 * @l: pointer to type local_t
74 *
75 * Atomically increments @l by 1
76 * and returns true if the result is zero, or false for all
77 * other cases.
78 */
79static inline int local_inc_and_test(local_t *l)
80{
Peter Zijlstra0c44c2d2013-09-11 15:19:24 +020081 GEN_UNARY_RMWcc(_ASM_INC, l->a.counter, "%0", "e");
Harvey Harrison5638f992008-01-30 13:31:26 +010082}
83
84/**
85 * local_add_negative - add and test if negative
86 * @i: integer value to add
87 * @l: pointer to type local_t
88 *
89 * Atomically adds @i to @l and returns true
90 * if the result is negative, or false when
91 * result is greater than or equal to zero.
92 */
93static inline int local_add_negative(long i, local_t *l)
94{
H. Peter Anvine0f6dec2013-12-04 14:31:28 -080095 GEN_BINARY_RMWcc(_ASM_ADD, l->a.counter, "er", i, "%0", "s");
Harvey Harrison5638f992008-01-30 13:31:26 +010096}
97
98/**
99 * local_add_return - add and return
100 * @i: integer value to add
101 * @l: pointer to type local_t
102 *
103 * Atomically adds @i to @l and returns @i + @l
104 */
105static inline long local_add_return(long i, local_t *l)
106{
H. Peter Anvin7ac468b2012-11-28 11:50:25 -0800107 long __i = i;
Joe Perches69cde652008-03-23 01:02:39 -0700108 asm volatile(_ASM_XADD "%0, %1;"
109 : "+r" (i), "+m" (l->a.counter)
110 : : "memory");
Harvey Harrison5638f992008-01-30 13:31:26 +0100111 return i + __i;
Harvey Harrison5638f992008-01-30 13:31:26 +0100112}
113
114static inline long local_sub_return(long i, local_t *l)
115{
Harvey Harrison01c57fb2008-01-30 13:31:26 +0100116 return local_add_return(-i, l);
Harvey Harrison5638f992008-01-30 13:31:26 +0100117}
118
Harvey Harrison01c57fb2008-01-30 13:31:26 +0100119#define local_inc_return(l) (local_add_return(1, l))
120#define local_dec_return(l) (local_sub_return(1, l))
Harvey Harrison5638f992008-01-30 13:31:26 +0100121
122#define local_cmpxchg(l, o, n) \
123 (cmpxchg_local(&((l)->a.counter), (o), (n)))
124/* Always has a lock prefix */
125#define local_xchg(l, n) (xchg(&((l)->a.counter), (n)))
126
127/**
128 * local_add_unless - add unless the number is a given value
129 * @l: pointer of type local_t
130 * @a: the amount to add to l...
131 * @u: ...unless l is equal to u.
132 *
133 * Atomically adds @a to @l, so long as it was not @u.
134 * Returns non-zero if @l was not @u, and zero otherwise.
135 */
136#define local_add_unless(l, a, u) \
137({ \
138 long c, old; \
Joe Perches69cde652008-03-23 01:02:39 -0700139 c = local_read((l)); \
Harvey Harrison5638f992008-01-30 13:31:26 +0100140 for (;;) { \
141 if (unlikely(c == (u))) \
142 break; \
Joe Perches69cde652008-03-23 01:02:39 -0700143 old = local_cmpxchg((l), c, c + (a)); \
Harvey Harrison5638f992008-01-30 13:31:26 +0100144 if (likely(old == c)) \
145 break; \
146 c = old; \
147 } \
148 c != (u); \
149})
150#define local_inc_not_zero(l) local_add_unless((l), 1, 0)
151
152/* On x86_32, these are no better than the atomic variants.
153 * On x86-64 these are better than the atomic variants on SMP kernels
154 * because they dont use a lock prefix.
155 */
156#define __local_inc(l) local_inc(l)
157#define __local_dec(l) local_dec(l)
Harvey Harrison01c57fb2008-01-30 13:31:26 +0100158#define __local_add(i, l) local_add((i), (l))
159#define __local_sub(i, l) local_sub((i), (l))
Harvey Harrison5638f992008-01-30 13:31:26 +0100160
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700161#endif /* _ASM_X86_LOCAL_H */