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 <asm/system.h>
19 typedef struct { volatile int counter; } atomic_t;
20 #define ATOMIC_INIT(i) { (i) }
22 #define atomic_read(v) ((v)->counter)
23 #define atomic_set(v, i) (((v)->counter) = i)
26 * atomic_sub_return - subtract the atomic variable
27 * @i: integer value to subtract
28 * @v: pointer of type atomic_t
30 * Atomically subtracts @i from @v. Returns the resulting value.
32 static inline int atomic_sub_return(int i, atomic_t *v)
37 "/* atomic_sub_return */\n"
43 : "=&r"(result), "=o"(v->counter)
44 : "m"(v->counter), "rKs21"(i)
51 * atomic_add_return - add integer to atomic variable
52 * @i: integer value to add
53 * @v: pointer of type atomic_t
55 * Atomically adds @i to @v. Returns the resulting value.
57 static inline int atomic_add_return(int i, atomic_t *v)
61 if (__builtin_constant_p(i) && (i >= -1048575) && (i <= 1048576))
62 result = atomic_sub_return(-i, v);
65 "/* atomic_add_return */\n"
71 : "=&r"(result), "=o"(v->counter)
72 : "m"(v->counter), "r"(i)
79 * atomic_sub_unless - sub unless the number is a given value
80 * @v: pointer of type atomic_t
81 * @a: the amount to add to v...
82 * @u: ...unless v is equal to u.
84 * If the atomic value v is not equal to u, this function subtracts a
85 * from v, and returns non zero. If v is equal to u then it returns
86 * zero. This is done as an atomic operation.
88 static inline int atomic_sub_unless(atomic_t *v, int a, int u)
93 "/* atomic_sub_unless */\n"
103 : "=&r"(tmp), "=&r"(result), "=o"(v->counter)
104 : "m"(v->counter), "rKs21"(a), "rKs21"(u)
111 * atomic_add_unless - add unless the number is a given value
112 * @v: pointer of type atomic_t
113 * @a: the amount to add to v...
114 * @u: ...unless v is equal to u.
116 * If the atomic value v is not equal to u, this function adds a to v,
117 * and returns non zero. If v is equal to u then it returns zero. This
118 * is done as an atomic operation.
120 static inline int atomic_add_unless(atomic_t *v, int a, int u)
124 if (__builtin_constant_p(a) && (a >= -1048575) && (a <= 1048576))
125 result = atomic_sub_unless(v, -a, u);
129 "/* atomic_add_unless */\n"
139 : "=&r"(tmp), "=&r"(result), "=o"(v->counter)
140 : "m"(v->counter), "r"(a), "ir"(u)
148 * atomic_sub_if_positive - conditionally subtract integer from atomic variable
149 * @i: integer value to subtract
150 * @v: pointer of type atomic_t
152 * Atomically test @v and subtract @i if @v is greater or equal than @i.
153 * The function returns the old value of @v minus @i.
155 static inline int atomic_sub_if_positive(int i, atomic_t *v)
160 "/* atomic_sub_if_positive */\n"
168 : "=&r"(result), "=o"(v->counter)
169 : "m"(v->counter), "ir"(i)
175 #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
176 #define atomic_cmpxchg(v, o, n) (cmpxchg(&((v)->counter), (o), (n)))
178 #define atomic_sub(i, v) (void)atomic_sub_return(i, v)
179 #define atomic_add(i, v) (void)atomic_add_return(i, v)
180 #define atomic_dec(v) atomic_sub(1, (v))
181 #define atomic_inc(v) atomic_add(1, (v))
183 #define atomic_dec_return(v) atomic_sub_return(1, v)
184 #define atomic_inc_return(v) atomic_add_return(1, v)
186 #define atomic_sub_and_test(i, v) (atomic_sub_return(i, v) == 0)
187 #define atomic_inc_and_test(v) (atomic_add_return(1, v) == 0)
188 #define atomic_dec_and_test(v) (atomic_sub_return(1, v) == 0)
189 #define atomic_add_negative(i, v) (atomic_add_return(i, v) < 0)
191 #define atomic_inc_not_zero(v) atomic_add_unless(v, 1, 0)
192 #define atomic_dec_if_positive(v) atomic_sub_if_positive(1, v)
194 #define smp_mb__before_atomic_dec() barrier()
195 #define smp_mb__after_atomic_dec() barrier()
196 #define smp_mb__before_atomic_inc() barrier()
197 #define smp_mb__after_atomic_inc() barrier()
199 #include <asm-generic/atomic.h>
201 #endif /* __ASM_AVR32_ATOMIC_H */