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 much more slower
6 * than regular operations.
8 * This file is subject to the terms and conditions of the GNU General Public
9 * License. See the file "COPYING" in the main directory of this archive
12 * Copyright (C) 1996, 1997, 2000 by Ralf Baechle
14 #ifndef __ASM_ATOMIC_H
15 #define __ASM_ATOMIC_H
17 #include <linux/config.h>
19 typedef struct { volatile int counter; } atomic_t;
22 #define ATOMIC_INIT(i) { (i) }
25 * atomic_read - read atomic variable
26 * @v: pointer of type atomic_t
28 * Atomically reads the value of @v. Note that the guaranteed
29 * useful range of an atomic_t is only 24 bits.
31 #define atomic_read(v) ((v)->counter)
34 * atomic_set - set atomic variable
35 * @v: pointer of type atomic_t
38 * Atomically sets the value of @v to @i. Note that the guaranteed
39 * useful range of an atomic_t is only 24 bits.
41 #define atomic_set(v,i) ((v)->counter = (i))
43 #ifndef CONFIG_CPU_HAS_LLSC
45 #include <asm/system.h>
48 * The MIPS I implementation is only atomic with respect to
49 * interrupts. R3000 based multiprocessor machines are rare anyway ...
51 * atomic_add - add integer to atomic variable
52 * @i: integer value to add
53 * @v: pointer of type atomic_t
55 * Atomically adds @i to @v. Note that the guaranteed useful range
56 * of an atomic_t is only 24 bits.
58 extern __inline__ void atomic_add(int i, atomic_t * v)
62 local_irq_save(flags);
64 local_irq_restore(flags);
68 * atomic_sub - subtract the atomic variable
69 * @i: integer value to subtract
70 * @v: pointer of type atomic_t
72 * Atomically subtracts @i from @v. Note that the guaranteed
73 * useful range of an atomic_t is only 24 bits.
75 extern __inline__ void atomic_sub(int i, atomic_t * v)
79 local_irq_save(flags);
81 local_irq_restore(flags);
84 extern __inline__ int atomic_add_return(int i, atomic_t * v)
89 local_irq_save(flags);
93 local_irq_restore(flags);
98 extern __inline__ int atomic_sub_return(int i, atomic_t * v)
103 local_irq_save(flags);
107 local_irq_restore(flags);
115 * ... while for MIPS II and better we can use ll/sc instruction. This
116 * implementation is SMP safe ...
120 * atomic_add - add integer to atomic variable
121 * @i: integer value to add
122 * @v: pointer of type atomic_t
124 * Atomically adds @i to @v. Note that the guaranteed useful range
125 * of an atomic_t is only 24 bits.
127 extern __inline__ void atomic_add(int i, atomic_t * v)
131 __asm__ __volatile__(
132 "1: ll %0, %1 # atomic_add\n"
136 : "=&r" (temp), "=m" (v->counter)
137 : "Ir" (i), "m" (v->counter));
141 * atomic_sub - subtract the atomic variable
142 * @i: integer value to subtract
143 * @v: pointer of type atomic_t
145 * Atomically subtracts @i from @v. Note that the guaranteed
146 * useful range of an atomic_t is only 24 bits.
148 extern __inline__ void atomic_sub(int i, atomic_t * v)
152 __asm__ __volatile__(
153 "1: ll %0, %1 # atomic_sub\n"
157 : "=&r" (temp), "=m" (v->counter)
158 : "Ir" (i), "m" (v->counter));
162 * Same as above, but return the result value
164 extern __inline__ int atomic_add_return(int i, atomic_t * v)
166 unsigned long temp, result;
168 __asm__ __volatile__(
169 ".set push # atomic_add_return\n"
172 " addu %0, %1, %3 \n"
175 " addu %0, %1, %3 \n"
178 : "=&r" (result), "=&r" (temp), "=m" (v->counter)
179 : "Ir" (i), "m" (v->counter)
185 extern __inline__ int atomic_sub_return(int i, atomic_t * v)
187 unsigned long temp, result;
189 __asm__ __volatile__(
191 ".set noreorder # atomic_sub_return\n"
193 " subu %0, %1, %3 \n"
196 " subu %0, %1, %3 \n"
199 : "=&r" (result), "=&r" (temp), "=m" (v->counter)
200 : "Ir" (i), "m" (v->counter)
207 #define atomic_dec_return(v) atomic_sub_return(1,(v))
208 #define atomic_inc_return(v) atomic_add_return(1,(v))
211 * atomic_sub_and_test - subtract value from variable and test result
212 * @i: integer value to subtract
213 * @v: pointer of type atomic_t
215 * Atomically subtracts @i from @v and returns
216 * true if the result is zero, or false for all
217 * other cases. Note that the guaranteed
218 * useful range of an atomic_t is only 24 bits.
220 #define atomic_sub_and_test(i,v) (atomic_sub_return((i), (v)) == 0)
223 * atomic_inc_and_test - increment and test
224 * @v: pointer of type atomic_t
226 * Atomically increments @v by 1
227 * and returns true if the result is zero, or false for all
228 * other cases. Note that the guaranteed
229 * useful range of an atomic_t is only 24 bits.
231 #define atomic_inc_and_test(v) (atomic_inc_return(v) == 0)
234 * atomic_dec_and_test - decrement by 1 and test
235 * @v: pointer of type atomic_t
237 * Atomically decrements @v by 1 and
238 * returns true if the result is 0, or false for all other
239 * cases. Note that the guaranteed
240 * useful range of an atomic_t is only 24 bits.
242 #define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0)
245 * atomic_inc - increment atomic variable
246 * @v: pointer of type atomic_t
248 * Atomically increments @v by 1. Note that the guaranteed
249 * useful range of an atomic_t is only 24 bits.
251 #define atomic_inc(v) atomic_add(1,(v))
254 * atomic_dec - decrement and test
255 * @v: pointer of type atomic_t
257 * Atomically decrements @v by 1. Note that the guaranteed
258 * useful range of an atomic_t is only 24 bits.
260 #define atomic_dec(v) atomic_sub(1,(v))
263 * atomic_add_negative - add and test if negative
264 * @v: pointer of type atomic_t
265 * @i: integer value to add
267 * Atomically adds @i to @v and returns true
268 * if the result is negative, or false when
269 * result is greater than or equal to zero. Note that the guaranteed
270 * useful range of an atomic_t is only 24 bits.
272 #define atomic_add_negative(i,v) (atomic_add_return(i, (v)) < 0)
274 /* Atomic operations are already serializing */
275 #define smp_mb__before_atomic_dec() smp_mb()
276 #define smp_mb__after_atomic_dec() smp_mb()
277 #define smp_mb__before_atomic_inc() smp_mb()
278 #define smp_mb__after_atomic_inc() smp_mb()
280 #endif /* defined(__KERNEL__) */
282 #endif /* __ASM_ATOMIC_H */