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, 1999, 2000 by Ralf Baechle
17 #include <asm/sgidefs.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 extern __inline__ void atomic_add(int i, volatile atomic_t * v)
48 "1:\tll\t%0,%1\t\t\t# atomic_add\n\t"
52 : "=&r" (temp), "=m" (v->counter)
53 : "Ir" (i), "m" (v->counter));
57 * atomic_sub - subtract the atomic variable
58 * @i: integer value to subtract
59 * @v: pointer of type atomic_t
61 * Atomically subtracts @i from @v. Note that the guaranteed
62 * useful range of an atomic_t is only 24 bits.
64 extern __inline__ void atomic_sub(int i, volatile atomic_t * v)
69 "1:\tll\t%0,%1\t\t\t# atomic_sub\n\t"
73 : "=&r" (temp), "=m" (v->counter)
74 : "Ir" (i), "m" (v->counter));
78 * Same as above, but return the result value
80 extern __inline__ int atomic_add_return(int i, atomic_t * v)
82 unsigned long temp, result;
85 ".set\tnoreorder\t\t\t# atomic_add_return\n"
93 : "=&r" (result), "=&r" (temp), "=m" (v->counter)
94 : "Ir" (i), "m" (v->counter)
100 extern __inline__ int atomic_sub_return(int i, atomic_t * v)
102 unsigned long temp, result;
104 __asm__ __volatile__(
105 ".set\tnoreorder\t\t\t# atomic_sub_return\n"
113 : "=&r" (result), "=&r" (temp), "=m" (v->counter)
114 : "Ir" (i), "m" (v->counter)
120 #define atomic_dec_return(v) atomic_sub_return(1,(v))
121 #define atomic_inc_return(v) atomic_add_return(1,(v))
124 * atomic_sub_and_test - subtract value from variable and test result
125 * @i: integer value to subtract
126 * @v: pointer of type atomic_t
128 * Atomically subtracts @i from @v and returns
129 * true if the result is zero, or false for all
130 * other cases. Note that the guaranteed
131 * useful range of an atomic_t is only 24 bits.
133 #define atomic_sub_and_test(i,v) (atomic_sub_return((i), (v)) == 0)
136 * atomic_inc_and_test - increment and test
137 * @v: pointer of type atomic_t
139 * Atomically increments @v by 1
140 * and returns true if the result is zero, or false for all
141 * other cases. Note that the guaranteed
142 * useful range of an atomic_t is only 24 bits.
143 * atomic_inc_and_test is currently not implemented for mips64.
147 * atomic_dec_and_test - decrement by 1 and test
148 * @v: pointer of type atomic_t
150 * Atomically decrements @v by 1 and
151 * returns true if the result is 0, or false for all other
152 * cases. Note that the guaranteed
153 * useful range of an atomic_t is only 24 bits.
155 #define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0)
158 * atomic_inc - increment atomic variable
159 * @v: pointer of type atomic_t
161 * Atomically increments @v by 1. Note that the guaranteed
162 * useful range of an atomic_t is only 24 bits.
164 #define atomic_inc(v) atomic_add(1,(v))
167 * atomic_dec - decrement and test
168 * @v: pointer of type atomic_t
170 * Atomically decrements @v by 1. Note that the guaranteed
171 * useful range of an atomic_t is only 24 bits.
173 #define atomic_dec(v) atomic_sub(1,(v))
176 * atomic_add_negative - add and test if negative
177 * @v: pointer of type atomic_t
178 * @i: integer value to add
180 * Atomically adds @i to @v and returns true
181 * if the result is negative, or false when
182 * result is greater than or equal to zero. Note that the guaranteed
183 * useful range of an atomic_t is only 24 bits.
185 * atomic_add_negative is currently not implemented for mips64.
188 /* Atomic operations are already serializing */
189 #define smp_mb__before_atomic_dec() smp_mb()
190 #define smp_mb__after_atomic_dec() smp_mb()
191 #define smp_mb__before_atomic_inc() smp_mb()
192 #define smp_mb__after_atomic_inc() smp_mb()
194 #endif /* defined(__KERNEL__) */
196 #endif /* _ASM_ATOMIC_H */