Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #ifndef __ARCH_X86_64_ATOMIC__ |
| 2 | #define __ARCH_X86_64_ATOMIC__ |
| 3 | |
Gerd Hoffmann | d167a51 | 2006-06-26 13:56:16 +0200 | [diff] [blame] | 4 | #include <asm/alternative.h> |
Jeff Dike | a436ed9 | 2007-05-08 00:35:02 -0700 | [diff] [blame] | 5 | #include <asm/cmpxchg.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | |
| 7 | /* atomic_t should be 32 bit signed type */ |
| 8 | |
| 9 | /* |
| 10 | * Atomic operations that C can't guarantee us. Useful for |
| 11 | * resource counting etc.. |
| 12 | */ |
| 13 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | /* |
| 15 | * Make sure gcc doesn't try to be clever and move things around |
| 16 | * on us. We need to use _exactly_ the address the user gave us, |
| 17 | * not some alias that contains the same information. |
| 18 | */ |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 19 | typedef struct { |
| 20 | int counter; |
| 21 | } atomic_t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
| 23 | #define ATOMIC_INIT(i) { (i) } |
| 24 | |
| 25 | /** |
| 26 | * atomic_read - read atomic variable |
| 27 | * @v: pointer of type atomic_t |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 28 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | * Atomically reads the value of @v. |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 30 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #define atomic_read(v) ((v)->counter) |
| 32 | |
| 33 | /** |
| 34 | * atomic_set - set atomic variable |
| 35 | * @v: pointer of type atomic_t |
| 36 | * @i: required value |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 37 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | * Atomically sets the value of @v to @i. |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 39 | */ |
| 40 | #define atomic_set(v, i) (((v)->counter) = (i)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * atomic_add - add integer to atomic variable |
| 44 | * @i: integer value to add |
| 45 | * @v: pointer of type atomic_t |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 46 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | * Atomically adds @i to @v. |
| 48 | */ |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 49 | static inline void atomic_add(int i, atomic_t *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | { |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 51 | asm volatile(LOCK_PREFIX "addl %1,%0" |
| 52 | : "=m" (v->counter) |
| 53 | : "ir" (i), "m" (v->counter)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | /** |
| 57 | * atomic_sub - subtract the atomic variable |
| 58 | * @i: integer value to subtract |
| 59 | * @v: pointer of type atomic_t |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 60 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | * Atomically subtracts @i from @v. |
| 62 | */ |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 63 | static inline void atomic_sub(int i, atomic_t *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | { |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 65 | asm volatile(LOCK_PREFIX "subl %1,%0" |
| 66 | : "=m" (v->counter) |
| 67 | : "ir" (i), "m" (v->counter)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /** |
| 71 | * atomic_sub_and_test - subtract value from variable and test result |
| 72 | * @i: integer value to subtract |
| 73 | * @v: pointer of type atomic_t |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 74 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | * Atomically subtracts @i from @v and returns |
| 76 | * true if the result is zero, or false for all |
| 77 | * other cases. |
| 78 | */ |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 79 | static inline int atomic_sub_and_test(int i, atomic_t *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | { |
| 81 | unsigned char c; |
| 82 | |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 83 | asm volatile(LOCK_PREFIX "subl %2,%0; sete %1" |
| 84 | : "=m" (v->counter), "=qm" (c) |
| 85 | : "ir" (i), "m" (v->counter) : "memory"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | return c; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * atomic_inc - increment atomic variable |
| 91 | * @v: pointer of type atomic_t |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 92 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | * Atomically increments @v by 1. |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 94 | */ |
| 95 | static inline void atomic_inc(atomic_t *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | { |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 97 | asm volatile(LOCK_PREFIX "incl %0" |
| 98 | : "=m" (v->counter) |
| 99 | : "m" (v->counter)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /** |
| 103 | * atomic_dec - decrement atomic variable |
| 104 | * @v: pointer of type atomic_t |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 105 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | * Atomically decrements @v by 1. |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 107 | */ |
| 108 | static inline void atomic_dec(atomic_t *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | { |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 110 | asm volatile(LOCK_PREFIX "decl %0" |
| 111 | : "=m" (v->counter) |
| 112 | : "m" (v->counter)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /** |
| 116 | * atomic_dec_and_test - decrement and test |
| 117 | * @v: pointer of type atomic_t |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 118 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | * Atomically decrements @v by 1 and |
| 120 | * returns true if the result is 0, or false for all other |
| 121 | * cases. |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 122 | */ |
| 123 | static inline int atomic_dec_and_test(atomic_t *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | { |
| 125 | unsigned char c; |
| 126 | |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 127 | asm volatile(LOCK_PREFIX "decl %0; sete %1" |
| 128 | : "=m" (v->counter), "=qm" (c) |
| 129 | : "m" (v->counter) : "memory"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | return c != 0; |
| 131 | } |
| 132 | |
| 133 | /** |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 134 | * atomic_inc_and_test - increment and test |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | * @v: pointer of type atomic_t |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 136 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | * Atomically increments @v by 1 |
| 138 | * and returns true if the result is zero, or false for all |
| 139 | * other cases. |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 140 | */ |
| 141 | static inline int atomic_inc_and_test(atomic_t *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | { |
| 143 | unsigned char c; |
| 144 | |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 145 | asm volatile(LOCK_PREFIX "incl %0; sete %1" |
| 146 | : "=m" (v->counter), "=qm" (c) |
| 147 | : "m" (v->counter) : "memory"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | return c != 0; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * atomic_add_negative - add and test if negative |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | * @i: integer value to add |
Hugh Dickins | 7c72aaf | 2005-11-23 13:37:40 -0800 | [diff] [blame] | 154 | * @v: pointer of type atomic_t |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 155 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | * Atomically adds @i to @v and returns true |
| 157 | * if the result is negative, or false when |
| 158 | * result is greater than or equal to zero. |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 159 | */ |
| 160 | static inline int atomic_add_negative(int i, atomic_t *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | { |
| 162 | unsigned char c; |
| 163 | |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 164 | asm volatile(LOCK_PREFIX "addl %2,%0; sets %1" |
| 165 | : "=m" (v->counter), "=qm" (c) |
| 166 | : "ir" (i), "m" (v->counter) : "memory"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | return c; |
| 168 | } |
| 169 | |
Hugh Dickins | 7c72aaf | 2005-11-23 13:37:40 -0800 | [diff] [blame] | 170 | /** |
| 171 | * atomic_add_return - add and return |
| 172 | * @i: integer value to add |
| 173 | * @v: pointer of type atomic_t |
| 174 | * |
| 175 | * Atomically adds @i to @v and returns @i + @v |
| 176 | */ |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 177 | static inline int atomic_add_return(int i, atomic_t *v) |
Hugh Dickins | 7c72aaf | 2005-11-23 13:37:40 -0800 | [diff] [blame] | 178 | { |
| 179 | int __i = i; |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 180 | asm volatile(LOCK_PREFIX "xaddl %0, %1" |
| 181 | : "+r" (i), "+m" (v->counter) |
| 182 | : : "memory"); |
Hugh Dickins | 7c72aaf | 2005-11-23 13:37:40 -0800 | [diff] [blame] | 183 | return i + __i; |
| 184 | } |
| 185 | |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 186 | static inline int atomic_sub_return(int i, atomic_t *v) |
Hugh Dickins | 7c72aaf | 2005-11-23 13:37:40 -0800 | [diff] [blame] | 187 | { |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 188 | return atomic_add_return(-i, v); |
Hugh Dickins | 7c72aaf | 2005-11-23 13:37:40 -0800 | [diff] [blame] | 189 | } |
| 190 | |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 191 | #define atomic_inc_return(v) (atomic_add_return(1, v)) |
| 192 | #define atomic_dec_return(v) (atomic_sub_return(1, v)) |
Hugh Dickins | 7c72aaf | 2005-11-23 13:37:40 -0800 | [diff] [blame] | 193 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | /* An 64bit atomic type */ |
| 195 | |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 196 | typedef struct { |
| 197 | long counter; |
| 198 | } atomic64_t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | |
| 200 | #define ATOMIC64_INIT(i) { (i) } |
| 201 | |
| 202 | /** |
| 203 | * atomic64_read - read atomic64 variable |
| 204 | * @v: pointer of type atomic64_t |
| 205 | * |
| 206 | * Atomically reads the value of @v. |
| 207 | * Doesn't imply a read memory barrier. |
| 208 | */ |
| 209 | #define atomic64_read(v) ((v)->counter) |
| 210 | |
| 211 | /** |
| 212 | * atomic64_set - set atomic64 variable |
| 213 | * @v: pointer to type atomic64_t |
| 214 | * @i: required value |
| 215 | * |
| 216 | * Atomically sets the value of @v to @i. |
| 217 | */ |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 218 | #define atomic64_set(v, i) (((v)->counter) = (i)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | |
| 220 | /** |
| 221 | * atomic64_add - add integer to atomic64 variable |
| 222 | * @i: integer value to add |
| 223 | * @v: pointer to type atomic64_t |
| 224 | * |
| 225 | * Atomically adds @i to @v. |
| 226 | */ |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 227 | static inline void atomic64_add(long i, atomic64_t *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | { |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 229 | asm volatile(LOCK_PREFIX "addq %1,%0" |
| 230 | : "=m" (v->counter) |
Mathieu Desnoyers | 3c3b5c3 | 2008-08-16 03:39:26 -0400 | [diff] [blame] | 231 | : "er" (i), "m" (v->counter)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | /** |
| 235 | * atomic64_sub - subtract the atomic64 variable |
| 236 | * @i: integer value to subtract |
| 237 | * @v: pointer to type atomic64_t |
| 238 | * |
| 239 | * Atomically subtracts @i from @v. |
| 240 | */ |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 241 | static inline void atomic64_sub(long i, atomic64_t *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | { |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 243 | asm volatile(LOCK_PREFIX "subq %1,%0" |
| 244 | : "=m" (v->counter) |
Mathieu Desnoyers | 3c3b5c3 | 2008-08-16 03:39:26 -0400 | [diff] [blame] | 245 | : "er" (i), "m" (v->counter)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | /** |
| 249 | * atomic64_sub_and_test - subtract value from variable and test result |
| 250 | * @i: integer value to subtract |
| 251 | * @v: pointer to type atomic64_t |
| 252 | * |
| 253 | * Atomically subtracts @i from @v and returns |
| 254 | * true if the result is zero, or false for all |
| 255 | * other cases. |
| 256 | */ |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 257 | static inline int atomic64_sub_and_test(long i, atomic64_t *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | { |
| 259 | unsigned char c; |
| 260 | |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 261 | asm volatile(LOCK_PREFIX "subq %2,%0; sete %1" |
| 262 | : "=m" (v->counter), "=qm" (c) |
Mathieu Desnoyers | 3c3b5c3 | 2008-08-16 03:39:26 -0400 | [diff] [blame] | 263 | : "er" (i), "m" (v->counter) : "memory"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | return c; |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * atomic64_inc - increment atomic64 variable |
| 269 | * @v: pointer to type atomic64_t |
| 270 | * |
| 271 | * Atomically increments @v by 1. |
| 272 | */ |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 273 | static inline void atomic64_inc(atomic64_t *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | { |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 275 | asm volatile(LOCK_PREFIX "incq %0" |
| 276 | : "=m" (v->counter) |
| 277 | : "m" (v->counter)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | /** |
| 281 | * atomic64_dec - decrement atomic64 variable |
| 282 | * @v: pointer to type atomic64_t |
| 283 | * |
| 284 | * Atomically decrements @v by 1. |
| 285 | */ |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 286 | static inline void atomic64_dec(atomic64_t *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | { |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 288 | asm volatile(LOCK_PREFIX "decq %0" |
| 289 | : "=m" (v->counter) |
| 290 | : "m" (v->counter)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | /** |
| 294 | * atomic64_dec_and_test - decrement and test |
| 295 | * @v: pointer to type atomic64_t |
| 296 | * |
| 297 | * Atomically decrements @v by 1 and |
| 298 | * returns true if the result is 0, or false for all other |
| 299 | * cases. |
| 300 | */ |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 301 | static inline int atomic64_dec_and_test(atomic64_t *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | { |
| 303 | unsigned char c; |
| 304 | |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 305 | asm volatile(LOCK_PREFIX "decq %0; sete %1" |
| 306 | : "=m" (v->counter), "=qm" (c) |
| 307 | : "m" (v->counter) : "memory"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | return c != 0; |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * atomic64_inc_and_test - increment and test |
| 313 | * @v: pointer to type atomic64_t |
| 314 | * |
| 315 | * Atomically increments @v by 1 |
| 316 | * and returns true if the result is zero, or false for all |
| 317 | * other cases. |
| 318 | */ |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 319 | static inline int atomic64_inc_and_test(atomic64_t *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | { |
| 321 | unsigned char c; |
| 322 | |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 323 | asm volatile(LOCK_PREFIX "incq %0; sete %1" |
| 324 | : "=m" (v->counter), "=qm" (c) |
| 325 | : "m" (v->counter) : "memory"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | return c != 0; |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * atomic64_add_negative - add and test if negative |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | * @i: integer value to add |
Hugh Dickins | 7c72aaf | 2005-11-23 13:37:40 -0800 | [diff] [blame] | 332 | * @v: pointer to type atomic64_t |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | * |
| 334 | * Atomically adds @i to @v and returns true |
| 335 | * if the result is negative, or false when |
| 336 | * result is greater than or equal to zero. |
| 337 | */ |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 338 | static inline int atomic64_add_negative(long i, atomic64_t *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | { |
| 340 | unsigned char c; |
| 341 | |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 342 | asm volatile(LOCK_PREFIX "addq %2,%0; sets %1" |
| 343 | : "=m" (v->counter), "=qm" (c) |
Mathieu Desnoyers | 3c3b5c3 | 2008-08-16 03:39:26 -0400 | [diff] [blame] | 344 | : "er" (i), "m" (v->counter) : "memory"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | return c; |
| 346 | } |
| 347 | |
| 348 | /** |
Hugh Dickins | 7c72aaf | 2005-11-23 13:37:40 -0800 | [diff] [blame] | 349 | * atomic64_add_return - add and return |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | * @i: integer value to add |
Hugh Dickins | 7c72aaf | 2005-11-23 13:37:40 -0800 | [diff] [blame] | 351 | * @v: pointer to type atomic64_t |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | * |
| 353 | * Atomically adds @i to @v and returns @i + @v |
| 354 | */ |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 355 | static inline long atomic64_add_return(long i, atomic64_t *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | { |
Hugh Dickins | 7c72aaf | 2005-11-23 13:37:40 -0800 | [diff] [blame] | 357 | long __i = i; |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 358 | asm volatile(LOCK_PREFIX "xaddq %0, %1;" |
| 359 | : "+r" (i), "+m" (v->counter) |
| 360 | : : "memory"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | return i + __i; |
| 362 | } |
| 363 | |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 364 | static inline long atomic64_sub_return(long i, atomic64_t *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | { |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 366 | return atomic64_add_return(-i, v); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | } |
| 368 | |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 369 | #define atomic64_inc_return(v) (atomic64_add_return(1, (v))) |
| 370 | #define atomic64_dec_return(v) (atomic64_sub_return(1, (v))) |
Hugh Dickins | 7c72aaf | 2005-11-23 13:37:40 -0800 | [diff] [blame] | 371 | |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 372 | #define atomic64_cmpxchg(v, old, new) (cmpxchg(&((v)->counter), (old), (new))) |
Mathieu Desnoyers | 79d365a | 2007-05-08 00:34:36 -0700 | [diff] [blame] | 373 | #define atomic64_xchg(v, new) (xchg(&((v)->counter), new)) |
| 374 | |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 375 | #define atomic_cmpxchg(v, old, new) (cmpxchg(&((v)->counter), (old), (new))) |
| 376 | #define atomic_xchg(v, new) (xchg(&((v)->counter), (new))) |
Nick Piggin | 4a6dae6 | 2005-11-13 16:07:24 -0800 | [diff] [blame] | 377 | |
Nick Piggin | 8426e1f | 2005-11-13 16:07:25 -0800 | [diff] [blame] | 378 | /** |
| 379 | * atomic_add_unless - add unless the number is a given value |
| 380 | * @v: pointer of type atomic_t |
| 381 | * @a: the amount to add to v... |
| 382 | * @u: ...unless v is equal to u. |
| 383 | * |
| 384 | * Atomically adds @a to @v, so long as it was not @u. |
| 385 | * Returns non-zero if @v was not @u, and zero otherwise. |
| 386 | */ |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 387 | static inline int atomic_add_unless(atomic_t *v, int a, int u) |
Mathieu Desnoyers | 2856f5e | 2007-05-08 00:34:38 -0700 | [diff] [blame] | 388 | { |
| 389 | int c, old; |
| 390 | c = atomic_read(v); |
| 391 | for (;;) { |
| 392 | if (unlikely(c == (u))) |
| 393 | break; |
| 394 | old = atomic_cmpxchg((v), c, c + (a)); |
| 395 | if (likely(old == c)) |
| 396 | break; |
| 397 | c = old; |
| 398 | } |
| 399 | return c != (u); |
| 400 | } |
| 401 | |
Nick Piggin | 8426e1f | 2005-11-13 16:07:25 -0800 | [diff] [blame] | 402 | #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0) |
| 403 | |
Mathieu Desnoyers | 79d365a | 2007-05-08 00:34:36 -0700 | [diff] [blame] | 404 | /** |
| 405 | * atomic64_add_unless - add unless the number is a given value |
| 406 | * @v: pointer of type atomic64_t |
| 407 | * @a: the amount to add to v... |
| 408 | * @u: ...unless v is equal to u. |
| 409 | * |
| 410 | * Atomically adds @a to @v, so long as it was not @u. |
| 411 | * Returns non-zero if @v was not @u, and zero otherwise. |
| 412 | */ |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 413 | static inline int atomic64_add_unless(atomic64_t *v, long a, long u) |
Mathieu Desnoyers | 2856f5e | 2007-05-08 00:34:38 -0700 | [diff] [blame] | 414 | { |
| 415 | long c, old; |
| 416 | c = atomic64_read(v); |
| 417 | for (;;) { |
| 418 | if (unlikely(c == (u))) |
| 419 | break; |
| 420 | old = atomic64_cmpxchg((v), c, c + (a)); |
| 421 | if (likely(old == c)) |
| 422 | break; |
| 423 | c = old; |
| 424 | } |
| 425 | return c != (u); |
| 426 | } |
| 427 | |
Cliff Wickman | 73e991f | 2008-06-04 15:33:17 -0500 | [diff] [blame] | 428 | /** |
| 429 | * atomic_inc_short - increment of a short integer |
| 430 | * @v: pointer to type int |
| 431 | * |
| 432 | * Atomically adds 1 to @v |
| 433 | * Returns the new value of @u |
| 434 | */ |
| 435 | static inline short int atomic_inc_short(short int *v) |
| 436 | { |
| 437 | asm(LOCK_PREFIX "addw $1, %0" : "+m" (*v)); |
| 438 | return *v; |
| 439 | } |
| 440 | |
| 441 | /** |
| 442 | * atomic_or_long - OR of two long integers |
| 443 | * @v1: pointer to type unsigned long |
| 444 | * @v2: pointer to type unsigned long |
| 445 | * |
| 446 | * Atomically ORs @v1 and @v2 |
| 447 | * Returns the result of the OR |
| 448 | */ |
| 449 | static inline void atomic_or_long(unsigned long *v1, unsigned long v2) |
| 450 | { |
| 451 | asm(LOCK_PREFIX "orq %1, %0" : "+m" (*v1) : "r" (v2)); |
| 452 | } |
| 453 | |
Mathieu Desnoyers | 79d365a | 2007-05-08 00:34:36 -0700 | [diff] [blame] | 454 | #define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1, 0) |
| 455 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | /* These are x86-specific, used by some header files */ |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 457 | #define atomic_clear_mask(mask, addr) \ |
| 458 | asm volatile(LOCK_PREFIX "andl %0,%1" \ |
| 459 | : : "r" (~(mask)), "m" (*(addr)) : "memory") |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | |
Joe Perches | 7edb3cd | 2008-03-23 01:01:42 -0700 | [diff] [blame] | 461 | #define atomic_set_mask(mask, addr) \ |
| 462 | asm volatile(LOCK_PREFIX "orl %0,%1" \ |
| 463 | : : "r" ((unsigned)(mask)), "m" (*(addr)) \ |
| 464 | : "memory") |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | |
| 466 | /* Atomic operations are already serializing on x86 */ |
| 467 | #define smp_mb__before_atomic_dec() barrier() |
| 468 | #define smp_mb__after_atomic_dec() barrier() |
| 469 | #define smp_mb__before_atomic_inc() barrier() |
| 470 | #define smp_mb__after_atomic_inc() barrier() |
| 471 | |
Christoph Lameter | d3cb487 | 2006-01-06 00:11:20 -0800 | [diff] [blame] | 472 | #include <asm-generic/atomic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | #endif |