2 * Atomic operations that C can't guarantee us. Useful for
3 * resource counting etc.
5 * But use these as seldom as possible since they are slower than
8 * Copyright (C) 2004-2006 Atmel Corporation
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
14 #ifndef __ASM_AVR32_ATOMIC_H
15 #define __ASM_AVR32_ATOMIC_H
17 #include <linux/types.h>
18 #include <asm/cmpxchg.h>
20 #define ATOMIC_INIT(i) { (i) }
22 #define atomic_read(v) ACCESS_ONCE((v)->counter)
23 #define atomic_set(v, i) (((v)->counter) = i)
25 #define ATOMIC_OP_RETURN(op, asm_op, asm_con) \
26 static inline int __atomic_##op##_return(int i, atomic_t *v) \
31 "/* atomic_" #op "_return */\n" \
34 " " #asm_op " %0, %3\n" \
37 : "=&r" (result), "=o" (v->counter) \
38 : "m" (v->counter), #asm_con (i) \
44 ATOMIC_OP_RETURN(sub, sub, rKs21)
45 ATOMIC_OP_RETURN(add, add, r)
47 #undef ATOMIC_OP_RETURN
50 * Probably found the reason why we want to use sub with the signed 21-bit
51 * limit, it uses one less register than the add instruction that can add up to
54 * Both instructions are 32-bit, to use a 16-bit instruction the immediate is
57 * sub 32-bit, type IV, takes a register and subtracts a 21-bit immediate.
58 * add 32-bit, type II, adds two register values together.
60 #define IS_21BIT_CONST(i) \
61 (__builtin_constant_p(i) && ((i) >= -1048575) && ((i) <= 1048576))
64 * atomic_add_return - add integer to atomic variable
65 * @i: integer value to add
66 * @v: pointer of type atomic_t
68 * Atomically adds @i to @v. Returns the resulting value.
70 static inline int atomic_add_return(int i, atomic_t *v)
72 if (IS_21BIT_CONST(i))
73 return __atomic_sub_return(-i, v);
75 return __atomic_add_return(i, v);
79 * atomic_sub_return - subtract the atomic variable
80 * @i: integer value to subtract
81 * @v: pointer of type atomic_t
83 * Atomically subtracts @i from @v. Returns the resulting value.
85 static inline int atomic_sub_return(int i, atomic_t *v)
87 if (IS_21BIT_CONST(i))
88 return __atomic_sub_return(i, v);
90 return __atomic_add_return(-i, v);
94 * __atomic_add_unless - add unless the number is a given value
95 * @v: pointer of type atomic_t
96 * @a: the amount to add to v...
97 * @u: ...unless v is equal to u.
99 * Atomically adds @a to @v, so long as it was not @u.
100 * Returns the old value of @v.
102 static inline int __atomic_add_unless(atomic_t *v, int a, int u)
104 int tmp, old = atomic_read(v);
106 if (IS_21BIT_CONST(a)) {
108 "/* __atomic_sub_unless */\n"
117 : "=&r"(tmp), "=o"(v->counter)
118 : "m"(v->counter), "rKs21"(-a), "rKs21"(u)
122 "/* __atomic_add_unless */\n"
131 : "=&r"(tmp), "=o"(v->counter)
132 : "m"(v->counter), "r"(a), "ir"(u)
139 #undef IS_21BIT_CONST
142 * atomic_sub_if_positive - conditionally subtract integer from atomic variable
143 * @i: integer value to subtract
144 * @v: pointer of type atomic_t
146 * Atomically test @v and subtract @i if @v is greater or equal than @i.
147 * The function returns the old value of @v minus @i.
149 static inline int atomic_sub_if_positive(int i, atomic_t *v)
154 "/* atomic_sub_if_positive */\n"
162 : "=&r"(result), "=o"(v->counter)
163 : "m"(v->counter), "ir"(i)
169 #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
170 #define atomic_cmpxchg(v, o, n) (cmpxchg(&((v)->counter), (o), (n)))
172 #define atomic_sub(i, v) (void)atomic_sub_return(i, v)
173 #define atomic_add(i, v) (void)atomic_add_return(i, v)
174 #define atomic_dec(v) atomic_sub(1, (v))
175 #define atomic_inc(v) atomic_add(1, (v))
177 #define atomic_dec_return(v) atomic_sub_return(1, v)
178 #define atomic_inc_return(v) atomic_add_return(1, v)
180 #define atomic_sub_and_test(i, v) (atomic_sub_return(i, v) == 0)
181 #define atomic_inc_and_test(v) (atomic_add_return(1, v) == 0)
182 #define atomic_dec_and_test(v) (atomic_sub_return(1, v) == 0)
183 #define atomic_add_negative(i, v) (atomic_add_return(i, v) < 0)
185 #define atomic_dec_if_positive(v) atomic_sub_if_positive(1, v)
187 #endif /* __ASM_AVR32_ATOMIC_H */